Skip to content

Commit

Permalink
Merge pull request #40 from hmrc/ODPR-1720
Browse files Browse the repository at this point in the history
[ODPR-1720] - Adding further verification methods if HMRC-FATCA-ORG o…
  • Loading branch information
georgievh authored Nov 27, 2024
2 parents 58de711 + c93c16b commit 9d5d0c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
24 changes: 21 additions & 3 deletions app/services/UserAllowListService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,24 @@ import scala.concurrent.{ExecutionContext, Future}

class UserAllowListService @Inject()(connector: UserAllowListConnector, appConfig: FrontendAppConfig)
(implicit ec: ExecutionContext) {

private val utrAllowListFeature = "UTR"
private val vrnAllowListFeature = "VRN"
private val fatcaAllowListFeature = "FATCAID"

def isUserAllowed(enrolments: Enrolments)(implicit hc: HeaderCarrier): Future[Boolean] =
if (appConfig.userAllowListEnabled) {
allowListedByUtr(enrolments).flatMap {
case true => Future.successful(true)
case false => allowListedByVrn(enrolments)
case true => Future.successful(true)
case false => allowListedByVrn(enrolments).flatMap {
case true => Future.successful(true)
case false => allowListedByFatcaId(enrolments)
}
}
} else {
Future.successful(true)
}

private def allowListedByUtr(enrolments: Enrolments)(implicit hc: HeaderCarrier): Future[Boolean] =
getCtUtrEnrolment(enrolments)
.map(utr => connector.check(utrAllowListFeature, utr))
Expand Down Expand Up @@ -71,4 +76,17 @@ class UserAllowListService @Inject()(connector: UserAllowListConnector, appConfi
.map(_.value)
}
}

private def allowListedByFatcaId(enrolments: Enrolments)(implicit hc: HeaderCarrier): Future[Boolean] =
getFatcaEnrolment(enrolments)
.map(connector.check(fatcaAllowListFeature, _))
.getOrElse(Future.successful(false))

private def getFatcaEnrolment(enrolments: Enrolments): Option[String] =
enrolments.getEnrolment("HMRC-FATCA-ORG")
.flatMap { enrolment =>
enrolment.identifiers
.find(_.key == "FATCAID")
.map(_.value)
}
}
29 changes: 25 additions & 4 deletions test/services/UserAllowListServiceSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.scalatest.concurrent.ScalaFutures
import org.scalatestplus.mockito.MockitoSugar
import uk.gov.hmrc.auth.core.{Enrolment, EnrolmentIdentifier, Enrolments}
import uk.gov.hmrc.http.HeaderCarrier

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

Expand All @@ -45,9 +46,10 @@ class UserAllowListServiceSpec extends SpecBase with MockitoSugar with BeforeAnd
private val ctEnrolment = Enrolment("IR-CT", Seq(EnrolmentIdentifier("UTR", "utr")), "activated")
private val mtdEnrolment = Enrolment("HMRC-MTD-VAT", Seq(EnrolmentIdentifier("VRN", "vrn")), "activated")
private val vatEnrolment = Enrolment("HMCE-VATDEC-ORG", Seq(EnrolmentIdentifier("VATRegNo", "vatref")), "activated")

private val fatcaEnrolment = Enrolment("HMRC-FATCA-ORG", Seq(EnrolmentIdentifier("FATCAID", "fatcaId")), "activated")

private implicit val hc: HeaderCarrier = HeaderCarrier()

"isUserAllowed" - {

"when the allowlist feature is disabled" - {
Expand Down Expand Up @@ -96,19 +98,29 @@ class UserAllowListServiceSpec extends SpecBase with MockitoSugar with BeforeAnd

service.isUserAllowed(enrolments).futureValue mustEqual true
}

"when the user has an HMRC-FATCA-ORG enrolment in the list" in {
when(mockAppConfig.userAllowListEnabled).thenReturn(true)
when(mockConnector.check(eqTo("UTR"), any())(any())).thenReturn(Future.successful(false))
when(mockConnector.check(eqTo("FATCAID"), any())(any())).thenReturn(Future.successful(true))

val enrolments = Enrolments(Set(ctEnrolment, fatcaEnrolment))

service.isUserAllowed(enrolments).futureValue mustEqual true
}
}

"must return false" - {

"when the user has no CT or VAT enrolments" in {

when(mockAppConfig.userAllowListEnabled) thenReturn true

service.isUserAllowed(emptyEnrolments).futureValue mustEqual false
}

"when the user has a CT enrolment but it isn't on the list" in {

when(mockAppConfig.userAllowListEnabled) thenReturn true
when(mockConnector.check(eqTo("UTR"), any())(any())) thenReturn Future.successful(false)

Expand Down Expand Up @@ -147,6 +159,15 @@ class UserAllowListServiceSpec extends SpecBase with MockitoSugar with BeforeAnd

service.isUserAllowed(enrolments).futureValue mustEqual false
}

"when the user has an HMRC-FATCA-ORG enrolment but it isn't on the list" in {
when(mockAppConfig.userAllowListEnabled).thenReturn(true)
when(mockConnector.check(eqTo("FATCAID"), any())(any())).thenReturn(Future.successful(false))

val enrolments = Enrolments(Set(fatcaEnrolment))

service.isUserAllowed(enrolments).futureValue mustEqual false
}
}
}
}
Expand Down

0 comments on commit 9d5d0c2

Please sign in to comment.