Replace expected @Test attributes with AssertJ

Replace JUnit expected @Test attributes with AssertJ calls.
This commit is contained in:
Phillip Webb
2020-09-10 21:33:16 -07:00
committed by Josh Cummings
parent 20baa7d409
commit c502312719
243 changed files with 2115 additions and 1591 deletions

View File

@@ -30,6 +30,7 @@ import org.springframework.security.ldap.authentication.SpringSecurityAuthentica
import org.springframework.security.ldap.userdetails.LdapUserDetailsImpl;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* @author Luke Taylor
@@ -57,11 +58,11 @@ public class SpringSecurityAuthenticationSourceTests {
assertThat(source.getPrincipal()).isEqualTo("");
}
@Test(expected = IllegalArgumentException.class)
@Test
public void getPrincipalRejectsNonLdapUserDetailsObject() {
AuthenticationSource source = new SpringSecurityAuthenticationSource();
SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(new Object(), "password"));
source.getPrincipal();
assertThatIllegalArgumentException().isThrownBy(source::getPrincipal);
}
@Test

View File

@@ -73,23 +73,23 @@ public class LdapAuthenticationProviderTests {
() -> ldapProvider.authenticate(new UsernamePasswordAuthenticationToken("", "bobspassword")));
}
@Test(expected = BadCredentialsException.class)
@Test
public void usernameNotFoundExceptionIsHiddenByDefault() {
final LdapAuthenticator authenticator = mock(LdapAuthenticator.class);
final UsernamePasswordAuthenticationToken joe = new UsernamePasswordAuthenticationToken("joe", "password");
given(authenticator.authenticate(joe)).willThrow(new UsernameNotFoundException("nobody"));
LdapAuthenticationProvider provider = new LdapAuthenticationProvider(authenticator);
provider.authenticate(joe);
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> provider.authenticate(joe));
}
@Test(expected = UsernameNotFoundException.class)
@Test
public void usernameNotFoundExceptionIsNotHiddenIfConfigured() {
final LdapAuthenticator authenticator = mock(LdapAuthenticator.class);
final UsernamePasswordAuthenticationToken joe = new UsernamePasswordAuthenticationToken("joe", "password");
given(authenticator.authenticate(joe)).willThrow(new UsernameNotFoundException("nobody"));
LdapAuthenticationProvider provider = new LdapAuthenticationProvider(authenticator);
provider.setHideUserNotFoundExceptions(false);
provider.authenticate(joe);
assertThatExceptionOfType(UsernameNotFoundException.class).isThrownBy(() -> provider.authenticate(joe));
}
@Test

View File

@@ -49,6 +49,7 @@ import org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAu
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.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
@@ -140,14 +141,14 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
assertThat(result.isAuthenticated()).isTrue();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void setSearchFilterNull() {
this.provider.setSearchFilter(null);
assertThatIllegalArgumentException().isThrownBy(() -> this.provider.setSearchFilter(null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void setSearchFilterEmpty() {
this.provider.setSearchFilter(" ");
assertThatIllegalArgumentException().isThrownBy(() -> this.provider.setSearchFilter(" "));
}
@Test
@@ -164,35 +165,36 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
this.provider.authenticate(new UsernamePasswordAuthenticationToken("joe@mydomain.eu", "password"));
}
@Test(expected = BadCredentialsException.class)
@Test
public void failedUserSearchCausesBadCredentials() throws Exception {
DirContext ctx = mock(DirContext.class);
given(ctx.getNameInNamespace()).willReturn("");
given(ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class)))
.willThrow(new NameNotFoundException());
this.provider.contextFactory = createContextFactoryReturning(ctx);
this.provider.authenticate(this.joe);
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.provider.authenticate(this.joe));
}
// SEC-2017
@Test(expected = BadCredentialsException.class)
@Test
public void noUserSearchCausesUsernameNotFound() throws Exception {
DirContext ctx = mock(DirContext.class);
given(ctx.getNameInNamespace()).willReturn("");
given(ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class)))
.willReturn(new EmptyEnumeration<>());
this.provider.contextFactory = createContextFactoryReturning(ctx);
this.provider.authenticate(this.joe);
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.provider.authenticate(this.joe));
}
// SEC-2500
@Test(expected = BadCredentialsException.class)
@Test
public void sec2500PreventAnonymousBind() {
this.provider.authenticate(new UsernamePasswordAuthenticationToken("rwinch", ""));
assertThatExceptionOfType(BadCredentialsException.class)
.isThrownBy(() -> this.provider.authenticate(new UsernamePasswordAuthenticationToken("rwinch", "")));
}
@Test
@SuppressWarnings("unchecked")
@Test(expected = IncorrectResultSizeDataAccessException.class)
public void duplicateUserSearchCausesError() throws Exception {
DirContext ctx = mock(DirContext.class);
given(ctx.getNameInNamespace()).willReturn("");
@@ -204,30 +206,31 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
given(ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class)))
.willReturn(searchResults);
this.provider.contextFactory = createContextFactoryReturning(ctx);
this.provider.authenticate(this.joe);
assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class)
.isThrownBy(() -> this.provider.authenticate(this.joe));
}
static final String msg = "[LDAP: error code 49 - 80858585: LdapErr: DSID-DECAFF0, comment: AcceptSecurityContext error, data ";
@Test(expected = BadCredentialsException.class)
@Test
public void userNotFoundIsCorrectlyMapped() {
this.provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "525, xxxx]"));
this.provider.setConvertSubErrorCodesToExceptions(true);
this.provider.authenticate(this.joe);
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.provider.authenticate(this.joe));
}
@Test(expected = BadCredentialsException.class)
@Test
public void incorrectPasswordIsCorrectlyMapped() {
this.provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "52e, xxxx]"));
this.provider.setConvertSubErrorCodesToExceptions(true);
this.provider.authenticate(this.joe);
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.provider.authenticate(this.joe));
}
@Test(expected = BadCredentialsException.class)
@Test
public void notPermittedIsCorrectlyMapped() {
this.provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "530, xxxx]"));
this.provider.setConvertSubErrorCodesToExceptions(true);
this.provider.authenticate(this.joe);
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.provider.authenticate(this.joe));
}
@Test
@@ -242,47 +245,48 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
.isEqualTo(dataCode));
}
@Test(expected = CredentialsExpiredException.class)
@Test
public void expiredPasswordIsCorrectlyMapped() {
this.provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "532, xxxx]"));
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.provider.authenticate(this.joe));
this.provider.setConvertSubErrorCodesToExceptions(true);
this.provider.authenticate(this.joe);
assertThatExceptionOfType(CredentialsExpiredException.class)
.isThrownBy(() -> this.provider.authenticate(this.joe));
}
@Test(expected = DisabledException.class)
@Test
public void accountDisabledIsCorrectlyMapped() {
this.provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "533, xxxx]"));
this.provider.setConvertSubErrorCodesToExceptions(true);
this.provider.authenticate(this.joe);
assertThatExceptionOfType(DisabledException.class).isThrownBy(() -> this.provider.authenticate(this.joe));
}
@Test(expected = AccountExpiredException.class)
@Test
public void accountExpiredIsCorrectlyMapped() {
this.provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "701, xxxx]"));
this.provider.setConvertSubErrorCodesToExceptions(true);
this.provider.authenticate(this.joe);
assertThatExceptionOfType(AccountExpiredException.class).isThrownBy(() -> this.provider.authenticate(this.joe));
}
@Test(expected = LockedException.class)
@Test
public void accountLockedIsCorrectlyMapped() {
this.provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "775, xxxx]"));
this.provider.setConvertSubErrorCodesToExceptions(true);
this.provider.authenticate(this.joe);
assertThatExceptionOfType(LockedException.class).isThrownBy(() -> this.provider.authenticate(this.joe));
}
@Test(expected = BadCredentialsException.class)
@Test
public void unknownErrorCodeIsCorrectlyMapped() {
this.provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "999, xxxx]"));
this.provider.setConvertSubErrorCodesToExceptions(true);
this.provider.authenticate(this.joe);
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.provider.authenticate(this.joe));
}
@Test(expected = BadCredentialsException.class)
@Test
public void errorWithNoSubcodeIsHandledCleanly() {
this.provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg));
this.provider.setConvertSubErrorCodesToExceptions(true);
this.provider.authenticate(this.joe);
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.provider.authenticate(this.joe));
}
@Test
@@ -293,13 +297,15 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
}).withCauseInstanceOf(org.springframework.ldap.CommunicationException.class);
}
@Test(expected = org.springframework.security.authentication.InternalAuthenticationServiceException.class)
@Test
public void connectionExceptionIsWrappedInInternalException() throws Exception {
ActiveDirectoryLdapAuthenticationProvider noneReachableProvider = new ActiveDirectoryLdapAuthenticationProvider(
"mydomain.eu", NON_EXISTING_LDAP_PROVIDER, "dc=ad,dc=eu,dc=mydomain");
noneReachableProvider
.setContextEnvironmentProperties(Collections.singletonMap("com.sun.jndi.ldap.connect.timeout", "5"));
noneReachableProvider.doAuthentication(this.joe);
assertThatExceptionOfType(
org.springframework.security.authentication.InternalAuthenticationServiceException.class)
.isThrownBy(() -> noneReachableProvider.doAuthentication(this.joe));
}
@Test
@@ -309,14 +315,15 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
checkAuthentication("dc=ad,dc=eu,dc=mydomain", provider);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void setContextEnvironmentPropertiesNull() {
this.provider.setContextEnvironmentProperties(null);
assertThatIllegalArgumentException().isThrownBy(() -> this.provider.setContextEnvironmentProperties(null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void setContextEnvironmentPropertiesEmpty() {
this.provider.setContextEnvironmentProperties(new Hashtable<>());
assertThatIllegalArgumentException()
.isThrownBy(() -> this.provider.setContextEnvironmentProperties(new Hashtable<>()));
}
@Test

View File

@@ -30,6 +30,7 @@ import org.junit.Test;
import org.springframework.ldap.UncategorizedLdapException;
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.BDDMockito.given;
import static org.mockito.BDDMockito.willThrow;
@@ -67,18 +68,20 @@ public class PasswordPolicyAwareContextSourceTests {
assertThat(this.ctxSource.getContext("user", "ignored")).isNotNull();
}
@Test(expected = UncategorizedLdapException.class)
@Test
public void standardExceptionIsPropagatedWhenExceptionRaisedAndNoControlsAreSet() throws Exception {
willThrow(new NamingException("some LDAP exception")).given(this.ctx).reconnect(any(Control[].class));
this.ctxSource.getContext("user", "ignored");
assertThatExceptionOfType(UncategorizedLdapException.class)
.isThrownBy(() -> this.ctxSource.getContext("user", "ignored"));
}
@Test(expected = PasswordPolicyException.class)
@Test
public void lockedPasswordPolicyControlRaisesPasswordPolicyException() throws Exception {
given(this.ctx.getResponseControls()).willReturn(new Control[] {
new PasswordPolicyResponseControl(PasswordPolicyResponseControlTests.OPENLDAP_LOCKED_CTRL) });
willThrow(new NamingException("locked message")).given(this.ctx).reconnect(any(Control[].class));
this.ctxSource.getContext("user", "ignored");
assertThatExceptionOfType(PasswordPolicyException.class)
.isThrownBy(() -> this.ctxSource.getContext("user", "ignored"));
}
}

View File

@@ -31,6 +31,7 @@ import org.springframework.security.ldap.authentication.MockUserSearch;
import org.springframework.security.ldap.authentication.NullLdapAuthoritiesPopulator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests for {@link LdapUserDetailsService}
@@ -39,14 +40,15 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class LdapUserDetailsServiceTests {
@Test(expected = IllegalArgumentException.class)
@Test
public void rejectsNullSearchObject() {
new LdapUserDetailsService(null, new NullLdapAuthoritiesPopulator());
assertThatIllegalArgumentException()
.isThrownBy(() -> new LdapUserDetailsService(null, new NullLdapAuthoritiesPopulator()));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void rejectsNullAuthoritiesPopulator() {
new LdapUserDetailsService(new MockUserSearch(), null);
assertThatIllegalArgumentException().isThrownBy(() -> new LdapUserDetailsService(new MockUserSearch(), null));
}
@Test