diff --git a/app/uk/gov/hmrc/ngrraldfrontend/controllers/InterimRentSetByTheCourtController.scala b/app/uk/gov/hmrc/ngrraldfrontend/controllers/InterimRentSetByTheCourtController.scala index 84e36795..0fd756a0 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/controllers/InterimRentSetByTheCourtController.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/controllers/InterimRentSetByTheCourtController.scala @@ -29,6 +29,7 @@ import uk.gov.hmrc.ngrraldfrontend.models.forms.InterimRentSetByTheCourtForm.for import uk.gov.hmrc.ngrraldfrontend.navigation.Navigator import uk.gov.hmrc.ngrraldfrontend.pages.InterimSetByTheCourtPage import uk.gov.hmrc.ngrraldfrontend.repo.SessionRepository +import uk.gov.hmrc.ngrraldfrontend.utils.DateKeyFinder import uk.gov.hmrc.ngrraldfrontend.views.html.InterimRentSetByTheCourtView import uk.gov.hmrc.ngrraldfrontend.views.html.components.InputText import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendController @@ -43,7 +44,7 @@ class InterimRentSetByTheCourtController @Inject()(interimRentSetByTheCourtView: sessionRepository: SessionRepository, navigator: Navigator, mcc: MessagesControllerComponents)(implicit appConfig: AppConfig, ec: ExecutionContext) - extends FrontendController(mcc) with I18nSupport { + extends FrontendController(mcc) with I18nSupport with DateKeyFinder { def generateInputText(form: Form[InterimRentSetByTheCourtForm], inputFieldName: String)(implicit messages: Messages): HtmlFormat.Appendable = { inputText( @@ -80,13 +81,8 @@ class InterimRentSetByTheCourtController @Inject()(interimRentSetByTheCourtView: formWithErrors => { val correctedFormErrors = formWithErrors.errors.map { formError => (formError.key, formError.messages) match - case (key, messages) if messages.contains("interimRentSetByTheCourt.startDate.before.1900.error") || - messages.contains("interimRentSetByTheCourt.year.required.error") => - formError.copy(key = "date.year") - case ("date", messages) if messages.contains("interimRentSetByTheCourt.year.format.error") => - formError.copy(key = "date.year") - case ("date", messages) => - formError.copy(key = "date.month") + case (key, messages) if messages.head.contains("interimRentSetByTheCourt.date") => + setCorrectKey(formError, "interimRentSetByTheCourt", "date") case _ => formError } diff --git a/app/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutRentController.scala b/app/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutRentController.scala index 7ee67bcd..352bddf1 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutRentController.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutRentController.scala @@ -49,7 +49,9 @@ class TellUsAboutRentController @Inject()(view: TellUsAboutYourAgreementView, def submit: Action[AnyContent] = { (authenticate andThen getData).async { implicit request => for { - updatedAnswers <- Future.fromTry(request.userAnswers.getOrElse(UserAnswers(request.credId)).set(TellUsAboutRentPage, RentAgreement)) + updatedAnswers <- Future.fromTry(request.userAnswers + .map(answers => answers.getCurrentJourneyUserAnswers(TellUsAboutRentPage, answers, request.credId)) + .getOrElse(UserAnswers(request.credId)).set(TellUsAboutRentPage, RentAgreement)) _ <- sessionRepository.set(updatedAnswers) } yield Redirect(navigator.nextPage(TellUsAboutRentPage, NormalMode, updatedAnswers)) } diff --git a/app/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourNewAgreementController.scala b/app/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourNewAgreementController.scala index 7e459bd1..05c77c92 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourNewAgreementController.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourNewAgreementController.scala @@ -49,7 +49,10 @@ class TellUsAboutYourNewAgreementController @Inject()(view: TellUsAboutYourAgree def submit: Action[AnyContent] = { (authenticate andThen getData).async { implicit request => for { - updatedAnswers <- Future.fromTry(request.userAnswers.getOrElse(UserAnswers(request.credId)).set(TellUsAboutYourNewAgreementPage, NewAgreement)) + updatedAnswers <- Future.fromTry(request.userAnswers + .map(answers => answers.getCurrentJourneyUserAnswers(TellUsAboutYourNewAgreementPage, answers, request.credId)) + .getOrElse(UserAnswers(request.credId)) + .set(TellUsAboutYourNewAgreementPage, NewAgreement)) _ <- sessionRepository.set(updatedAnswers) } yield Redirect(navigator.nextPage(TellUsAboutYourNewAgreementPage, NormalMode, updatedAnswers)) } diff --git a/app/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourRenewedAgreementController.scala b/app/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourRenewedAgreementController.scala index 984d9e1c..3826b3d1 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourRenewedAgreementController.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourRenewedAgreementController.scala @@ -49,7 +49,10 @@ class TellUsAboutYourRenewedAgreementController @Inject()(view: TellUsAboutYourA def submit: Action[AnyContent] = { (authenticate andThen getData).async { implicit request => for { - updatedAnswers <- Future.fromTry(request.userAnswers.getOrElse(UserAnswers(request.credId)).set(TellUsAboutYourRenewedAgreementPage, RenewedAgreement)) + updatedAnswers <- Future.fromTry(request.userAnswers + .map(answers => answers.getCurrentJourneyUserAnswers(TellUsAboutYourRenewedAgreementPage, answers, request.credId)) + .getOrElse(UserAnswers(request.credId)) + .set(TellUsAboutYourRenewedAgreementPage, RenewedAgreement)) _ <- sessionRepository.set(updatedAnswers) } yield Redirect(navigator.nextPage(TellUsAboutYourRenewedAgreementPage, NormalMode, updatedAnswers)) } diff --git a/app/uk/gov/hmrc/ngrraldfrontend/models/UserAnswers.scala b/app/uk/gov/hmrc/ngrraldfrontend/models/UserAnswers.scala index 505219a7..c691fc06 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/models/UserAnswers.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/models/UserAnswers.scala @@ -47,6 +47,11 @@ final case class UserAnswers( page.cleanup(Some(value), updatedAnswers) } } + + def getCurrentJourneyUserAnswers[A](page: Gettable[A], userAnswers: UserAnswers, credId: String)(implicit rds: Reads[A]): UserAnswers = + userAnswers.get(page) match + case Some(_) => userAnswers + case _ => UserAnswers(credId) } object UserAnswers { diff --git a/app/uk/gov/hmrc/ngrraldfrontend/models/forms/CommonFormValidators.scala b/app/uk/gov/hmrc/ngrraldfrontend/models/forms/CommonFormValidators.scala index 3822fa4a..6098f83b 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/models/forms/CommonFormValidators.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/models/forms/CommonFormValidators.scala @@ -94,10 +94,10 @@ trait CommonFormValidators { monthYearEmptyValidation(input.asInstanceOf[NGRMonthYear], errorKeys) ) - protected def isMonthYearValid[A](monthError: String, yearError: String) : Constraint[A] = + protected def isMonthYearValid[A](errorKey: String) : Constraint[A] = Constraint((input: A) => val date = input.asInstanceOf[NGRMonthYear] - monthYearValidation(date, monthError, yearError) + monthYearValidation(date, errorKey) ) protected def isMonthYearAfter1900[A](errorKey: String): Constraint[A] = @@ -146,8 +146,7 @@ trait CommonFormValidators { Invalid(errorKey) else Valid - - + private def monthYearAfter1900Validation(date: NGRMonthYear, errorKey: String) = val maybeYear = date.year.toIntOption maybeYear match { @@ -165,16 +164,14 @@ trait CommonFormValidators { else Valid - protected def monthYearValidation(date: NGRMonthYear, monthError: String, yearError:String) = { + private def monthYearValidation(date: NGRMonthYear, errorKey: String) = { val maybeMonth = date.month.toIntOption val maybeYear = date.year.toIntOption (maybeMonth, maybeYear) match { - case (Some(month), Some(year)) if month > 0 && month <= 12 => + case (Some(month), Some(year)) if month > 0 && month <= 12 && year.toString.length == 4 => Valid - case (_, None) => - Invalid(yearError) case (_, _) => - Invalid(monthError) + Invalid(errorKey) } } } diff --git a/app/uk/gov/hmrc/ngrraldfrontend/models/forms/InterimRentSetByTheCourtForm.scala b/app/uk/gov/hmrc/ngrraldfrontend/models/forms/InterimRentSetByTheCourtForm.scala index dea9f3ec..c1e45f60 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/models/forms/InterimRentSetByTheCourtForm.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/models/forms/InterimRentSetByTheCourtForm.scala @@ -37,12 +37,6 @@ object InterimRentSetByTheCourtForm extends CommonFormValidators with MonthYearM def unapply(interimRentSetByTheCourtForm: InterimRentSetByTheCourtForm): Option[(BigDecimal, NGRMonthYear)] = Some(interimRentSetByTheCourtForm.amount, interimRentSetByTheCourtForm.date) - private def errorKeys(whichDate: String): Map[DateErrorKeys, String] = Map( - Required -> s"$whichDate.required.error", - Month -> s"$whichDate.month.required.error", - Year -> s"$whichDate.year.required.error" - ) - val form: Form[InterimRentSetByTheCourtForm] = Form( mapping( "interimAmount" -> text() @@ -60,9 +54,9 @@ object InterimRentSetByTheCourtForm extends CommonFormValidators with MonthYearM "date" -> monthYearMapping .verifying( firstError( - isMonthYearEmpty(errorKeys("interimRentSetByTheCourt")), - isMonthYearValid(monthError = "interimRentSetByTheCourt.month.format.error", yearError = "interimRentSetByTheCourt.year.format.error"), - isMonthYearAfter1900("interimRentSetByTheCourt.startDate.before.1900.error") + isMonthYearEmpty(errorKeys("interimRentSetByTheCourt", "date")), + isMonthYearValid("interimRentSetByTheCourt.date.invalid.error"), + isMonthYearAfter1900("interimRentSetByTheCourt.date.before.1900.error") ) ), )(InterimRentSetByTheCourtForm.apply)(InterimRentSetByTheCourtForm.unapply) diff --git a/app/uk/gov/hmrc/ngrraldfrontend/models/forms/WhatYourRentIncludesForm.scala b/app/uk/gov/hmrc/ngrraldfrontend/models/forms/WhatYourRentIncludesForm.scala index 154e7de0..e99d65d1 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/models/forms/WhatYourRentIncludesForm.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/models/forms/WhatYourRentIncludesForm.scala @@ -149,12 +149,12 @@ object WhatYourRentIncludesForm extends CommonFormValidators with Mappings { if (whatYourRentIncludesForm.livingAccommodationRadio.equals("livingAccommodationYes")) { if (bedroomNumber.isEmpty) Invalid("whatYourRentIncludes.bedroom.number.required.error") - else if (Try(bedroomNumber.get.toLong).isFailure) + else if (!bedroomNumber.get.matches(wholePositiveNumberRegexp.pattern())) Invalid("whatYourRentIncludes.bedroom.number.invalid.error") + else if (bedroomNumber.get.toDoubleOption.getOrElse(0d) > 99) + Invalid("whatYourRentIncludes.bedroom.number.maximum.error") else if (bedroomNumber.get.toLong < 1) Invalid("whatYourRentIncludes.bedroom.number.minimum.error") - else if (bedroomNumber.get.toLong > 99) - Invalid("whatYourRentIncludes.bedroom.number.maximum.error") else Valid } @@ -165,13 +165,13 @@ object WhatYourRentIncludesForm extends CommonFormValidators with Mappings { def form: Form[WhatYourRentIncludesForm] = { Form( mapping( - livingAccommodationRadio -> radioText(livingAccommodationRadioError), - rentPartAddressRadio -> radioText(rentPartAddressRadioError), - rentEmptyShellRadio -> radioText(rentEmptyShellRadioError), + livingAccommodationRadio -> radioText(livingAccommodationRadioError), + rentPartAddressRadio -> radioText(rentPartAddressRadioError), + rentEmptyShellRadio -> radioText(rentEmptyShellRadioError), rentIncBusinessRatesRadio -> radioText(rentIncBusinessRatesRadioError), - rentIncWaterChargesRadio -> radioText(rentIncWaterChargesRadioError), - rentIncServiceRadio -> radioText(rentIncServiceRadioError), - bedroomNumbers -> optional(text().transform[String](_.strip(), identity)) + rentIncWaterChargesRadio -> radioText(rentIncWaterChargesRadioError), + rentIncServiceRadio -> radioText(rentIncServiceRadioError), + bedroomNumbers -> optional(text().transform[String](_.strip(), identity)) )(WhatYourRentIncludesForm.apply)(WhatYourRentIncludesForm.unapply) .verifying(isBedroomNumberValid) ) diff --git a/app/uk/gov/hmrc/ngrraldfrontend/views/InterimRentSetByTheCourtView.scala.html b/app/uk/gov/hmrc/ngrraldfrontend/views/InterimRentSetByTheCourtView.scala.html index b1f631d2..c6d8a99c 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/views/InterimRentSetByTheCourtView.scala.html +++ b/app/uk/gov/hmrc/ngrraldfrontend/views/InterimRentSetByTheCourtView.scala.html @@ -34,7 +34,7 @@ @layout(pageTitle = Some(messages("interimRentSetByTheCourt.title")), showBackLink = true, fullWidth = false) { @formHelper(action = uk.gov.hmrc.ngrraldfrontend.controllers.routes.InterimRentSetByTheCourtController.submit(mode), Symbol("autoComplete") -> "off") { @if(form.errors.nonEmpty) { - @govukErrorSummary(ErrorSummaryViewModel(form)) + @govukErrorSummary(ErrorSummaryViewModel(form, Some("interimRentSetByTheCourt"), Some(Seq("date")))) } @propertyAddress

@messages("interimRentSetByTheCourt.title")

diff --git a/app/uk/gov/hmrc/ngrraldfrontend/views/components/InputDateForMonthYear.scala.html b/app/uk/gov/hmrc/ngrraldfrontend/views/components/InputDateForMonthYear.scala.html index 6ad8753d..5585d6e7 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/views/components/InputDateForMonthYear.scala.html +++ b/app/uk/gov/hmrc/ngrraldfrontend/views/components/InputDateForMonthYear.scala.html @@ -42,8 +42,7 @@ id = s"$id.month", classes = s"govuk-input--width-2${if( form(s"$id.month").hasErrors - || form.errors.exists(_.key.contains("date")) - || form.errors.exists(_.args.contains("month")))" govuk-input--error" else ""}", + || form.errors.exists(_.key.equals("date")))" govuk-input--error" else ""}", name = s"$id.month", label = Some(messages("date.month")), value = form(s"$id.month").value @@ -52,8 +51,7 @@ id = s"$id.year", classes = s"govuk-input--width-4${if( form(s"$id.year").hasErrors - || form.errors.exists(_.key.contains("date")) - || form.errors.exists(_.args.contains("year")))" govuk-input--error" else ""}", + || form.errors.exists(_.key.equals("date")))" govuk-input--error" else ""}", name = s"$id.year", label = Some(messages("date.year")), value = form(s"$id.year").value diff --git a/conf/messages b/conf/messages index 77da58f2..2a272c07 100644 --- a/conf/messages +++ b/conf/messages @@ -363,12 +363,11 @@ date.year = Year interimRentSetByTheCourt.interimAmount.required.error = Enter how much the interim rent was, in pounds interimRentSetByTheCourt.interimAmount.tooLarge.error = Interim rent must be £9,999,999.99 or less interimRentSetByTheCourt.interimAmount.format.error = Interim rent must be a number, like 50,000 -interimRentSetByTheCourt.month.format.error = Date your interim rent started must be a real date -interimRentSetByTheCourt.year.format.error = Date your interim rent started must be a real date -interimRentSetByTheCourt.required.error = Date you started paying the interim rent must include a month and year -interimRentSetByTheCourt.month.required.error = Date you started paying the interim rent must include a month -interimRentSetByTheCourt.year.required.error = Date you started paying the interim rent must include a year -interimRentSetByTheCourt.startDate.before.1900.error = The date you started paying interim rent must be 1900 or after +interimRentSetByTheCourt.date.invalid.error = Date your interim rent started must be a real date +interimRentSetByTheCourt.date.required.error = Date you started paying the interim rent must include a month and year +interimRentSetByTheCourt.date.month.required.error = Date you started paying the interim rent must include a month +interimRentSetByTheCourt.date.year.required.error = Date you started paying the interim rent must include a year +interimRentSetByTheCourt.date.before.1900.error = The date you started paying interim rent must be 1900 or after #RentFreePeriod rentFreePeriod.title = Rent-free period diff --git a/test/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourNewAgreementControllerSpec.scala b/test/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourNewAgreementControllerSpec.scala index 1d8d76ea..69761f6d 100644 --- a/test/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourNewAgreementControllerSpec.scala +++ b/test/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourNewAgreementControllerSpec.scala @@ -20,6 +20,7 @@ import org.mockito.Mockito.when import org.mockito.ArgumentMatchers.any import org.scalatest.matchers.should.Matchers.shouldBe import play.api.http.Status.{OK, SEE_OTHER} +import play.api.libs.json.Json import play.api.test.Helpers.{await, contentAsString, defaultAwaitTimeout, redirectLocation, status} import uk.gov.hmrc.http.NotFoundException import uk.gov.hmrc.ngrraldfrontend.helpers.ControllerSpecSupport @@ -28,6 +29,7 @@ import uk.gov.hmrc.ngrraldfrontend.models.registration.CredId import uk.gov.hmrc.ngrraldfrontend.models.{NormalMode, UserAnswers} import uk.gov.hmrc.ngrraldfrontend.views.html.TellUsAboutYourAgreementView +import java.time.Instant import scala.concurrent.Future class TellUsAboutYourNewAgreementControllerSpec extends ControllerSpecSupport { @@ -53,12 +55,32 @@ class TellUsAboutYourNewAgreementControllerSpec extends ControllerSpecSupport { } } "method submit" must { - "Return OK and the correct view" in { + "Return SEE_OTHER and the correct view" in { when(mockSessionRepository.set(any())).thenReturn(Future.successful(true)) val result = controllerProperty(None).submit(authenticatedFakeRequest) status(result) mustBe SEE_OTHER redirectLocation(result) shouldBe Some(routes.LandlordController.show(NormalMode).url) } + "Return SEE_OTHER and the correct view when user resumes new agreement journey" in { + when(mockSessionRepository.set(any())).thenReturn(Future.successful(true)) + val result = controllerProperty(Some(UserAnswers(credId.value, Json.obj( + "tellUsAboutYourNewAgreement" -> "NewAgreement", + "landlord" -> Json.obj( + "landlordName" -> "Anna" + ) + ), Instant.now))).submit(authenticatedFakeRequest) + status(result) mustBe SEE_OTHER + redirectLocation(result) shouldBe Some(routes.LandlordController.show(NormalMode).url) + } + "Return SEE_OTHER and the correct view when user switches to new agreement journey" in { + when(mockSessionRepository.set(any())).thenReturn(Future.successful(true)) + val result = controllerProperty(Some(UserAnswers(credId.value, Json.obj( + "tellUsAboutRenewedAgreement" -> "RenewedAgreement", + "whatTypeOfLeaseRenewal" -> "SurrenderAndRenewal" + ), Instant.now))).submit(authenticatedFakeRequest) + status(result) mustBe SEE_OTHER + redirectLocation(result) shouldBe Some(routes.LandlordController.show(NormalMode).url) + } } } } diff --git a/test/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourRenewedAgreementControllerSpec.scala b/test/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourRenewedAgreementControllerSpec.scala index 88766dcf..b7052f77 100644 --- a/test/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourRenewedAgreementControllerSpec.scala +++ b/test/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourRenewedAgreementControllerSpec.scala @@ -20,25 +20,27 @@ import org.mockito.ArgumentMatchers.any import org.mockito.Mockito.when import org.scalatest.matchers.should.Matchers.shouldBe import play.api.http.Status.{OK, SEE_OTHER} +import play.api.libs.json.Json import play.api.test.Helpers.{await, contentAsString, defaultAwaitTimeout, redirectLocation, status} import uk.gov.hmrc.http.NotFoundException import uk.gov.hmrc.ngrraldfrontend.helpers.ControllerSpecSupport -import uk.gov.hmrc.ngrraldfrontend.models.NormalMode +import uk.gov.hmrc.ngrraldfrontend.models.{NormalMode, UserAnswers} import uk.gov.hmrc.ngrraldfrontend.models.registration.CredId import uk.gov.hmrc.ngrraldfrontend.views.html.TellUsAboutYourAgreementView +import java.time.Instant import scala.concurrent.Future class TellUsAboutYourRenewedAgreementControllerSpec extends ControllerSpecSupport { val pageTitle = "Tell us about your renewed agreement" val view: TellUsAboutYourAgreementView = inject[TellUsAboutYourAgreementView] val controllerNoProperty: TellUsAboutYourRenewedAgreementController = new TellUsAboutYourRenewedAgreementController(view, fakeAuth, mcc, fakeData(None), mockSessionRepository, mockNavigator)(mockConfig) - val controllerProperty: TellUsAboutYourRenewedAgreementController = new TellUsAboutYourRenewedAgreementController(view, fakeAuth, mcc, fakeDataProperty(Some(property), None), mockSessionRepository, mockNavigator)(mockConfig) + val controllerProperty = (answers: Option[UserAnswers]) => new TellUsAboutYourRenewedAgreementController(view, fakeAuth, mcc, fakeDataProperty(Some(property), answers), mockSessionRepository, mockNavigator)(mockConfig) "Tell us about your new agreement controller" must { "method show" must { "Return OK and the correct view" in { - val result = controllerProperty.show()(authenticatedFakeRequest) + val result = controllerProperty(None).show()(authenticatedFakeRequest) status(result) mustBe OK val content = contentAsString(result) content must include(pageTitle) @@ -53,9 +55,29 @@ class TellUsAboutYourRenewedAgreementControllerSpec extends ControllerSpecSuppor } "method submit" must { - "Return OK and the correct view" in { + "Return SEE_OTHER and the correct view" in { + when(mockSessionRepository.set(any())).thenReturn(Future.successful(true)) + val result = controllerProperty(None).submit()(authenticatedFakeRequest) + status(result) mustBe SEE_OTHER + redirectLocation(result) shouldBe Some(routes.WhatTypeOfLeaseRenewalController.show(NormalMode).url) + } + "Return SEE_OTHER and the correct view when user resumes renewed agreement journey" in { + when(mockSessionRepository.set(any())).thenReturn(Future.successful(true)) + val result = controllerProperty(Some(UserAnswers(credId.value, Json.obj( + "tellUsAboutRenewedAgreement" -> "RenewedAgreement", + "whatTypeOfLeaseRenewal" -> "SurrenderAndRenewal" + ), Instant.now))).submit()(authenticatedFakeRequest) + status(result) mustBe SEE_OTHER + redirectLocation(result) shouldBe Some(routes.WhatTypeOfLeaseRenewalController.show(NormalMode).url) + } + "Return SEE_OTHER and the correct view when user switches to renewed agreement journey" in { when(mockSessionRepository.set(any())).thenReturn(Future.successful(true)) - val result = controllerProperty.submit()(authenticatedFakeRequest) + val result = controllerProperty(Some(UserAnswers(credId.value, Json.obj( + "tellUsAboutYourNewAgreement" -> "NewAgreement", + "landlord" -> Json.obj( + "landlordName" -> "Anna" + ) + ), Instant.now))).submit()(authenticatedFakeRequest) status(result) mustBe SEE_OTHER redirectLocation(result) shouldBe Some(routes.WhatTypeOfLeaseRenewalController.show(NormalMode).url) } diff --git a/test/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourRentControllerSpec.scala b/test/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourRentControllerSpec.scala index bb881a76..75b60c18 100644 --- a/test/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourRentControllerSpec.scala +++ b/test/uk/gov/hmrc/ngrraldfrontend/controllers/TellUsAboutYourRentControllerSpec.scala @@ -20,25 +20,27 @@ import org.scalatest.matchers.should.Matchers.shouldBe import org.mockito.ArgumentMatchers.any import org.mockito.Mockito.when import play.api.http.Status.{OK, SEE_OTHER} +import play.api.libs.json.Json import play.api.test.Helpers.{await, contentAsString, defaultAwaitTimeout, redirectLocation, status} import uk.gov.hmrc.http.NotFoundException import uk.gov.hmrc.ngrraldfrontend.helpers.ControllerSpecSupport -import uk.gov.hmrc.ngrraldfrontend.models.NormalMode +import uk.gov.hmrc.ngrraldfrontend.models.{NormalMode, UserAnswers} import uk.gov.hmrc.ngrraldfrontend.models.registration.CredId import uk.gov.hmrc.ngrraldfrontend.views.html.TellUsAboutYourAgreementView +import java.time.Instant import scala.concurrent.Future class TellUsAboutYourRentControllerSpec extends ControllerSpecSupport { val pageTitle = "Tell us about your rent review" val view: TellUsAboutYourAgreementView = inject[TellUsAboutYourAgreementView] - val controllerProperty: TellUsAboutRentController = new TellUsAboutRentController(view, fakeAuth, mockNavigator, mcc, fakeDataProperty(Some(property),None), mockSessionRepository)(mockConfig) + val controllerProperty = (answers: Option[UserAnswers]) => new TellUsAboutRentController(view, fakeAuth, mockNavigator, mcc, fakeDataProperty(Some(property), answers), mockSessionRepository)(mockConfig) val controllerNoProperty: TellUsAboutRentController = new TellUsAboutRentController(view, fakeAuth, mockNavigator, mcc, fakeData(None), mockSessionRepository)(mockConfig) "Tell us about your rent controller" must { "method show" must { "Return OK and the correct view" in { - val result = controllerProperty.show()(authenticatedFakeRequest) + val result = controllerProperty(None).show()(authenticatedFakeRequest) status(result) mustBe OK val content = contentAsString(result) content must include(pageTitle) @@ -52,9 +54,29 @@ class TellUsAboutYourRentControllerSpec extends ControllerSpecSupport { } } "method submit" must { - "Return OK and the correct view" in { + "Return SEE_OTHER and the correct view" in { + when(mockSessionRepository.set(any())).thenReturn(Future.successful(true)) + val result = controllerProperty(None).submit()(authenticatedFakeRequest) + status(result) mustBe SEE_OTHER + redirectLocation(result) shouldBe Some(routes.LandlordController.show(NormalMode).url) + } + "Return SEE_OTHER and the correct view when user resumes rent review journey" in { + when(mockSessionRepository.set(any())).thenReturn(Future.successful(true)) + val result = controllerProperty(Some(UserAnswers(credId.value, Json.obj( + "tellUsAboutRent" -> "RentAgreement", + "landlord" -> Json.obj( + "landlordName" -> "Anna" + ) + ), Instant.now))).submit()(authenticatedFakeRequest) + status(result) mustBe SEE_OTHER + redirectLocation(result) shouldBe Some(routes.LandlordController.show(NormalMode).url) + } + "Return SEE_OTHER and the correct view when user switched to rent review journey" in { when(mockSessionRepository.set(any())).thenReturn(Future.successful(true)) - val result = controllerProperty.submit()(authenticatedFakeRequest) + val result = controllerProperty(Some(UserAnswers(credId.value, Json.obj( + "tellUsAboutRenewedAgreement" -> "RenewedAgreement", + "whatTypeOfLeaseRenewal" -> "SurrenderAndRenewal" + ), Instant.now))).submit()(authenticatedFakeRequest) status(result) mustBe SEE_OTHER redirectLocation(result) shouldBe Some(routes.LandlordController.show(NormalMode).url) } diff --git a/test/uk/gov/hmrc/ngrraldfrontend/controllers/WhatYourRentIncludesControllerSpec.scala b/test/uk/gov/hmrc/ngrraldfrontend/controllers/WhatYourRentIncludesControllerSpec.scala index 8b55d60d..6e9bd0a4 100644 --- a/test/uk/gov/hmrc/ngrraldfrontend/controllers/WhatYourRentIncludesControllerSpec.scala +++ b/test/uk/gov/hmrc/ngrraldfrontend/controllers/WhatYourRentIncludesControllerSpec.scala @@ -204,7 +204,7 @@ class WhatYourRentIncludesControllerSpec extends ControllerSpecSupport { "rentIncBusinessRatesRadio" -> "No", "rentIncWaterChargesRadio" -> "No", "rentIncServiceRadio" -> "Yes", - "bedroomNumbers" -> "-1" + "bedroomNumbers" -> "0" ) .withHeaders(HeaderNames.authorisation -> "Bearer 1"), None, None, None, Some(property), credId = Some(credId.value), None, None, nino = Nino(true, Some("")))) result.map(result => { diff --git a/test/uk/gov/hmrc/ngrraldfrontend/models/forms/InterimRentSetByTheCourtFormSpec.scala b/test/uk/gov/hmrc/ngrraldfrontend/models/forms/InterimRentSetByTheCourtFormSpec.scala index 2a6cef4f..ef9787d5 100644 --- a/test/uk/gov/hmrc/ngrraldfrontend/models/forms/InterimRentSetByTheCourtFormSpec.scala +++ b/test/uk/gov/hmrc/ngrraldfrontend/models/forms/InterimRentSetByTheCourtFormSpec.scala @@ -157,7 +157,7 @@ class InterimRentSetByTheCourtFormSpec extends AnyWordSpec with Matchers { ) val boundForm = InterimRentSetByTheCourtForm.form.bind(data) - boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.month.format.error")) + boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.date.invalid.error")) } "fail to bind year before 1900 for year input field" in { val data = Map( @@ -167,7 +167,7 @@ class InterimRentSetByTheCourtFormSpec extends AnyWordSpec with Matchers { ) val boundForm = InterimRentSetByTheCourtForm.form.bind(data) - boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.startDate.before.1900.error")) + boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.date.before.1900.error")) } "fail to bind missing month input" in { @@ -178,7 +178,7 @@ class InterimRentSetByTheCourtFormSpec extends AnyWordSpec with Matchers { ) val boundForm = InterimRentSetByTheCourtForm.form.bind(data) - boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.month.required.error")) + boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.date.month.required.error")) } "fail to bind missing year input" in { @@ -189,7 +189,7 @@ class InterimRentSetByTheCourtFormSpec extends AnyWordSpec with Matchers { ) val boundForm = InterimRentSetByTheCourtForm.form.bind(data) - boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.year.required.error")) + boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.date.year.required.error")) } "fail to bind non numeric format for month input" in { @@ -200,7 +200,7 @@ class InterimRentSetByTheCourtFormSpec extends AnyWordSpec with Matchers { ) val boundForm = InterimRentSetByTheCourtForm.form.bind(data) - boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.month.format.error")) + boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.date.invalid.error")) } "fail to bind non numeric format for year input" in { @@ -211,7 +211,7 @@ class InterimRentSetByTheCourtFormSpec extends AnyWordSpec with Matchers { ) val boundForm = InterimRentSetByTheCourtForm.form.bind(data) - boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.year.format.error")) + boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.date.invalid.error")) } } diff --git a/test/uk/gov/hmrc/ngrraldfrontend/models/forms/WhatYourRentIncludesFormSpec.scala b/test/uk/gov/hmrc/ngrraldfrontend/models/forms/WhatYourRentIncludesFormSpec.scala index 2209c5b7..939a4a54 100644 --- a/test/uk/gov/hmrc/ngrraldfrontend/models/forms/WhatYourRentIncludesFormSpec.scala +++ b/test/uk/gov/hmrc/ngrraldfrontend/models/forms/WhatYourRentIncludesFormSpec.scala @@ -159,7 +159,7 @@ class WhatYourRentIncludesFormSpec extends AnyWordSpec with Matchers { boundForm.hasErrors shouldBe true boundForm.errors should contain(FormError("", List("whatYourRentIncludes.bedroom.number.invalid.error"))) } - "fail to bind when bedroomNumbers input is less than 1" in { + "fail to bind when bedroomNumbers input is mines" in { val data = Map( "livingAccommodationRadio" -> "livingAccommodationYes", "rentPartAddressRadio" -> "Yes", @@ -171,6 +171,21 @@ class WhatYourRentIncludesFormSpec extends AnyWordSpec with Matchers { ) val boundForm = WhatYourRentIncludesForm.form.bind(data) + boundForm.hasErrors shouldBe true + boundForm.errors should contain(FormError("", List("whatYourRentIncludes.bedroom.number.invalid.error"))) + } + "fail to bind when bedroomNumbers input is less than 1" in { + val data = Map( + "livingAccommodationRadio" -> "livingAccommodationYes", + "rentPartAddressRadio" -> "Yes", + "rentEmptyShellRadio" -> "Yes", + "rentIncBusinessRatesRadio" -> "Yes", + "rentIncWaterChargesRadio" -> "Yes", + "rentIncServiceRadio" -> "Yes", + "bedroomNumbers" -> "0" + ) + val boundForm = WhatYourRentIncludesForm.form.bind(data) + boundForm.hasErrors shouldBe true boundForm.errors should contain(FormError("", List("whatYourRentIncludes.bedroom.number.minimum.error"))) } @@ -189,7 +204,7 @@ class WhatYourRentIncludesFormSpec extends AnyWordSpec with Matchers { boundForm.hasErrors shouldBe true boundForm.errors should contain(FormError("", List("whatYourRentIncludes.bedroom.number.maximum.error"))) } - "fail to bind when bedroomNumbers input is 12 digits long" in { + "fail to bind when bedroomNumbers input is 30 digits long" in { val data = Map( "livingAccommodationRadio" -> "livingAccommodationYes", "rentPartAddressRadio" -> "Yes", @@ -197,7 +212,7 @@ class WhatYourRentIncludesFormSpec extends AnyWordSpec with Matchers { "rentIncBusinessRatesRadio" -> "Yes", "rentIncWaterChargesRadio" -> "Yes", "rentIncServiceRadio" -> "Yes", - "bedroomNumbers" -> "123123123123" + "bedroomNumbers" -> "123123123123123123123123345678" ) val boundForm = WhatYourRentIncludesForm.form.bind(data)