Skip to content

Commit

Permalink
fix(gha): Fix github status log and add tests (#1316)
Browse files Browse the repository at this point in the history
* fix(gha): Fix github status log and add tests

* chore(language): Update test message to be better

Co-authored-by: David Byron <[email protected]>

---------

Co-authored-by: David Byron <[email protected]>
  • Loading branch information
jasonmcintosh and dbyron-sf authored Aug 4, 2023
1 parent e8238a3 commit 7d8130d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public GithubService githubService(
.setEndpoint(githubEndpoint)
.setConverter(new JacksonConverter())
.setClient(retrofitClient)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setLogLevel(retrofitLogLevel != null ? retrofitLogLevel : RestAdapter.LogLevel.BASIC)
.setLog(new Slf4jRetrofitLogger(GithubService.class))
.build()
.create(GithubService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package com.netflix.spinnaker.echo.config
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import retrofit.Endpoint
import retrofit.Endpoints
import retrofit.RestAdapter
import retrofit.client.Client
import retrofit.client.Header
import retrofit.client.Response
import retrofit.mime.TypedByteArray
import spock.lang.Specification
import spock.lang.Subject

Expand Down Expand Up @@ -37,6 +43,79 @@ class GithubConfigSpec extends Specification {
then:
endpoint.url == ownEndpoint
}


def 'default log level does not output authorization headers and matches basic API call structure'() {
given:
def systemError = System.out;
def testErr = new ByteArrayOutputStream();
System.setOut(new PrintStream(testErr))

Client mockClient = Stub(Client) {
execute(_) >> {
return new Response("http://example.com", 200, "Success!", new ArrayList<Header>(), new TypedByteArray("", "SOmething workedddd".bytes))
}

}
def ghService = new GithubConfig().githubService(Endpoints.newFixedEndpoint("http://example.com"), mockClient, null)

when:
ghService.getCommit("SECRET", "repo-name", "sha12345");

then:
def logOutput = testErr.toString()
logOutput.contains("HTTP GET http://example.com/repos/repo-name/commits/sha12345")
!logOutput.contains("SECRET")
!logOutput.contains("Authorization")

cleanup:
System.setOut(systemError)
System.out.print(testErr)
}

def 'When no log set, no log output!'() {
given:
def systemError = System.out;
def testErr = new ByteArrayOutputStream();
System.setOut(new PrintStream(testErr))

Client mockClient = Stub(Client) {
execute(_) >> new Response("http://example.com", 200, "Ok", new ArrayList<Header>(), new TypedByteArray("", "response".bytes))
}
def ghService = new GithubConfig().githubService(Endpoints.newFixedEndpoint("http://example.com"), mockClient, RestAdapter.LogLevel.NONE)

when:
ghService.getCommit("", "", "");

then:
!testErr.toString().contains("GET")

cleanup:
System.setOut(systemError)
System.out.print(testErr)
}

def 'Log when full has header information and auth headers- dont do this in prod!'() {
given:
def systemError = System.out;
def testErr = new ByteArrayOutputStream();
System.setOut(new PrintStream(testErr))

Client mockClient = Stub(Client) {
execute(_) >> new Response("http://example.com", 200, "Ok", new ArrayList<Header>(), new TypedByteArray("", "response".bytes))
}
def ghService = new GithubConfig().githubService(Endpoints.newFixedEndpoint("http://example.com"), mockClient, RestAdapter.LogLevel.FULL)

when:
ghService.getCommit("", "", "");

then:
testErr.toString().contains("Authorization")

cleanup:
System.setOut(systemError)
System.out.print(testErr)
}
}

@SpringBootTest(
Expand Down

0 comments on commit 7d8130d

Please sign in to comment.