From d1d54aa754ddb0cea7f201e898d8fce52110eac3 Mon Sep 17 00:00:00 2001 From: Darren Walker <9861667+FluentSynergyDW@users.noreply.github.com> Date: Wed, 3 Jul 2019 13:56:33 +0100 Subject: [PATCH 1/2] APIS-4025 Implemented application search by Client ID. --- .../repository/ApplicationRepository.scala | 4 +- .../ApplicationRepositorySpec.scala | 90 ++++++++++++++----- 2 files changed, 70 insertions(+), 24 deletions(-) diff --git a/app/uk/gov/hmrc/thirdpartyapplication/repository/ApplicationRepository.scala b/app/uk/gov/hmrc/thirdpartyapplication/repository/ApplicationRepository.scala index 6157338c6..94b19a82e 100644 --- a/app/uk/gov/hmrc/thirdpartyapplication/repository/ApplicationRepository.scala +++ b/app/uk/gov/hmrc/thirdpartyapplication/repository/ApplicationRepository.scala @@ -243,7 +243,7 @@ class ApplicationRepository @Inject()(mongo: ReactiveMongoComponent) case PrivilegedAccess => accessTypeMatch(AccessType.PRIVILEGED) // Text Search - case ApplicationTextSearch => regexTextSearch(Seq("id", "name"), applicationSearch.textToSearch.getOrElse("")) + case ApplicationTextSearch => regexTextSearch(Seq("id", "name", "tokens.production.clientId"), applicationSearch.textToSearch.getOrElse("")) } } @@ -302,7 +302,7 @@ class ApplicationRepository @Inject()(mongo: ReactiveMongoComponent) def processAll(function: ApplicationData => Unit): Future[Unit] = { collection - .find(Json.obj()) + .find(Json.obj(), Option.empty[ApplicationData]) .cursor[ApplicationData]() .enumerator() .run(Iteratee.foreach(function)) diff --git a/test/it/uk/gov/hmrc/thirdpartyapplication/repository/ApplicationRepositorySpec.scala b/test/it/uk/gov/hmrc/thirdpartyapplication/repository/ApplicationRepositorySpec.scala index 608b2f5a4..9a91d3a6e 100644 --- a/test/it/uk/gov/hmrc/thirdpartyapplication/repository/ApplicationRepositorySpec.scala +++ b/test/it/uk/gov/hmrc/thirdpartyapplication/repository/ApplicationRepositorySpec.scala @@ -41,12 +41,27 @@ class ApplicationRepositorySpec extends UnitSpec with MongoSpecSupport with BeforeAndAfterEach with BeforeAndAfterAll with ApplicationStateUtil with IndexVerification with MockitoSugar with Eventually with Matchers { - private val reactiveMongoComponent = new ReactiveMongoComponent { override def mongoConnector: MongoConnector = mongoConnectorForTest } + private val reactiveMongoComponent = new ReactiveMongoComponent { + override def mongoConnector: MongoConnector = mongoConnectorForTest + } private val applicationRepository = new ApplicationRepository(reactiveMongoComponent) private val subscriptionRepository = new SubscriptionRepository(reactiveMongoComponent) - private def generateClientId = alphanumeric.take(10).mkString + private def generateClientId = { + val lengthOfRandomClientId = 10 + alphanumeric.take(lengthOfRandomClientId).mkString + } + + private def generateClientSecret = { + val lengthOfRandomSecret = 5 + nextString(lengthOfRandomSecret) + } + + private def generateAccessToken = { + val lengthOfRandomToken = 5 + nextString(lengthOfRandomToken) + } override def beforeEach() { Seq(applicationRepository, subscriptionRepository).foreach { db => @@ -119,6 +134,7 @@ class ApplicationRepositorySpec extends UnitSpec with MongoSpecSupport await(applicationRepository.save(anApplicationData(applicationId, UUID.randomUUID().toString, UUID.randomUUID().toString))) def applicationById: JsObject = Json.obj("id" -> applicationId.toString) + def removeLastAccessField: JsObject = Json.obj("$unset" -> Json.obj("lastAccess" -> "")) applicationRepository.findAndUpdate(applicationById, removeLastAccessField) @@ -373,21 +389,21 @@ class ApplicationRepositorySpec extends UnitSpec with MongoSpecSupport "fetchVerifiableBy" should { "retrieve the application with verificationCode when in pendingRequesterVerification state" in { - val application = anApplicationData(UUID.randomUUID(), "aaa", "111", state = pendingRequesterVerificationState("requestorEmail@example.com")) + val application = anApplicationData(UUID.randomUUID(), state = pendingRequesterVerificationState("requestorEmail@example.com")) await(applicationRepository.save(application)) val retrieved = await(applicationRepository.fetchVerifiableUpliftBy(generatedVerificationCode)) retrieved shouldBe Some(application) } "retrieve the application with verificationCode when in production state" in { - val application = anApplicationData(UUID.randomUUID(), "aaa", "111", state = productionState("requestorEmail@example.com")) + val application = anApplicationData(UUID.randomUUID(), state = productionState("requestorEmail@example.com")) await(applicationRepository.save(application)) val retrieved = await(applicationRepository.fetchVerifiableUpliftBy(generatedVerificationCode)) retrieved shouldBe Some(application) } "not retrieve the application with an unknown verificationCode" in { - val application = anApplicationData(UUID.randomUUID(), "aaa", "111", state = pendingRequesterVerificationState("requestorEmail@example.com")) + val application = anApplicationData(UUID.randomUUID(), state = pendingRequesterVerificationState("requestorEmail@example.com")) await(applicationRepository.save(application)) val retrieved = await(applicationRepository.fetchVerifiableUpliftBy("aDifferentVerificationCode")) retrieved shouldBe None @@ -667,6 +683,28 @@ class ApplicationRepositorySpec extends UnitSpec with MongoSpecSupport result.applications.head.id shouldBe applicationId } + "return applications with search text matching client id" in { + val applicationId = UUID.randomUUID() + val clientId = generateClientId + val applicationName = "Test Application" + + val application = aNamedApplicationData(applicationId, applicationName, prodClientId = clientId, sandboxClientId = generateClientId) + val randomOtherApplication = anApplicationData(UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId) + await(applicationRepository.save(application)) + await(applicationRepository.save(randomOtherApplication)) + + val applicationSearch = new ApplicationSearch(filters = Seq(ApplicationTextSearch), textToSearch = Some(clientId.toString)) + + val result = await(applicationRepository.searchApplications(applicationSearch)) + + result.totals.size shouldBe 1 + result.totals.head.total shouldBe 2 + result.matching.size shouldBe 1 + result.matching.head.total shouldBe 1 + result.applications.size shouldBe 1 + result.applications.head.tokens.production.clientId shouldBe clientId + } + "return applications with matching search text and other filters" in { val applicationName = "Test Application" @@ -714,8 +752,8 @@ class ApplicationRepositorySpec extends UnitSpec with MongoSpecSupport "return applications with terms of use agreed" in { val applicationId = UUID.randomUUID() val applicationName = "Test Application" - val termsOfUseAgreement = new TermsOfUseAgreement("a@b.com", HmrcTime.now, "v1") - val checkInformation = new CheckInformation(termsOfUseAgreements = Seq(termsOfUseAgreement)) + val termsOfUseAgreement = TermsOfUseAgreement("a@b.com", HmrcTime.now, "v1") + val checkInformation = CheckInformation(termsOfUseAgreements = Seq(termsOfUseAgreement)) val applicationWithTermsOfUseAgreed = aNamedApplicationData( @@ -739,8 +777,8 @@ class ApplicationRepositorySpec extends UnitSpec with MongoSpecSupport "return applications with terms of use not agreed where checkInformation value does not exist in database" in { val applicationId = UUID.randomUUID() val applicationName = "Test Application" - val termsOfUseAgreement = new TermsOfUseAgreement("a@b.com", HmrcTime.now, "v1") - val checkInformation = new CheckInformation(termsOfUseAgreements = Seq(termsOfUseAgreement)) + val termsOfUseAgreement = TermsOfUseAgreement("a@b.com", HmrcTime.now, "v1") + val checkInformation = CheckInformation(termsOfUseAgreements = Seq(termsOfUseAgreement)) val applicationWithNoCheckInformation = aNamedApplicationData(applicationId, applicationName, prodClientId = generateClientId, sandboxClientId = generateClientId) @@ -764,10 +802,10 @@ class ApplicationRepositorySpec extends UnitSpec with MongoSpecSupport "return applications with terms of use not agreed where termsOfUseAgreements array is empty in database" in { val applicationId = UUID.randomUUID() val applicationName = "Test Application" - val termsOfUseAgreement = new TermsOfUseAgreement("a@b.com", HmrcTime.now, "v1") - val checkInformation = new CheckInformation(termsOfUseAgreements = Seq(termsOfUseAgreement)) + val termsOfUseAgreement = TermsOfUseAgreement("a@b.com", HmrcTime.now, "v1") + val checkInformation = CheckInformation(termsOfUseAgreements = Seq(termsOfUseAgreement)) - val emptyCheckInformation = new CheckInformation(termsOfUseAgreements = Seq.empty) + val emptyCheckInformation = CheckInformation(termsOfUseAgreements = Seq.empty) val applicationWithNoTermsOfUseAgreed = aNamedApplicationData( @@ -841,8 +879,10 @@ class ApplicationRepositorySpec extends UnitSpec with MongoSpecSupport "return applications sorted by name ascending" in { val firstName = "AAA first" val secondName = "ZZZ second" - val firstApplication = aNamedApplicationData(name = firstName, id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId) - val secondApplication = aNamedApplicationData(name = secondName, id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId) + val firstApplication = + aNamedApplicationData(name = firstName, id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId) + val secondApplication = + aNamedApplicationData(name = secondName, id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId) await(applicationRepository.save(secondApplication)) await(applicationRepository.save(firstApplication)) @@ -862,8 +902,10 @@ class ApplicationRepositorySpec extends UnitSpec with MongoSpecSupport "return applications sorted by name descending" in { val firstName = "AAA first" val secondName = "ZZZ second" - val firstApplication = aNamedApplicationData(name = firstName, id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId) - val secondApplication = aNamedApplicationData(name = secondName, id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId) + val firstApplication = + aNamedApplicationData(name = firstName, id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId) + val secondApplication = + aNamedApplicationData(name = secondName, id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId) await(applicationRepository.save(firstApplication)) await(applicationRepository.save(secondApplication)) @@ -883,8 +925,10 @@ class ApplicationRepositorySpec extends UnitSpec with MongoSpecSupport "return applications sorted by submitted ascending" in { val firstCreatedOn = HmrcTime.now.minusDays(2) val secondCreatedOn = HmrcTime.now.minusDays(1) - val firstApplication = anApplicationData(id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId).copy(createdOn = firstCreatedOn) - val secondApplication = anApplicationData(id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId).copy(createdOn = secondCreatedOn) + val firstApplication = + anApplicationData(id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId).copy(createdOn = firstCreatedOn) + val secondApplication = + anApplicationData(id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId).copy(createdOn = secondCreatedOn) await(applicationRepository.save(secondApplication)) await(applicationRepository.save(firstApplication)) @@ -904,8 +948,10 @@ class ApplicationRepositorySpec extends UnitSpec with MongoSpecSupport "return applications sorted by submitted descending" in { val firstCreatedOn = HmrcTime.now.minusDays(2) val secondCreatedOn = HmrcTime.now.minusDays(1) - val firstApplication = anApplicationData(id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId).copy(createdOn = firstCreatedOn) - val secondApplication = anApplicationData(id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId).copy(createdOn = secondCreatedOn) + val firstApplication = + anApplicationData(id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId).copy(createdOn = firstCreatedOn) + val secondApplication = + anApplicationData(id = UUID.randomUUID(), prodClientId = generateClientId, sandboxClientId = generateClientId).copy(createdOn = secondCreatedOn) await(applicationRepository.save(firstApplication)) await(applicationRepository.save(secondApplication)) @@ -986,8 +1032,8 @@ class ApplicationRepositorySpec extends UnitSpec with MongoSpecSupport "password", "myapplication", ApplicationTokens( - EnvironmentToken(prodClientId, nextString(5), nextString(5)), - EnvironmentToken(sandboxClientId, nextString(5), nextString(5))), + EnvironmentToken(prodClientId, generateClientSecret, generateAccessToken), + EnvironmentToken(sandboxClientId, generateClientSecret, generateAccessToken)), state, access, checkInformation = checkInformation) From fa9899975a41585719e3a93c385d61b1cf046dd1 Mon Sep 17 00:00:00 2001 From: Darren Walker <9861667+FluentSynergyDW@users.noreply.github.com> Date: Wed, 3 Jul 2019 14:08:45 +0100 Subject: [PATCH 2/2] APIS-4025 Dependency updates. --- build.sbt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build.sbt b/build.sbt index 028e71e5c..243b49858 100644 --- a/build.sbt +++ b/build.sbt @@ -12,15 +12,15 @@ lazy val appDependencies: Seq[ModuleID] = compile ++ test lazy val compile = Seq( "uk.gov.hmrc" %% "bootstrap-play-25" % "4.13.0", - "uk.gov.hmrc" %% "mongo-lock" % "6.12.0-play-25", + "uk.gov.hmrc" %% "mongo-lock" % "6.15.0-play-25", "uk.gov.hmrc" %% "play-scheduling" % "6.0.0", "uk.gov.hmrc" %% "play-json-union-formatter" % "1.5.0", - "uk.gov.hmrc" %% "play-hmrc-api" % "3.4.0-play-25", + "uk.gov.hmrc" %% "play-hmrc-api" % "3.6.0-play-25", "com.typesafe.play" %% "play-iteratees" % PlayVersion.current, "org.reactivemongo" %% "reactivemongo-iteratees" % "0.16.4" ) lazy val test = Seq( - "uk.gov.hmrc" %% "reactivemongo-test" % "4.14.0-play-25" % "test,it", + "uk.gov.hmrc" %% "reactivemongo-test" % "4.15.0-play-25" % "test,it", "uk.gov.hmrc" %% "hmrctest" % "3.9.0-play-25" % "test,it", "org.pegdown" % "pegdown" % "1.6.0" % "test,it", "org.scalaj" %% "scalaj-http" % "2.3.0" % "test,it",