Replace try/catch with AssertJ
Replace manual try/catch/fail blocks with AssertJ calls.
This commit is contained in:
committed by
Josh Cummings
parent
d9276ed8f3
commit
910b81928f
+14
-38
@@ -25,7 +25,8 @@ import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatNoException;
|
||||
|
||||
/**
|
||||
* Tests for {@link RoleHierarchyImpl}.
|
||||
@@ -102,48 +103,23 @@ public class RoleHierarchyImplTests {
|
||||
@Test
|
||||
public void testCyclesInRoleHierarchy() {
|
||||
RoleHierarchyImpl roleHierarchyImpl = new RoleHierarchyImpl();
|
||||
try {
|
||||
roleHierarchyImpl.setHierarchy("ROLE_A > ROLE_A");
|
||||
fail("Cycle in role hierarchy was not detected!");
|
||||
}
|
||||
catch (CycleInRoleHierarchyException ex) {
|
||||
}
|
||||
try {
|
||||
roleHierarchyImpl.setHierarchy("ROLE_A > ROLE_B\nROLE_B > ROLE_A");
|
||||
fail("Cycle in role hierarchy was not detected!");
|
||||
}
|
||||
catch (CycleInRoleHierarchyException ex) {
|
||||
}
|
||||
try {
|
||||
roleHierarchyImpl.setHierarchy("ROLE_A > ROLE_B\nROLE_B > ROLE_C\nROLE_C > ROLE_A");
|
||||
fail("Cycle in role hierarchy was not detected!");
|
||||
}
|
||||
catch (CycleInRoleHierarchyException ex) {
|
||||
}
|
||||
try {
|
||||
roleHierarchyImpl.setHierarchy(
|
||||
"ROLE_A > ROLE_B\nROLE_B > ROLE_C\nROLE_C > ROLE_E\nROLE_E > ROLE_D\nROLE_D > ROLE_B");
|
||||
fail("Cycle in role hierarchy was not detected!");
|
||||
}
|
||||
catch (CycleInRoleHierarchyException ex) {
|
||||
}
|
||||
try {
|
||||
roleHierarchyImpl.setHierarchy("ROLE_C > ROLE_B\nROLE_B > ROLE_A\nROLE_A > ROLE_B");
|
||||
fail("Cycle in role hierarchy was not detected!");
|
||||
}
|
||||
catch (CycleInRoleHierarchyException ex) {
|
||||
}
|
||||
assertThatExceptionOfType(CycleInRoleHierarchyException.class)
|
||||
.isThrownBy(() -> roleHierarchyImpl.setHierarchy("ROLE_A > ROLE_A"));
|
||||
assertThatExceptionOfType(CycleInRoleHierarchyException.class)
|
||||
.isThrownBy(() -> roleHierarchyImpl.setHierarchy("ROLE_A > ROLE_B\nROLE_B > ROLE_A"));
|
||||
assertThatExceptionOfType(CycleInRoleHierarchyException.class)
|
||||
.isThrownBy(() -> roleHierarchyImpl.setHierarchy("ROLE_A > ROLE_B\nROLE_B > ROLE_C\nROLE_C > ROLE_A"));
|
||||
assertThatExceptionOfType(CycleInRoleHierarchyException.class).isThrownBy(() -> roleHierarchyImpl
|
||||
.setHierarchy("ROLE_A > ROLE_B\nROLE_B > ROLE_C\nROLE_C > ROLE_E\nROLE_E > ROLE_D\nROLE_D > ROLE_B"));
|
||||
assertThatExceptionOfType(CycleInRoleHierarchyException.class)
|
||||
.isThrownBy(() -> roleHierarchyImpl.setHierarchy("ROLE_C > ROLE_B\nROLE_B > ROLE_A\nROLE_A > ROLE_B"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoCyclesInRoleHierarchy() {
|
||||
RoleHierarchyImpl roleHierarchyImpl = new RoleHierarchyImpl();
|
||||
try {
|
||||
roleHierarchyImpl.setHierarchy("ROLE_A > ROLE_B\nROLE_A > ROLE_C\nROLE_C > ROLE_D\nROLE_B > ROLE_D");
|
||||
}
|
||||
catch (CycleInRoleHierarchyException ex) {
|
||||
fail("A cycle in role hierarchy was incorrectly detected!");
|
||||
}
|
||||
assertThatNoException().isThrownBy(() -> roleHierarchyImpl
|
||||
.setHierarchy("ROLE_A > ROLE_B\nROLE_A > ROLE_C\nROLE_C > ROLE_D\nROLE_B > ROLE_D"));
|
||||
}
|
||||
|
||||
// SEC-863
|
||||
|
||||
+4
-22
@@ -31,7 +31,7 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.util.SimpleMethodInvocation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests {@link AfterInvocationProviderManager}.
|
||||
@@ -72,13 +72,7 @@ public class AfterInvocationProviderManagerTests {
|
||||
public void testRejectsEmptyProvidersList() {
|
||||
AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
|
||||
List list = new Vector();
|
||||
try {
|
||||
manager.setProviders(list);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
assertThat(true).isTrue();
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> manager.setProviders(list));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -88,25 +82,13 @@ public class AfterInvocationProviderManagerTests {
|
||||
list.add(new MockAfterInvocationProvider("swap1", MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP1")));
|
||||
list.add(45);
|
||||
list.add(new MockAfterInvocationProvider("swap3", MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP3")));
|
||||
try {
|
||||
manager.setProviders(list);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
assertThat(true).isTrue();
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> manager.setProviders(list));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRejectsNullProvidersList() throws Exception {
|
||||
AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
|
||||
try {
|
||||
manager.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
assertThat(true).isTrue();
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(manager::afterPropertiesSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+2
-6
@@ -26,6 +26,7 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
@@ -96,12 +97,7 @@ public class RunAsManagerImplTests {
|
||||
@Test
|
||||
public void testStartupDetectsMissingKey() throws Exception {
|
||||
RunAsManagerImpl runAs = new RunAsManagerImpl();
|
||||
try {
|
||||
runAs.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(runAs::afterPropertiesSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+3
-9
@@ -22,7 +22,7 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests {@link RunAsUserToken}.
|
||||
@@ -52,14 +52,8 @@ public class RunAsUserTokenTests {
|
||||
|
||||
@Test
|
||||
public void testNoArgConstructorDoesntExist() {
|
||||
Class<RunAsUserToken> clazz = RunAsUserToken.class;
|
||||
try {
|
||||
clazz.getDeclaredConstructor((Class[]) null);
|
||||
fail("Should have thrown NoSuchMethodException");
|
||||
}
|
||||
catch (NoSuchMethodException expected) {
|
||||
assertThat(true).isTrue();
|
||||
}
|
||||
assertThatExceptionOfType(NoSuchMethodException.class)
|
||||
.isThrownBy(() -> RunAsUserToken.class.getDeclaredConstructor((Class[]) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+5
-19
@@ -47,7 +47,7 @@ import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
@@ -251,12 +251,8 @@ public class MethodSecurityInterceptorTests {
|
||||
given(this.authman.authenticate(this.token)).willReturn(this.token);
|
||||
willThrow(new AccessDeniedException("rejected")).given(this.adm).decide(any(Authentication.class),
|
||||
any(MethodInvocation.class), any(List.class));
|
||||
try {
|
||||
this.advisedTarget.makeUpperCase("HELLO");
|
||||
fail("Expected Exception");
|
||||
}
|
||||
catch (AccessDeniedException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(AccessDeniedException.class)
|
||||
.isThrownBy(() -> this.advisedTarget.makeUpperCase("HELLO"));
|
||||
verify(this.eventPublisher).publishEvent(any(AuthorizationFailureEvent.class));
|
||||
}
|
||||
|
||||
@@ -297,12 +293,7 @@ public class MethodSecurityInterceptorTests {
|
||||
this.interceptor.setRunAsManager(runAs);
|
||||
mdsReturnsUserRole();
|
||||
given(runAs.buildRunAs(eq(this.token), any(MethodInvocation.class), any(List.class))).willReturn(runAsToken);
|
||||
try {
|
||||
this.advisedTarget.makeUpperCase("hello");
|
||||
fail("Expected Exception");
|
||||
}
|
||||
catch (RuntimeException success) {
|
||||
}
|
||||
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> this.advisedTarget.makeUpperCase("hello"));
|
||||
// Check we've changed back
|
||||
assertThat(SecurityContextHolder.getContext()).isSameAs(ctx);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.token);
|
||||
@@ -323,12 +314,7 @@ public class MethodSecurityInterceptorTests {
|
||||
AfterInvocationManager aim = mock(AfterInvocationManager.class);
|
||||
this.interceptor.setAfterInvocationManager(aim);
|
||||
given(mi.proceed()).willThrow(new Throwable());
|
||||
try {
|
||||
this.interceptor.invoke(mi);
|
||||
fail("Expected exception");
|
||||
}
|
||||
catch (Throwable expected) {
|
||||
}
|
||||
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> this.interceptor.invoke(mi));
|
||||
verifyZeroInteractions(aim);
|
||||
}
|
||||
|
||||
|
||||
+8
-25
@@ -45,7 +45,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
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.given;
|
||||
@@ -127,12 +127,8 @@ public class AspectJMethodSecurityInterceptorTests {
|
||||
public void callbackIsNotInvokedWhenPermissionDenied() {
|
||||
willThrow(new AccessDeniedException("denied")).given(this.adm).decide(any(), any(), any());
|
||||
SecurityContextHolder.getContext().setAuthentication(this.token);
|
||||
try {
|
||||
this.interceptor.invoke(this.joinPoint, this.aspectJCallback);
|
||||
fail("Expected AccessDeniedException");
|
||||
}
|
||||
catch (AccessDeniedException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(AccessDeniedException.class)
|
||||
.isThrownBy(() -> this.interceptor.invoke(this.joinPoint, this.aspectJCallback));
|
||||
verify(this.aspectJCallback, never()).proceedWithObject();
|
||||
}
|
||||
|
||||
@@ -156,12 +152,8 @@ public class AspectJMethodSecurityInterceptorTests {
|
||||
AfterInvocationManager aim = mock(AfterInvocationManager.class);
|
||||
this.interceptor.setAfterInvocationManager(aim);
|
||||
given(this.aspectJCallback.proceedWithObject()).willThrow(new RuntimeException());
|
||||
try {
|
||||
this.interceptor.invoke(this.joinPoint, this.aspectJCallback);
|
||||
fail("Expected exception");
|
||||
}
|
||||
catch (RuntimeException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(RuntimeException.class)
|
||||
.isThrownBy(() -> this.interceptor.invoke(this.joinPoint, this.aspectJCallback));
|
||||
verifyZeroInteractions(aim);
|
||||
}
|
||||
|
||||
@@ -178,12 +170,8 @@ public class AspectJMethodSecurityInterceptorTests {
|
||||
this.interceptor.setRunAsManager(runAs);
|
||||
given(runAs.buildRunAs(eq(this.token), any(MethodInvocation.class), any(List.class))).willReturn(runAsToken);
|
||||
given(this.aspectJCallback.proceedWithObject()).willThrow(new RuntimeException());
|
||||
try {
|
||||
this.interceptor.invoke(this.joinPoint, this.aspectJCallback);
|
||||
fail("Expected Exception");
|
||||
}
|
||||
catch (RuntimeException success) {
|
||||
}
|
||||
assertThatExceptionOfType(RuntimeException.class)
|
||||
.isThrownBy(() -> this.interceptor.invoke(this.joinPoint, this.aspectJCallback));
|
||||
// Check we've changed back
|
||||
assertThat(SecurityContextHolder.getContext()).isSameAs(ctx);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.token);
|
||||
@@ -202,12 +190,7 @@ public class AspectJMethodSecurityInterceptorTests {
|
||||
this.interceptor.setRunAsManager(runAs);
|
||||
given(runAs.buildRunAs(eq(this.token), any(MethodInvocation.class), any(List.class))).willReturn(runAsToken);
|
||||
given(this.joinPoint.proceed()).willThrow(new RuntimeException());
|
||||
try {
|
||||
this.interceptor.invoke(this.joinPoint);
|
||||
fail("Expected Exception");
|
||||
}
|
||||
catch (RuntimeException success) {
|
||||
}
|
||||
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> this.interceptor.invoke(this.joinPoint));
|
||||
// Check we've changed back
|
||||
assertThat(SecurityContextHolder.getContext()).isSameAs(ctx);
|
||||
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.token);
|
||||
|
||||
+5
-20
@@ -17,6 +17,7 @@
|
||||
package org.springframework.security.access.vote;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
@@ -28,7 +29,7 @@ import org.springframework.security.access.SecurityConfig;
|
||||
import org.springframework.security.core.Authentication;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests {@link AbstractAccessDecisionManager}.
|
||||
@@ -86,23 +87,12 @@ public class AbstractAccessDecisionManagerTests {
|
||||
|
||||
@Test
|
||||
public void testRejectsEmptyList() {
|
||||
List list = new Vector();
|
||||
try {
|
||||
new MockDecisionManagerImpl(list);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new MockDecisionManagerImpl(Collections.emptyList()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRejectsNullVotersList() {
|
||||
try {
|
||||
new MockDecisionManagerImpl(null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new MockDecisionManagerImpl(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -113,12 +103,7 @@ public class AbstractAccessDecisionManagerTests {
|
||||
|
||||
@Test
|
||||
public void testWillNotStartIfDecisionVotersNotSet() {
|
||||
try {
|
||||
new MockDecisionManagerImpl(null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new MockDecisionManagerImpl(null));
|
||||
}
|
||||
|
||||
private class MockDecisionManagerImpl extends AbstractAccessDecisionManager {
|
||||
|
||||
+2
-7
@@ -30,7 +30,7 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests {@link AuthenticatedVoter}.
|
||||
@@ -82,12 +82,7 @@ public class AuthenticatedVoterTests {
|
||||
@Test
|
||||
public void testSetterRejectsNull() {
|
||||
AuthenticatedVoter voter = new AuthenticatedVoter();
|
||||
try {
|
||||
voter.setAuthenticationTrustResolver(null);
|
||||
fail("Expected IAE");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> voter.setAuthenticationTrustResolver(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+4
-19
@@ -28,7 +28,7 @@ import org.springframework.security.access.SecurityConfig;
|
||||
import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests {@link UnanimousBased}.
|
||||
@@ -73,12 +73,7 @@ public class UnanimousBasedTests {
|
||||
TestingAuthenticationToken auth = makeTestToken();
|
||||
UnanimousBased mgr = makeDecisionManager();
|
||||
List<ConfigAttribute> config = SecurityConfig.createList(new String[] { "ROLE_1", "DENY_FOR_SURE" });
|
||||
try {
|
||||
mgr.decide(auth, new Object(), config);
|
||||
fail("Should have thrown AccessDeniedException");
|
||||
}
|
||||
catch (AccessDeniedException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(() -> mgr.decide(auth, new Object(), config));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -94,12 +89,7 @@ public class UnanimousBasedTests {
|
||||
TestingAuthenticationToken auth = makeTestToken();
|
||||
UnanimousBased mgr = makeDecisionManager();
|
||||
List<ConfigAttribute> config = SecurityConfig.createList("ROLE_WE_DO_NOT_HAVE");
|
||||
try {
|
||||
mgr.decide(auth, new Object(), config);
|
||||
fail("Should have thrown AccessDeniedException");
|
||||
}
|
||||
catch (AccessDeniedException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(() -> mgr.decide(auth, new Object(), config));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -116,12 +106,7 @@ public class UnanimousBasedTests {
|
||||
UnanimousBased mgr = makeDecisionManager();
|
||||
assertThat(!mgr.isAllowIfAllAbstainDecisions()).isTrue(); // check default
|
||||
List<ConfigAttribute> config = SecurityConfig.createList("IGNORED_BY_ALL");
|
||||
try {
|
||||
mgr.decide(auth, new Object(), config);
|
||||
fail("Should have thrown AccessDeniedException");
|
||||
}
|
||||
catch (AccessDeniedException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(() -> mgr.decide(auth, new Object(), config));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+18
-55
@@ -28,7 +28,7 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
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.given;
|
||||
@@ -171,12 +171,8 @@ public class ProviderManagerTests {
|
||||
public void authenticationExceptionIsRethrownIfNoLaterProviderAuthenticates() {
|
||||
ProviderManager mgr = new ProviderManager(Arrays
|
||||
.asList(createProviderWhichThrows(new BadCredentialsException("")), createProviderWhichReturns(null)));
|
||||
try {
|
||||
mgr.authenticate(mock(Authentication.class));
|
||||
fail("Expected BadCredentialsException");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class)
|
||||
.isThrownBy(() -> mgr.authenticate(mock(Authentication.class)));
|
||||
}
|
||||
|
||||
// SEC-546
|
||||
@@ -186,12 +182,8 @@ public class ProviderManagerTests {
|
||||
});
|
||||
AuthenticationProvider otherProvider = mock(AuthenticationProvider.class);
|
||||
ProviderManager authMgr = new ProviderManager(Arrays.asList(iThrowAccountStatusException, otherProvider));
|
||||
try {
|
||||
authMgr.authenticate(mock(Authentication.class));
|
||||
fail("Expected AccountStatusException");
|
||||
}
|
||||
catch (AccountStatusException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(AccountStatusException.class)
|
||||
.isThrownBy(() -> authMgr.authenticate(mock(Authentication.class)));
|
||||
verifyNoInteractions(otherProvider);
|
||||
}
|
||||
|
||||
@@ -212,12 +204,8 @@ public class ProviderManagerTests {
|
||||
});
|
||||
AuthenticationManager parent = mock(AuthenticationManager.class);
|
||||
ProviderManager mgr = new ProviderManager(Collections.singletonList(iThrowAccountStatusException), parent);
|
||||
try {
|
||||
mgr.authenticate(mock(Authentication.class));
|
||||
fail("Expected exception");
|
||||
}
|
||||
catch (AccountStatusException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(AccountStatusException.class)
|
||||
.isThrownBy(() -> mgr.authenticate(mock(Authentication.class)));
|
||||
verifyNoInteractions(parent);
|
||||
}
|
||||
|
||||
@@ -232,13 +220,8 @@ public class ProviderManagerTests {
|
||||
ProviderManager mgr = new ProviderManager(
|
||||
Collections.singletonList(createProviderWhichThrows(new BadCredentialsException(""))), parent);
|
||||
mgr.setAuthenticationEventPublisher(publisher);
|
||||
try {
|
||||
mgr.authenticate(authReq);
|
||||
fail("Expected exception");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
verify(publisher).publishAuthenticationFailure(expected, authReq);
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> mgr.authenticate(authReq))
|
||||
.satisfies((ex) -> verify(publisher).publishAuthenticationFailure(ex, authReq));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -251,15 +234,10 @@ public class ProviderManagerTests {
|
||||
mgr.setAuthenticationEventPublisher(publisher);
|
||||
// Set a provider that throws an exception - this is the exception we expect to be
|
||||
// propagated
|
||||
final BadCredentialsException expected = new BadCredentialsException("I'm the one from the parent");
|
||||
BadCredentialsException expected = new BadCredentialsException("I'm the one from the parent");
|
||||
given(parent.authenticate(authReq)).willThrow(expected);
|
||||
try {
|
||||
mgr.authenticate(authReq);
|
||||
fail("Expected exception");
|
||||
}
|
||||
catch (BadCredentialsException ex) {
|
||||
assertThat(ex).isSameAs(expected);
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> mgr.authenticate(authReq))
|
||||
.isSameAs(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -271,13 +249,7 @@ public class ProviderManagerTests {
|
||||
final Authentication authReq = mock(Authentication.class);
|
||||
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
|
||||
mgr.setAuthenticationEventPublisher(publisher);
|
||||
try {
|
||||
mgr.authenticate(authReq);
|
||||
fail("Expected exception");
|
||||
}
|
||||
catch (LockedException ex) {
|
||||
assertThat(ex).isSameAs(expected);
|
||||
}
|
||||
assertThatExceptionOfType(LockedException.class).isThrownBy(() -> mgr.authenticate(authReq));
|
||||
verify(publisher).publishAuthenticationFailure(expected, authReq);
|
||||
}
|
||||
|
||||
@@ -287,13 +259,9 @@ public class ProviderManagerTests {
|
||||
InternalAuthenticationServiceException expected = new InternalAuthenticationServiceException("Expected");
|
||||
ProviderManager mgr = new ProviderManager(Arrays.asList(createProviderWhichThrows(expected),
|
||||
createProviderWhichThrows(new BadCredentialsException("Oops"))), null);
|
||||
final Authentication authReq = mock(Authentication.class);
|
||||
try {
|
||||
mgr.authenticate(authReq);
|
||||
fail("Expected Exception");
|
||||
}
|
||||
catch (InternalAuthenticationServiceException success) {
|
||||
}
|
||||
Authentication authReq = mock(Authentication.class);
|
||||
assertThatExceptionOfType(InternalAuthenticationServiceException.class)
|
||||
.isThrownBy(() -> mgr.authenticate(authReq));
|
||||
}
|
||||
|
||||
// gh-6281
|
||||
@@ -307,13 +275,8 @@ public class ProviderManagerTests {
|
||||
parentMgr.setAuthenticationEventPublisher(publisher);
|
||||
childMgr.setAuthenticationEventPublisher(publisher);
|
||||
final Authentication authReq = mock(Authentication.class);
|
||||
try {
|
||||
childMgr.authenticate(authReq);
|
||||
fail("Expected exception");
|
||||
}
|
||||
catch (BadCredentialsException ex) {
|
||||
assertThat(ex).isSameAs(badCredentialsExParent);
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> childMgr.authenticate(authReq))
|
||||
.isSameAs(badCredentialsExParent);
|
||||
verify(publisher).publishAuthenticationFailure(badCredentialsExParent, authReq); // Parent
|
||||
// publishes
|
||||
verifyNoMoreInteractions(publisher); // Child should not publish (duplicate event)
|
||||
|
||||
+11
-15
@@ -21,7 +21,7 @@ import org.junit.Test;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests {@link UsernamePasswordAuthenticationToken}.
|
||||
@@ -32,29 +32,25 @@ public class UsernamePasswordAuthenticationTokenTests {
|
||||
|
||||
@Test
|
||||
public void authenticatedPropertyContractIsSatisfied() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password",
|
||||
UsernamePasswordAuthenticationToken grantedToken = new UsernamePasswordAuthenticationToken("Test", "Password",
|
||||
AuthorityUtils.NO_AUTHORITIES);
|
||||
// check default given we passed some GrantedAuthorty[]s (well, we passed empty
|
||||
// list)
|
||||
assertThat(token.isAuthenticated()).isTrue();
|
||||
assertThat(grantedToken.isAuthenticated()).isTrue();
|
||||
// check explicit set to untrusted (we can safely go from trusted to untrusted,
|
||||
// but not the reverse)
|
||||
token.setAuthenticated(false);
|
||||
assertThat(!token.isAuthenticated()).isTrue();
|
||||
grantedToken.setAuthenticated(false);
|
||||
assertThat(!grantedToken.isAuthenticated()).isTrue();
|
||||
// Now let's create a UsernamePasswordAuthenticationToken without any
|
||||
// GrantedAuthorty[]s (different constructor)
|
||||
token = new UsernamePasswordAuthenticationToken("Test", "Password");
|
||||
assertThat(!token.isAuthenticated()).isTrue();
|
||||
UsernamePasswordAuthenticationToken noneGrantedToken = new UsernamePasswordAuthenticationToken("Test",
|
||||
"Password");
|
||||
assertThat(!noneGrantedToken.isAuthenticated()).isTrue();
|
||||
// check we're allowed to still set it to untrusted
|
||||
token.setAuthenticated(false);
|
||||
assertThat(!token.isAuthenticated()).isTrue();
|
||||
noneGrantedToken.setAuthenticated(false);
|
||||
assertThat(!noneGrantedToken.isAuthenticated()).isTrue();
|
||||
// check denied changing it to trusted
|
||||
try {
|
||||
token.setAuthenticated(true);
|
||||
fail("Should have prohibited setAuthenticated(true)");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> noneGrantedToken.setAuthenticated(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+4
-13
@@ -26,7 +26,8 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests {@link AnonymousAuthenticationProvider}.
|
||||
@@ -40,22 +41,12 @@ public class AnonymousAuthenticationProviderTests {
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider("qwerty");
|
||||
AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("WRONG_KEY", "Test",
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
|
||||
try {
|
||||
aap.authenticate(token);
|
||||
fail("Should have thrown BadCredentialsException");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> aap.authenticate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetectsMissingKey() {
|
||||
try {
|
||||
new AnonymousAuthenticationProvider(null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new AnonymousAuthenticationProvider(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+9
-32
@@ -27,7 +27,8 @@ import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests {@link AnonymousAuthenticationToken}.
|
||||
@@ -40,30 +41,11 @@ public class AnonymousAuthenticationTokenTests {
|
||||
|
||||
@Test
|
||||
public void testConstructorRejectsNulls() {
|
||||
try {
|
||||
new AnonymousAuthenticationToken(null, "Test", ROLES_12);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
try {
|
||||
new AnonymousAuthenticationToken("key", null, ROLES_12);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
try {
|
||||
new AnonymousAuthenticationToken("key", "Test", null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
try {
|
||||
new AnonymousAuthenticationToken("key", "Test", AuthorityUtils.NO_AUTHORITIES);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new AnonymousAuthenticationToken(null, "Test", ROLES_12));
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new AnonymousAuthenticationToken("key", null, ROLES_12));
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new AnonymousAuthenticationToken("key", "Test", null));
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new AnonymousAuthenticationToken("key", "Test", AuthorityUtils.NO_AUTHORITIES));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -85,13 +67,8 @@ public class AnonymousAuthenticationTokenTests {
|
||||
|
||||
@Test
|
||||
public void testNoArgConstructorDoesntExist() {
|
||||
Class<?> clazz = AnonymousAuthenticationToken.class;
|
||||
try {
|
||||
clazz.getDeclaredConstructor((Class[]) null);
|
||||
fail("Should have thrown NoSuchMethodException");
|
||||
}
|
||||
catch (NoSuchMethodException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(NoSuchMethodException.class)
|
||||
.isThrownBy(() -> AnonymousAuthenticationToken.class.getDeclaredConstructor((Class[]) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+30
-142
@@ -50,6 +50,7 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
@@ -77,12 +78,7 @@ public class DaoAuthenticationProviderTests {
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown BadCredentialsException");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -92,12 +88,8 @@ public class DaoAuthenticationProviderTests {
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken("rod", null);
|
||||
try {
|
||||
provider.authenticate(authenticationToken);
|
||||
fail("Expected BadCredenialsException");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class)
|
||||
.isThrownBy(() -> provider.authenticate(authenticationToken));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -106,12 +98,7 @@ public class DaoAuthenticationProviderTests {
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserPeterAccountExpired());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown AccountExpiredException");
|
||||
}
|
||||
catch (AccountExpiredException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(AccountExpiredException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -120,35 +107,20 @@ public class DaoAuthenticationProviderTests {
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserPeterAccountLocked());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown LockedException");
|
||||
}
|
||||
catch (LockedException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(LockedException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticateFailsIfCredentialsExpired() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("peter", "opal");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserPeterCredentialsExpired());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown CredentialsExpiredException");
|
||||
}
|
||||
catch (CredentialsExpiredException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(CredentialsExpiredException.class)
|
||||
.isThrownBy(() -> provider.authenticate(new UsernamePasswordAuthenticationToken("peter", "opal")));
|
||||
// Check that wrong password causes BadCredentialsException, rather than
|
||||
// CredentialsExpiredException
|
||||
token = new UsernamePasswordAuthenticationToken("peter", "wrong_password");
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown BadCredentialsException");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(
|
||||
() -> provider.authenticate(new UsernamePasswordAuthenticationToken("peter", "wrong_password")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -157,12 +129,7 @@ public class DaoAuthenticationProviderTests {
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserPeter());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown DisabledException");
|
||||
}
|
||||
catch (DisabledException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(DisabledException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -171,12 +138,8 @@ public class DaoAuthenticationProviderTests {
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceSimulateBackendError());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown InternalAuthenticationServiceException");
|
||||
}
|
||||
catch (InternalAuthenticationServiceException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(InternalAuthenticationServiceException.class)
|
||||
.isThrownBy(() -> provider.authenticate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -185,12 +148,7 @@ public class DaoAuthenticationProviderTests {
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown BadCredentialsException");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -199,12 +157,7 @@ public class DaoAuthenticationProviderTests {
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown BadCredentialsException");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -215,12 +168,7 @@ public class DaoAuthenticationProviderTests {
|
||||
// UsernameNotFoundExceptions
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown UsernameNotFoundException");
|
||||
}
|
||||
catch (UsernameNotFoundException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(UsernameNotFoundException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -230,12 +178,7 @@ public class DaoAuthenticationProviderTests {
|
||||
assertThat(provider.isHideUserNotFoundExceptions()).isTrue();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown BadCredentialsException");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -245,19 +188,9 @@ public class DaoAuthenticationProviderTests {
|
||||
assertThat(provider.isHideUserNotFoundExceptions()).isTrue();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown BadCredentialsException");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
provider.setPasswordEncoder(PasswordEncoderFactories.createDelegatingPasswordEncoder());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown BadCredentialsException");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -266,12 +199,7 @@ public class DaoAuthenticationProviderTests {
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown BadCredentialsException");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -389,14 +317,8 @@ public class DaoAuthenticationProviderTests {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("rod", "koala");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceReturnsNull());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Should have thrown AuthenticationServiceException");
|
||||
}
|
||||
catch (AuthenticationServiceException expected) {
|
||||
assertThat("UserDetailsService returned null, which is an interface contract violation")
|
||||
.isEqualTo(expected.getMessage());
|
||||
}
|
||||
assertThatExceptionOfType(AuthenticationServiceException.class).isThrownBy(() -> provider.authenticate(token))
|
||||
.withMessage("UserDetailsService returned null, which is an interface contract violation");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -436,12 +358,7 @@ public class DaoAuthenticationProviderTests {
|
||||
@Test
|
||||
public void testStartupFailsIfNoAuthenticationDao() throws Exception {
|
||||
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
||||
try {
|
||||
provider.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(provider::afterPropertiesSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -450,12 +367,7 @@ public class DaoAuthenticationProviderTests {
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
assertThat(provider.getUserCache().getClass()).isEqualTo(NullUserCache.class);
|
||||
provider.setUserCache(null);
|
||||
try {
|
||||
provider.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(provider::afterPropertiesSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -486,12 +398,7 @@ public class DaoAuthenticationProviderTests {
|
||||
provider.setPasswordEncoder(encoder);
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.afterPropertiesSet();
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Expected Exception");
|
||||
}
|
||||
catch (UsernameNotFoundException success) {
|
||||
}
|
||||
assertThatExceptionOfType(UsernameNotFoundException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
// ensure encoder invoked w/ non-null strings since PasswordEncoder impls may fail
|
||||
// if encoded password is null
|
||||
verify(encoder).matches(isA(String.class), isA(String.class));
|
||||
@@ -507,12 +414,7 @@ public class DaoAuthenticationProviderTests {
|
||||
MockUserDetailsServiceUserRod userDetailsService = new MockUserDetailsServiceUserRod();
|
||||
userDetailsService.password = encoder.encode((CharSequence) token.getCredentials());
|
||||
provider.setUserDetailsService(userDetailsService);
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Expected Exception");
|
||||
}
|
||||
catch (UsernameNotFoundException success) {
|
||||
}
|
||||
assertThatExceptionOfType(UsernameNotFoundException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -521,12 +423,7 @@ public class DaoAuthenticationProviderTests {
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setHideUserNotFoundExceptions(false);
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Expected Exception");
|
||||
}
|
||||
catch (UsernameNotFoundException success) {
|
||||
}
|
||||
assertThatExceptionOfType(UsernameNotFoundException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -554,12 +451,8 @@ public class DaoAuthenticationProviderTests {
|
||||
List<Long> userNotFoundTimes = new ArrayList<>(sampleSize);
|
||||
for (int i = 0; i < sampleSize; i++) {
|
||||
long start = System.currentTimeMillis();
|
||||
try {
|
||||
provider.authenticate(notFoundUser);
|
||||
fail("Expected Exception");
|
||||
}
|
||||
catch (UsernameNotFoundException success) {
|
||||
}
|
||||
assertThatExceptionOfType(UsernameNotFoundException.class)
|
||||
.isThrownBy(() -> provider.authenticate(notFoundUser));
|
||||
userNotFoundTimes.add(System.currentTimeMillis() - start);
|
||||
}
|
||||
double userFoundAvg = avg(userFoundTimes);
|
||||
@@ -584,12 +477,7 @@ public class DaoAuthenticationProviderTests {
|
||||
provider.setHideUserNotFoundExceptions(false);
|
||||
provider.setPasswordEncoder(encoder);
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
try {
|
||||
provider.authenticate(token);
|
||||
fail("Expected Exception");
|
||||
}
|
||||
catch (UsernameNotFoundException success) {
|
||||
}
|
||||
assertThatExceptionOfType(UsernameNotFoundException.class).isThrownBy(() -> provider.authenticate(token));
|
||||
verify(encoder, times(0)).matches(anyString(), anyString());
|
||||
}
|
||||
|
||||
|
||||
+4
-13
@@ -24,7 +24,7 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests {@link AbstractAuthenticationEvent} and its subclasses.
|
||||
@@ -59,22 +59,13 @@ public class AuthenticationEventTests {
|
||||
@Test
|
||||
public void testRejectsNullAuthentication() {
|
||||
AuthenticationException exception = new DisabledException("TEST");
|
||||
try {
|
||||
new AuthenticationFailureDisabledEvent(null, exception);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new AuthenticationFailureDisabledEvent(null, exception));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRejectsNullAuthenticationException() {
|
||||
try {
|
||||
new AuthenticationFailureDisabledEvent(getAuthentication(), null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new AuthenticationFailureDisabledEvent(getAuthentication(), null));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-13
@@ -44,7 +44,7 @@ import org.springframework.security.core.session.SessionDestroyedEvent;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isA;
|
||||
@@ -112,23 +112,15 @@ public class DefaultJaasAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void authenticateBadPassword() {
|
||||
try {
|
||||
this.provider.authenticate(new UsernamePasswordAuthenticationToken("user", "asdf"));
|
||||
fail("LoginException should have been thrown for the bad password");
|
||||
}
|
||||
catch (AuthenticationException success) {
|
||||
}
|
||||
assertThatExceptionOfType(AuthenticationException.class)
|
||||
.isThrownBy(() -> this.provider.authenticate(new UsernamePasswordAuthenticationToken("user", "asdf")));
|
||||
verifyFailedLogin();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticateBadUser() {
|
||||
try {
|
||||
this.provider.authenticate(new UsernamePasswordAuthenticationToken("asdf", "password"));
|
||||
fail("LoginException should have been thrown for the bad user");
|
||||
}
|
||||
catch (AuthenticationException success) {
|
||||
}
|
||||
assertThatExceptionOfType(AuthenticationException.class).isThrownBy(
|
||||
() -> this.provider.authenticate(new UsernamePasswordAuthenticationToken("asdf", "password")));
|
||||
verifyFailedLogin();
|
||||
}
|
||||
|
||||
|
||||
+12
-33
@@ -46,6 +46,8 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.session.SessionDestroyedEvent;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -73,12 +75,8 @@ public class JaasAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testBadPassword() {
|
||||
try {
|
||||
this.jaasProvider.authenticate(new UsernamePasswordAuthenticationToken("user", "asdf"));
|
||||
fail("LoginException should have been thrown for the bad password");
|
||||
}
|
||||
catch (AuthenticationException ex) {
|
||||
}
|
||||
assertThatExceptionOfType(AuthenticationException.class).isThrownBy(
|
||||
() -> this.jaasProvider.authenticate(new UsernamePasswordAuthenticationToken("user", "asdf")));
|
||||
assertThat(this.eventCheck.failedEvent).as("Failure event not fired").isNotNull();
|
||||
assertThat(this.eventCheck.failedEvent.getException()).withFailMessage("Failure event exception was null")
|
||||
.isNotNull();
|
||||
@@ -87,12 +85,8 @@ public class JaasAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testBadUser() {
|
||||
try {
|
||||
this.jaasProvider.authenticate(new UsernamePasswordAuthenticationToken("asdf", "password"));
|
||||
fail("LoginException should have been thrown for the bad user");
|
||||
}
|
||||
catch (AuthenticationException ex) {
|
||||
}
|
||||
assertThatExceptionOfType(AuthenticationException.class).isThrownBy(
|
||||
() -> this.jaasProvider.authenticate(new UsernamePasswordAuthenticationToken("asdf", "password")));
|
||||
assertThat(this.eventCheck.failedEvent).as("Failure event not fired").isNotNull();
|
||||
assertThat(this.eventCheck.failedEvent.getException()).withFailMessage("Failure event exception was null")
|
||||
.isNotNull();
|
||||
@@ -115,13 +109,8 @@ public class JaasAuthenticationProviderTests {
|
||||
myJaasProvider.setAuthorityGranters(this.jaasProvider.getAuthorityGranters());
|
||||
myJaasProvider.setCallbackHandlers(this.jaasProvider.getCallbackHandlers());
|
||||
myJaasProvider.setLoginContextName(this.jaasProvider.getLoginContextName());
|
||||
try {
|
||||
myJaasProvider.afterPropertiesSet();
|
||||
fail("Should have thrown ApplicationContextException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
assertThat(expected.getMessage().startsWith("loginConfig must be set on")).isTrue();
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> myJaasProvider.afterPropertiesSet())
|
||||
.withMessageStartingWith("loginConfig must be set on");
|
||||
}
|
||||
|
||||
// SEC-1239
|
||||
@@ -160,21 +149,11 @@ public class JaasAuthenticationProviderTests {
|
||||
myJaasProvider.setCallbackHandlers(this.jaasProvider.getCallbackHandlers());
|
||||
myJaasProvider.setLoginConfig(this.jaasProvider.getLoginConfig());
|
||||
myJaasProvider.setLoginContextName(null);
|
||||
try {
|
||||
myJaasProvider.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
assertThat(expected.getMessage()).startsWith("loginContextName must be set on");
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(myJaasProvider::afterPropertiesSet)
|
||||
.withMessageStartingWith("loginContextName must be set on");
|
||||
myJaasProvider.setLoginContextName("");
|
||||
try {
|
||||
myJaasProvider.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
assertThat(expected.getMessage().startsWith("loginContextName must be set on"));
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(myJaasProvider::afterPropertiesSet)
|
||||
.withMessageStartingWith("loginContextName must be set on");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+4
-14
@@ -31,7 +31,7 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests SecurityContextLoginModule
|
||||
@@ -71,12 +71,7 @@ public class SecurityContextLoginModuleTests {
|
||||
|
||||
@Test
|
||||
public void testLoginException() {
|
||||
try {
|
||||
this.module.login();
|
||||
fail("LoginException expected, there is no Authentication in the SecurityContext");
|
||||
}
|
||||
catch (LoginException ex) {
|
||||
}
|
||||
assertThatExceptionOfType(LoginException.class).isThrownBy(this.module::login);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -101,13 +96,8 @@ public class SecurityContextLoginModuleTests {
|
||||
|
||||
@Test
|
||||
public void testNullAuthenticationInSecurityContext() {
|
||||
try {
|
||||
SecurityContextHolder.getContext().setAuthentication(null);
|
||||
this.module.login();
|
||||
fail("LoginException expected, the authentication is null in the SecurityContext");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
}
|
||||
SecurityContextHolder.getContext().setAuthentication(null);
|
||||
assertThatExceptionOfType(Exception.class).isThrownBy(this.module::login);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+2
-7
@@ -23,7 +23,7 @@ import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -47,12 +47,7 @@ public class RemoteAuthenticationManagerImplTests {
|
||||
@Test
|
||||
public void testStartupChecksAuthenticationManagerSet() throws Exception {
|
||||
RemoteAuthenticationManagerImpl manager = new RemoteAuthenticationManagerImpl();
|
||||
try {
|
||||
manager.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(manager::afterPropertiesSet);
|
||||
manager.setAuthenticationManager(mock(AuthenticationManager.class));
|
||||
manager.afterPropertiesSet();
|
||||
}
|
||||
|
||||
+7
-19
@@ -26,7 +26,8 @@ import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests {@link RemoteAuthenticationProvider}.
|
||||
@@ -39,12 +40,8 @@ public class RemoteAuthenticationProviderTests {
|
||||
public void testExceptionsGetPassedBackToCaller() {
|
||||
RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
|
||||
provider.setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(false));
|
||||
try {
|
||||
provider.authenticate(new UsernamePasswordAuthenticationToken("rod", "password"));
|
||||
fail("Should have thrown RemoteAuthenticationException");
|
||||
}
|
||||
catch (RemoteAuthenticationException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(RemoteAuthenticationException.class)
|
||||
.isThrownBy(() -> provider.authenticate(new UsernamePasswordAuthenticationToken("rod", "password")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -57,12 +54,7 @@ public class RemoteAuthenticationProviderTests {
|
||||
@Test
|
||||
public void testStartupChecksAuthenticationManagerSet() throws Exception {
|
||||
RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
|
||||
try {
|
||||
provider.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(provider::afterPropertiesSet);
|
||||
provider.setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(true));
|
||||
provider.afterPropertiesSet();
|
||||
}
|
||||
@@ -81,12 +73,8 @@ public class RemoteAuthenticationProviderTests {
|
||||
public void testNullCredentialsDoesNotCauseNullPointerException() {
|
||||
RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
|
||||
provider.setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(false));
|
||||
try {
|
||||
provider.authenticate(new UsernamePasswordAuthenticationToken("rod", null));
|
||||
fail("Expected Exception");
|
||||
}
|
||||
catch (RemoteAuthenticationException success) {
|
||||
}
|
||||
assertThatExceptionOfType(RemoteAuthenticationException.class)
|
||||
.isThrownBy(() -> provider.authenticate(new UsernamePasswordAuthenticationToken("rod", null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+4
-13
@@ -26,7 +26,8 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests {@link RememberMeAuthenticationProvider}.
|
||||
@@ -40,22 +41,12 @@ public class RememberMeAuthenticationProviderTests {
|
||||
RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider("qwerty");
|
||||
RememberMeAuthenticationToken token = new RememberMeAuthenticationToken("WRONG_KEY", "Test",
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
|
||||
try {
|
||||
aap.authenticate(token);
|
||||
fail("Should have thrown BadCredentialsException");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> aap.authenticate(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetectsMissingKey() {
|
||||
try {
|
||||
new RememberMeAuthenticationProvider(null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new RememberMeAuthenticationProvider(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+7
-22
@@ -16,7 +16,7 @@
|
||||
|
||||
package org.springframework.security.authentication.rememberme;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -27,7 +27,7 @@ import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests {@link RememberMeAuthenticationToken}.
|
||||
@@ -40,26 +40,11 @@ public class RememberMeAuthenticationTokenTests {
|
||||
|
||||
@Test
|
||||
public void testConstructorRejectsNulls() {
|
||||
try {
|
||||
new RememberMeAuthenticationToken(null, "Test", ROLES_12);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
try {
|
||||
new RememberMeAuthenticationToken("key", null, ROLES_12);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
try {
|
||||
List<GrantedAuthority> authsContainingNull = new ArrayList<>();
|
||||
authsContainingNull.add(null);
|
||||
new RememberMeAuthenticationToken("key", "Test", authsContainingNull);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new RememberMeAuthenticationToken(null, "Test", ROLES_12));
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new RememberMeAuthenticationToken("key", null, ROLES_12));
|
||||
assertThatIllegalArgumentException().isThrownBy(
|
||||
() -> new RememberMeAuthenticationToken("key", "Test", Arrays.asList((GrantedAuthority) null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+4
-16
@@ -26,7 +26,8 @@ import org.junit.Test;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.assertThatNoException;
|
||||
|
||||
/**
|
||||
* @author TSARDD
|
||||
@@ -39,26 +40,13 @@ public class SimpleRoles2GrantedAuthoritiesMapperTests {
|
||||
SimpleAttributes2GrantedAuthoritiesMapper mapper = new SimpleAttributes2GrantedAuthoritiesMapper();
|
||||
mapper.setConvertAttributeToLowerCase(true);
|
||||
mapper.setConvertAttributeToUpperCase(true);
|
||||
try {
|
||||
mapper.afterPropertiesSet();
|
||||
fail("Expected exception not thrown");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
catch (Exception unexpected) {
|
||||
fail("Unexpected exception: " + unexpected);
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(mapper::afterPropertiesSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testAfterPropertiesSet() {
|
||||
SimpleAttributes2GrantedAuthoritiesMapper mapper = new SimpleAttributes2GrantedAuthoritiesMapper();
|
||||
try {
|
||||
mapper.afterPropertiesSet();
|
||||
}
|
||||
catch (Exception unexpected) {
|
||||
fail("Unexpected exception: " + unexpected);
|
||||
}
|
||||
assertThatNoException().isThrownBy(mapper::afterPropertiesSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+2
-7
@@ -22,7 +22,7 @@ import org.junit.Test;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests {@link SecurityContextHolder}.
|
||||
@@ -55,12 +55,7 @@ public class SecurityContextHolderTests {
|
||||
|
||||
@Test
|
||||
public void testRejectsNulls() {
|
||||
try {
|
||||
SecurityContextHolder.setContext(null);
|
||||
fail("Should have rejected null");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> SecurityContextHolder.setContext(null));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-10
@@ -22,7 +22,7 @@ import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* @author TSARDD
|
||||
@@ -34,15 +34,7 @@ public class UserDetailsByNameServiceWrapperTests {
|
||||
@Test
|
||||
public final void testAfterPropertiesSet() {
|
||||
UserDetailsByNameServiceWrapper svc = new UserDetailsByNameServiceWrapper();
|
||||
try {
|
||||
svc.afterPropertiesSet();
|
||||
fail("AfterPropertiesSet didn't throw expected exception");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
catch (Exception unexpected) {
|
||||
fail("AfterPropertiesSet throws unexpected exception");
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(svc::afterPropertiesSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -30,7 +30,8 @@ import org.springframework.security.core.authority.AuthorityUtils;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests {@link User}.
|
||||
@@ -63,50 +64,25 @@ public class UserTests {
|
||||
|
||||
@Test
|
||||
public void testNoArgConstructorDoesntExist() {
|
||||
Class<User> clazz = User.class;
|
||||
try {
|
||||
clazz.getDeclaredConstructor((Class[]) null);
|
||||
fail("Should have thrown NoSuchMethodException");
|
||||
}
|
||||
catch (NoSuchMethodException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(NoSuchMethodException.class)
|
||||
.isThrownBy(() -> User.class.getDeclaredConstructor((Class[]) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullValuesRejected() {
|
||||
try {
|
||||
new User(null, "koala", true, true, true, true, ROLE_12);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
try {
|
||||
new User("rod", null, true, true, true, true, ROLE_12);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
try {
|
||||
List<GrantedAuthority> auths = AuthorityUtils.createAuthorityList("ROLE_ONE");
|
||||
auths.add(null);
|
||||
new User("rod", "koala", true, true, true, true, auths);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new User(null, "koala", true, true, true, true, ROLE_12));
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new User("rod", null, true, true, true, true, ROLE_12));
|
||||
List<GrantedAuthority> auths = AuthorityUtils.createAuthorityList("ROLE_ONE");
|
||||
auths.add(null);
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new User("rod", "koala", true, true, true, true, auths));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullWithinGrantedAuthorityElementIsRejected() {
|
||||
try {
|
||||
List<GrantedAuthority> auths = AuthorityUtils.createAuthorityList("ROLE_ONE");
|
||||
auths.add(null);
|
||||
auths.add(new SimpleGrantedAuthority("ROLE_THREE"));
|
||||
new User(null, "koala", true, true, true, true, auths);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
List<GrantedAuthority> auths = AuthorityUtils.createAuthorityList("ROLE_ONE");
|
||||
auths.add(null);
|
||||
auths.add(new SimpleGrantedAuthority("ROLE_THREE"));
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new User(null, "koala", true, true, true, true, auths));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+8
-24
@@ -25,7 +25,8 @@ import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -92,23 +93,14 @@ public class JdbcDaoImplTests {
|
||||
@Test
|
||||
public void testLookupFailsIfUserHasNoGrantedAuthorities() throws Exception {
|
||||
JdbcDaoImpl dao = makePopulatedJdbcDao();
|
||||
try {
|
||||
dao.loadUserByUsername("cooper");
|
||||
fail("Should have thrown UsernameNotFoundException");
|
||||
}
|
||||
catch (UsernameNotFoundException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(UsernameNotFoundException.class).isThrownBy(() -> dao.loadUserByUsername("cooper"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLookupFailsWithWrongUsername() throws Exception {
|
||||
JdbcDaoImpl dao = makePopulatedJdbcDao();
|
||||
try {
|
||||
dao.loadUserByUsername("UNKNOWN_USER");
|
||||
fail("Should have thrown UsernameNotFoundException");
|
||||
}
|
||||
catch (UsernameNotFoundException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(UsernameNotFoundException.class)
|
||||
.isThrownBy(() -> dao.loadUserByUsername("UNKNOWN_USER"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -152,24 +144,16 @@ public class JdbcDaoImplTests {
|
||||
@Test
|
||||
public void testStartupFailsIfDataSourceNotSet() {
|
||||
JdbcDaoImpl dao = new JdbcDaoImpl();
|
||||
try {
|
||||
dao.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
assertThatIllegalArgumentException().isThrownBy(dao::afterPropertiesSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartupFailsIfUserMapSetToNull() {
|
||||
JdbcDaoImpl dao = new JdbcDaoImpl();
|
||||
try {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> {
|
||||
dao.setDataSource(null);
|
||||
dao.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
|
||||
+3
-7
@@ -44,7 +44,7 @@ import org.springframework.security.core.userdetails.UserCache;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -229,12 +229,8 @@ public class JdbcUserDetailsManagerTests {
|
||||
AuthenticationManager am = mock(AuthenticationManager.class);
|
||||
given(am.authenticate(any(Authentication.class))).willThrow(new BadCredentialsException(""));
|
||||
this.manager.setAuthenticationManager(am);
|
||||
try {
|
||||
this.manager.changePassword("password", "newPassword");
|
||||
fail("Expected BadCredentialsException");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
assertThatExceptionOfType(BadCredentialsException.class)
|
||||
.isThrownBy(() -> this.manager.changePassword("password", "newPassword"));
|
||||
// Check password hasn't changed.
|
||||
UserDetails newJoe = this.manager.loadUserByUsername("joe");
|
||||
assertThat(newJoe.getPassword()).isEqualTo("password");
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.security.util;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
/**
|
||||
* @author Luke Taylor
|
||||
@@ -32,11 +33,7 @@ public class FieldUtilsTests {
|
||||
assertThat(FieldUtils.getFieldValue(tc, "nested.protectedField")).isEqualTo("z");
|
||||
FieldUtils.setProtectedFieldValue("protectedField", tc, "y");
|
||||
assertThat(FieldUtils.getProtectedFieldValue("protectedField", tc)).isEqualTo("y");
|
||||
try {
|
||||
FieldUtils.getProtectedFieldValue("nonExistentField", tc);
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
}
|
||||
assertThatIllegalStateException().isThrownBy(() -> FieldUtils.getProtectedFieldValue("nonExistentField", tc));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
|
||||
Reference in New Issue
Block a user