diff --git a/app/uk/gov/hmrc/thirdpartyapplication/controllers/ApplicationController.scala b/app/uk/gov/hmrc/thirdpartyapplication/controllers/ApplicationController.scala index 39ac2585b..85fdcd7f0 100644 --- a/app/uk/gov/hmrc/thirdpartyapplication/controllers/ApplicationController.scala +++ b/app/uk/gov/hmrc/thirdpartyapplication/controllers/ApplicationController.scala @@ -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 -> _) @@ -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))) } diff --git a/test/unit/uk/gov/hmrc/thirdpartyapplication/controllers/ApplicationControllerSpec.scala b/test/unit/uk/gov/hmrc/thirdpartyapplication/controllers/ApplicationControllerSpec.scala index 1b24b8fc3..ab8277089 100644 --- a/test/unit/uk/gov/hmrc/thirdpartyapplication/controllers/ApplicationControllerSpec.scala +++ b/test/unit/uk/gov/hmrc/thirdpartyapplication/controllers/ApplicationControllerSpec.scala @@ -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" -> "test@example.com", "X-Server-Token" -> "abc123") + val apiGatewayUserAgents: Seq[String] = Seq("APIPlatformAuthorizer", "wso2-gateway-customizations") val mockCredentialService = mock[CredentialService] val mockApplicationService = mock[ApplicationService] @@ -798,7 +799,7 @@ class ApplicationControllerSpec extends UnitSpec with ScalaFutures with MockitoS } - "fetch application" should { + "query dispatcher" should { val clientId = "A123XC" val serverToken = "b3c83934c02df8b111e7f9f8700000" @@ -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 @@ -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 @@ -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 {