Skip to content

Commit

Permalink
Polish Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jzheaux committed Feb 3, 2021
1 parent 873b9bd commit 107f38f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@
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;
Expand All @@ -40,26 +38,27 @@

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
*
*/
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;

private AuthenticationPrincipalArgumentResolver resolver;

@Before
public void setup() {
this.beanResolver = mock(BeanResolver.class);
this.resolver = new AuthenticationPrincipalArgumentResolver();
this.resolver.setBeanResolver(this.beanResolver);
}
Expand Down Expand Up @@ -142,8 +141,11 @@ public void resolveArgumentSpel() throws Exception {
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
Expand Down Expand Up @@ -281,8 +283,7 @@ public void showUserAnnotation(@AuthenticationPrincipal Object user) {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@
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;
Expand All @@ -42,6 +40,11 @@

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
Expand All @@ -50,17 +53,13 @@
*/
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);
}
Expand Down Expand Up @@ -118,12 +117,12 @@ public void resolveArgumentWithAuthentication() {
}

@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
Expand Down Expand Up @@ -234,7 +233,7 @@ private MethodParameter showSecurityContextAuthenticationAnnotation() {
}

public MethodParameter showSecurityContextAuthenticationWithBean() {
return getMethodParameter("showSecurityContextAuthenticationWithBean", Authentication.class);
return getMethodParameter("showSecurityContextAuthenticationWithBean", String.class);
}

private MethodParameter showSecurityContextAuthenticationWithOptionalPrincipal() {
Expand Down Expand Up @@ -319,7 +318,7 @@ public void showSecurityContextAuthenticationAnnotation(
}

public void showSecurityContextAuthenticationWithBean(
@CurrentSecurityContext(expression = "@test.apply(#this)") Authentication authentication) {
@CurrentSecurityContext(expression = "@test") String name) {
}

public void showSecurityContextAuthenticationWithOptionalPrincipal(
Expand Down

0 comments on commit 107f38f

Please sign in to comment.