Skip to content

Commit

Permalink
Merge pull request #40 from hmrc/API-3866
Browse files Browse the repository at this point in the history
API-3866: expanded the list of user agents for which we record usage …
  • Loading branch information
cjrowe authored Jul 1, 2019
2 parents 8fd6264 + 94289dd commit db97177
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ApplicationController @Inject()(val applicationService: ApplicationService
val applicationCacheExpiry = config.fetchApplicationTtlInSecs
val subscriptionCacheExpiry = config.fetchSubscriptionTtlInSecs

val AuthorizerUserAgent: String = "APIPlatformAuthorizer"
val apiGatewayUserAgents: Seq[String] = Seq("APIPlatformAuthorizer", "wso2-gateway-customizations")

override implicit def hc(implicit request: RequestHeader) = {
def header(key: String) = request.headers.get(key) map (key -> _)
Expand Down Expand Up @@ -226,9 +226,9 @@ class ApplicationController @Inject()(val applicationService: ApplicationService
notFoundMessage: String)(implicit hc: HeaderCarrier): Future[Result] =
fetchFunction().flatMap {
case Some(application) =>
// If request has orginated from AWS Authorizer, record usage of the Application
// If request has originated from an API gateway, record usage of the Application
hc.headers.find(_._1 == USER_AGENT).map(_._2) match {
case Some(AuthorizerUserAgent) =>
case Some(userAgent) if apiGatewayUserAgents.contains(userAgent) =>
applicationService.recordApplicationUsage(application.id).map(updatedApp => Ok(toJson(updatedApp)))
case _ => Future.successful(Ok(toJson(application)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ApplicationControllerSpec extends UnitSpec with ScalaFutures with MockitoS
trait Setup {
implicit val hc = HeaderCarrier().withExtraHeaders(X_REQUEST_ID_HEADER -> "requestId")
implicit lazy val request = FakeRequest().withHeaders("X-name" -> "blob", "X-email-address" -> "[email protected]", "X-Server-Token" -> "abc123")
val apiGatewayUserAgents: Seq[String] = Seq("APIPlatformAuthorizer", "wso2-gateway-customizations")

val mockCredentialService = mock[CredentialService]
val mockApplicationService = mock[ApplicationService]
Expand Down Expand Up @@ -798,7 +799,7 @@ class ApplicationControllerSpec extends UnitSpec with ScalaFutures with MockitoS

}

"fetch application" should {
"query dispatcher" should {
val clientId = "A123XC"
val serverToken = "b3c83934c02df8b111e7f9f8700000"

Expand Down Expand Up @@ -872,7 +873,7 @@ class ApplicationControllerSpec extends UnitSpec with ScalaFutures with MockitoS
result.header.headers.get(HeaderNames.VARY) shouldBe None
}

"update last accessed time when AWS Authorizer retrieves Application by Server Token" in new Setup {
"update last accessed time when an API gateway retrieves Application by Server Token" in new Setup {
val applicationId: UUID = UUID.randomUUID()
val applicationResponse: ApplicationResponse =
aNewApplicationResponse().copy(id = applicationId, lastAccess = Some(DateTime.now().minusDays(10))) //scalastyle:ignore magic.number
Expand All @@ -882,17 +883,19 @@ class ApplicationControllerSpec extends UnitSpec with ScalaFutures with MockitoS
when(underTest.applicationService.fetchByServerToken(serverToken)).thenReturn(Future(Some(applicationResponse)))
when(underTest.applicationService.recordApplicationUsage(applicationId)).thenReturn(Future(updatedApplicationResponse))

val result: Result =
await(underTest.queryDispatcher()(request.withHeaders(SERVER_TOKEN_HEADER -> serverToken, USER_AGENT -> "APIPlatformAuthorizer")))
apiGatewayUserAgents.foreach { userAgent =>
val result: Result =
await(underTest.queryDispatcher()(request.withHeaders(SERVER_TOKEN_HEADER -> serverToken, USER_AGENT -> userAgent)))

status(result) shouldBe SC_OK
result.header.headers.get(HeaderNames.CACHE_CONTROL) shouldBe Some(s"max-age=$applicationTtlInSecs")
result.header.headers.get(HeaderNames.VARY) shouldBe Some(SERVER_TOKEN_HEADER)
status(result) shouldBe SC_OK
result.header.headers.get(HeaderNames.CACHE_CONTROL) shouldBe Some(s"max-age=$applicationTtlInSecs")
result.header.headers.get(HeaderNames.VARY) shouldBe Some(SERVER_TOKEN_HEADER)

(jsonBodyOf(result) \ "lastAccess").as[Long] shouldBe updatedLastAccessTime.getMillis
(jsonBodyOf(result) \ "lastAccess").as[Long] shouldBe updatedLastAccessTime.getMillis
}
}

"update last accessed time when AWS Authorizer retrieves Application by Client Id" in new Setup {
"update last accessed time when an API gateway retrieves Application by Client Id" in new Setup {
val applicationId: UUID = UUID.randomUUID()
val applicationResponse: ApplicationResponse =
aNewApplicationResponse().copy(id = applicationId, lastAccess = Some(DateTime.now().minusDays(10))) //scalastyle:ignore magic.number
Expand All @@ -902,14 +905,16 @@ class ApplicationControllerSpec extends UnitSpec with ScalaFutures with MockitoS
when(underTest.applicationService.fetchByClientId(clientId)).thenReturn(Future(Some(applicationResponse)))
when(underTest.applicationService.recordApplicationUsage(applicationId)).thenReturn(Future(updatedApplicationResponse))

val result: Result =
await(underTest.queryDispatcher()(FakeRequest("GET", s"?clientId=$clientId").withHeaders(USER_AGENT -> "APIPlatformAuthorizer")))
apiGatewayUserAgents.foreach { userAgent =>
val result: Result =
await(underTest.queryDispatcher()(FakeRequest("GET", s"?clientId=$clientId").withHeaders(USER_AGENT -> userAgent)))

status(result) shouldBe SC_OK
result.header.headers.get(HeaderNames.CACHE_CONTROL) shouldBe Some(s"max-age=$applicationTtlInSecs")
result.header.headers.get(HeaderNames.VARY) shouldBe None
status(result) shouldBe SC_OK
result.header.headers.get(HeaderNames.CACHE_CONTROL) shouldBe Some(s"max-age=$applicationTtlInSecs")
result.header.headers.get(HeaderNames.VARY) shouldBe None

(jsonBodyOf(result) \ "lastAccess").as[Long] shouldBe updatedLastAccessTime.getMillis
(jsonBodyOf(result) \ "lastAccess").as[Long] shouldBe updatedLastAccessTime.getMillis
}
}

"fetchByServerToken does not update last accessed time in absence of appropriate User-Agent header" in new Setup {
Expand Down

0 comments on commit db97177

Please sign in to comment.