Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
5 changes: 5 additions & 0 deletions app/uk/gov/hmrc/ngrraldfrontend/models/UserAnswers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand Down Expand Up @@ -146,8 +146,7 @@ trait CommonFormValidators {
Invalid(errorKey)
else
Valid



private def monthYearAfter1900Validation(date: NGRMonthYear, errorKey: String) =
val maybeYear = date.year.toIntOption
maybeYear match {
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))))
}
<span class="govuk-caption-m">@propertyAddress</span>
<h1 class="govuk-heading-l">@messages("interimRentSetByTheCourt.title")</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 5 additions & 6 deletions conf/messages
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
Loading