Skip to content

Commit

Permalink
Merge pull request #85 from hmrc/SAR-672
Browse files Browse the repository at this point in the history
SAR-672 - Component Tests for user access
  • Loading branch information
suyangyu authored Jun 1, 2017
2 parents 7d26072 + 7f979dc commit 4dc2264
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
72 changes: 72 additions & 0 deletions it/controllers/throttling/UserAccessControllerISpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2017 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package controllers.throttling

import helpers.ComponentSpecBase
import helpers.WireMockDSL.HTTPVerbMapping.Get
import helpers.WireMockDSL._
import helpers.servicemocks.AuthStub._
import helpers.IntegrationTestConstants._
import play.api.http.Status._
import helpers.DatabaseHelpers._
import play.api.libs.concurrent.Execution.Implicits._

class UserAccessControllerISpec extends ComponentSpecBase {
"GET /throttle/:nino" should {
"return OK when the service has not received any requests" in {
stub when Get(authority) thenReturn stubbedAuthResponse
stub when Get(authIDs) thenReturn stubbedIDs

IncomeTaxSubscription.checkUserAccess(testNino) should have (
httpStatus(OK)
)
}

"return OK when the service has not received too many requests" in {
IncomeTaxSubscription.insertUserCount(nonFullUserCount)

stub when Get(authority) thenReturn stubbedAuthResponse
stub when Get(authIDs) thenReturn stubbedIDs

IncomeTaxSubscription.checkUserAccess(testNino) should have (
httpStatus(OK)
)
}

"return OK for a returning user" in {
IncomeTaxSubscription.insertUserCount(matchingUserCount)

stub when Get(authority) thenReturn stubbedAuthResponse
stub when Get(authIDs) thenReturn stubbedIDs

IncomeTaxSubscription.checkUserAccess(testNino) should have (
httpStatus(OK)
)
}

"return TOO_MANY_REQUESTS when the service has received too many requests and it is a new userID" in {
IncomeTaxSubscription.insertUserCount(maxUserCount)

stub when Get(authority) thenReturn stubbedAuthResponse
stub when Get(authIDs) thenReturn stubbedIDs

IncomeTaxSubscription.checkUserAccess(testNino) should have (
httpStatus(TOO_MANY_REQUESTS)
)
}
}
}
19 changes: 18 additions & 1 deletion it/helpers/ComponentSpecBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@
package helpers

import models.frontend.FERequest
import models.throttling.UserCount
import org.scalatest._
import org.scalatest.concurrent.{Eventually, IntegrationPatience, ScalaFutures}
import org.scalatestplus.play.guice.GuiceOneServerPerSuite
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.json.Writes
import play.api.libs.ws.WSResponse
import play.api.{Application, Environment, Mode}
import reactivemongo.api.commands.WriteResult
import repositories.{Repositories, ThrottleMongoRepository}
import uk.gov.hmrc.play.test.UnitSpec

import scala.concurrent.ExecutionContext

trait ComponentSpecBase extends UnitSpec
with GivenWhenThen with TestSuite
with GuiceOneServerPerSuite with ScalaFutures with IntegrationPatience with Matchers
Expand All @@ -49,33 +54,45 @@ trait ComponentSpecBase extends UnitSpec
"microservice.services.government-gateway.host" -> mockHost,
"microservice.services.government-gateway.port" -> mockPort,
"microservice.services.authenticator.host" -> mockHost,
"microservice.services.authenticator.port" -> mockPort
"microservice.services.authenticator.port" -> mockPort,
"microservice.services.throttle-threshold" -> "2"
)

override def beforeAll(): Unit = {
super.beforeAll()
startWiremock()
}

override def beforeEach(): Unit = {
super.beforeEach()
await(throttleMongoRepository.dropDb)
}

override def afterAll(): Unit = {
stopWiremock()
super.afterAll()
}

lazy val throttleMongoRepository = app.injector.instanceOf[Repositories].throttleRepository

object IncomeTaxSubscription {
def getSubscriptionStatus(nino: String): WSResponse = get(s"/subscription/$nino")

def get(uri: String): WSResponse = await(buildClient(uri).get())

def createSubscription(body: FERequest): WSResponse = post(s"/subscription/${body.nino}", body)

def checkUserAccess(nino: String): WSResponse = get(s"/throttle/$nino")

def post[T](uri: String, body: T)(implicit writes: Writes[T]): WSResponse = {
await(
buildClient(uri)
.withHeaders("Content-Type" -> "application/json")
.post(writes.writes(body).toString())
)
}

def insertUserCount(userCount: UserCount)(implicit ec: ExecutionContext): WriteResult = await(throttleMongoRepository.insert(userCount))
}

}
29 changes: 29 additions & 0 deletions it/helpers/DatabaseHelpers.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2017 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package helpers

import models.throttling.UserCount
import helpers.servicemocks.AuthStub._
import uk.gov.hmrc.time.DateTimeUtils

object DatabaseHelpers {
val id = DateTimeUtils.now.toString("yyyy-MM-dd")

val maxUserCount = UserCount(id, Set("otherID1", "otherID2"), 0)
val matchingUserCount = UserCount(id, Set(internalID, "otherID"), 0)
val nonFullUserCount = UserCount(id, Set("otherID"), 0)
}
8 changes: 6 additions & 2 deletions it/helpers/servicemocks/AuthStub.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ object AuthStub {
val authIDs = "/uri/to/ids"
val authority = "/auth/authority"

val gatewayID = "12345"
val internalID = "internal"
val externalID = "external"

val stubbedAuthResponse: JsObject = {
Json.obj(
"uri" -> "/auth/oid/58a2e8c82e00008c005d4699",
"userDetailsLink" -> "/uri/to/user-details",
"credentials" -> Json.obj(
"gatewayId" -> "12345"
"gatewayId" -> gatewayID
),
"ids" -> authIDs
)
}

val stubbedIDs = UserIds(internalId = "internal", externalId = "external")
val stubbedIDs = UserIds(internalId = internalID, externalId = externalID)

def stubGetAuthoritySuccess(): Unit = {
val authBody = IntegrationTestConstants.Auth.authResponseJson("/auth/oid/58a2e8c82e00008c005d4699", "/uri/to/user-details", "12345", authIDs).toString()
Expand Down

0 comments on commit 4dc2264

Please sign in to comment.