Skip to content

Commit

Permalink
Merge pull request #36645 from sberyozkin/security_test_extension_aft…
Browse files Browse the repository at this point in the history
…er_each_fix

QuarkusSecurityTestExtension afterEach call should not be made for tests without @testsecurity
  • Loading branch information
sberyozkin authored Oct 23, 2023
2 parents 44100f9 + d443ab8 commit 13ba9d0
Showing 1 changed file with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,26 @@ public class QuarkusSecurityTestExtension implements QuarkusTestBeforeEachCallba

@Override
public void afterEach(QuarkusTestMethodContext context) {
CDI.current().select(TestAuthController.class).get().setEnabled(true);
CDI.current().select(TestIdentityAssociation.class).get().setTestIdentity(null);
try {
if (getAnnotationContainer(context).isPresent()) {
CDI.current().select(TestAuthController.class).get().setEnabled(true);
CDI.current().select(TestIdentityAssociation.class).get().setTestIdentity(null);
}
} catch (Exception e) {
throw new RuntimeException("Unable to reset TestAuthController and TestIdentityAssociation", e);
}

}

@Override
public void beforeEach(QuarkusTestMethodContext context) {
try {
//the usual ClassLoader hacks to get our copy of the TestSecurity annotation
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Class<?> original = cl.loadClass(context.getTestMethod().getDeclaringClass().getName());
Method method = original.getDeclaredMethod(context.getTestMethod().getName(),
Arrays.stream(context.getTestMethod().getParameterTypes()).map(s -> {
if (s.isPrimitive()) {
return s;
}
try {
return Class.forName(s.getName(), false, cl);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}).toArray(Class<?>[]::new));
Annotation[] allAnnotations;
Optional<AnnotationContainer<TestSecurity>> annotationContainerOptional = AnnotationUtils.findAnnotation(method,
TestSecurity.class);
if (annotationContainerOptional.isEmpty()) {
annotationContainerOptional = AnnotationUtils.findAnnotation(original, TestSecurity.class);
}
Optional<AnnotationContainer<TestSecurity>> annotationContainerOptional = getAnnotationContainer(context);
if (annotationContainerOptional.isEmpty()) {
return;
}
var annotationContainer = annotationContainerOptional.get();
allAnnotations = annotationContainer.getElement().getAnnotations();
Annotation[] allAnnotations = annotationContainer.getElement().getAnnotations();
TestSecurity testSecurity = annotationContainer.getAnnotation();
CDI.current().select(TestAuthController.class).get().setEnabled(testSecurity.authorizationEnabled());
if (testSecurity.user().isEmpty()) {
Expand All @@ -80,6 +68,30 @@ public void beforeEach(QuarkusTestMethodContext context) {

}

private Optional<AnnotationContainer<TestSecurity>> getAnnotationContainer(QuarkusTestMethodContext context)
throws Exception {
//the usual ClassLoader hacks to get our copy of the TestSecurity annotation
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Class<?> original = cl.loadClass(context.getTestMethod().getDeclaringClass().getName());
Method method = original.getDeclaredMethod(context.getTestMethod().getName(),
Arrays.stream(context.getTestMethod().getParameterTypes()).map(s -> {
if (s.isPrimitive()) {
return s;
}
try {
return Class.forName(s.getName(), false, cl);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}).toArray(Class<?>[]::new));
Optional<AnnotationContainer<TestSecurity>> annotationContainerOptional = AnnotationUtils.findAnnotation(method,
TestSecurity.class);
if (annotationContainerOptional.isEmpty()) {
annotationContainerOptional = AnnotationUtils.findAnnotation(original, TestSecurity.class);
}
return annotationContainerOptional;
}

private SecurityIdentity augment(SecurityIdentity identity, Annotation[] annotations) {
Instance<TestSecurityIdentityAugmentor> producer = CDI.current().select(TestSecurityIdentityAugmentor.class);
if (producer.isResolvable()) {
Expand Down

0 comments on commit 13ba9d0

Please sign in to comment.