Skip to content

Commit

Permalink
613: Fix up test for when the RS token is still valid
Browse files Browse the repository at this point in the history
  • Loading branch information
halprin committed Oct 25, 2023
1 parent bb8e091 commit 315fb67
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ public class ReportStreamOrderSender implements OrderSender {

private String rsTokenCache;

protected synchronized String getRsTokenCache() {
return this.rsTokenCache;
}

protected synchronized void setRsTokenCache(String token) {
this.rsTokenCache = token;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,62 +379,52 @@ class ReportStreamOrderSenderTest extends Specification {
def mockAuthEngine = Mock(AuthEngine)
def mockFormatter = Mock(Formatter)
def mockCache = Mock(Cache)

mockCache.get(_ as String) >> "shouldn't be returned"

//mock the auth engine so that the JWT looks like it is invalid
mockAuthEngine.getExpirationDate(_) >> LocalDateTime.now().plus(10, ChronoUnit.SECONDS)

def freshTokenFromRs = "new token"
mockFormatter.convertJsonToObject(_, _ as TypeReference) >> Map.of("access_token", freshTokenFromRs)

TestApplicationContext.register(Formatter, mockFormatter)
TestApplicationContext.register(AuthEngine, mockAuthEngine)
TestApplicationContext.register(HttpClient, mockClient)
TestApplicationContext.register(Cache, mockCache)
mockCache.get(_ as String) >> "shouldn't be returned"
TestApplicationContext.register(Secrets, Mock(Secrets))
TestApplicationContext.injectRegisteredImplementations()

//mock the auth engine so that the JWT looks like it is invalid
mockAuthEngine.getExpirationDate(_) >> LocalDateTime.now().plus(10, ChronoUnit.SECONDS)

def tokenFromRS = "new token"
mockFormatter.convertJsonToObject(_, _ as TypeReference) >> Map.of("access_token", tokenFromRS)
TestApplicationContext.injectRegisteredImplementations()

when:
def token = ReportStreamOrderSender.getInstance().getRsToken()

then:
1 * mockClient.post(_, _, _)
token == tokenFromRS
token == freshTokenFromRs
}

def "getRsToken when cache token is valid"() {
def "getRsToken when cache token is valid, return that cached token"() {
given:
def orderSender = ReportStreamOrderSender.getInstance()
orderSender.setRsTokenCache("valid Token")
TestApplicationContext.register(OrderSender, orderSender)
def mockAuthEngine = Mock(AuthEngine)
def mockCache = Mock(Cache)

def mockFormatter = Mock(Formatter)
mockFormatter.convertJsonToObject(_ as String, _ as TypeReference) >> Map.of("access_token", "fake token")
TestApplicationContext.register(Formatter, mockFormatter)
def cachedRsToken = "DogCow goes Moof!"
mockCache.get(_ as String) >> cachedRsToken

def mockLogFormatter = Mock(Formatter)
mockLogFormatter.convertJsonToObject(_ as String, _ as TypeReference) >> null
TestApplicationContext.register(Formatter, mockLogFormatter)
//mock the auth engine so that the JWT looks valid
mockAuthEngine.getExpirationDate(_) >> LocalDateTime.now().plus(60, ChronoUnit.SECONDS)

def mockAuthEngine = Mock(AuthEngine)
mockAuthEngine.generateSenderToken(_ as String, _ as String, _ as String, _ as String, 300) >> "fake token"
mockAuthEngine.getExpirationDate(_ as String) >> LocalDateTime.now().plus(25, ChronoUnit.SECONDS)
TestApplicationContext.register(AuthEngine, mockAuthEngine)

def mockClient = Mock(HttpClient)
mockClient.post(_ as String, _ as Map, _ as String) >> """{"foo":"foo value", "access_token":fake token, "boo":"boo value"}"""
TestApplicationContext.register(HttpClient, mockClient)

def mockSecrets = Mock(Secrets)
mockSecrets.getKey(_ as String) >> "fakePrivateKey"
TestApplicationContext.register(Secrets, mockSecrets)
TestApplicationContext.register(Cache, mockCache)

TestApplicationContext.injectRegisteredImplementations()

when:
def token = orderSender.getRsToken()
def token = ReportStreamOrderSender.getInstance().getRsToken()

then:
token == orderSender.getRsTokenCache()
token == cachedRsToken
}

def "logRsSubmissionId logs submissionId if convertJsonToObject is successful"() {
Expand Down

0 comments on commit 315fb67

Please sign in to comment.