SEC-456: Added test class for UserDetailsServiceLdapAuthoritiesPopulator
This commit is contained in:
parent
9292317e1c
commit
208d1ee8e2
|
@ -4,6 +4,7 @@ import org.springframework.security.ldap.LdapAuthoritiesPopulator;
|
|||
import org.springframework.security.GrantedAuthority;
|
||||
import org.springframework.security.userdetails.UserDetailsService;
|
||||
import org.springframework.ldap.core.DirContextOperations;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Simple LdapAuthoritiesPopulator which delegates to a UserDetailsService, using the name which
|
||||
|
@ -15,13 +16,14 @@ import org.springframework.ldap.core.DirContextOperations;
|
|||
* @since 2.0
|
||||
*/
|
||||
public class UserDetailsServiceLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator {
|
||||
private UserDetailsService userService;
|
||||
private UserDetailsService userDetailsService;
|
||||
|
||||
public UserDetailsServiceLdapAuthoritiesPopulator(UserDetailsService userService) {
|
||||
this.userService = userService;
|
||||
Assert.notNull(userService, "userDetailsService cannot be null");
|
||||
this.userDetailsService = userService;
|
||||
}
|
||||
|
||||
public GrantedAuthority[] getGrantedAuthorities(DirContextOperations userData, String username) {
|
||||
return userService.loadUserByUsername(username).getAuthorities();
|
||||
return userDetailsService.loadUserByUsername(username).getAuthorities();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package org.springframework.security.ldap.populator;
|
||||
|
||||
import org.springframework.security.userdetails.UserDetailsService;
|
||||
import org.springframework.security.userdetails.MockUserDetailsService;
|
||||
import org.springframework.security.GrantedAuthority;
|
||||
|
||||
import org.springframework.ldap.core.DirContextAdapter;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Luke Taylor
|
||||
* @version $Id$
|
||||
*/
|
||||
public class UserDetailsServiceLdapAuthoritiesPopulatorTests {
|
||||
UserDetailsService uds = new MockUserDetailsService();
|
||||
|
||||
@Test
|
||||
public void delegationToUserDetailsServiceReturnsCorrectRoles() throws Exception {
|
||||
UserDetailsServiceLdapAuthoritiesPopulator populator = new UserDetailsServiceLdapAuthoritiesPopulator(uds);
|
||||
|
||||
GrantedAuthority[] auths = populator.getGrantedAuthorities(new DirContextAdapter(), "valid");
|
||||
|
||||
assertEquals(1, auths.length);
|
||||
assertEquals("ROLE_USER", auths[0].getAuthority());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package org.springframework.security.userdetails;
|
||||
|
||||
import org.springframework.security.GrantedAuthority;
|
||||
import org.springframework.security.GrantedAuthorityImpl;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* A test UserDetailsService containing a set of standard usernames corresponding to their account status:
|
||||
* valid, locked, disabled, credentialsExpired, expired. All passwords are "".
|
||||
*
|
||||
* @author Luke Taylor
|
||||
* @version $Id$
|
||||
*/
|
||||
public class MockUserDetailsService implements UserDetailsService {
|
||||
private Map users = new HashMap();
|
||||
private GrantedAuthority[] auths = new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_USER")};
|
||||
|
||||
public MockUserDetailsService() {
|
||||
users.put("valid", new User("valid", "",true,true,true,true,auths));
|
||||
users.put("locked", new User("locked", "",true,true,true,false,auths));
|
||||
users.put("disabled", new User("disabled", "",false,true,true,true,auths));
|
||||
users.put("credentialsExpired", new User("credentialsExpired", "",true,true,false,true,auths));
|
||||
users.put("expired", new User("expired", "",true,false,true,true,auths));
|
||||
}
|
||||
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
|
||||
if (users.get(username) == null) {
|
||||
throw new UsernameNotFoundException("User not found: " + username);
|
||||
}
|
||||
|
||||
return (UserDetails) users.get(username);
|
||||
}
|
||||
}
|
|
@ -1,23 +1,13 @@
|
|||
package org.springframework.security.userdetails.decorator;
|
||||
|
||||
import org.springframework.security.userdetails.UserDetailsService;
|
||||
import org.springframework.security.userdetails.UserDetails;
|
||||
import org.springframework.security.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.userdetails.User;
|
||||
import org.springframework.security.GrantedAuthority;
|
||||
import org.springframework.security.GrantedAuthorityImpl;
|
||||
import org.springframework.security.userdetails.MockUserDetailsService;
|
||||
import org.springframework.security.LockedException;
|
||||
import org.springframework.security.DisabledException;
|
||||
import org.springframework.security.CredentialsExpiredException;
|
||||
import org.springframework.security.AccountExpiredException;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @author Luke Taylor
|
||||
* @version $Id$
|
||||
|
@ -50,20 +40,4 @@ public class StatusCheckingUserDetailsServiceTests {
|
|||
us.loadUserByUsername("expired");
|
||||
}
|
||||
|
||||
class MockUserDetailsService implements UserDetailsService {
|
||||
private Map users = new HashMap();
|
||||
private GrantedAuthority[] auths = new GrantedAuthority[] {new GrantedAuthorityImpl("A")};
|
||||
|
||||
MockUserDetailsService() {
|
||||
users.put("valid", new User("valid", "",true,true,true,true,auths));
|
||||
users.put("locked", new User("locked", "",true,true,true,false,auths));
|
||||
users.put("disabled", new User("disabled", "",false,true,true,true,auths));
|
||||
users.put("credentialsExpired", new User("credentialsExpired", "",true,true,false,true,auths));
|
||||
users.put("expired", new User("expired", "",true,false,true,true,auths));
|
||||
}
|
||||
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
|
||||
return (UserDetails) users.get(username);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue