From a53d02231206216c9cfbf19fa9b24dfb522838dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Fri, 20 May 2016 16:42:34 +1000 Subject: [PATCH] Support WithSecurityContextFactory on superclass Fixes gh-3888 --- gradle/javaprojects.gradle | 1 + ...hSecurityContextTestExecutionListener.java | 27 ++++++++- .../context/showcase/WithMockUserParent.java | 26 ++++++++ .../showcase/WithMockUserParentTests.java | 60 +++++++++++++++++++ 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 test/src/test/java/org/springframework/security/test/context/showcase/WithMockUserParent.java create mode 100644 test/src/test/java/org/springframework/security/test/context/showcase/WithMockUserParentTests.java diff --git a/gradle/javaprojects.gradle b/gradle/javaprojects.gradle index 847ce71b8e..60fe33adbd 100644 --- a/gradle/javaprojects.gradle +++ b/gradle/javaprojects.gradle @@ -215,6 +215,7 @@ javadoc { 'Spring Security ACL':['org.springframework.security.acls*'], 'Spring Security Config':['org.springframework.security.config*'], 'Spring Security Taglibs':['org.springframework.security.taglibs*'], + 'Spring Security Test':['org.springframework.security.test*'], ] addStringOption('-quiet') } diff --git a/test/src/main/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListener.java b/test/src/main/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListener.java index 5e6328dc49..3cfb27d200 100644 --- a/test/src/main/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListener.java +++ b/test/src/main/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequ import org.springframework.test.context.TestContext; import org.springframework.test.context.TestExecutionListener; import org.springframework.test.context.support.AbstractTestExecutionListener; +import org.springframework.test.util.MetaAnnotationUtils; import org.springframework.test.web.servlet.MockMvc; /** @@ -39,6 +40,7 @@ import org.springframework.test.web.servlet.MockMvc; * too. * * @author Rob Winch + * @author Eddú Meléndez * @since 4.0 */ public class WithSecurityContextTestExecutionListener extends @@ -83,6 +85,27 @@ public class WithSecurityContextTestExecutionListener extends return null; } + @SuppressWarnings({ "rawtypes", "unchecked" }) + private SecurityContext createSecurityContext(Class annotated, + TestContext context) { + MetaAnnotationUtils.AnnotationDescriptor + withSecurityContext = MetaAnnotationUtils.findAnnotationDescriptor( + annotated, WithSecurityContext.class); + if (withSecurityContext != null) { + WithSecurityContextFactory factory = createFactory(withSecurityContext.getAnnotation(), context); + Class type = (Class) GenericTypeResolver.resolveTypeArgument(factory.getClass(), WithSecurityContextFactory.class); + Annotation annotation = findAnnotation(annotated, type); + try { + return factory.createSecurityContext(annotation); + } + catch (RuntimeException e) { + throw new IllegalStateException( + "Unable to create SecurityContext using " + annotation, e); + } + } + return null; + } + private Annotation findAnnotation(AnnotatedElement annotated, Class type) { Annotation findAnnotation = AnnotationUtils.findAnnotation(annotated, type); @@ -131,4 +154,4 @@ public class WithSecurityContextTestExecutionListener extends public int getOrder() { return 1000; } -} \ No newline at end of file +} diff --git a/test/src/test/java/org/springframework/security/test/context/showcase/WithMockUserParent.java b/test/src/test/java/org/springframework/security/test/context/showcase/WithMockUserParent.java new file mode 100644 index 0000000000..b2c86cddc7 --- /dev/null +++ b/test/src/test/java/org/springframework/security/test/context/showcase/WithMockUserParent.java @@ -0,0 +1,26 @@ +/* + * Copyright 2002-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.test.context.showcase; + +import org.springframework.security.test.context.support.WithMockUser; + +/** + * @author Eddú Meléndez + */ +@WithMockUser +public class WithMockUserParent { + +} \ No newline at end of file diff --git a/test/src/test/java/org/springframework/security/test/context/showcase/WithMockUserParentTests.java b/test/src/test/java/org/springframework/security/test/context/showcase/WithMockUserParentTests.java new file mode 100644 index 0000000000..9cdc497be2 --- /dev/null +++ b/test/src/test/java/org/springframework/security/test/context/showcase/WithMockUserParentTests.java @@ -0,0 +1,60 @@ +/* + * Copyright 2002-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.test.context.showcase; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.test.context.showcase.service.HelloMessageService; +import org.springframework.security.test.context.showcase.service.MessageService; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Eddú Meléndez + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = WithMockUserParentTests.Config.class) +public class WithMockUserParentTests extends WithMockUserParent { + + @Autowired + private MessageService messageService; + + @Test + public void getMessageWithMockUser() { + String message = messageService.getMessage(); + assertThat(message).contains("user"); + } + + @EnableGlobalMethodSecurity(prePostEnabled = true) + @ComponentScan(basePackageClasses = HelloMessageService.class) + static class Config { + // @formatter:off + @Autowired + public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { + auth + .inMemoryAuthentication() + .withUser("user").password("password").roles("USER"); + } + // @formatter:on + } +} \ No newline at end of file