|
| 1 | +package com.digitalsanctuary.spring.demo.registration; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import com.digitalsanctuary.spring.user.registration.RegistrationContext; |
| 10 | +import com.digitalsanctuary.spring.user.registration.RegistrationDecision; |
| 11 | +import com.digitalsanctuary.spring.user.registration.RegistrationSource; |
| 12 | + |
| 13 | +class DomainRegistrationGuardTest { |
| 14 | + |
| 15 | + private final DomainRegistrationGuard guard = new DomainRegistrationGuard("@example.com"); |
| 16 | + |
| 17 | + @Test |
| 18 | + void formRegistrationWithAllowedDomainIsAllowed() { |
| 19 | + RegistrationContext context = new RegistrationContext("user@example.com", RegistrationSource.FORM, null); |
| 20 | + RegistrationDecision decision = guard.evaluate(context); |
| 21 | + assertTrue(decision.allowed()); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + void formRegistrationWithDisallowedDomainIsDenied() { |
| 26 | + RegistrationContext context = new RegistrationContext("user@other.com", RegistrationSource.FORM, null); |
| 27 | + RegistrationDecision decision = guard.evaluate(context); |
| 28 | + assertFalse(decision.allowed()); |
| 29 | + assertTrue(decision.reason().contains("@example.com")); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void passwordlessRegistrationWithAllowedDomainIsAllowed() { |
| 34 | + RegistrationContext context = new RegistrationContext("user@example.com", RegistrationSource.PASSWORDLESS, null); |
| 35 | + RegistrationDecision decision = guard.evaluate(context); |
| 36 | + assertTrue(decision.allowed()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void passwordlessRegistrationWithDisallowedDomainIsDenied() { |
| 41 | + RegistrationContext context = new RegistrationContext("user@other.com", RegistrationSource.PASSWORDLESS, null); |
| 42 | + RegistrationDecision decision = guard.evaluate(context); |
| 43 | + assertFalse(decision.allowed()); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void oauth2RegistrationIsAlwaysAllowed() { |
| 48 | + RegistrationContext context = new RegistrationContext("user@other.com", RegistrationSource.OAUTH2, "google"); |
| 49 | + RegistrationDecision decision = guard.evaluate(context); |
| 50 | + assertTrue(decision.allowed()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void oidcRegistrationIsAlwaysAllowed() { |
| 55 | + RegistrationContext context = new RegistrationContext("user@other.com", RegistrationSource.OIDC, "keycloak"); |
| 56 | + RegistrationDecision decision = guard.evaluate(context); |
| 57 | + assertTrue(decision.allowed()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void nullEmailIsDenied() { |
| 62 | + RegistrationContext context = new RegistrationContext(null, RegistrationSource.FORM, null); |
| 63 | + RegistrationDecision decision = guard.evaluate(context); |
| 64 | + assertFalse(decision.allowed()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void domainCheckIsCaseInsensitive() { |
| 69 | + RegistrationContext context = new RegistrationContext("user@EXAMPLE.COM", RegistrationSource.FORM, null); |
| 70 | + RegistrationDecision decision = guard.evaluate(context); |
| 71 | + assertTrue(decision.allowed()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void customDomainIsRespected() { |
| 76 | + DomainRegistrationGuard customGuard = new DomainRegistrationGuard("@mycompany.org"); |
| 77 | + RegistrationContext allowed = new RegistrationContext("user@mycompany.org", RegistrationSource.FORM, null); |
| 78 | + RegistrationContext denied = new RegistrationContext("user@example.com", RegistrationSource.FORM, null); |
| 79 | + assertTrue(customGuard.evaluate(allowed).allowed()); |
| 80 | + assertFalse(customGuard.evaluate(denied).allowed()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void configuredDomainIsCaseInsensitive() { |
| 85 | + DomainRegistrationGuard upperGuard = new DomainRegistrationGuard("@EXAMPLE.COM"); |
| 86 | + RegistrationContext context = new RegistrationContext("user@example.com", RegistrationSource.FORM, null); |
| 87 | + assertTrue(upperGuard.evaluate(context).allowed()); |
| 88 | + } |
| 89 | +} |
0 commit comments