-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathTaxCodeChangeControllerSpec.scala
More file actions
206 lines (166 loc) · 8.08 KB
/
TaxCodeChangeControllerSpec.scala
File metadata and controls
206 lines (166 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright 2025 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
import builders.RequestBuilder
import cats.data.EitherT
import cats.instances.future.*
import controllers.auth.AuthenticatedRequest
import org.mockito.ArgumentMatchers.{any, eq as meq}
import org.mockito.Mockito.when
import play.api.mvc.AnyContentAsFormUrlEncoded
import play.api.test.FakeRequest
import play.api.test.Helpers.{status, *}
import uk.gov.hmrc.http.UpstreamErrorResponse
import uk.gov.hmrc.tai.model.TaxYear
import uk.gov.hmrc.tai.model.domain.income.OtherBasisOfOperation
import uk.gov.hmrc.tai.model.domain.tax.TotalTax
import uk.gov.hmrc.tai.model.domain.{TaxCodeChange, TaxCodeRecord}
import uk.gov.hmrc.tai.service.*
import uk.gov.hmrc.tai.service.yourTaxFreeAmount.{DescribedYourTaxFreeAmountService, TaxCodeChangeReasonsService}
import uk.gov.hmrc.tai.util.yourTaxFreeAmount.TaxFreeInfo
import uk.gov.hmrc.tai.viewModels.taxCodeChange.{TaxCodeChangeViewModel, YourTaxFreeAmountViewModel}
import utils.BaseSpec
import views.html.taxCodeChange.{TaxCodeComparisonView, WhatHappensNextView, YourTaxFreeAmountView}
import java.time.LocalDate
import scala.concurrent.Future
class TaxCodeChangeControllerSpec extends BaseSpec with ControllerViewTestHelper {
"whatHappensNext" must {
"show 'What happens next' page" when {
"the request has an authorised session" in {
implicit val request: AuthenticatedRequest[AnyContentAsFormUrlEncoded] =
AuthenticatedRequest(RequestBuilder.buildFakeRequestWithAuth("GET"), authedUser, fakePerson(nino))
val result = createController().whatHappensNext()(request)
status(result) mustBe OK
result rendersTheSameViewAs whatHappensNextView()
}
}
}
"yourTaxFreeAmount" must {
"show 'Your tax-free amount' page with comparison" when {
"taxFreeAmountComparison is enabled and the request has an authorised session" in {
val expectedViewModel: YourTaxFreeAmountViewModel =
YourTaxFreeAmountViewModel(
Some(TaxFreeInfo("previousTaxDate", 0, 0)),
TaxFreeInfo("currentTaxDate", 0, 0),
Seq.empty,
Seq.empty
)
when(
describedYourTaxFreeAmountService.taxFreeAmountComparison(meq(FakeAuthRetrievals.nino))(any(), any(), any())
)
.thenReturn(Future.successful(expectedViewModel))
val result = createController().yourTaxFreeAmount()(RequestBuilder.buildFakeRequestWithAuth("GET"))
status(result) mustBe OK
result rendersTheSameViewAs yourTaxFreeAmountView(expectedViewModel)(
AuthenticatedRequest(
RequestBuilder
.buildFakeRequestWithAuth("GET"),
authedUser,
fakePerson(nino)
),
implicitly,
implicitly
)
}
}
}
"taxCodeComparison" must {
"show 'Your tax code comparison' page" in {
implicit val request: FakeRequest[AnyContentAsFormUrlEncoded] = RequestBuilder.buildFakeRequestWithAuth("GET")
val taxCodeChange = TaxCodeChange(List(taxCodeRecord1), List(taxCodeRecord2))
val scottishRates = Map.empty[String, BigDecimal]
when(taxCodeChangeService.hasTaxCodeChanged(any())(any())).thenReturn(EitherT.rightT(true))
when(taxAccountService.scottishBandRates(any(), any(), any())(any()))
.thenReturn(Future.successful(Map[String, BigDecimal]()))
when(taxAccountService.totalTax(meq(FakeAuthRetrievals.nino), any())(any()))
.thenReturn(Future.successful(TotalTax(0, Seq.empty, None, None, None)))
when(taxCodeChangeService.taxCodeChange(any())(any())).thenReturn(EitherT.rightT(taxCodeChange))
when(yourTaxFreeAmountService.taxFreeAmountComparison(any(), any())(any(), any(), any()))
.thenReturn(Future.successful(mock[YourTaxFreeAmountComparison]))
val reasons = Seq("a reason")
when(taxCodeChangeReasonsService.combineTaxCodeChangeReasons(any(), any(), meq(taxCodeChange))(any()))
.thenReturn(reasons)
when(taxCodeChangeReasonsService.isAGenericReason(meq(reasons))(any())).thenReturn(false)
val result = createController().taxCodeComparison()(request)
val expectedViewModel =
TaxCodeChangeViewModel(
taxCodeChange,
scottishRates,
reasons,
isAGenericReason = false,
maybeUserName = Some("Firstname Surname")
)
status(result) mustBe OK
result rendersTheSameViewAs taxCodeComparisonView(expectedViewModel, appConfig)
}
"show an error page when there is any issue in upstream, though hasTaxCodeChanged is true" in {
implicit val request: FakeRequest[AnyContentAsFormUrlEncoded] = RequestBuilder.buildFakeRequestWithAuth("GET")
val taxCodeChange = TaxCodeChange(List(taxCodeRecord1), List(taxCodeRecord2))
when(taxCodeChangeService.hasTaxCodeChanged(any())(any())).thenReturn(EitherT.rightT(true))
when(taxCodeChangeService.taxCodeChange(any())(any())).thenReturn(EitherT.rightT(taxCodeChange))
when(yourTaxFreeAmountService.taxFreeAmountComparison(any(), any())(any(), any(), any()))
.thenReturn(Future.failed(UpstreamErrorResponse("Error from upstream", INTERNAL_SERVER_ERROR)))
val result = createController().taxCodeComparison()(request)
status(result) mustBe INTERNAL_SERVER_ERROR
}
"show the tax account summary when hasTaxCodeChanged is false" in {
implicit val request: FakeRequest[AnyContentAsFormUrlEncoded] = RequestBuilder.buildFakeRequestWithAuth("GET")
when(taxCodeChangeService.hasTaxCodeChanged(any())(any())).thenReturn(EitherT.rightT(false))
val result = createController().taxCodeComparison()(request)
status(result) mustBe SEE_OTHER
redirectLocation(result) mustBe Some(controllers.routes.TaxAccountSummaryController.onPageLoad().url)
}
}
val giftAmount = 1000
val startDate: LocalDate = TaxYear().start
val taxCodeRecord1: TaxCodeRecord = TaxCodeRecord(
"D0",
startDate,
startDate.plusDays(1),
OtherBasisOfOperation,
"Employer 1",
pensionIndicator = false,
Some("1234"),
primary = true
)
val taxCodeRecord2: TaxCodeRecord = taxCodeRecord1.copy(startDate = startDate.plusDays(1), endDate = TaxYear().end)
val personService: PersonService = mock[PersonService]
val taxCodeChangeService: TaxCodeChangeService = mock[TaxCodeChangeService]
val taxAccountService: TaxAccountService = mock[TaxAccountService]
val describedYourTaxFreeAmountService: DescribedYourTaxFreeAmountService = mock[DescribedYourTaxFreeAmountService]
val yourTaxFreeAmountService: YourTaxFreeAmountService = mock[YourTaxFreeAmountService]
val taxCodeChangeReasonsService: TaxCodeChangeReasonsService = mock[TaxCodeChangeReasonsService]
private def createController() = new TaxCodeChangeTestController
private val whatHappensNextView = inject[WhatHappensNextView]
private val yourTaxFreeAmountView = inject[YourTaxFreeAmountView]
private val taxCodeComparisonView = inject[TaxCodeComparisonView]
private val errorPagesHandler = inject[ErrorPagesHandler]
private class TaxCodeChangeTestController
extends TaxCodeChangeController(
taxCodeChangeService,
taxAccountService,
describedYourTaxFreeAmountService,
mockAuthJourney,
yourTaxFreeAmountService,
taxCodeChangeReasonsService,
appConfig,
mcc,
taxCodeComparisonView,
yourTaxFreeAmountView,
whatHappensNextView,
errorPagesHandler
) {}
}