1
0
mirror of synced 2026-05-22 21:33:16 +00:00

Remove blank lines from all tests

Remove all blank lines from test code so that test methods are
visually grouped together. This generally helps to make the test
classes easer to scan, however, the "given" / "when" / "then"
blocks used by some tests are now not as easy to discern.

Issue gh-8945
This commit is contained in:
Phillip Webb
2020-08-01 19:33:21 -07:00
committed by Rob Winch
parent 5bdd757108
commit a5aa6b3d7f
787 changed files with 9 additions and 10241 deletions
@@ -37,16 +37,13 @@ public class LdapUtilsTests {
public void testCloseContextSwallowsNamingException() throws Exception {
final DirContext dirCtx = mock(DirContext.class);
willThrow(new NamingException()).given(dirCtx).close();
LdapUtils.closeContext(dirCtx);
}
@Test
public void testGetRelativeNameReturnsEmptyStringForDnEqualToBaseName() throws Exception {
final DirContext mockCtx = mock(DirContext.class);
given(mockCtx.getNameInNamespace()).willReturn("dc=springframework,dc=org");
assertThat(LdapUtils.getRelativeName("dc=springframework,dc=org", mockCtx)).isEqualTo("");
}
@@ -54,7 +51,6 @@ public class LdapUtilsTests {
public void testGetRelativeNameReturnsFullDnWithEmptyBaseName() throws Exception {
final DirContext mockCtx = mock(DirContext.class);
given(mockCtx.getNameInNamespace()).willReturn("");
assertThat(LdapUtils.getRelativeName("cn=jane,dc=springframework,dc=org", mockCtx))
.isEqualTo("cn=jane,dc=springframework,dc=org");
}
@@ -63,7 +59,6 @@ public class LdapUtilsTests {
public void testGetRelativeNameWorksWithArbitrarySpaces() throws Exception {
final DirContext mockCtx = mock(DirContext.class);
given(mockCtx.getNameInNamespace()).willReturn("dc=springsecurity,dc = org");
assertThat(LdapUtils.getRelativeName("cn=jane smith, dc = springsecurity , dc=org", mockCtx))
.isEqualTo("cn=jane smith");
}
@@ -52,7 +52,6 @@ public class SpringSecurityAuthenticationSourceTests {
@Test
public void principalIsEmptyForAnonymousUser() {
AuthenticationSource source = new SpringSecurityAuthenticationSource();
SecurityContextHolder.getContext().setAuthentication(
new AnonymousAuthenticationToken("key", "anonUser", AuthorityUtils.createAuthorityList("ignored")));
assertThat(source.getPrincipal()).isEqualTo("");
@@ -62,7 +61,6 @@ public class SpringSecurityAuthenticationSourceTests {
public void getPrincipalRejectsNonLdapUserDetailsObject() {
AuthenticationSource source = new SpringSecurityAuthenticationSource();
SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(new Object(), "password"));
source.getPrincipal();
}
@@ -70,7 +68,6 @@ public class SpringSecurityAuthenticationSourceTests {
public void expectedCredentialsAreReturned() {
AuthenticationSource source = new SpringSecurityAuthenticationSource();
SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(new Object(), "password"));
assertThat(source.getCredentials()).isEqualTo("password");
}
@@ -82,7 +79,6 @@ public class SpringSecurityAuthenticationSourceTests {
AuthenticationSource source = new SpringSecurityAuthenticationSource();
SecurityContextHolder.getContext()
.setAuthentication(new TestingAuthenticationToken(user.createUserDetails(), null));
assertThat(source.getPrincipal()).isEqualTo("uid=joe,ou=users");
}
@@ -60,16 +60,13 @@ public class SpringSecurityLdapTemplateTests {
String searchResultName = "ldap://example.com/dc=springframework,dc=org";
Object[] params = new Object[] {};
DirContextAdapter searchResultObject = mock(DirContextAdapter.class);
given(this.ctx.search(any(DistinguishedName.class), eq(filter), eq(params), this.searchControls.capture()))
.willReturn(this.resultsEnum);
given(this.resultsEnum.hasMore()).willReturn(true, false);
given(this.resultsEnum.next()).willReturn(this.searchResult);
given(this.searchResult.getObject()).willReturn(searchResultObject);
SpringSecurityLdapTemplate.searchForSingleEntryInternal(this.ctx, mock(SearchControls.class), base, filter,
params);
assertThat(this.searchControls.getValue().getReturningObjFlag()).isTrue();
}
@@ -53,7 +53,6 @@ public class LdapAuthenticationProviderTests {
public void testSupportsUsernamePasswordAuthenticationToken() {
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator(),
new MockAuthoritiesPopulator());
assertThat(ldapProvider.supports(UsernamePasswordAuthenticationToken.class)).isTrue();
}
@@ -61,7 +60,6 @@ public class LdapAuthenticationProviderTests {
public void testDefaultMapperIsSet() {
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator(),
new MockAuthoritiesPopulator());
assertThat(ldapProvider.getUserDetailsContextMapper() instanceof LdapUserDetailsMapper).isTrue();
}
@@ -69,14 +67,12 @@ public class LdapAuthenticationProviderTests {
public void testEmptyOrNullUserNameThrowsException() {
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator(),
new MockAuthoritiesPopulator());
try {
ldapProvider.authenticate(new UsernamePasswordAuthenticationToken(null, "password"));
fail("Expected BadCredentialsException for empty username");
}
catch (BadCredentialsException expected) {
}
try {
ldapProvider.authenticate(new UsernamePasswordAuthenticationToken("", "bobspassword"));
fail("Expected BadCredentialsException for null username");
@@ -90,7 +86,6 @@ public class LdapAuthenticationProviderTests {
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);
}
@@ -100,7 +95,6 @@ public class LdapAuthenticationProviderTests {
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);
@@ -113,9 +107,7 @@ public class LdapAuthenticationProviderTests {
LdapUserDetailsMapper userMapper = new LdapUserDetailsMapper();
userMapper.setRoleAttributes(new String[] { "ou" });
ldapProvider.setUserDetailsContextMapper(userMapper);
assertThat(ldapProvider.getAuthoritiesPopulator()).isNotNull();
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben",
"benspassword");
Object authDetails = new Object();
@@ -128,7 +120,6 @@ public class LdapAuthenticationProviderTests {
assertThat(user.getPassword()).isEqualTo("{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=");
assertThat(user.getUsername()).isEqualTo("ben");
assertThat(populator.getRequestedUsername()).isEqualTo("ben");
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities())).contains("ROLE_FROM_ENTRY");
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities())).contains("ROLE_FROM_POPULATOR");
}
@@ -138,12 +129,10 @@ public class LdapAuthenticationProviderTests {
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator(),
new MockAuthoritiesPopulator());
ldapProvider.setUseAuthenticationRequestCredentials(false);
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben",
"benspassword");
Authentication authResult = ldapProvider.authenticate(authRequest);
assertThat(authResult.getCredentials()).isEqualTo("{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=");
}
@Test
@@ -166,7 +155,6 @@ public class LdapAuthenticationProviderTests {
LdapAuthenticator mockAuthenticator = mock(LdapAuthenticator.class);
CommunicationException expectedCause = new CommunicationException(new javax.naming.CommunicationException());
given(mockAuthenticator.authenticate(authRequest)).willThrow(expectedCause);
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(mockAuthenticator);
try {
ldapProvider.authenticate(authRequest);
@@ -185,19 +173,15 @@ public class LdapAuthenticationProviderTests {
ctx.setAttributeValue("ou", "FROM_ENTRY");
String username = authentication.getName();
String password = (String) authentication.getCredentials();
if (username.equals("ben") && password.equals("benspassword")) {
ctx.setDn(new DistinguishedName("cn=ben,ou=people,dc=springframework,dc=org"));
ctx.setAttributeValue("userPassword", "{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=");
return ctx;
}
else if (username.equals("jen") && password.equals("")) {
ctx.setDn(new DistinguishedName("cn=jen,ou=people,dc=springframework,dc=org"));
return ctx;
}
throw new BadCredentialsException("Authentication failed.");
}
@@ -43,22 +43,16 @@ public class PasswordComparisonAuthenticatorMockTests {
final BaseLdapPathContextSource source = mock(BaseLdapPathContextSource.class);
final BasicAttributes attrs = new BasicAttributes();
attrs.put(new BasicAttribute("uid", "bob"));
PasswordComparisonAuthenticator authenticator = new PasswordComparisonAuthenticator(source);
authenticator.setUserDnPatterns(new String[] { "cn={0},ou=people" });
// Get the mock to return an empty attribute set
given(source.getReadOnlyContext()).willReturn(dirCtx);
given(dirCtx.getAttributes(eq("cn=Bob,ou=people"), any(String[].class))).willReturn(attrs);
given(dirCtx.getNameInNamespace()).willReturn("dc=springframework,dc=org");
// Setup a single return value (i.e. success)
final NamingEnumeration searchResults = new BasicAttributes("", null).getAll();
given(dirCtx.search(eq("cn=Bob,ou=people"), eq("(userPassword={0})"), any(Object[].class),
any(SearchControls.class))).willReturn(searchResults);
authenticator.authenticate(new UsernamePasswordAuthenticationToken("Bob", "bobspassword"));
}
@@ -98,43 +98,33 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
@Test
public void customSearchFilterIsUsedForSuccessfulAuthentication() throws Exception {
String customSearchFilter = "(&(objectClass=user)(sAMAccountName={0}))";
DirContext ctx = mock(DirContext.class);
given(ctx.getNameInNamespace()).willReturn("");
DirContextAdapter dca = new DirContextAdapter();
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
given(ctx.search(any(Name.class), eq(customSearchFilter), any(Object[].class), any(SearchControls.class)))
.willReturn(new MockNamingEnumeration(sr));
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider(
"mydomain.eu", "ldap://192.168.1.200/");
customProvider.contextFactory = createContextFactoryReturning(ctx);
customProvider.setSearchFilter(customSearchFilter);
Authentication result = customProvider.authenticate(this.joe);
assertThat(result.isAuthenticated()).isTrue();
}
@Test
public void defaultSearchFilter() throws Exception {
final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))";
DirContext ctx = mock(DirContext.class);
given(ctx.getNameInNamespace()).willReturn("");
DirContextAdapter dca = new DirContextAdapter();
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
given(ctx.search(any(Name.class), eq(defaultSearchFilter), any(Object[].class), any(SearchControls.class)))
.willReturn(new MockNamingEnumeration(sr));
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider(
"mydomain.eu", "ldap://192.168.1.200/");
customProvider.contextFactory = createContextFactoryReturning(ctx);
Authentication result = customProvider.authenticate(this.joe);
assertThat(result.isAuthenticated()).isTrue();
verify(ctx).search(any(DistinguishedName.class), eq(defaultSearchFilter), any(Object[].class),
any(SearchControls.class));
@@ -145,21 +135,16 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
public void bindPrincipalAndUsernameUsed() throws Exception {
final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))";
ArgumentCaptor<Object[]> captor = ArgumentCaptor.forClass(Object[].class);
DirContext ctx = mock(DirContext.class);
given(ctx.getNameInNamespace()).willReturn("");
DirContextAdapter dca = new DirContextAdapter();
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
given(ctx.search(any(Name.class), eq(defaultSearchFilter), captor.capture(), any(SearchControls.class)))
.willReturn(new MockNamingEnumeration(sr));
ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider(
"mydomain.eu", "ldap://192.168.1.200/");
customProvider.contextFactory = createContextFactoryReturning(ctx);
Authentication result = customProvider.authenticate(this.joe);
assertThat(captor.getValue()).containsExactly("joe@mydomain.eu", "joe");
assertThat(result.isAuthenticated()).isTrue();
}
@@ -179,20 +164,17 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
this.provider = new ActiveDirectoryLdapAuthenticationProvider(null, "ldap://192.168.1.200/");
DirContext ctx = mock(DirContext.class);
given(ctx.getNameInNamespace()).willReturn("");
DirContextAdapter dca = new DirContextAdapter();
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
given(ctx.search(eq(new DistinguishedName("DC=mydomain,DC=eu")), any(String.class), any(Object[].class),
any(SearchControls.class))).willReturn(new MockNamingEnumeration(sr));
this.provider.contextFactory = createContextFactoryReturning(ctx);
try {
this.provider.authenticate(this.joe);
fail("Expected BadCredentialsException for user with no domain information");
}
catch (BadCredentialsException expected) {
}
this.provider.authenticate(new UsernamePasswordAuthenticationToken("joe@mydomain.eu", "password"));
}
@@ -202,9 +184,7 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
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);
}
@@ -215,9 +195,7 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
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);
}
@@ -239,9 +217,7 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
given(searchResults.next()).willReturn(searchResult);
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);
}
@@ -274,7 +250,6 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
this.provider.contextFactory = createContextFactoryThrowing(
new AuthenticationException(msg + dataCode + ", xxxx]"));
this.provider.setConvertSubErrorCodesToExceptions(true);
this.thrown.expect(BadCredentialsException.class);
this.thrown.expect(new BaseMatcher<BadCredentialsException>() {
private Matcher<Object> causeInstance = CoreMatchers
@@ -297,21 +272,18 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
this.causeDataCode.describeTo(desc);
}
});
this.provider.authenticate(this.joe);
}
@Test(expected = CredentialsExpiredException.class)
public void expiredPasswordIsCorrectlyMapped() {
this.provider.contextFactory = createContextFactoryThrowing(new AuthenticationException(msg + "532, xxxx]"));
try {
this.provider.authenticate(this.joe);
fail("BadCredentialsException should had been thrown");
}
catch (BadCredentialsException expected) {
}
this.provider.setConvertSubErrorCodesToExceptions(true);
this.provider.authenticate(this.joe);
}
@@ -379,7 +351,6 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(
"mydomain.eu", EXISTING_LDAP_PROVIDER, "dc=ad,dc=eu,dc=mydomain");
checkAuthentication("dc=ad,dc=eu,dc=mydomain", provider);
}
@Test(expected = IllegalArgumentException.class)
@@ -395,10 +366,8 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
@Test
public void contextEnvironmentPropertiesUsed() {
Hashtable<String, Object> env = new Hashtable<>();
env.put("java.naming.ldap.factory.socket", "unknown.package.NonExistingSocketFactory");
this.provider.setContextEnvironmentProperties(env);
try {
this.provider.authenticate(this.joe);
fail("CommunicationException was expected with a root cause of ClassNotFoundException");
@@ -433,24 +402,17 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
throws NamingException {
DirContext ctx = mock(DirContext.class);
given(ctx.getNameInNamespace()).willReturn("");
DirContextAdapter dca = new DirContextAdapter();
SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
@SuppressWarnings("deprecation")
DistinguishedName searchBaseDn = new DistinguishedName(rootDn);
given(ctx.search(eq(searchBaseDn), any(String.class), any(Object[].class), any(SearchControls.class)))
.willReturn(new MockNamingEnumeration(sr)).willReturn(new MockNamingEnumeration(sr));
provider.contextFactory = createContextFactoryReturning(ctx);
Authentication result = provider.authenticate(this.joe);
assertThat(result.getAuthorities()).isEmpty();
dca.addAttributeValue("memberOf", "CN=Admin,CN=Users,DC=mydomain,DC=eu");
result = provider.authenticate(this.joe);
assertThat(result.getAuthorities()).hasSize(1);
}
@@ -54,7 +54,6 @@ public class PasswordPolicyAwareContextSourceTests {
if ("manager".equals(env.get(Context.SECURITY_PRINCIPAL))) {
return PasswordPolicyAwareContextSourceTests.this.ctx;
}
return null;
}
};
@@ -71,7 +70,6 @@ public class PasswordPolicyAwareContextSourceTests {
@Test(expected = UncategorizedLdapException.class)
public void standardExceptionIsPropagatedWhenExceptionRaisedAndNoControlsAreSet() throws Exception {
willThrow(new NamingException("some LDAP exception")).given(this.ctx).reconnect(any(Control[].class));
this.ctxSource.getContext("user", "ignored");
}
@@ -79,9 +77,7 @@ public class PasswordPolicyAwareContextSourceTests {
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");
}
@@ -33,7 +33,6 @@ public class PasswordPolicyControlFactoryTests {
public void returnsNullForUnrecognisedOID() {
PasswordPolicyControlFactory ctrlFactory = new PasswordPolicyControlFactory();
Control wrongCtrl = mock(Control.class);
given(wrongCtrl.getID()).willReturn("wrongId");
assertThat(ctrlFactory.getControlInstance(wrongCtrl)).isNull();
}
@@ -42,7 +41,6 @@ public class PasswordPolicyControlFactoryTests {
public void returnsControlForCorrectOID() {
PasswordPolicyControlFactory ctrlFactory = new PasswordPolicyControlFactory();
Control control = mock(Control.class);
given(control.getID()).willReturn(PasswordPolicyControl.OID);
given(control.getEncodedValue()).willReturn(PasswordPolicyResponseControlTests.OPENLDAP_LOCKED_CTRL);
Control result = ctrlFactory.getControlInstance(control);
@@ -66,7 +66,6 @@ public class PasswordPolicyResponseControlTests {
//
// //com.sun.jndi.ldap.LdapPoolManager.showStats(System.out);
// }
// private PasswordPolicyResponseControl getPPolicyResponseCtl(InitialLdapContext ctx)
// throws NamingException {
// Control[] ctrls = ctx.getResponseControls();
@@ -79,13 +78,10 @@ public class PasswordPolicyResponseControlTests {
//
// return null;
// }
@Test
public void openLDAP33SecondsTillPasswordExpiryCtrlIsParsedCorrectly() {
byte[] ctrlBytes = { 0x30, 0x05, (byte) 0xA0, 0x03, (byte) 0xA0, 0x1, 0x21 };
PasswordPolicyResponseControl ctrl = new PasswordPolicyResponseControl(ctrlBytes);
assertThat(ctrl.hasWarning()).isTrue();
assertThat(ctrl.getTimeBeforeExpiration()).isEqualTo(33);
}
@@ -93,9 +89,7 @@ public class PasswordPolicyResponseControlTests {
@Test
public void openLDAP496GraceLoginsRemainingCtrlIsParsedCorrectly() {
byte[] ctrlBytes = { 0x30, 0x06, (byte) 0xA0, 0x04, (byte) 0xA1, 0x02, 0x01, (byte) 0xF0 };
PasswordPolicyResponseControl ctrl = new PasswordPolicyResponseControl(ctrlBytes);
assertThat(ctrl.hasWarning()).isTrue();
assertThat(ctrl.getGraceLoginsRemaining()).isEqualTo(496);
}
@@ -105,7 +99,6 @@ public class PasswordPolicyResponseControlTests {
@Test
public void openLDAP5GraceLoginsRemainingCtrlIsParsedCorrectly() {
PasswordPolicyResponseControl ctrl = new PasswordPolicyResponseControl(OPENLDAP_5_LOGINS_REMAINING_CTRL);
assertThat(ctrl.hasWarning()).isTrue();
assertThat(ctrl.getGraceLoginsRemaining()).isEqualTo(5);
}
@@ -115,7 +108,6 @@ public class PasswordPolicyResponseControlTests {
@Test
public void openLDAPAccountLockedCtrlIsParsedCorrectly() {
PasswordPolicyResponseControl ctrl = new PasswordPolicyResponseControl(OPENLDAP_LOCKED_CTRL);
assertThat(ctrl.hasError() && ctrl.isLocked()).isTrue();
assertThat(ctrl.hasWarning()).isFalse();
}
@@ -123,9 +115,7 @@ public class PasswordPolicyResponseControlTests {
@Test
public void openLDAPPasswordExpiredCtrlIsParsedCorrectly() {
byte[] ctrlBytes = { 0x30, 0x03, (byte) 0xA1, 0x01, 0x00 };
PasswordPolicyResponseControl ctrl = new PasswordPolicyResponseControl(ctrlBytes);
assertThat(ctrl.hasError() && ctrl.isExpired()).isTrue();
assertThat(ctrl.hasWarning()).isFalse();
}
@@ -35,7 +35,6 @@ public class InetOrgPersonTests {
public void testUsernameIsMappedFromContextUidIfNotSet() {
InetOrgPerson.Essence essence = new InetOrgPerson.Essence(createUserContext());
InetOrgPerson p = (InetOrgPerson) essence.createUserDetails();
assertThat(p.getUsername()).isEqualTo("ghengis");
}
@@ -55,7 +54,6 @@ public class InetOrgPersonTests {
InetOrgPerson.Essence essence = new InetOrgPerson.Essence(createUserContext());
essence.setUsername("joe");
InetOrgPerson p = (InetOrgPerson) essence.createUserDetails();
assertThat(p.getUsername()).isEqualTo("joe");
assertThat(p.getUid()).isEqualTo("ghengis");
}
@@ -64,7 +62,6 @@ public class InetOrgPersonTests {
public void attributesMapCorrectlyFromContext() {
InetOrgPerson.Essence essence = new InetOrgPerson.Essence(createUserContext());
InetOrgPerson p = (InetOrgPerson) essence.createUserDetails();
assertThat(p.getCarLicense()).isEqualTo("HORS1");
assertThat(p.getMail()).isEqualTo("ghengis@mongolia");
assertThat(p.getGivenName()).isEqualTo("Ghengis");
@@ -89,7 +86,6 @@ public class InetOrgPersonTests {
public void testPasswordIsSetFromContextUserPassword() {
InetOrgPerson.Essence essence = new InetOrgPerson.Essence(createUserContext());
InetOrgPerson p = (InetOrgPerson) essence.createUserDetails();
assertThat(p.getPassword()).isEqualTo("pillage");
}
@@ -102,7 +98,6 @@ public class InetOrgPersonTests {
ctx2.setDn(new DistinguishedName("ignored=ignored"));
InetOrgPerson p = (InetOrgPerson) (new InetOrgPerson.Essence(ctx1)).createUserDetails();
p.populateContext(ctx2);
assertThat(ctx2).isEqualTo(ctx1);
}
@@ -116,13 +111,11 @@ public class InetOrgPersonTests {
InetOrgPerson p = (InetOrgPerson) (new InetOrgPerson.Essence(ctx1)).createUserDetails();
InetOrgPerson p2 = (InetOrgPerson) new InetOrgPerson.Essence(p).createUserDetails();
p2.populateContext(ctx2);
assertThat(ctx2).isEqualTo(ctx1);
}
private DirContextAdapter createUserContext() {
DirContextAdapter ctx = new DirContextAdapter();
ctx.setDn(new DistinguishedName("ignored=ignored"));
ctx.setAttributeValue("uid", "ghengis");
ctx.setAttributeValue("userPassword", "pillage");
@@ -147,7 +140,6 @@ public class InetOrgPersonTests {
ctx.setAttributeValue("sn", "Khan");
ctx.setAttributeValue("street", "Westward Avenue");
ctx.setAttributeValue("telephoneNumber", "+442075436521");
return ctx;
}
@@ -35,7 +35,6 @@ public class LdapUserDetailsImplTests {
mutableLdapUserDetails.setDn("uid=username1,ou=people,dc=example,dc=com");
mutableLdapUserDetails.setUsername("username1");
mutableLdapUserDetails.setPassword("password");
LdapUserDetails ldapUserDetails = mutableLdapUserDetails.createUserDetails();
assertThat(ldapUserDetails).isInstanceOf(CredentialsContainer.class);
ldapUserDetails.eraseCredentials();
@@ -40,17 +40,12 @@ public class LdapUserDetailsMapperTests {
LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
mapper.setConvertToUpperCase(false);
mapper.setRolePrefix("");
mapper.setRoleAttributes(new String[] { "userRole" });
DirContextAdapter ctx = new DirContextAdapter();
ctx.setAttributeValues("userRole", new String[] { "X", "Y", "Z" });
ctx.setAttributeValue("uid", "ani");
LdapUserDetailsImpl user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani",
AuthorityUtils.NO_AUTHORITIES);
assertThat(user.getAuthorities()).hasSize(3);
}
@@ -60,18 +55,13 @@ public class LdapUserDetailsMapperTests {
@Test
public void testNonRetrievedRoleAttributeIsIgnored() {
LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
mapper.setRoleAttributes(new String[] { "userRole", "nonRetrievedAttribute" });
BasicAttributes attrs = new BasicAttributes();
attrs.put(new BasicAttribute("userRole", "x"));
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())).contains("ROLE_X");
}
@@ -79,17 +69,13 @@ public class LdapUserDetailsMapperTests {
@Test
public void testPasswordAttributeIsMappedCorrectly() {
LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
mapper.setPasswordAttributeName("myappsPassword");
BasicAttributes attrs = new BasicAttributes();
attrs.put(new BasicAttribute("myappsPassword", "mypassword".getBytes()));
DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName"));
ctx.setAttributeValue("uid", "ani");
LdapUserDetails user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani",
AuthorityUtils.NO_AUTHORITIES);
assertThat(user.getPassword()).isEqualTo("mypassword");
}
@@ -52,13 +52,10 @@ public class LdapUserDetailsServiceTests {
@Test
public void correctAuthoritiesAreReturned() {
DirContextAdapter userData = new DirContextAdapter(new DistinguishedName("uid=joe"));
LdapUserDetailsService service = new LdapUserDetailsService(new MockUserSearch(userData),
new MockAuthoritiesPopulator());
service.setUserDetailsMapper(new LdapUserDetailsMapper());
UserDetails user = service.loadUserByUsername("doesntmatterwegetjoeanyway");
Set<String> authorities = AuthorityUtils.authorityListToSet(user.getAuthorities());
assertThat(authorities).hasSize(1);
assertThat(authorities.contains("ROLE_FROM_POPULATOR")).isTrue();
@@ -67,7 +64,6 @@ public class LdapUserDetailsServiceTests {
@Test
public void nullPopulatorConstructorReturnsEmptyAuthoritiesList() {
DirContextAdapter userData = new DirContextAdapter(new DistinguishedName("uid=joe"));
LdapUserDetailsService service = new LdapUserDetailsService(new MockUserSearch(userData));
UserDetails user = service.loadUserByUsername("doesntmatterwegetjoeanyway");
assertThat(user.getAuthorities()).isEmpty();
@@ -44,10 +44,8 @@ public class UserDetailsServiceLdapAuthoritiesPopulatorTests {
given(uds.loadUserByUsername("joe")).willReturn(user);
List authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
given(user.getAuthorities()).willReturn(authorities);
UserDetailsServiceLdapAuthoritiesPopulator populator = new UserDetailsServiceLdapAuthoritiesPopulator(uds);
Collection<? extends GrantedAuthority> auths = populator.getGrantedAuthorities(new DirContextAdapter(), "joe");
assertThat(auths).hasSize(1);
assertThat(AuthorityUtils.authorityListToSet(auths).contains("ROLE_USER")).isTrue();
}