@@ -74,9 +74,10 @@ class EnrolmentsAuthServiceSpec extends ServiceSpec with MockAppConfig {
7474 behave like authorisedSupportingAgent(authValidationEnabled, initialPredicate, primaryAgentPredicate, supportingAgentPredicate)
7575
7676 behave like disallowSupportingAgentForPrimaryOnlyEndpoint(authValidationEnabled, initialPredicate, primaryAgentPredicate)
77-
77+ behave like disallowInvalidAffinityGroup(authValidationEnabled, initialPredicate)
7878 behave like disallowUsersWithoutEnrolments(authValidationEnabled, initialPredicate)
7979 behave like disallowWhenNotLoggedIn(authValidationEnabled, initialPredicate)
80+ behave like handleUnexpectedError(authValidationEnabled, initialPredicate)
8081 }
8182
8283 def authorisedIndividual (authValidationEnabled : Boolean , initialPredicate : Predicate ): Unit =
@@ -237,6 +238,21 @@ class EnrolmentsAuthServiceSpec extends ServiceSpec with MockAppConfig {
237238 result shouldBe Left (ClientOrAgentNotAuthorisedError )
238239 }
239240
241+ def disallowInvalidAffinityGroup (authValidationEnabled : Boolean , initialPredicate : Predicate ): Unit =
242+ " disallow users with an invalid affinity group" in new Test {
243+ mockConfidenceLevelCheckConfig(authValidationEnabled = authValidationEnabled)
244+
245+ val retrievalsResult = new ~ (None , Enrolments (Set .empty))
246+
247+ MockedAuthConnector
248+ .authorised(initialPredicate, affinityGroup and authorisedEnrolments)
249+ .once()
250+ .returns(Future .successful(retrievalsResult))
251+
252+ val result : AuthOutcome = await(enrolmentsAuthService.authorised(mtdId))
253+ result shouldBe Left (ClientOrAgentNotAuthorisedError )
254+ }
255+
240256 def disallowWhenNotLoggedIn (authValidationEnabled : Boolean , initialPredicate : Predicate ): Unit =
241257 " disallow users that are not logged in" in new Test {
242258 mockConfidenceLevelCheckConfig(authValidationEnabled = authValidationEnabled)
@@ -264,6 +280,22 @@ class EnrolmentsAuthServiceSpec extends ServiceSpec with MockAppConfig {
264280 }
265281 }
266282
283+ def handleUnexpectedError (authValidationEnabled : Boolean , initialPredicate : Predicate ): Unit =
284+ " return InternalError on unexpected error" in new Test {
285+ val exception = new RuntimeException (" Unexpected error" )
286+
287+ mockConfidenceLevelCheckConfig(authValidationEnabled = authValidationEnabled)
288+
289+ MockedAuthConnector
290+ .authorised(initialPredicate, affinityGroup and authorisedEnrolments)
291+ .once()
292+ .returns(Future .failed(exception))
293+
294+ val result : AuthOutcome = await(enrolmentsAuthService.authorised(mtdId))
295+ result shouldBe Left (InternalError )
296+
297+ }
298+
267299 trait Test {
268300 val mockAuthConnector : AuthConnector = mock[AuthConnector ]
269301
0 commit comments