Skip to content

Commit

Permalink
Merge pull request #86 from hmrc/SAR-672
Browse files Browse the repository at this point in the history
Sar 672
  • Loading branch information
suyangyu authored Jun 5, 2017
2 parents 4dc2264 + 31cb4c6 commit 8c0a427
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 224 deletions.
113 changes: 41 additions & 72 deletions it/controllers/subscription/SubscriptionControllerISpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,106 +16,75 @@

package controllers.subscription

import connectors._
import helpers.ComponentSpecBase
import helpers.IntegrationTestConstants._
import helpers.WireMockDSL.HTTPVerbMapping.{Get, Post}
import helpers.WireMockDSL._
import helpers.servicemocks.AuthStub._
import helpers.servicemocks.BusinessDetailsStub._
import helpers.servicemocks.GGAdminStub._
import helpers.servicemocks.SubscriptionStub._
import helpers.servicemocks._
import models.frontend.FESuccessResponse
import play.api.http.Status._
import play.api.libs.json.Json

class SubscriptionControllerISpec extends ComponentSpecBase {
"subscribe" should {
"call the subscription service successfully when auth succeeds for a business registration" in {
stub when Get(authority) thenReturn stubbedAuthResponse
stub when Get(authIDs) thenReturn stubbedIDs
multiline(
stub
when Post(RegistrationConnector.newRegistrationUri(testNino), registerRequestPayload)
thenReturn registrationResponse
)
multiline(
stub
when Post(SubscriptionConnector.businessSubscribeUri(testNino), businessSubscriptionRequestPayload)
thenReturn testBusinessSubscriptionResponse
)
stub when Post(GGAdminConnector.addKnownFactsUri) thenReturn testAddKnownFactsResponse
stub when Post(GGConnector.enrolUri) thenReturn OK
Given("I setup the wiremock stubs")
AuthStub.stubAuthSuccess()
RegistrationStub.stubNewRegistrationSuccess()
SubscriptionStub.stubBusinessSubscribeSuccess()
GGAdminStub.stubAddKnownFactsSuccess()
GGConnectorStub.stubEnrolSuccess()
AuthenticatorStub.stubRefreshProfileSuccess()

stub when Post(AuthenticatorConnector.refreshProfileUri) thenReturn NO_CONTENT
When("I call POST /subscription/:nino where nino is the test nino with a Business Request")
val res = IncomeTaxSubscription.createSubscription(feBusinessRequest)

IncomeTaxSubscription.createSubscription(feBusinessRequest) should have(
Then("The result should have a HTTP status of OK and a body containing the MTDID")
res should have(
httpStatus(OK),
jsonBodyAs[FESuccessResponse](FESuccessResponse(Some(testMtditId)))
)
}

"call the subscription service successfully when auth succeeds for a property registration" in {
stub when Get(authority) thenReturn stubbedAuthResponse
stub when Get(authIDs) thenReturn stubbedIDs
multiline(
stub
when Post(RegistrationConnector.newRegistrationUri(testNino), registerRequestPayload)
thenReturn registrationResponse
)
multiline(
stub
when Post(SubscriptionConnector.propertySubscribeUri(testNino), Json.obj())
thenReturn testPropertySubscriptionResponse
)
stub when Post(GGAdminConnector.addKnownFactsUri) thenReturn testAddKnownFactsResponse
stub when Post(GGConnector.enrolUri) thenReturn OK
stub when Post(AuthenticatorConnector.refreshProfileUri) thenReturn NO_CONTENT
Given("I setup the wiremock stubs")
AuthStub.stubAuthSuccess()
RegistrationStub.stubNewRegistrationSuccess()
SubscriptionStub.stubPropertySubscribeSuccess()
GGAdminStub.stubAddKnownFactsSuccess()
GGConnectorStub.stubEnrolSuccess()
AuthenticatorStub.stubRefreshProfileSuccess()

When("I call POST /subscription/:nino where nino is the test nino with a Property Request")
val res = IncomeTaxSubscription.createSubscription(fePropertyRequest)

IncomeTaxSubscription.createSubscription(fePropertyRequest) should have(
Then("The result should have a HTTP status of OK and a body containing the MTDID")
res should have(
httpStatus(OK),
jsonBodyAs[FESuccessResponse](FESuccessResponse(Some(testMtditId)))
)
}

"call the subscription service successfully when auth succeeds for a business and property registration" in {
stub when Get(authority) thenReturn stubbedAuthResponse
stub when Get(authIDs) thenReturn stubbedIDs
multiline(
stub
when Post(RegistrationConnector.newRegistrationUri(testNino), registerRequestPayload)
thenReturn registrationResponse
)
multiline(
stub
when Post(SubscriptionConnector.propertySubscribeUri(testNino), Json.obj())
thenReturn testPropertySubscriptionResponse
)
multiline(
stub
when Post(SubscriptionConnector.businessSubscribeUri(testNino), businessSubscriptionRequestPayload)
thenReturn testBusinessSubscriptionResponse
)
stub when Post(GGAdminConnector.addKnownFactsUri) thenReturn testAddKnownFactsResponse
stub when Post(GGConnector.enrolUri) thenReturn OK
stub when Post(AuthenticatorConnector.refreshProfileUri) thenReturn NO_CONTENT
Given("I setup the wiremock stubs")
AuthStub.stubAuthSuccess()
RegistrationStub.stubNewRegistrationSuccess()
SubscriptionStub.stubBusinessSubscribeSuccess()
SubscriptionStub.stubPropertySubscribeSuccess()
GGAdminStub.stubAddKnownFactsSuccess()
GGConnectorStub.stubEnrolSuccess()
AuthenticatorStub.stubRefreshProfileSuccess()

When("I call POST /subscription/:nino where nino is the test nino with both a property request and a business request")
val res = IncomeTaxSubscription.createSubscription(feBothRequest)

IncomeTaxSubscription.createSubscription(feBothRequest) should have(
Then("The result should have a HTTP status of OK and a body containing the MTDID")
res should have(
httpStatus(OK),
jsonBodyAs[FESuccessResponse](FESuccessResponse(Some(testMtditId)))
)

stub verify Post(SubscriptionConnector.businessSubscribeUri(testNino), businessSubscriptionRequestPayload)
stub verify Post(SubscriptionConnector.propertySubscribeUri(testNino), Json.obj())
}

"fail when get authority fails" in {
stubGetAuthorityFailure()
Then("Business subscription should have been called")
SubscriptionStub.verifyBusinessSubscribe()

IncomeTaxSubscription.createSubscription(feBusinessRequest) should have(
httpStatus(UNAUTHORIZED),
emptyBody
)
Then("Property subscription should have been called")
SubscriptionStub.verifyPropertySubscribe()
}
}
}
30 changes: 11 additions & 19 deletions it/controllers/subscription/SubscriptionStatusControllerISpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,30 @@

package controllers.subscription

import connectors.BusinessDetailsConnector._
import helpers.ComponentSpecBase
import helpers.IntegrationTestConstants._
import helpers.WireMockDSL.HTTPVerbMapping.Get
import helpers.WireMockDSL._
import helpers.servicemocks.AuthStub._
import helpers.servicemocks.BusinessDetailsStub._
import helpers.servicemocks.{AuthStub, BusinessDetailsStub}
import models.frontend.FESuccessResponse
import play.api.http.Status._

class SubscriptionStatusControllerISpec extends ComponentSpecBase {
"subscribe" should {
"call the subscription service successfully when auth succeeds" in {
stub when Get(authority) thenReturn stubbedAuthResponse
stub when Get(authIDs) thenReturn stubbedIDs
stub when Get(getBusinessDetailsUri(testNino)) thenReturn registrationResponse
Given("I setup the wiremock stubs")
AuthStub.stubAuthSuccess()
BusinessDetailsStub.stubGetBusinessDetailsSuccess()

IncomeTaxSubscription.getSubscriptionStatus(testNino) should have(
When("I call GET /subscription/:nino where nino is the test nino")
val res = IncomeTaxSubscription.getSubscriptionStatus(testNino)

Then("The result should have a HTTP status of OK and a body containing the MTDID")
res should have(
httpStatus(OK),
jsonBodyAs[FESuccessResponse](FESuccessResponse(Some(testMtditId)))
)

stub verify Get(getBusinessDetailsUri(testNino))
}

"fail when get authority fails" in {
stubGetAuthorityFailure()

IncomeTaxSubscription.getSubscriptionStatus(testNino) should have(
httpStatus(UNAUTHORIZED),
emptyBody
)
Then("Get business details should have been called")
BusinessDetailsStub.verifyGetBusinessDetails()
}
}
}
53 changes: 36 additions & 17 deletions it/controllers/throttling/UserAccessControllerISpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,73 @@
package controllers.throttling

import helpers.ComponentSpecBase
import helpers.WireMockDSL.HTTPVerbMapping.Get
import helpers.WireMockDSL._
import helpers.servicemocks.AuthStub._
import helpers.DatabaseHelpers._
import helpers.IntegrationTestConstants._
import helpers.servicemocks.AuthStub
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
Given("The database is empty")
IncomeTaxSubscription.dropThrottleRepo()

Given("I setup the wiremock stubs")
AuthStub.stubAuthSuccess()

When("I call the endpoint")
val res = IncomeTaxSubscription.checkUserAccess(testNino)

IncomeTaxSubscription.checkUserAccess(testNino) should have (
Then("The result should have a HTTP status of OK")
res should have (
httpStatus(OK)
)
}

"return OK when the service has not received too many requests" in {
Given("The database contains a non full user count record")
IncomeTaxSubscription.insertUserCount(nonFullUserCount)

stub when Get(authority) thenReturn stubbedAuthResponse
stub when Get(authIDs) thenReturn stubbedIDs
Given("I setup the wiremock stubs")
AuthStub.stubAuthSuccess()

IncomeTaxSubscription.checkUserAccess(testNino) should have (
When("I call the endpoint")
val res = IncomeTaxSubscription.checkUserAccess(testNino)

Then("The result should have a HTTP status of OK")
res should have (
httpStatus(OK)
)
}

"return OK for a returning user" in {
Given("The database contains a full user count record which contains the matching user")
IncomeTaxSubscription.insertUserCount(matchingUserCount)

stub when Get(authority) thenReturn stubbedAuthResponse
stub when Get(authIDs) thenReturn stubbedIDs
Given("I setup the wiremock stubs")
AuthStub.stubAuthSuccess()

When("I call the endpoint")
val res = IncomeTaxSubscription.checkUserAccess(testNino)

IncomeTaxSubscription.checkUserAccess(testNino) should have (
Then("The result should have a HTTP status of OK")
res should have (
httpStatus(OK)
)
}

"return TOO_MANY_REQUESTS when the service has received too many requests and it is a new userID" in {
Given("The database contains a full user count record which contains the matching user")
IncomeTaxSubscription.insertUserCount(maxUserCount)

stub when Get(authority) thenReturn stubbedAuthResponse
stub when Get(authIDs) thenReturn stubbedIDs
Given("I setup the wiremock stubs")
AuthStub.stubAuthSuccess()

When("I call the endpoint")
val res = IncomeTaxSubscription.checkUserAccess(testNino)

IncomeTaxSubscription.checkUserAccess(testNino) should have (
Then("The result should have a HTTP status of TOO_MANY_REQUESTS")
res should have (
httpStatus(TOO_MANY_REQUESTS)
)
}
Expand Down
9 changes: 6 additions & 3 deletions it/helpers/ComponentSpecBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package helpers

import helpers.servicemocks.WireMockMethods
import models.frontend.FERequest
import models.throttling.UserCount
import org.scalatest._
Expand All @@ -28,14 +29,15 @@ import play.api.{Application, Environment, Mode}
import reactivemongo.api.commands.WriteResult
import repositories.{Repositories, ThrottleMongoRepository}
import uk.gov.hmrc.play.test.UnitSpec
import play.api.libs.concurrent.Execution.Implicits._

import scala.concurrent.ExecutionContext

trait ComponentSpecBase extends UnitSpec
with GivenWhenThen with TestSuite
with GuiceOneServerPerSuite with ScalaFutures with IntegrationPatience with Matchers
with WiremockHelper with BeforeAndAfterEach with BeforeAndAfterAll with Eventually
with CustomMatchers {
with CustomMatchers with WireMockMethods {

override implicit lazy val app: Application = new GuiceApplicationBuilder()
.in(Environment.simple(mode = Mode.Dev))
Expand Down Expand Up @@ -65,7 +67,7 @@ trait ComponentSpecBase extends UnitSpec

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

override def afterAll(): Unit = {
Expand All @@ -92,7 +94,8 @@ trait ComponentSpecBase extends UnitSpec
)
}

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

}
Loading

0 comments on commit 8c0a427

Please sign in to comment.