From 10946e8153fc757585ffdb68d282d241cbf1cba9 Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Wed, 3 Feb 2021 09:01:26 -0700 Subject: [PATCH] Polish Tests Issue gh-9331 --- ...icationPrincipalArgumentResolverTests.java | 24 ++++++++------- ...tSecurityContextArgumentResolverTests.java | 29 +++++++++---------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/web/src/test/java/org/springframework/security/web/method/annotation/AuthenticationPrincipalArgumentResolverTests.java b/web/src/test/java/org/springframework/security/web/method/annotation/AuthenticationPrincipalArgumentResolverTests.java index 36da3d5e61..d0251b990d 100644 --- a/web/src/test/java/org/springframework/security/web/method/annotation/AuthenticationPrincipalArgumentResolverTests.java +++ b/web/src/test/java/org/springframework/security/web/method/annotation/AuthenticationPrincipalArgumentResolverTests.java @@ -22,13 +22,11 @@ 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; @@ -38,24 +36,26 @@ import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.util.ReflectionUtils; +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 * */ 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) (principal) -> principal.property; - }); + private BeanResolver beanResolver; private Object expectedPrincipal; private AuthenticationPrincipalArgumentResolver resolver; @Before public void setup() { + beanResolver = mock(BeanResolver.class); resolver = new AuthenticationPrincipalArgumentResolver(); resolver.setBeanResolver(this.beanResolver); } @@ -144,8 +144,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 @@ -279,8 +282,7 @@ public class AuthenticationPrincipalArgumentResolverTests { @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( diff --git a/web/src/test/java/org/springframework/security/web/method/annotation/CurrentSecurityContextArgumentResolverTests.java b/web/src/test/java/org/springframework/security/web/method/annotation/CurrentSecurityContextArgumentResolverTests.java index f7cc7b20d8..260b1b3967 100644 --- a/web/src/test/java/org/springframework/security/web/method/annotation/CurrentSecurityContextArgumentResolverTests.java +++ b/web/src/test/java/org/springframework/security/web/method/annotation/CurrentSecurityContextArgumentResolverTests.java @@ -20,14 +20,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; @@ -41,6 +39,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 @@ -49,17 +52,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::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); } @@ -117,12 +116,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 @@ -239,7 +238,7 @@ public class CurrentSecurityContextArgumentResolverTests { } public MethodParameter showSecurityContextAuthenticationWithBean() { - return getMethodParameter("showSecurityContextAuthenticationWithBean", Authentication.class); + return getMethodParameter("showSecurityContextAuthenticationWithBean", String.class); } private MethodParameter showSecurityContextAuthenticationWithOptionalPrincipal() { @@ -305,7 +304,7 @@ public class CurrentSecurityContextArgumentResolverTests { } public void showSecurityContextAuthenticationWithBean( - @CurrentSecurityContext(expression = "@test.apply(#this)") Authentication authentication) { + @CurrentSecurityContext(expression = "@test") String name) { } public void showSecurityContextAuthenticationWithOptionalPrincipal(@CurrentSecurityContext(expression = "authentication?.principal") Object principal) {