Skip to content

Commit

Permalink
Merge pull request #43 from hmrc/APIS-4025
Browse files Browse the repository at this point in the history
APIS-4025 Implemented application search by Client ID.
  • Loading branch information
anjumabbas5 authored Jul 3, 2019
2 parents bd0b3f1 + fa98999 commit 229c14a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(""))
}
}

Expand Down Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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("[email protected]"))
val application = anApplicationData(UUID.randomUUID(), state = pendingRequesterVerificationState("[email protected]"))
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("[email protected]"))
val application = anApplicationData(UUID.randomUUID(), state = productionState("[email protected]"))
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("[email protected]"))
val application = anApplicationData(UUID.randomUUID(), state = pendingRequesterVerificationState("[email protected]"))
await(applicationRepository.save(application))
val retrieved = await(applicationRepository.fetchVerifiableUpliftBy("aDifferentVerificationCode"))
retrieved shouldBe None
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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("[email protected]", HmrcTime.now, "v1")
val checkInformation = new CheckInformation(termsOfUseAgreements = Seq(termsOfUseAgreement))
val termsOfUseAgreement = TermsOfUseAgreement("[email protected]", HmrcTime.now, "v1")
val checkInformation = CheckInformation(termsOfUseAgreements = Seq(termsOfUseAgreement))

val applicationWithTermsOfUseAgreed =
aNamedApplicationData(
Expand All @@ -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("[email protected]", HmrcTime.now, "v1")
val checkInformation = new CheckInformation(termsOfUseAgreements = Seq(termsOfUseAgreement))
val termsOfUseAgreement = TermsOfUseAgreement("[email protected]", HmrcTime.now, "v1")
val checkInformation = CheckInformation(termsOfUseAgreements = Seq(termsOfUseAgreement))

val applicationWithNoCheckInformation =
aNamedApplicationData(applicationId, applicationName, prodClientId = generateClientId, sandboxClientId = generateClientId)
Expand All @@ -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("[email protected]", HmrcTime.now, "v1")
val checkInformation = new CheckInformation(termsOfUseAgreements = Seq(termsOfUseAgreement))
val termsOfUseAgreement = TermsOfUseAgreement("[email protected]", 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(
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 229c14a

Please sign in to comment.