Skip to content

Commit

Permalink
SASS-9686 Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StuartJamesProctor committed Nov 27, 2024
1 parent 8a8ca84 commit db72a90
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import forms.foreign.expenses.ConsolidatedOrIndividualExpensesFormProvider
import javax.inject.Inject
import models.{ConsolidatedOrIndividualExpenses, Mode}
import navigation.ForeignPropertyNavigator
import pages.foreign.CalculatedPremiumLeaseTaxablePage
import pages.foreign.expenses.ConsolidatedOrIndividualExpensesPage
import play.api.i18n.{MessagesApi, I18nSupport}
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
Expand All @@ -35,7 +36,7 @@ import scala.concurrent.{ExecutionContext, Future}
class ConsolidatedOrIndividualExpensesController @Inject()(
override val messagesApi: MessagesApi,
sessionRepository: SessionRepository,
navigator: ForeignPropertyNavigator,
foreignPropertyNavigator: ForeignPropertyNavigator,
identify: IdentifierAction,
getData: DataRetrievalAction,
requireData: DataRequiredAction,
Expand Down Expand Up @@ -66,7 +67,7 @@ class ConsolidatedOrIndividualExpensesController @Inject()(
for {
updatedAnswers <- Future.fromTry(request.userAnswers.set(ConsolidatedOrIndividualExpensesPage(countryCode), value))
_ <- sessionRepository.set(updatedAnswers)
} yield Redirect(controllers.foreign.expenses.routes.ConsolidatedOrIndividualExpensesController.onPageLoad(taxYear, countryCode, mode).url)
} yield Redirect(foreignPropertyNavigator.nextPage(ConsolidatedOrIndividualExpensesPage(countryCode), taxYear, mode, request.userAnswers, updatedAnswers))
)
}
}
2 changes: 1 addition & 1 deletion conf/messages.cy
Original file line number Diff line number Diff line change
Expand Up @@ -2128,7 +2128,7 @@ consolidatedOrIndividualExpenses.radio.legend.individual = *Missing Welsh*
consolidatedOrIndividualExpenses.radio.legend.agent = *Missing Welsh*
consolidatedOrIndividualExpenses.radio.question.label = *Missing Welsh*
consolidatedOrIndividualExpenses.checkYourAnswersLabel = *Missing Welsh*
onsolidatedOrIndividualExpenses.error.required.individual = *Missing Welsh*
consolidatedOrIndividualExpenses.error.required.individual = *Missing Welsh*
consolidatedOrIndividualExpenses.error.required.agent = *Missing Welsh*
consolidatedOrIndividualExpenses.amount.error.required.individual = *Missing Welsh*
consolidatedOrIndividualExpenses.amount.error.required.agent = *Missing Welsh*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,57 @@
package controllers
/*
* Copyright 2024 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.foreign.expenses

import base.SpecBase
import controllers.routes
import controllers.foreign.expenses.routes.ConsolidatedOrIndividualExpensesController
import forms.foreign.expenses.ConsolidatedOrIndividualExpensesFormProvider
import models.{NormalMode, ConsolidatedOrIndividualExpenses, UserAnswers}
import navigation.{FakeNavigator, Navigator}
import models.{ConsolidatedOrIndividualExpenses, UserAnswers, NormalMode}
import navigation.{FakeForeignPropertyNavigator, ForeignPropertyNavigator}
import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito.when
import org.scalatestplus.mockito.MockitoSugar
import pages.foreign.expenses.ConsolidatedOrIndividualExpensesPage
import play.api.data.Form
import play.api.inject.bind
import play.api.mvc.Call
import play.api.test.FakeRequest
import play.api.test.Helpers._
import repositories.SessionRepository
import views.html.ConsolidatedOrIndividualExpensesView
import views.html.foreign.expenses.ConsolidatedOrIndividualExpensesView

import scala.concurrent.Future

class ConsolidatedOrIndividualExpensesControllerSpec extends SpecBase with MockitoSugar {

def onwardRoute = Call("GET", "/foo")

lazy val consolidatedOrIndividualExpensesRoute = routes.ConsolidatedOrIndividualExpensesController.onPageLoad(NormalMode).url

val isAgentMessageKey = "agent"
val formProvider = new ConsolidatedOrIndividualExpensesFormProvider()
val form = formProvider()
val form: Form[ConsolidatedOrIndividualExpenses] = formProvider(isAgentMessageKey)
val taxYear: Int = 2024
val countryCode: String = "GRC"

lazy val consolidatedOrIndividualExpensesRoute = ConsolidatedOrIndividualExpensesController.onPageLoad(taxYear, countryCode, NormalMode).url

"ConsolidatedOrIndividualExpenses Controller" - {

"must return OK and the correct view for a GET" in {

val application = applicationBuilder(userAnswers = Some(emptyUserAnswers)).build()
val application = applicationBuilder(userAnswers = Some(emptyUserAnswers), isAgent = true).build()

running(application) {
val request = FakeRequest(GET, consolidatedOrIndividualExpensesRoute)
Expand All @@ -40,15 +61,23 @@ class ConsolidatedOrIndividualExpensesControllerSpec extends SpecBase with Mocki
val view = application.injector.instanceOf[ConsolidatedOrIndividualExpensesView]

status(result) mustEqual OK
contentAsString(result) mustEqual view(form, NormalMode)(request, messages(application)).toString
contentAsString(result) mustEqual view(form, NormalMode, isAgentMessageKey, taxYear, countryCode)(request, messages(application)).toString
}
}

"must populate the view correctly on a GET when the question has previously been answered" in {

val userAnswers = UserAnswers(userAnswersId).set(ConsolidatedOrIndividualExpensesPage, ConsolidatedOrIndividualExpenses.values.head).success.value
val amount = 100
val formAnswers = ConsolidatedOrIndividualExpenses(
consolidatedOrIndividualExpensesYesNo = true,
Some(amount)
)
val userAnswers = UserAnswers(userAnswersId).set(
ConsolidatedOrIndividualExpensesPage(countryCode),
formAnswers
).success.value

val application = applicationBuilder(userAnswers = Some(userAnswers)).build()
val application = applicationBuilder(userAnswers = Some(userAnswers), isAgent = true).build()

running(application) {
val request = FakeRequest(GET, consolidatedOrIndividualExpensesRoute)
Expand All @@ -58,28 +87,28 @@ class ConsolidatedOrIndividualExpensesControllerSpec extends SpecBase with Mocki
val result = route(application, request).value

status(result) mustEqual OK
contentAsString(result) mustEqual view(form.fill(ConsolidatedOrIndividualExpenses.values.head), NormalMode)(request, messages(application)).toString
contentAsString(result) mustEqual view(form.fill(formAnswers), NormalMode, isAgentMessageKey, taxYear, countryCode)(request, messages(application)).toString
}
}

"must redirect to the next page when valid data is submitted" in {
"must redirect to the next page when valid data is submitted for yes" in {

val mockSessionRepository = mock[SessionRepository]

when(mockSessionRepository.set(any())) thenReturn Future.successful(true)

val application =
applicationBuilder(userAnswers = Some(emptyUserAnswers))
applicationBuilder(userAnswers = Some(emptyUserAnswers), isAgent = true)
.overrides(
bind[Navigator].toInstance(new FakeNavigator(onwardRoute)),
bind[ForeignPropertyNavigator].toInstance(new FakeForeignPropertyNavigator(onwardRoute)),
bind[SessionRepository].toInstance(mockSessionRepository)
)
.build()

running(application) {
val request =
FakeRequest(POST, consolidatedOrIndividualExpensesRoute)
.withFormUrlEncodedBody(("value", ConsolidatedOrIndividualExpenses.values.head.toString))
.withFormUrlEncodedBody(("consolidatedOrIndividualExpenses", "false"), ("consolidatedExpensesAmount", "100"))

val result = route(application, request).value

Expand All @@ -90,27 +119,27 @@ class ConsolidatedOrIndividualExpensesControllerSpec extends SpecBase with Mocki

"must return a Bad Request and errors when invalid data is submitted" in {

val application = applicationBuilder(userAnswers = Some(emptyUserAnswers)).build()
val application = applicationBuilder(userAnswers = Some(emptyUserAnswers), isAgent = true).build()

running(application) {
val request =
FakeRequest(POST, consolidatedOrIndividualExpensesRoute)
.withFormUrlEncodedBody(("value", "invalid value"))
.withFormUrlEncodedBody(("consolidatedOrIndividualExpensesYesNo", "invalid value"))

val boundForm = form.bind(Map("value" -> "invalid value"))
val boundForm = form.bind(Map("consolidatedOrIndividualExpensesYesNo" -> "invalid value"))

val view = application.injector.instanceOf[ConsolidatedOrIndividualExpensesView]

val result = route(application, request).value

status(result) mustEqual BAD_REQUEST
contentAsString(result) mustEqual view(boundForm, NormalMode)(request, messages(application)).toString
contentAsString(result) mustEqual view(boundForm, NormalMode, isAgentMessageKey, taxYear, countryCode)(request, messages(application)).toString
}
}

"must redirect to Journey Recovery for a GET if no existing data is found" in {

val application = applicationBuilder(userAnswers = None, true).build()
val application = applicationBuilder(userAnswers = None, isAgent = true).build()

running(application) {
val request = FakeRequest(GET, consolidatedOrIndividualExpensesRoute)
Expand All @@ -124,12 +153,12 @@ class ConsolidatedOrIndividualExpensesControllerSpec extends SpecBase with Mocki

"redirect to Journey Recovery for a POST if no existing data is found" in {

val application = applicationBuilder(userAnswers = None, true).build()
val application = applicationBuilder(userAnswers = None, isAgent = true).build()

running(application) {
val request =
FakeRequest(POST, consolidatedOrIndividualExpensesRoute)
.withFormUrlEncodedBody(("value", ConsolidatedOrIndividualExpenses.values.head.toString))
.withFormUrlEncodedBody(("value", "true"))

val result = route(application, request).value

Expand Down
30 changes: 0 additions & 30 deletions test/forms/ConsolidatedOrIndividualExpensesFormProviderSpec.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2024 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 forms.foreign.expenses

import forms.behaviours.OptionFieldBehaviours
import models.ConsolidatedOrIndividualExpenses
import play.api.data.FormError

import scala.collection.immutable.ArraySeq

class ConsolidatedOrIndividualExpensesFormProviderSpec extends OptionFieldBehaviours {

val requiredKey = "consolidatedOrIndividualExpenses.error.required.agent"
val invalidKey = "error.boolean"

val form = new ConsolidatedOrIndividualExpensesFormProvider()("agent")

".consolidatedExpensesAmount" - {
"consolidatedOrIndividualExpenses" - {
"and an amount is entered, should successfully bind" in {
val boundForm = form.bind(Map("consolidatedOrIndividualExpenses" -> "true", "consolidatedExpensesAmount" -> "4534.65"))
boundForm.value.value mustBe ConsolidatedOrIndividualExpenses(consolidatedOrIndividualExpensesYesNo = true, Some(4534.65))
boundForm.errors mustBe empty
}

"and no amount is entered, should fail to bind" in {
val boundForm = form.bind(Map("consolidatedOrIndividualExpenses" -> "true"))
boundForm.errors must contain(FormError("consolidatedExpensesAmount", "consolidatedOrIndividualExpenses.amount.error.required.agent"))
}

"and a non numeric value is entered then should fail to bind" in {
val boundForm = form.bind(Map("consolidatedOrIndividualExpenses" -> "true", "consolidatedExpensesAmount" -> "non-numeric-value"))
boundForm.errors must contain(FormError("consolidatedExpensesAmount", "consolidatedOrIndividualExpenses.amount.error.nonNumerical"))
}


"and an amount is entered that has more than 2 decimal places then it should fail to bind" in {
val boundForm = form.bind(Map("consolidatedOrIndividualExpenses" -> "true", "consolidatedExpensesAmount" -> "4534.6545"))
boundForm.errors must contain(FormError("consolidatedExpensesAmount", "consolidatedOrIndividualExpenses.amount.error.twoDecimalPlaces.agent"))
}


"and an amount is entered that is out of range then should fail to bind" in {
val boundForm = form.bind(Map("consolidatedOrIndividualExpenses" -> "true", "consolidatedExpensesAmount" -> "4533455353453534543534"))
boundForm.errors must contain(
FormError(
"consolidatedExpensesAmount",
"consolidatedOrIndividualExpenses.amount.error.outOfRange",
ArraySeq(0, 1000000000)
)
)
}
}
}
}
48 changes: 0 additions & 48 deletions test/models/ConsolidatedOrIndividualExpensesSpec.scala

This file was deleted.

0 comments on commit db72a90

Please sign in to comment.