|
| 1 | +package io.quarkus.security.test.cdi; |
| 2 | + |
| 3 | +import static io.quarkus.security.test.utils.IdentityMock.ADMIN; |
| 4 | +import static io.quarkus.security.test.utils.IdentityMock.ANONYMOUS; |
| 5 | +import static io.quarkus.security.test.utils.IdentityMock.USER; |
| 6 | +import static io.quarkus.security.test.utils.SecurityTestUtils.assertFailureFor; |
| 7 | +import static io.quarkus.security.test.utils.SecurityTestUtils.assertSuccess; |
| 8 | + |
| 9 | +import jakarta.annotation.Priority; |
| 10 | +import jakarta.annotation.security.PermitAll; |
| 11 | +import jakarta.annotation.security.RolesAllowed; |
| 12 | +import jakarta.enterprise.context.ApplicationScoped; |
| 13 | +import jakarta.enterprise.inject.Alternative; |
| 14 | +import jakarta.inject.Inject; |
| 15 | +import jakarta.inject.Singleton; |
| 16 | +import jakarta.interceptor.Interceptor; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 20 | + |
| 21 | +import io.quarkus.arc.Unremovable; |
| 22 | +import io.quarkus.security.ForbiddenException; |
| 23 | +import io.quarkus.security.UnauthorizedException; |
| 24 | +import io.quarkus.security.spi.runtime.AuthorizationController; |
| 25 | +import io.quarkus.security.test.utils.AuthData; |
| 26 | +import io.quarkus.security.test.utils.IdentityMock; |
| 27 | +import io.quarkus.security.test.utils.SecurityTestUtils; |
| 28 | +import io.quarkus.test.QuarkusUnitTest; |
| 29 | + |
| 30 | +public class CustomAuthorizationControllerTest { |
| 31 | + |
| 32 | + @RegisterExtension |
| 33 | + static final QuarkusUnitTest config = new QuarkusUnitTest() |
| 34 | + .withApplicationRoot((jar) -> jar |
| 35 | + .addClasses(IdentityMock.class, AuthData.class, SecurityTestUtils.class, |
| 36 | + CustomAuthorizationController.class, SecuredBean.class)); |
| 37 | + |
| 38 | + @Inject |
| 39 | + CustomAuthorizationController authorizationController; |
| 40 | + |
| 41 | + @Inject |
| 42 | + SecuredBean bean; |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testAuthorizationEnabled() { |
| 46 | + authorizationController.enabled = true; |
| 47 | + assertFailureFor(() -> bean.methodLevelRolesAllowed(), UnauthorizedException.class, ANONYMOUS); |
| 48 | + assertFailureFor(() -> bean.methodLevelRolesAllowed(), ForbiddenException.class, USER); |
| 49 | + assertSuccess(() -> bean.classLevelPermitAll(), "classLevelPermitAll", USER); |
| 50 | + assertSuccess(() -> bean.methodLevelRolesAllowed(), "methodLevelRolesAllowed", ADMIN); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testAuthorizationDisabled() { |
| 55 | + authorizationController.enabled = false; |
| 56 | + assertSuccess(() -> bean.methodLevelRolesAllowed(), "methodLevelRolesAllowed", ANONYMOUS); |
| 57 | + assertSuccess(() -> bean.methodLevelRolesAllowed(), "methodLevelRolesAllowed", USER); |
| 58 | + assertSuccess(() -> bean.classLevelPermitAll(), "classLevelPermitAll", USER); |
| 59 | + } |
| 60 | + |
| 61 | + @Unremovable |
| 62 | + @Alternative |
| 63 | + @Priority(Interceptor.Priority.LIBRARY_AFTER) |
| 64 | + @Singleton |
| 65 | + public static final class CustomAuthorizationController extends AuthorizationController { |
| 66 | + |
| 67 | + private volatile boolean enabled = true; |
| 68 | + |
| 69 | + public boolean isAuthorizationEnabled() { |
| 70 | + return enabled; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @PermitAll |
| 75 | + @ApplicationScoped |
| 76 | + public static class SecuredBean { |
| 77 | + |
| 78 | + public String classLevelPermitAll() { |
| 79 | + return "classLevelPermitAll"; |
| 80 | + } |
| 81 | + |
| 82 | + @RolesAllowed("admin") |
| 83 | + public String methodLevelRolesAllowed() { |
| 84 | + return "methodLevelRolesAllowed"; |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | +} |
0 commit comments