Skip to content

Commit

Permalink
Valdiate for unexpected pages
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNickWilson committed May 14, 2024
1 parent 2acfc2d commit 6fa7c0b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
14 changes: 9 additions & 5 deletions app/models/TraderProfile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package models

import cats.data.{Ior, IorNec}
import cats.data.{Ior, IorNec, NonEmptyChain}
import cats.implicits._
import pages.{HasNiphlPage, HasNirmsPage, NiphlNumberPage, NirmsNumberPage, UkimsNumberPage}
import play.api.libs.json.{Json, OFormat}
Expand All @@ -40,13 +40,17 @@ object TraderProfile {

private def getNirms(answers: UserAnswers): IorNec[ValidationError, Option[String]] =
answers.getIor(HasNirmsPage).flatMap {
case true => answers.getIor(NirmsNumberPage).map(Some(_))
case false => Ior.Right(None)
case true =>
answers.getIor(NirmsNumberPage).map(Some(_))
case false =>
if (answers.isDefined(NirmsNumberPage)) Ior.Left(NonEmptyChain.one(UnexpectedPage(NirmsNumberPage))) else Ior.Right(None)
}

private def getNiphl(answers: UserAnswers): IorNec[ValidationError, Option[String]] =
answers.getIor(HasNiphlPage).flatMap {
case true => answers.getIor(NiphlNumberPage).map(Some(_))
case false => Ior.Right(None)
case true =>
answers.getIor(NiphlNumberPage).map(Some(_))
case false =>
if (answers.isDefined(NiphlNumberPage)) Ior.Left(NonEmptyChain.one(UnexpectedPage(NiphlNumberPage))) else Ior.Right(None)
}
}
19 changes: 19 additions & 0 deletions test/models/TraderProfileSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,25 @@ class TraderProfileSpec extends AnyFreeSpec with Matchers with TryValues with Op
data must not be defined
errors.value.toChain.toList must contain only PageMissing(NiphlNumberPage)
}

"when the user said they don't have optional data but it is present" in {

val answers =
UserAnswers("id")
.set(UkimsNumberPage, "1").success.value
.set(HasNirmsPage, false).success.value
.set(NirmsNumberPage, "2").success.value
.set(HasNiphlPage, false).success.value
.set(NiphlNumberPage, "3").success.value

val (errors, data) = TraderProfile.build(answers).pad

data must not be defined
errors.value.toChain.toList must contain theSameElementsAs Seq(
UnexpectedPage(NirmsNumberPage),
UnexpectedPage(NiphlNumberPage)
)
}
}
}
}

0 comments on commit 6fa7c0b

Please sign in to comment.