diff --git a/app/navigation/verify/VerifyNavigator.scala b/app/navigation/verify/VerifyNavigator.scala index af0f3b68..51fa87fb 100644 --- a/app/navigation/verify/VerifyNavigator.scala +++ b/app/navigation/verify/VerifyNavigator.scala @@ -16,7 +16,6 @@ package navigation.verify -import controllers.routes import jakarta.inject.Singleton import models.{CheckMode, Mode, NormalMode, UserAnswers} import navigation.NavigatorForJourney @@ -65,19 +64,15 @@ class VerifyNavigator @Inject() () extends NavigatorForJourney { private def navigatorFromContractorEmailConfirmationNotStoredPage(mode: Mode)(ua: UserAnswers): Call = (ua.get(ContractorEmailConfirmationNotStoredPage), mode) match { - case (Some(true), _) => - // ToDo: navigate to next page after the page is implemented - controllers.verify.routes.ContractorEmailConfirmationNotStoredController.onPageLoad(NormalMode) - case (Some(false), NormalMode) => - // ToDo: navigate to next page after the page is implemented - controllers.verify.routes.ContractorEmailConfirmationNotStoredController.onPageLoad(NormalMode) - case (Some(false), CheckMode) => - // ToDo: navigate CYA page after the page is implemented and implement the logic to handle dependent pages - routes.IndexController.onPageLoad() + case (Some(true), m) => + controllers.verify.routes.EmailAddressController.onPageLoad(m) + + case (Some(false), _) => + controllers.verify.routes.VerificationDeclarationController.onPageLoad() case _ => - routes.IndexController.onPageLoad() + controllers.routes.JourneyRecoveryController.onPageLoad() } private def navigatorFromSelectSubcontractorPage(mode: Mode)(ua: UserAnswers): Call = { @@ -87,20 +82,17 @@ class VerifyNavigator @Inject() () extends NavigatorForJourney { private def navigatorFromContractorEmailConfirmationStoredPage(mode: Mode)(ua: UserAnswers): Call = (ua.get(ContractorEmailConfirmationStoredPage), mode) match { + case (Some(CurrentEmail), _) => - // TODO: navigate to VF-06 Verify declaration once implemented - controllers.routes.JourneyRecoveryController.onPageLoad() + controllers.verify.routes.VerificationDeclarationController.onPageLoad() - case (Some(DifferentEmail), _) => - // TODO: navigate to SM-01b Enter alternate email once implemented - controllers.routes.JourneyRecoveryController.onPageLoad() + case (Some(DifferentEmail), m) => + controllers.verify.routes.EmailAddressController.onPageLoad(m) case (Some(DoNotSend), _) => - // TODO: navigate to VF-06 Verify declaration once implemented - controllers.routes.JourneyRecoveryController.onPageLoad() + controllers.verify.routes.VerificationDeclarationController.onPageLoad() case _ => controllers.routes.JourneyRecoveryController.onPageLoad() } - } diff --git a/app/pages/verify/ContractorEmailConfirmationNotStoredPage.scala b/app/pages/verify/ContractorEmailConfirmationNotStoredPage.scala index b0b68372..41e3cf4b 100644 --- a/app/pages/verify/ContractorEmailConfirmationNotStoredPage.scala +++ b/app/pages/verify/ContractorEmailConfirmationNotStoredPage.scala @@ -16,12 +16,20 @@ package pages.verify +import models.UserAnswers import pages.QuestionPage import play.api.libs.json.JsPath +import scala.util.Try case object ContractorEmailConfirmationNotStoredPage extends QuestionPage[Boolean] with VerifyJourney { override def path: JsPath = JsPath \ toString override def toString: String = "contractorEmailConfirmationNotStored" + + override def cleanup(value: Option[Boolean], userAnswers: UserAnswers): Try[UserAnswers] = + value match { + case Some(false) => userAnswers.remove(EmailAddressPage) + case _ => super.cleanup(value, userAnswers) + } } diff --git a/app/pages/verify/ContractorEmailConfirmationStoredPage.scala b/app/pages/verify/ContractorEmailConfirmationStoredPage.scala index 26d0b1cb..58f90fda 100644 --- a/app/pages/verify/ContractorEmailConfirmationStoredPage.scala +++ b/app/pages/verify/ContractorEmailConfirmationStoredPage.scala @@ -22,7 +22,7 @@ import models.verify.ContractorEmailConfirmationStored.DifferentEmail import pages.QuestionPage import play.api.libs.json.JsPath -import scala.util.{Success, Try} +import scala.util.Try case object ContractorEmailConfirmationStoredPage extends QuestionPage[ContractorEmailConfirmationStored] @@ -37,9 +37,8 @@ case object ContractorEmailConfirmationStoredPage userAnswers: UserAnswers ): Try[UserAnswers] = value match { - case Some(DifferentEmail) => Success(userAnswers) - case _ => - // TODO: remove ContractorEmailConfirmationAlternateEmailPage once SM-01b is implemented - Success(userAnswers) + case Some(DifferentEmail) => super.cleanup(value, userAnswers) // keep EmailAddressPage + case Some(_) => userAnswers.remove(EmailAddressPage) // CurrentEmail / DoNotSend + case None => super.cleanup(value, userAnswers) } } diff --git a/test/navigation/verify/VerifyNavigatorSpec.scala b/test/navigation/verify/VerifyNavigatorSpec.scala index 83584056..60045502 100644 --- a/test/navigation/verify/VerifyNavigatorSpec.scala +++ b/test/navigation/verify/VerifyNavigatorSpec.scala @@ -40,26 +40,26 @@ class VerifyNavigatorSpec extends SpecBase { navigator.nextPage(UnknownPage, NormalMode, UserAnswers("id")) mustBe journeyRecovery } - "must go from ContractorEmailConfirmationNotStoredPage to ContractorEmailConfirmationNotStoredController when answer is true" in { + "must go from ContractorEmailConfirmationNotStoredPage to EmailAddressController when answer is true" in { val ua = emptyUserAnswers.setOrException(ContractorEmailConfirmationNotStoredPage, true) navigator.nextPage(ContractorEmailConfirmationNotStoredPage, NormalMode, ua) mustBe - controllers.verify.routes.ContractorEmailConfirmationNotStoredController.onPageLoad(NormalMode) + controllers.verify.routes.EmailAddressController.onPageLoad(NormalMode) } - "must go from ContractorEmailConfirmationNotStoredPage to ContractorEmailConfirmationNotStoredController when answer is false" in { + "must go from ContractorEmailConfirmationNotStoredPage to VerificationDeclarationController when answer is false" in { val ua = emptyUserAnswers.setOrException(ContractorEmailConfirmationNotStoredPage, false) navigator.nextPage(ContractorEmailConfirmationNotStoredPage, NormalMode, ua) mustBe - controllers.verify.routes.ContractorEmailConfirmationNotStoredController.onPageLoad(NormalMode) + controllers.verify.routes.VerificationDeclarationController.onPageLoad() } - "must go from ContractorEmailConfirmationNotStoredPage to Index when answer is not present" in { + "must go from ContractorEmailConfirmationNotStoredPage to JourneyRecovery when answer is not present" in { navigator.nextPage( ContractorEmailConfirmationNotStoredPage, NormalMode, emptyUserAnswers - ) mustBe routes.IndexController.onPageLoad() + ) mustBe journeyRecovery } "must go from SelectSubcontractorPage to SelectSubcontractorController in NormalMode" in { @@ -74,28 +74,34 @@ class VerifyNavigatorSpec extends SpecBase { .url } - "must go from ContractorEmailConfirmationStoredPage to JourneyRecovery when answer is CurrentEmail" in { + "must go from ContractorEmailConfirmationStoredPage to VerificationDeclarationController when answer is CurrentEmail" in { val ua = emptyUserAnswers.setOrException( ContractorEmailConfirmationStoredPage, ContractorEmailConfirmationStored.CurrentEmail ) - navigator.nextPage(ContractorEmailConfirmationStoredPage, NormalMode, ua) mustBe journeyRecovery + + navigator.nextPage(ContractorEmailConfirmationStoredPage, NormalMode, ua) mustBe + controllers.verify.routes.VerificationDeclarationController.onPageLoad() } - "must go from ContractorEmailConfirmationStoredPage to JourneyRecovery when answer is DifferentEmail" in { + "must go from ContractorEmailConfirmationStoredPage to EmailAddressController when answer is DifferentEmail" in { val ua = emptyUserAnswers.setOrException( ContractorEmailConfirmationStoredPage, ContractorEmailConfirmationStored.DifferentEmail ) - navigator.nextPage(ContractorEmailConfirmationStoredPage, NormalMode, ua) mustBe journeyRecovery + + navigator.nextPage(ContractorEmailConfirmationStoredPage, NormalMode, ua) mustBe + controllers.verify.routes.EmailAddressController.onPageLoad(NormalMode) } - "must go from ContractorEmailConfirmationStoredPage to JourneyRecovery when answer is DoNotSend" in { + "must go from ContractorEmailConfirmationStoredPage to VerificationDeclarationController when answer is DoNotSend" in { val ua = emptyUserAnswers.setOrException( ContractorEmailConfirmationStoredPage, ContractorEmailConfirmationStored.DoNotSend ) - navigator.nextPage(ContractorEmailConfirmationStoredPage, NormalMode, ua) mustBe journeyRecovery + + navigator.nextPage(ContractorEmailConfirmationStoredPage, NormalMode, ua) mustBe + controllers.verify.routes.VerificationDeclarationController.onPageLoad() } "must go from ContractorEmailConfirmationStoredPage to JourneyRecovery when answer is not present" in { @@ -117,26 +123,26 @@ class VerifyNavigatorSpec extends SpecBase { navigator.nextPage(UnknownPage, CheckMode, UserAnswers("id")) mustBe journeyRecovery } - "must go from ContractorEmailConfirmationNotStoredPage to ContractorEmailConfirmationNotStoredController when answer is true" in { + "must go from ContractorEmailConfirmationNotStoredPage to EmailAddressController when answer is true" in { val ua = emptyUserAnswers.setOrException(ContractorEmailConfirmationNotStoredPage, true) navigator.nextPage(ContractorEmailConfirmationNotStoredPage, CheckMode, ua) mustBe - controllers.verify.routes.ContractorEmailConfirmationNotStoredController.onPageLoad(NormalMode) + controllers.verify.routes.EmailAddressController.onPageLoad(CheckMode) } - "must go from ContractorEmailConfirmationNotStoredPage to Index when answer is false" in { + "must go from ContractorEmailConfirmationNotStoredPage to VerificationDeclarationController when answer is false" in { val ua = emptyUserAnswers.setOrException(ContractorEmailConfirmationNotStoredPage, false) navigator.nextPage(ContractorEmailConfirmationNotStoredPage, CheckMode, ua) mustBe - routes.IndexController.onPageLoad() + controllers.verify.routes.VerificationDeclarationController.onPageLoad() } - "must go from ContractorEmailConfirmationNotStoredPage to Index when answer is not present" in { + "must go from ContractorEmailConfirmationNotStoredPage to JourneyRecovery when answer is not present" in { navigator.nextPage( ContractorEmailConfirmationNotStoredPage, CheckMode, emptyUserAnswers - ) mustBe routes.IndexController.onPageLoad() + ) mustBe journeyRecovery } "must go from SelectSubcontractorPage to SelectSubcontractorController in CheckMode" in { @@ -151,28 +157,31 @@ class VerifyNavigatorSpec extends SpecBase { .url } - "must go from ContractorEmailConfirmationStoredPage to JourneyRecovery when answer is CurrentEmail" in { + "must go from ContractorEmailConfirmationStoredPage to VerificationDeclarationpage when answer is CurrentEmail" in { val ua = emptyUserAnswers.setOrException( ContractorEmailConfirmationStoredPage, ContractorEmailConfirmationStored.CurrentEmail ) - navigator.nextPage(ContractorEmailConfirmationStoredPage, CheckMode, ua) mustBe journeyRecovery + navigator.nextPage(ContractorEmailConfirmationStoredPage, CheckMode, ua) mustBe + controllers.verify.routes.VerificationDeclarationController.onPageLoad() } - "must go from ContractorEmailConfirmationStoredPage to JourneyRecovery when answer is DifferentEmail" in { + "must go from ContractorEmailConfirmationStoredPage to JEmailAddressPage when answer is DifferentEmail" in { val ua = emptyUserAnswers.setOrException( ContractorEmailConfirmationStoredPage, ContractorEmailConfirmationStored.DifferentEmail ) - navigator.nextPage(ContractorEmailConfirmationStoredPage, CheckMode, ua) mustBe journeyRecovery + navigator.nextPage(ContractorEmailConfirmationStoredPage, CheckMode, ua) mustBe + controllers.verify.routes.EmailAddressController.onPageLoad(CheckMode) } - "must go from ContractorEmailConfirmationStoredPage to JourneyRecovery when answer is DoNotSend" in { + "must go from ContractorEmailConfirmationStoredPage to VerificationDeclarationPage when answer is DoNotSend" in { val ua = emptyUserAnswers.setOrException( ContractorEmailConfirmationStoredPage, ContractorEmailConfirmationStored.DoNotSend ) - navigator.nextPage(ContractorEmailConfirmationStoredPage, CheckMode, ua) mustBe journeyRecovery + navigator.nextPage(ContractorEmailConfirmationStoredPage, CheckMode, ua) mustBe + controllers.verify.routes.VerificationDeclarationController.onPageLoad() } "must go from ContractorEmailConfirmationStoredPage to JourneyRecovery when answer is not present" in { diff --git a/test/pages/verify/ContractorEmailConfirmationNotStoredPageSpec.scala b/test/pages/verify/ContractorEmailConfirmationNotStoredPageSpec.scala index 8082446a..66379234 100644 --- a/test/pages/verify/ContractorEmailConfirmationNotStoredPageSpec.scala +++ b/test/pages/verify/ContractorEmailConfirmationNotStoredPageSpec.scala @@ -16,6 +16,7 @@ package pages.verify +import models.UserAnswers import pages.behaviours.PageBehaviours class ContractorEmailConfirmationNotStoredPageSpec extends PageBehaviours { @@ -23,10 +24,51 @@ class ContractorEmailConfirmationNotStoredPageSpec extends PageBehaviours { "ContractorEmailConfirmationNotStoredPage" - { beRetrievable[Boolean](ContractorEmailConfirmationNotStoredPage) - beSettable[Boolean](ContractorEmailConfirmationNotStoredPage) - beRemovable[Boolean](ContractorEmailConfirmationNotStoredPage) - } + "cleanup" - { + + "must remove EmailAddressPage when answer is false (stale email must be cleared)" in { + val answers = + UserAnswers("id") + .setOrException(EmailAddressPage, "new@test.com") + .setOrException(ContractorEmailConfirmationNotStoredPage, true) // previous choice + + val result = + ContractorEmailConfirmationNotStoredPage + .cleanup(Some(false), answers) + .success + .value + + result.get(EmailAddressPage) mustBe None + } + + "must keep EmailAddressPage when answer is true" in { + val answers = + UserAnswers("id") + .setOrException(EmailAddressPage, "new@test.com") + + val result = + ContractorEmailConfirmationNotStoredPage + .cleanup(Some(true), answers) + .success + .value + + result.get(EmailAddressPage) mustBe Some("new@test.com") + } + + "must keep answers unchanged when no answer is provided" in { + val answers = UserAnswers("id") + + val result = + ContractorEmailConfirmationNotStoredPage + .cleanup(None, answers) + .success + .value + + result mustEqual answers + } + } + } } diff --git a/test/pages/verify/ContractorEmailConfirmationStoredPageSpec.scala b/test/pages/verify/ContractorEmailConfirmationStoredPageSpec.scala index 16f0054d..cf550419 100644 --- a/test/pages/verify/ContractorEmailConfirmationStoredPageSpec.scala +++ b/test/pages/verify/ContractorEmailConfirmationStoredPageSpec.scala @@ -25,56 +25,64 @@ class ContractorEmailConfirmationStoredPageSpec extends PageBehaviours { "ContractorEmailConfirmationStoredPage" - { beRetrievable[ContractorEmailConfirmationStored](ContractorEmailConfirmationStoredPage) - beSettable[ContractorEmailConfirmationStored](ContractorEmailConfirmationStoredPage) - beRemovable[ContractorEmailConfirmationStored](ContractorEmailConfirmationStoredPage) "cleanup" - { - "must retain user answers when the answer is DifferentEmail" in { - val answers = UserAnswers("id") - .setOrException(ContractorEmailConfirmationStoredPage, ContractorEmailConfirmationStored.DifferentEmail) + "must retain EmailAddressPage when the answer is DifferentEmail" in { + val answers = + UserAnswers("id") + .setOrException(EmailAddressPage, "alt@test.com") + .setOrException(ContractorEmailConfirmationStoredPage, ContractorEmailConfirmationStored.DifferentEmail) - val result = ContractorEmailConfirmationStoredPage - .cleanup(Some(ContractorEmailConfirmationStored.DifferentEmail), answers) - .success - .value + val result = + ContractorEmailConfirmationStoredPage + .cleanup(Some(ContractorEmailConfirmationStored.DifferentEmail), answers) + .success + .value - result mustEqual answers + result.get(EmailAddressPage) mustBe Some("alt@test.com") } - "must retain user answers when the answer is CurrentEmail" in { - val answers = UserAnswers("id") - .setOrException(ContractorEmailConfirmationStoredPage, ContractorEmailConfirmationStored.CurrentEmail) + "must remove EmailAddressPage when the answer is CurrentEmail (stale email must be cleared)" in { + val answers = + UserAnswers("id") + .setOrException(EmailAddressPage, "alt@test.com") + .setOrException(ContractorEmailConfirmationStoredPage, ContractorEmailConfirmationStored.DifferentEmail) - val result = ContractorEmailConfirmationStoredPage - .cleanup(Some(ContractorEmailConfirmationStored.CurrentEmail), answers) - .success - .value + val result = + ContractorEmailConfirmationStoredPage + .cleanup(Some(ContractorEmailConfirmationStored.CurrentEmail), answers) + .success + .value - result mustEqual answers + result.get(EmailAddressPage) mustBe None } - "must retain user answers when the answer is DoNotSend" in { - val answers = UserAnswers("id") - .setOrException(ContractorEmailConfirmationStoredPage, ContractorEmailConfirmationStored.DoNotSend) + "must remove EmailAddressPage when the answer is DoNotSend (stale email must be cleared)" in { + val answers = + UserAnswers("id") + .setOrException(EmailAddressPage, "alt@test.com") + .setOrException(ContractorEmailConfirmationStoredPage, ContractorEmailConfirmationStored.DifferentEmail) - val result = ContractorEmailConfirmationStoredPage - .cleanup(Some(ContractorEmailConfirmationStored.DoNotSend), answers) - .success - .value + val result = + ContractorEmailConfirmationStoredPage + .cleanup(Some(ContractorEmailConfirmationStored.DoNotSend), answers) + .success + .value - result mustEqual answers + result.get(EmailAddressPage) mustBe None } "must retain user answers when there is no answer" in { val answers = UserAnswers("id") - val result = ContractorEmailConfirmationStoredPage - .cleanup(None, answers) - .success - .value + val result = + ContractorEmailConfirmationStoredPage + .cleanup(None, answers) + .success + .value result mustEqual answers }