Added Testing

Issue gh-16177
This commit is contained in:
Josh Cummings
2024-12-09 17:54:05 -07:00
parent f565b23b51
commit 4cbaabb239
6 changed files with 203 additions and 9 deletions

View File

@@ -28,6 +28,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.AnnotatedMethod;
import org.springframework.expression.BeanResolver;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.annotation.AnnotationTemplateExpressionDefaults;
@@ -214,10 +215,22 @@ public class AuthenticationPrincipalArgumentResolverTests {
.isEqualTo(this.expectedPrincipal);
}
@Test
public void resolveArgumentWhenAliasForOnInterfaceThenInherits() throws Exception {
CustomUserPrincipal principal = new CustomUserPrincipal();
setAuthenticationPrincipal(principal);
assertThat(this.resolver.resolveArgument(showUserNoConcreteAnnotation(), null, null, null))
.isEqualTo(principal.property);
}
private MethodParameter showUserNoAnnotation() {
return getMethodParameter("showUserNoAnnotation", String.class);
}
private MethodParameter showUserNoConcreteAnnotation() {
return getMethodParameter("showUserNoConcreteAnnotation", String.class);
}
private MethodParameter showUserAnnotationString() {
return getMethodParameter("showUserAnnotation", String.class);
}
@@ -272,7 +285,7 @@ public class AuthenticationPrincipalArgumentResolverTests {
private MethodParameter getMethodParameter(String methodName, Class<?>... paramTypes) {
Method method = ReflectionUtils.findMethod(TestController.class, methodName, paramTypes);
return new MethodParameter(method, 0);
return new AnnotatedMethod(method).getMethodParameters()[0];
}
private void setAuthenticationPrincipal(Object principal) {
@@ -295,6 +308,16 @@ public class AuthenticationPrincipalArgumentResolverTests {
}
@Target({ ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@AuthenticationPrincipal
@interface Property {
@AliasFor(attribute = "expression", annotation = AuthenticationPrincipal.class)
String value() default "id";
}
@Retention(RetentionPolicy.RUNTIME)
@AuthenticationPrincipal
public @interface CurrentUser2 {
@@ -312,11 +335,22 @@ public class AuthenticationPrincipalArgumentResolverTests {
}
public static class TestController {
public interface TestInterface {
void showUserNoConcreteAnnotation(@Property("property") String property);
}
public static class TestController implements TestInterface {
public void showUserNoAnnotation(String user) {
}
@Override
public void showUserNoConcreteAnnotation(String user) {
}
public void showUserAnnotation(@AuthenticationPrincipal String user) {
}

View File

@@ -32,6 +32,7 @@ import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.AnnotatedMethod;
import org.springframework.core.annotation.SynthesizingMethodParameter;
import org.springframework.expression.BeanResolver;
import org.springframework.security.core.Authentication;
@@ -230,6 +231,19 @@ public class AuthenticationPrincipalArgumentResolverTests {
assertThat(result.block()).isEqualTo(principal.id);
}
@Test
public void resolveArgumentWhenAliasForOnInterfaceThenInherits() {
CustomUserPrincipal principal = new CustomUserPrincipal();
given(this.authentication.getPrincipal()).willReturn(principal);
ResolvableMethod method = ResolvableMethod.on(TestController.class)
.named("showUserNoConcreteAnnotation")
.build();
MethodParameter parameter = new AnnotatedMethod(method.method()).getMethodParameters()[0];
Mono<Object> result = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange)
.contextWrite(ReactiveSecurityContextHolder.withAuthentication(this.authentication));
assertThat(result.block()).isEqualTo(principal.property);
}
private MethodParameter arg0(String methodName) {
ResolvableMethod method = ResolvableMethod.on(getClass()).named(methodName).build();
return new SynthesizingMethodParameter(method.method(), 0);
@@ -317,6 +331,8 @@ public class AuthenticationPrincipalArgumentResolverTests {
public final int id = 1;
public final String property = "property";
public Object getPrincipal() {
return this;
}
@@ -340,4 +356,29 @@ public class AuthenticationPrincipalArgumentResolverTests {
}
@Target({ ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@AuthenticationPrincipal
@interface Property {
@AliasFor(attribute = "expression", annotation = AuthenticationPrincipal.class)
String value() default "id";
}
private interface TestInterface {
void showUserNoConcreteAnnotation(@Property("property") String property);
}
private static class TestController implements TestInterface {
@Override
public void showUserNoConcreteAnnotation(String user) {
}
}
}