Convert to assertj
Fixes gh-3175
This commit is contained in:
@@ -48,7 +48,7 @@ public class LdapUtilsTests {
|
||||
|
||||
when(mockCtx.getNameInNamespace()).thenReturn("dc=springframework,dc=org");
|
||||
|
||||
assertThat(dc=org").as("").isCloseTo(LdapUtils.getRelativeName("dc=springframework, within(mockCtx)));
|
||||
assertThat(LdapUtils.getRelativeName("dc=springframework,dc=org",mockCtx)).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -56,8 +56,7 @@ public class LdapUtilsTests {
|
||||
final DirContext mockCtx = mock(DirContext.class);
|
||||
when(mockCtx.getNameInNamespace()).thenReturn("");
|
||||
|
||||
assertEquals("cn=jane,dc=springframework,dc=org",
|
||||
LdapUtils.getRelativeName("cn=jane,dc=springframework,dc=org", mockCtx));
|
||||
assertThat(LdapUtils.getRelativeName("cn=jane,dc=springframework,dc=org", mockCtx)).isEqualTo("cn=jane,dc=springframework,dc=org");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -65,8 +64,8 @@ public class LdapUtilsTests {
|
||||
final DirContext mockCtx = mock(DirContext.class);
|
||||
when(mockCtx.getNameInNamespace()).thenReturn("dc=springsecurity,dc = org");
|
||||
|
||||
assertEquals("cn=jane smith", LdapUtils.getRelativeName(
|
||||
"cn=jane smith, dc = springsecurity , dc=org", mockCtx));
|
||||
assertThat(LdapUtils.getRelativeName(
|
||||
"cn=jane smith, dc = springsecurity , dc=org", mockCtx)).isEqualTo("cn=jane smith");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -75,23 +74,19 @@ public class LdapUtilsTests {
|
||||
assertThat(LdapUtils.parseRootDnFromUrl("ldap://monkeymachine:11389")).isEqualTo("");
|
||||
assertThat(LdapUtils.parseRootDnFromUrl("ldap://monkeymachine/")).isEqualTo("");
|
||||
assertThat(LdapUtils.parseRootDnFromUrl("ldap://monkeymachine.co.uk/")).isEqualTo("");
|
||||
assertEquals(
|
||||
"dc=springframework,dc=org",
|
||||
assertThat(
|
||||
LdapUtils
|
||||
.parseRootDnFromUrl("ldaps://monkeymachine.co.uk/dc=springframework,dc=org"));
|
||||
assertEquals("dc=springframework,dc=org",
|
||||
LdapUtils.parseRootDnFromUrl("ldap:///dc=springframework,dc=org"));
|
||||
assertEquals(
|
||||
"dc=springframework,dc=org",
|
||||
.parseRootDnFromUrl("ldaps://monkeymachine.co.uk/dc=springframework,dc=org")).isEqualTo("dc=springframework,dc=org");
|
||||
assertThat(
|
||||
LdapUtils.parseRootDnFromUrl("ldap:///dc=springframework,dc=org")).isEqualTo("dc=springframework,dc=org");
|
||||
assertThat(
|
||||
LdapUtils
|
||||
.parseRootDnFromUrl("ldap://monkeymachine/dc=springframework,dc=org"));
|
||||
assertEquals(
|
||||
"dc=springframework,dc=org/ou=blah",
|
||||
.parseRootDnFromUrl("ldap://monkeymachine/dc=springframework,dc=org")).isEqualTo("dc=springframework,dc=org");
|
||||
assertThat(
|
||||
LdapUtils
|
||||
.parseRootDnFromUrl("ldap://monkeymachine.co.uk/dc=springframework,dc=org/ou=blah"));
|
||||
assertEquals(
|
||||
"dc=springframework,dc=org/ou=blah",
|
||||
.parseRootDnFromUrl("ldap://monkeymachine.co.uk/dc=springframework,dc=org/ou=blah")).isEqualTo("dc=springframework,dc=org/ou=blah");
|
||||
assertThat(
|
||||
LdapUtils
|
||||
.parseRootDnFromUrl("ldap://monkeymachine.co.uk:389/dc=springframework,dc=org/ou=blah"));
|
||||
.parseRootDnFromUrl("ldap://monkeymachine.co.uk:389/dc=springframework,dc=org/ou=blah")).isEqualTo("dc=springframework,dc=org/ou=blah");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,9 +136,9 @@ public class LdapAuthenticationProviderTests {
|
||||
assertThat(user.getUsername()).isEqualTo("ben");
|
||||
assertThat(populator.getRequestedUsername()).isEqualTo("ben");
|
||||
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities()).isTrue().contains(
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities()).contains(
|
||||
"ROLE_FROM_ENTRY"));
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities()).isTrue().contains(
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities()).contains(
|
||||
"ROLE_FROM_POPULATOR"));
|
||||
}
|
||||
|
||||
@@ -167,7 +167,7 @@ public class LdapAuthenticationProviderTests {
|
||||
UserDetails user = (UserDetails) ldapProvider.authenticate(authRequest)
|
||||
.getPrincipal();
|
||||
assertThat(user.getAuthorities()).hasSize(1);
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities()).isTrue().contains(
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities()).contains(
|
||||
"ROLE_FROM_ENTRY"));
|
||||
}
|
||||
|
||||
|
||||
@@ -42,16 +42,16 @@ public class LdapShaPasswordEncoderTests {
|
||||
|
||||
@Test
|
||||
public void invalidPasswordFails() {
|
||||
assertFalse(sha.isPasswordValid("{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=",
|
||||
"wrongpassword", null));
|
||||
assertThat(sha.isPasswordValid("{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=",
|
||||
"wrongpassword", null)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidSaltedPasswordFails() {
|
||||
assertFalse(sha.isPasswordValid("{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgX",
|
||||
"wrongpassword", null));
|
||||
assertFalse(sha.isPasswordValid("{SSHA}PQy2j+6n5ytA+YlAKkM8Fh4p6u2JxfVd",
|
||||
"wrongpassword", null));
|
||||
assertThat(sha.isPasswordValid("{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgX",
|
||||
"wrongpassword", null)).isFalse();
|
||||
assertThat(sha.isPasswordValid("{SSHA}PQy2j+6n5ytA+YlAKkM8Fh4p6u2JxfVd",
|
||||
"wrongpassword", null)).isFalse();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@@ -65,15 +65,15 @@ public class LdapShaPasswordEncoderTests {
|
||||
@Test
|
||||
public void validPasswordSucceeds() {
|
||||
sha.setForceLowerCasePrefix(false);
|
||||
assertTrue(sha.isPasswordValid("{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=",
|
||||
"boabspasswurd", null));
|
||||
assertTrue(sha.isPasswordValid("{sha}ddSFGmjXYPbZC+NXR2kCzBRjqiE=",
|
||||
"boabspasswurd", null));
|
||||
assertThat(sha.isPasswordValid("{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=",
|
||||
"boabspasswurd", null)).isTrue();
|
||||
assertThat(sha.isPasswordValid("{sha}ddSFGmjXYPbZC+NXR2kCzBRjqiE=",
|
||||
"boabspasswurd", null)).isTrue();
|
||||
sha.setForceLowerCasePrefix(true);
|
||||
assertTrue(sha.isPasswordValid("{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=",
|
||||
"boabspasswurd", null));
|
||||
assertTrue(sha.isPasswordValid("{sha}ddSFGmjXYPbZC+NXR2kCzBRjqiE=",
|
||||
"boabspasswurd", null));
|
||||
assertThat(sha.isPasswordValid("{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=",
|
||||
"boabspasswurd", null)).isTrue();
|
||||
assertThat(sha.isPasswordValid("{sha}ddSFGmjXYPbZC+NXR2kCzBRjqiE=",
|
||||
"boabspasswurd", null)).isTrue();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,40 +82,40 @@ public class LdapShaPasswordEncoderTests {
|
||||
@Test
|
||||
public void validSaltedPasswordSucceeds() {
|
||||
sha.setForceLowerCasePrefix(false);
|
||||
assertTrue(sha.isPasswordValid("{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgX",
|
||||
"boabspasswurd", null));
|
||||
assertTrue(sha.isPasswordValid("{ssha}PQy2j+6n5ytA+YlAKkM8Fh4p6u2JxfVd",
|
||||
"boabspasswurd", null));
|
||||
assertThat(sha.isPasswordValid("{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgX",
|
||||
"boabspasswurd", null)).isTrue();
|
||||
assertThat(sha.isPasswordValid("{ssha}PQy2j+6n5ytA+YlAKkM8Fh4p6u2JxfVd",
|
||||
"boabspasswurd", null)).isTrue();
|
||||
sha.setForceLowerCasePrefix(true);
|
||||
assertTrue(sha.isPasswordValid("{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgX",
|
||||
"boabspasswurd", null));
|
||||
assertTrue(sha.isPasswordValid("{ssha}PQy2j+6n5ytA+YlAKkM8Fh4p6u2JxfVd",
|
||||
"boabspasswurd", null));
|
||||
assertThat(sha.isPasswordValid("{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgX",
|
||||
"boabspasswurd", null)).isTrue();
|
||||
assertThat(sha.isPasswordValid("{ssha}PQy2j+6n5ytA+YlAKkM8Fh4p6u2JxfVd",
|
||||
"boabspasswurd", null)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
// SEC-1031
|
||||
public void fullLengthOfHashIsUsedInComparison() throws Exception {
|
||||
// Change the first hash character from '2' to '3'
|
||||
assertFalse(sha.isPasswordValid("{SSHA}35ro4PKC8jhQZ26jVsozhX/xaP0suHgX",
|
||||
"boabspasswurd", null));
|
||||
assertThat(sha.isPasswordValid("{SSHA}35ro4PKC8jhQZ26jVsozhX/xaP0suHgX",
|
||||
"boabspasswurd", null)).isFalse();
|
||||
// Change the last hash character from 'X' to 'Y'
|
||||
assertFalse(sha.isPasswordValid("{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgY",
|
||||
"boabspasswurd", null));
|
||||
assertThat(sha.isPasswordValid("{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgY",
|
||||
"boabspasswurd", null)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void correctPrefixCaseIsUsed() {
|
||||
sha.setForceLowerCasePrefix(false);
|
||||
assertEquals("{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=",
|
||||
assertThat("{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=").isEqualTo(
|
||||
sha.encodePassword("boabspasswurd", null));
|
||||
assertThat(sha.encodePassword("somepassword", "salt".getBytes()).isTrue().startsWith(
|
||||
assertThat(sha.encodePassword("somepassword", "salt".getBytes()).startsWith(
|
||||
"{SSHA}"));
|
||||
|
||||
sha.setForceLowerCasePrefix(true);
|
||||
assertEquals("{sha}ddSFGmjXYPbZC+NXR2kCzBRjqiE=",
|
||||
assertThat("{sha}ddSFGmjXYPbZC+NXR2kCzBRjqiE=").isEqualTo(
|
||||
sha.encodePassword("boabspasswurd", null));
|
||||
assertThat(sha.encodePassword("somepassword", "salt".getBytes()).isTrue().startsWith(
|
||||
assertThat(sha.encodePassword("somepassword", "salt".getBytes()).startsWith(
|
||||
"{ssha}"));
|
||||
|
||||
}
|
||||
|
||||
@@ -46,8 +46,6 @@ import javax.naming.directory.SearchResult;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider.ContextFactory;
|
||||
@@ -327,7 +325,7 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
|
||||
try {
|
||||
provider.authenticate(joe);
|
||||
fail();
|
||||
fail("BadCredentialsException should had been thrown");
|
||||
}
|
||||
catch (BadCredentialsException expected) {
|
||||
}
|
||||
|
||||
@@ -1,26 +1,17 @@
|
||||
|
||||
package org.springframework.security.ldap.ppolicy;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.LockedException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.ldap.authentication.BindAuthenticator;
|
||||
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider;
|
||||
import org.springframework.security.ldap.userdetails.LdapUserDetailsImpl;
|
||||
|
||||
/**
|
||||
* Test cases which run against an OpenLDAP server.
|
||||
* <p>
|
||||
* Run the script in the module root to start the server and import the data before
|
||||
* running.
|
||||
*
|
||||
* @author Luke Taylor
|
||||
* @since 3.0
|
||||
*/
|
||||
public class OpenLDAPIntegrationTestSuite {
|
||||
|
||||
PasswordPolicyAwareContextSource cs;
|
||||
/*
|
||||
* @Before public void createContextSource() throws Exception { cs = new
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.springframework.security.ldap.ppolicy;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
@@ -32,7 +32,6 @@ public class PasswordPolicyControlFactoryTests {
|
||||
PasswordPolicyResponseControlTests.OPENLDAP_LOCKED_CTRL);
|
||||
Control result = ctrlFactory.getControlInstance(control);
|
||||
assertThat(result).isNotNull();
|
||||
assertTrue(Arrays.equals(PasswordPolicyResponseControlTests.OPENLDAP_LOCKED_CTRL,
|
||||
result.getEncodedValue()));
|
||||
assertThat(PasswordPolicyResponseControlTests.OPENLDAP_LOCKED_CTRL).isEqualTo(result.getEncodedValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* @author Filip Hanik
|
||||
@@ -33,10 +32,8 @@ public class LdapAuthorityTests {
|
||||
public void testGetDn() throws Exception {
|
||||
assertThat(authority.getDn()).isEqualTo(DN);
|
||||
assertThat(authority.getAttributeValues(SpringSecurityLdapTemplate.DN_KEY)).isNotNull();
|
||||
assertThat(authority.getAttributeValues(SpringSecurityLdapTemplate.DN_KEY).isEqualTo(1)
|
||||
.size());
|
||||
assertEquals(DN,
|
||||
authority.getFirstAttributeValue(SpringSecurityLdapTemplate.DN_KEY));
|
||||
assertThat(authority.getAttributeValues(SpringSecurityLdapTemplate.DN_KEY)).hasSize(1);
|
||||
assertThat(authority.getFirstAttributeValue(SpringSecurityLdapTemplate.DN_KEY)).isEqualTo(DN);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -15,11 +15,12 @@
|
||||
|
||||
package org.springframework.security.ldap.userdetails;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import javax.naming.directory.BasicAttribute;
|
||||
import javax.naming.directory.BasicAttributes;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.ldap.core.DirContextAdapter;
|
||||
import org.springframework.ldap.core.DistinguishedName;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
@@ -29,8 +30,9 @@ import org.springframework.security.core.authority.AuthorityUtils;
|
||||
*
|
||||
* @author Luke Taylor
|
||||
*/
|
||||
public class LdapUserDetailsMapperTests extends TestCase {
|
||||
public class LdapUserDetailsMapperTests {
|
||||
|
||||
@Test
|
||||
public void testMultipleRoleAttributeValuesAreMappedToAuthorities() throws Exception {
|
||||
LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
|
||||
mapper.setConvertToUpperCase(false);
|
||||
@@ -52,6 +54,7 @@ public class LdapUserDetailsMapperTests extends TestCase {
|
||||
/**
|
||||
* SEC-303. Non-retrieved role attribute causes NullPointerException
|
||||
*/
|
||||
@Test
|
||||
public void testNonRetrievedRoleAttributeIsIgnored() throws Exception {
|
||||
LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
|
||||
|
||||
@@ -60,18 +63,19 @@ public class LdapUserDetailsMapperTests extends TestCase {
|
||||
BasicAttributes attrs = new BasicAttributes();
|
||||
attrs.put(new BasicAttribute("userRole", "x"));
|
||||
|
||||
DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName(
|
||||
"cn=someName"));
|
||||
DirContextAdapter ctx = new DirContextAdapter(attrs,
|
||||
new DistinguishedName("cn=someName"));
|
||||
ctx.setAttributeValue("uid", "ani");
|
||||
|
||||
LdapUserDetailsImpl user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx,
|
||||
"ani", AuthorityUtils.NO_AUTHORITIES);
|
||||
|
||||
assertThat(user.getAuthorities()).hasSize(1);
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities()).isTrue().contains(
|
||||
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities()).contains(
|
||||
"ROLE_X"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPasswordAttributeIsMappedCorrectly() throws Exception {
|
||||
LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
|
||||
|
||||
@@ -79,12 +83,12 @@ public class LdapUserDetailsMapperTests extends TestCase {
|
||||
BasicAttributes attrs = new BasicAttributes();
|
||||
attrs.put(new BasicAttribute("myappsPassword", "mypassword".getBytes()));
|
||||
|
||||
DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName(
|
||||
"cn=someName"));
|
||||
DirContextAdapter ctx = new DirContextAdapter(attrs,
|
||||
new DistinguishedName("cn=someName"));
|
||||
ctx.setAttributeValue("uid", "ani");
|
||||
|
||||
LdapUserDetails user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx,
|
||||
"ani", AuthorityUtils.NO_AUTHORITIES);
|
||||
LdapUserDetails user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani",
|
||||
AuthorityUtils.NO_AUTHORITIES);
|
||||
|
||||
assertThat(user.getPassword()).isEqualTo("mypassword");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user