- Notifications
You must be signed in to change notification settings - Fork3k
Tests for QuarkusSecurityTestExtension not calling CDI.current()#51284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
mocenas wants to merge1 commit intoquarkusio:mainChoose a base branch frommocenas:donate_test_security
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+181 −0
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Add reproducer for issue 36601
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commitec748bdc0da5952960991db6e3fe33c543f6ca7c
There are no files selected for viewing
95 changes: 95 additions & 0 deletions...ecurity/src/test/java/io/quarkus/test/security/callback/AbstractSecurityCallbackTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package io.quarkus.test.security.callback; | ||
| import java.security.Permission; | ||
| import java.security.Principal; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import jakarta.enterprise.inject.spi.CDI; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Tag; | ||
| import io.quarkus.security.credential.Credential; | ||
| import io.quarkus.security.identity.SecurityIdentity; | ||
| import io.quarkus.test.security.TestIdentityAssociation; | ||
| import io.smallrye.mutiny.Uni; | ||
| @Tag("https://github.com/quarkusio/quarkus/issues/36601") | ||
| /* | ||
| * This set of tests are testing QuarkusSecurityTestExtension callbacks @BeforeEach and @AfterEach | ||
| * These should not interfere with CDI in when tests are not annotated with @TestSecurity | ||
| * Way to measure if these callbacks are doing something is by testing TestIdentity in | ||
| * CDI.current().select(TestIdentityAssociation.class) | ||
| * This should be set to value in BeforeEach and then set to null in AfterEach in case, @TestSecurity is present | ||
| * and not touched otherwise. | ||
| * To properly test handling in these methods, this test package uses a testClass for every testCase, | ||
| * to isolate calls of *Each methods | ||
| */ | ||
| abstract public class AbstractSecurityCallbackTest { | ||
| protected static final SecurityIdentity MOCK_SECURITY_IDENTITY = new SecurityIdentity() { | ||
| @Override | ||
| public Principal getPrincipal() { | ||
| return () -> ""; | ||
| } | ||
| @Override | ||
| public boolean isAnonymous() { | ||
| return true; | ||
| } | ||
| @Override | ||
| public Set<String> getRoles() { | ||
| return Collections.emptySet(); | ||
| } | ||
| @Override | ||
| public boolean hasRole(String role) { | ||
| return false; | ||
| } | ||
| @Override | ||
| public <T extends Credential> T getCredential(Class<T> credentialType) { | ||
| return null; | ||
| } | ||
| @Override | ||
| public Set<Credential> getCredentials() { | ||
| return Collections.emptySet(); | ||
| } | ||
| @Override | ||
| public <T> T getAttribute(String name) { | ||
| return null; | ||
| } | ||
| @Override | ||
| public Map<String, Object> getAttributes() { | ||
| return Collections.emptyMap(); | ||
| } | ||
| @Override | ||
| public Uni<Boolean> checkPermission(Permission permission) { | ||
| return Uni.createFrom().item(false); | ||
| } | ||
| }; | ||
| protected static void assertTestIdentityIsNull() { | ||
| Assertions.assertNull(CDI.current().select(TestIdentityAssociation.class).get().getTestIdentity(), | ||
| "TestIdentity should be null"); | ||
| } | ||
| protected static void assertTestIdentityIsNotNull() { | ||
| Assertions.assertNotNull(CDI.current().select(TestIdentityAssociation.class).get().getTestIdentity(), | ||
| "TestIdentity should have value"); | ||
| } | ||
| protected static void setTestIdentityToValue() { | ||
| CDI.current().select(TestIdentityAssociation.class).get().setTestIdentity(MOCK_SECURITY_IDENTITY); | ||
| } | ||
| protected static void setTestIdentityToNull() { | ||
| CDI.current().select(TestIdentityAssociation.class).get().setTestIdentity(null); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions...rk/security/src/test/java/io/quarkus/test/security/callback/TestSecurityDisabledTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package io.quarkus.test.security.callback; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import io.quarkus.test.junit.QuarkusTest; | ||
| @QuarkusTest | ||
| public class TestSecurityDisabledTest extends AbstractSecurityCallbackTest { | ||
| @BeforeAll | ||
| public static void checkBeforeAll() { | ||
| // this is called before QuarkusSecurityTestExtension.beforeEach, so testIdentity should still be null | ||
| assertTestIdentityIsNull(); | ||
| } | ||
| @BeforeEach | ||
| public void checkBeforeEach() { | ||
| // this is called after QuarkusSecurityTestExtension.beforeEach, with no @TestSecurity, this should be still null | ||
| assertTestIdentityIsNull(); | ||
| } | ||
| @Test | ||
| public void checkTest() { | ||
| // QuarkusSecurityTestExtension.beforeEach should not set test identity | ||
| assertTestIdentityIsNull(); | ||
| // set testIdentity, so we can later check, if it is set to null or not | ||
| setTestIdentityToValue(); | ||
| } | ||
| @AfterEach | ||
| public void checkAfterEach() { | ||
| // testIdentity was set in test, it should be still set | ||
| assertTestIdentityIsNotNull(); | ||
| } | ||
| @AfterAll | ||
| public static void checkAfterAll() { | ||
| // testIdentity was set in test, it should be still set | ||
| assertTestIdentityIsNotNull(); | ||
| // reset testIdentity to null, so it won't break other tests | ||
| setTestIdentityToNull(); | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions...ork/security/src/test/java/io/quarkus/test/security/callback/TestSecurityEnabledTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package io.quarkus.test.security.callback; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import io.quarkus.test.junit.QuarkusTest; | ||
| import io.quarkus.test.security.TestSecurity; | ||
| @QuarkusTest | ||
| public class TestSecurityEnabledTest extends AbstractSecurityCallbackTest { | ||
| @BeforeAll | ||
| public static void checkBeforeAll() { | ||
| // this is called before QuarkusSecurityTestExtension.beforeEach, so testIdentity should still be null | ||
| assertTestIdentityIsNull(); | ||
| } | ||
| @BeforeEach | ||
| public void checkBeforeEach() { | ||
| // this is called after QuarkusSecurityTestExtension.beforeEach, so testIdentity should be set | ||
| assertTestIdentityIsNotNull(); | ||
| } | ||
| @Test | ||
| @TestSecurity(user = "myUser") | ||
| public void checkTestItself() { | ||
| // QuarkusSecurityTestExtension.beforeEach should set test identity | ||
| assertTestIdentityIsNotNull(); | ||
| } | ||
| @AfterAll | ||
| public static void checkAfterAll() { | ||
| // this is called after QuarkusSecurityTestExtension.afterEach, so testIdentity should be set to null again | ||
| assertTestIdentityIsNull(); | ||
| } | ||
| } |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.