parent
e30d78086a
commit
da7141eb5b
|
@ -21,14 +21,12 @@ import java.lang.annotation.Retention;
|
|||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.expression.AccessException;
|
||||
import org.springframework.expression.BeanResolver;
|
||||
import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
|
@ -40,6 +38,11 @@ import org.springframework.util.ReflectionUtils;
|
|||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.mock;
|
||||
import static org.mockito.BDDMockito.verify;
|
||||
import static org.mockito.BDDMockito.when;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
|
@ -47,12 +50,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|||
*/
|
||||
public class AuthenticationPrincipalArgumentResolverTests {
|
||||
|
||||
private final BeanResolver beanResolver = ((context, beanName) -> {
|
||||
if (!"test".equals(beanName)) {
|
||||
throw new AccessException("Could not resolve bean reference against BeanFactory");
|
||||
}
|
||||
return (Function<CustomUserPrincipal, String>) (principal) -> principal.property;
|
||||
});
|
||||
private BeanResolver beanResolver;
|
||||
|
||||
private Object expectedPrincipal;
|
||||
|
||||
|
@ -60,6 +58,7 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
|||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.beanResolver = mock(BeanResolver.class);
|
||||
this.resolver = new AuthenticationPrincipalArgumentResolver();
|
||||
this.resolver.setBeanResolver(this.beanResolver);
|
||||
}
|
||||
|
@ -142,8 +141,11 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
|||
public void resolveArgumentSpelBean() throws Exception {
|
||||
CustomUserPrincipal principal = new CustomUserPrincipal();
|
||||
setAuthenticationPrincipal(principal);
|
||||
when(this.beanResolver.resolve(any(), eq("test"))).thenReturn(principal.property);
|
||||
this.expectedPrincipal = principal.property;
|
||||
assertThat(this.resolver.resolveArgument(showUserSpelBean(), null, null, null)).isEqualTo(this.expectedPrincipal);
|
||||
assertThat(this.resolver.resolveArgument(showUserSpelBean(), null, null, null))
|
||||
.isEqualTo(this.expectedPrincipal);
|
||||
verify(this.beanResolver).resolve(any(), eq("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -281,8 +283,7 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
|||
public void showUserSpel(@AuthenticationPrincipal(expression = "property") String user) {
|
||||
}
|
||||
|
||||
public void showUserSpelBean(@AuthenticationPrincipal(
|
||||
expression = "@test.apply(#this)") String user) {
|
||||
public void showUserSpelBean(@AuthenticationPrincipal(expression = "@test") String user) {
|
||||
}
|
||||
|
||||
public void showUserSpelCopy(@AuthenticationPrincipal(
|
||||
|
|
|
@ -21,14 +21,12 @@ import java.lang.annotation.Retention;
|
|||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.expression.AccessException;
|
||||
import org.springframework.expression.BeanResolver;
|
||||
import org.springframework.expression.spel.SpelEvaluationException;
|
||||
import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||
|
@ -42,6 +40,11 @@ import org.springframework.util.ReflectionUtils;
|
|||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.mock;
|
||||
import static org.mockito.BDDMockito.verify;
|
||||
import static org.mockito.BDDMockito.when;
|
||||
|
||||
/**
|
||||
* @author Dan Zheng
|
||||
|
@ -50,17 +53,13 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|||
*/
|
||||
public class CurrentSecurityContextArgumentResolverTests {
|
||||
|
||||
private final BeanResolver beanResolver = ((context, beanName) -> {
|
||||
if (!"test".equals(beanName)) {
|
||||
throw new AccessException("Could not resolve bean reference against BeanFactory");
|
||||
}
|
||||
return (Function<SecurityContext, Authentication>) SecurityContext::getAuthentication;
|
||||
});
|
||||
private BeanResolver beanResolver;
|
||||
|
||||
private CurrentSecurityContextArgumentResolver resolver;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.beanResolver = mock(BeanResolver.class);
|
||||
this.resolver = new CurrentSecurityContextArgumentResolver();
|
||||
this.resolver.setBeanResolver(this.beanResolver);
|
||||
}
|
||||
|
@ -118,12 +117,12 @@ public class CurrentSecurityContextArgumentResolverTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentWithAuthenticationWithBean() {
|
||||
public void resolveArgumentWithAuthenticationWithBean() throws Exception {
|
||||
String principal = "john";
|
||||
setAuthenticationPrincipal(principal);
|
||||
Authentication auth1 = (Authentication) this.resolver
|
||||
.resolveArgument(showSecurityContextAuthenticationWithBean(), null, null, null);
|
||||
assertThat(auth1.getPrincipal()).isEqualTo(principal);
|
||||
when(this.beanResolver.resolve(any(), eq("test"))).thenReturn(principal);
|
||||
assertThat(this.resolver.resolveArgument(showSecurityContextAuthenticationWithBean(), null, null, null))
|
||||
.isEqualTo(principal);
|
||||
verify(this.beanResolver).resolve(any(), eq("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -234,7 +233,7 @@ public class CurrentSecurityContextArgumentResolverTests {
|
|||
}
|
||||
|
||||
public MethodParameter showSecurityContextAuthenticationWithBean() {
|
||||
return getMethodParameter("showSecurityContextAuthenticationWithBean", Authentication.class);
|
||||
return getMethodParameter("showSecurityContextAuthenticationWithBean", String.class);
|
||||
}
|
||||
|
||||
private MethodParameter showSecurityContextAuthenticationWithOptionalPrincipal() {
|
||||
|
@ -319,7 +318,7 @@ public class CurrentSecurityContextArgumentResolverTests {
|
|||
}
|
||||
|
||||
public void showSecurityContextAuthenticationWithBean(
|
||||
@CurrentSecurityContext(expression = "@test.apply(#this)") Authentication authentication) {
|
||||
@CurrentSecurityContext(expression = "@test") String name) {
|
||||
}
|
||||
|
||||
public void showSecurityContextAuthenticationWithOptionalPrincipal(
|
||||
|
|
Loading…
Reference in New Issue