|
21 | 21 | import java.lang.annotation.RetentionPolicy;
|
22 | 22 | import java.lang.annotation.Target;
|
23 | 23 | import java.lang.reflect.Method;
|
24 |
| -import java.util.function.Function; |
25 | 24 |
|
26 | 25 | import org.junit.After;
|
27 | 26 | import org.junit.Before;
|
28 | 27 | import org.junit.Test;
|
29 | 28 |
|
30 | 29 | import org.springframework.core.MethodParameter;
|
31 |
| -import org.springframework.expression.AccessException; |
32 | 30 | import org.springframework.expression.BeanResolver;
|
33 | 31 | import org.springframework.expression.spel.SpelEvaluationException;
|
34 | 32 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
|
42 | 40 |
|
43 | 41 | import static org.assertj.core.api.Assertions.assertThat;
|
44 | 42 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 43 | +import static org.mockito.ArgumentMatchers.any; |
| 44 | +import static org.mockito.ArgumentMatchers.eq; |
| 45 | +import static org.mockito.BDDMockito.mock; |
| 46 | +import static org.mockito.BDDMockito.verify; |
| 47 | +import static org.mockito.BDDMockito.when; |
45 | 48 |
|
46 | 49 | /**
|
47 | 50 | * @author Dan Zheng
|
|
50 | 53 | */
|
51 | 54 | public class CurrentSecurityContextArgumentResolverTests {
|
52 | 55 |
|
53 |
| - private final BeanResolver beanResolver = ((context, beanName) -> { |
54 |
| - if (!"test".equals(beanName)) { |
55 |
| - throw new AccessException("Could not resolve bean reference against BeanFactory"); |
56 |
| - } |
57 |
| - return (Function<SecurityContext, Authentication>) SecurityContext::getAuthentication; |
58 |
| - }); |
| 56 | + private BeanResolver beanResolver; |
59 | 57 |
|
60 | 58 | private CurrentSecurityContextArgumentResolver resolver;
|
61 | 59 |
|
62 | 60 | @Before
|
63 | 61 | public void setup() {
|
| 62 | + this.beanResolver = mock(BeanResolver.class); |
64 | 63 | this.resolver = new CurrentSecurityContextArgumentResolver();
|
65 | 64 | this.resolver.setBeanResolver(this.beanResolver);
|
66 | 65 | }
|
@@ -118,12 +117,12 @@ public void resolveArgumentWithAuthentication() {
|
118 | 117 | }
|
119 | 118 |
|
120 | 119 | @Test
|
121 |
| - public void resolveArgumentWithAuthenticationWithBean() { |
| 120 | + public void resolveArgumentWithAuthenticationWithBean() throws Exception { |
122 | 121 | String principal = "john";
|
123 |
| - setAuthenticationPrincipal(principal); |
124 |
| - Authentication auth1 = (Authentication) this.resolver |
125 |
| - .resolveArgument(showSecurityContextAuthenticationWithBean(), null, null, null); |
126 |
| - assertThat(auth1.getPrincipal()).isEqualTo(principal); |
| 122 | + when(this.beanResolver.resolve(any(), eq("test"))).thenReturn(principal); |
| 123 | + assertThat(this.resolver.resolveArgument(showSecurityContextAuthenticationWithBean(), null, null, null)) |
| 124 | + .isEqualTo(principal); |
| 125 | + verify(this.beanResolver).resolve(any(), eq("test")); |
127 | 126 | }
|
128 | 127 |
|
129 | 128 | @Test
|
@@ -234,7 +233,7 @@ private MethodParameter showSecurityContextAuthenticationAnnotation() {
|
234 | 233 | }
|
235 | 234 |
|
236 | 235 | public MethodParameter showSecurityContextAuthenticationWithBean() {
|
237 |
| - return getMethodParameter("showSecurityContextAuthenticationWithBean", Authentication.class); |
| 236 | + return getMethodParameter("showSecurityContextAuthenticationWithBean", String.class); |
238 | 237 | }
|
239 | 238 |
|
240 | 239 | private MethodParameter showSecurityContextAuthenticationWithOptionalPrincipal() {
|
@@ -319,7 +318,7 @@ public void showSecurityContextAuthenticationAnnotation(
|
319 | 318 | }
|
320 | 319 |
|
321 | 320 | public void showSecurityContextAuthenticationWithBean(
|
322 |
| - @CurrentSecurityContext(expression = "@test.apply(#this)") Authentication authentication) { |
| 321 | + @CurrentSecurityContext(expression = "@test") String name) { |
323 | 322 | }
|
324 | 323 |
|
325 | 324 | public void showSecurityContextAuthenticationWithOptionalPrincipal(
|
|
0 commit comments