Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QuarkusSecurityTestExtension afterEach call should not be made for tests without @TestSecurity #36645

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading