Register hints for @WithSecurityContext on class level
Issue gh-12215
This commit is contained in:
parent
222f8ae1a5
commit
1648151dd2
|
@ -17,6 +17,7 @@
|
||||||
package org.springframework.security.test.aot.hint;
|
package org.springframework.security.test.aot.hint;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.springframework.aot.hint.MemberCategory;
|
import org.springframework.aot.hint.MemberCategory;
|
||||||
import org.springframework.aot.hint.RuntimeHints;
|
import org.springframework.aot.hint.RuntimeHints;
|
||||||
|
@ -38,13 +39,21 @@ class WithSecurityContextTestRuntimeHints implements TestRuntimeHintsRegistrar {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerHints(RuntimeHints hints, Class<?> testClass, ClassLoader classLoader) {
|
public void registerHints(RuntimeHints hints, Class<?> testClass, ClassLoader classLoader) {
|
||||||
Arrays.stream(testClass.getDeclaredMethods())
|
Stream.concat(getClassAnnotations(testClass), getMethodAnnotations(testClass))
|
||||||
.map((method) -> MergedAnnotations.from(method, SUPERCLASS).get(WithSecurityContext.class))
|
|
||||||
.filter(MergedAnnotation::isPresent)
|
.filter(MergedAnnotation::isPresent)
|
||||||
.map((withSecurityContext) -> withSecurityContext.getClass("factory"))
|
.map((withSecurityContext) -> withSecurityContext.getClass("factory"))
|
||||||
.forEach((factory) -> registerDeclaredConstructors(hints, factory));
|
.forEach((factory) -> registerDeclaredConstructors(hints, factory));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Stream<MergedAnnotation<WithSecurityContext>> getClassAnnotations(Class<?> testClass) {
|
||||||
|
return MergedAnnotations.search(SUPERCLASS).from(testClass).stream(WithSecurityContext.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Stream<MergedAnnotation<WithSecurityContext>> getMethodAnnotations(Class<?> testClass) {
|
||||||
|
return Arrays.stream(testClass.getDeclaredMethods())
|
||||||
|
.map((method) -> MergedAnnotations.from(method, SUPERCLASS).get(WithSecurityContext.class));
|
||||||
|
}
|
||||||
|
|
||||||
private void registerDeclaredConstructors(RuntimeHints hints, Class<?> factory) {
|
private void registerDeclaredConstructors(RuntimeHints hints, Class<?> factory) {
|
||||||
hints.reflection().registerType(factory, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
|
hints.reflection().registerType(factory, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,8 @@ import org.springframework.aot.hint.TypeReference;
|
||||||
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
|
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
|
||||||
import org.springframework.security.core.context.SecurityContext;
|
import org.springframework.security.core.context.SecurityContext;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.test.context.showcase.WithMockCustomUser;
|
||||||
|
import org.springframework.security.test.context.showcase.WithMockCustomUserSecurityContextFactory;
|
||||||
import org.springframework.security.test.context.support.WithAnonymousUser;
|
import org.springframework.security.test.context.support.WithAnonymousUser;
|
||||||
import org.springframework.security.test.context.support.WithMockUser;
|
import org.springframework.security.test.context.support.WithMockUser;
|
||||||
import org.springframework.security.test.context.support.WithSecurityContext;
|
import org.springframework.security.test.context.support.WithSecurityContext;
|
||||||
|
@ -39,6 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
/**
|
/**
|
||||||
* Tests for {@link WithSecurityContextTestRuntimeHints}.
|
* Tests for {@link WithSecurityContextTestRuntimeHints}.
|
||||||
*/
|
*/
|
||||||
|
@WithMockCustomUser
|
||||||
class WithSecurityContextTestRuntimeHintsTests {
|
class WithSecurityContextTestRuntimeHintsTests {
|
||||||
|
|
||||||
private final RuntimeHints hints = new RuntimeHints();
|
private final RuntimeHints hints = new RuntimeHints();
|
||||||
|
@ -85,6 +88,12 @@ class WithSecurityContextTestRuntimeHintsTests {
|
||||||
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.hints);
|
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.hints);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void withMockCustomUserOnClassHasHints() {
|
||||||
|
assertThat(RuntimeHintsPredicates.reflection().onType(WithMockCustomUserSecurityContextFactory.class)
|
||||||
|
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.hints);
|
||||||
|
}
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@WithSecurityContext(factory = WithMockTestUserSecurityContextFactory.class)
|
@WithSecurityContext(factory = WithMockTestUserSecurityContextFactory.class)
|
||||||
@interface WithMockTestUser {
|
@interface WithMockTestUser {
|
||||||
|
|
|
@ -16,11 +16,15 @@
|
||||||
|
|
||||||
package org.springframework.security.test.context.showcase;
|
package org.springframework.security.test.context.showcase;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
|
||||||
import org.springframework.security.test.context.support.WithSecurityContext;
|
import org.springframework.security.test.context.support.WithSecurityContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rob Winch
|
* @author Rob Winch
|
||||||
*/
|
*/
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@WithSecurityContext(factory = WithMockCustomUserSecurityContextFactory.class)
|
@WithSecurityContext(factory = WithMockCustomUserSecurityContextFactory.class)
|
||||||
public @interface WithMockCustomUser {
|
public @interface WithMockCustomUser {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue