-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1515 from mocenas/security_after_each
Add tests for QuarkusSecurityTestExtension callbacks
- Loading branch information
Showing
3 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
...st/java/io/quarkus/ts/openshift/security/basic/callback/AbstractSecurityCallbackTest.java
This file contains 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,102 @@ | ||
package io.quarkus.ts.openshift.security.basic.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.scenarios.annotations.DisabledOnNative; | ||
import io.quarkus.test.security.TestIdentityAssociation; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
@DisabledOnNative | ||
@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 new Principal() { | ||
@Override | ||
public String getName() { | ||
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
49
...c/test/java/io/quarkus/ts/openshift/security/basic/callback/TestSecurityDisabledTest.java
This file contains 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.ts.openshift.security.basic.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
37
...rc/test/java/io/quarkus/ts/openshift/security/basic/callback/TestSecurityEnabledTest.java
This file contains 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.ts.openshift.security.basic.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(); | ||
} | ||
} |