|
| 1 | +/* |
| 2 | + * Copyright 2025 HM Revenue & Customs |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package v1 |
| 18 | + |
| 19 | +import api.models.errors.{MtdError, NinoFormatError, NotFoundError, RuleTaxYearNotSupportedError, RuleTaxYearRangeInvalidError, TaxYearFormatError} |
| 20 | +import api.services.{AuditStub, AuthStub, DownstreamStub, MtdIdLookupStub} |
| 21 | +import api.support.IntegrationBaseSpec |
| 22 | +import com.github.tomakehurst.wiremock.stubbing.StubMapping |
| 23 | +import play.api.libs.json.Json |
| 24 | +import play.api.libs.ws.{WSRequest, WSResponse} |
| 25 | +import play.api.test.Helpers.{ACCEPT, AUTHORIZATION, BAD_REQUEST, NOT_FOUND, NO_CONTENT, UNPROCESSABLE_ENTITY} |
| 26 | + |
| 27 | +class DeleteDisclosuresControllerHipISpec extends IntegrationBaseSpec { |
| 28 | + |
| 29 | + private trait Test { |
| 30 | + |
| 31 | + val nino: String = "AA123456A" |
| 32 | + val taxYear: String = "2021-22" |
| 33 | + |
| 34 | + private def uri: String = s"/$nino/$taxYear" |
| 35 | + |
| 36 | + def HipUri: String = s"/itsd/disclosures/$nino/$taxYear" |
| 37 | + |
| 38 | + def setupStubs(): StubMapping |
| 39 | + |
| 40 | + def request(): WSRequest = { |
| 41 | + setupStubs() |
| 42 | + buildRequest(uri) |
| 43 | + .withHttpHeaders( |
| 44 | + (ACCEPT, "application/vnd.hmrc.1.0+json"), |
| 45 | + (AUTHORIZATION, "Bearer 123") // some bearer token |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + "Calling the 'delete disclosures' endpoint" should { |
| 52 | + "return a 204 status code" when { |
| 53 | + "any valid request is made" in new Test { |
| 54 | + |
| 55 | + override def setupStubs(): StubMapping = { |
| 56 | + AuditStub.audit() |
| 57 | + AuthStub.authorised() |
| 58 | + MtdIdLookupStub.ninoFound(nino) |
| 59 | + DownstreamStub.onSuccess(DownstreamStub.DELETE, HipUri, NO_CONTENT) |
| 60 | + } |
| 61 | + |
| 62 | + val response: WSResponse = await(request().delete()) |
| 63 | + response.status shouldBe NO_CONTENT |
| 64 | + response.header("X-CorrelationId").nonEmpty shouldBe true |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + "return error according to spec" when { |
| 69 | + |
| 70 | + "validation error" when { |
| 71 | + def validationErrorTest(requestNino: String, requestTaxYear: String, expectedStatus: Int, expectedBody: MtdError): Unit = { |
| 72 | + s"validation fails with ${expectedBody.code} error" in new Test { |
| 73 | + |
| 74 | + override val nino: String = requestNino |
| 75 | + override val taxYear: String = requestTaxYear |
| 76 | + |
| 77 | + override def setupStubs(): StubMapping = { |
| 78 | + AuditStub.audit() |
| 79 | + AuthStub.authorised() |
| 80 | + MtdIdLookupStub.ninoFound(nino) |
| 81 | + } |
| 82 | + |
| 83 | + val response: WSResponse = await(request().delete()) |
| 84 | + response.status shouldBe expectedStatus |
| 85 | + response.json shouldBe Json.toJson(expectedBody) |
| 86 | + response.header("Content-Type") shouldBe Some("application/json") |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + val input = Seq( |
| 91 | + ("AA1123A", "2021-22", BAD_REQUEST, NinoFormatError), |
| 92 | + ("AA123456A", "2017-18", BAD_REQUEST, RuleTaxYearNotSupportedError), |
| 93 | + ("AA123456A", "20177", BAD_REQUEST, TaxYearFormatError), |
| 94 | + ("AA123456A", "2015-17", BAD_REQUEST, RuleTaxYearRangeInvalidError) |
| 95 | + ) |
| 96 | + input.foreach(args => (validationErrorTest _).tupled(args)) |
| 97 | + } |
| 98 | + |
| 99 | + "hip service error" when { |
| 100 | + def serviceErrorTest(hipStatus: Int, hipCode: String, expectedStatus: Int, expectedBody: MtdError): Unit = { |
| 101 | + s"ifs returns an $hipCode error and status $hipStatus" in new Test { |
| 102 | + |
| 103 | + override def setupStubs(): StubMapping = { |
| 104 | + AuditStub.audit() |
| 105 | + AuthStub.authorised() |
| 106 | + MtdIdLookupStub.ninoFound(nino) |
| 107 | + DownstreamStub.onError(DownstreamStub.DELETE, HipUri, hipStatus, errorBody(hipCode)) |
| 108 | + } |
| 109 | + |
| 110 | + val response: WSResponse = await(request().delete()) |
| 111 | + response.status shouldBe expectedStatus |
| 112 | + response.json shouldBe Json.toJson(expectedBody) |
| 113 | + response.header("Content-Type") shouldBe Some("application/json") |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + def errorBody(code: String): String = |
| 118 | + s""" |
| 119 | + |[ |
| 120 | + | { |
| 121 | + | "errorCode": "$code", |
| 122 | + | "errorDescription": "error message" |
| 123 | + | } |
| 124 | + |] |
| 125 | + """.stripMargin |
| 126 | + |
| 127 | + val input = Seq( |
| 128 | + (BAD_REQUEST, "1215", BAD_REQUEST, NinoFormatError), |
| 129 | + (BAD_REQUEST, "1117", BAD_REQUEST, TaxYearFormatError), |
| 130 | + (NOT_FOUND, "5010", NOT_FOUND, NotFoundError), |
| 131 | + (UNPROCESSABLE_ENTITY, "5000", BAD_REQUEST, RuleTaxYearNotSupportedError) |
| 132 | + ) |
| 133 | + input.foreach(args => (serviceErrorTest _).tupled(args)) |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments