From d443ab87db69e5eae16b4ce9d8b03f4597d16b49 Mon Sep 17 00:00:00 2001 From: Sergey Beryozkin Date: Mon, 23 Oct 2023 15:21:08 +0100 Subject: [PATCH] QuarkusSecurityTestExtension afterEach call should not be made for tests without @TestSecurity --- .../QuarkusSecurityTestExtension.java | 58 +++++++++++-------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/test-framework/security/src/main/java/io/quarkus/test/security/QuarkusSecurityTestExtension.java b/test-framework/security/src/main/java/io/quarkus/test/security/QuarkusSecurityTestExtension.java index d119fc4a4b2c6..a4fcd2b5a68f7 100644 --- a/test-framework/security/src/main/java/io/quarkus/test/security/QuarkusSecurityTestExtension.java +++ b/test-framework/security/src/main/java/io/quarkus/test/security/QuarkusSecurityTestExtension.java @@ -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> annotationContainerOptional = AnnotationUtils.findAnnotation(method, - TestSecurity.class); - if (annotationContainerOptional.isEmpty()) { - annotationContainerOptional = AnnotationUtils.findAnnotation(original, TestSecurity.class); - } + Optional> 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()) { @@ -80,6 +68,30 @@ public void beforeEach(QuarkusTestMethodContext context) { } + private Optional> 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> 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 producer = CDI.current().select(TestSecurityIdentityAugmentor.class); if (producer.isResolvable()) {