From 208d1ee8e26896902c7c393fcef1bbe7b7f1c6b1 Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Thu, 7 Feb 2008 13:31:25 +0000 Subject: [PATCH] SEC-456: Added test class for UserDetailsServiceLdapAuthoritiesPopulator --- ...etailsServiceLdapAuthoritiesPopulator.java | 8 +++-- ...sServiceLdapAuthoritiesPopulatorTests.java | 28 +++++++++++++++ .../userdetails/MockUserDetailsService.java | 36 +++++++++++++++++++ ...StatusCheckingUserDetailsServiceTests.java | 28 +-------------- 4 files changed, 70 insertions(+), 30 deletions(-) create mode 100644 core/src/test/java/org/springframework/security/ldap/populator/UserDetailsServiceLdapAuthoritiesPopulatorTests.java create mode 100644 core/src/test/java/org/springframework/security/userdetails/MockUserDetailsService.java diff --git a/core/src/main/java/org/springframework/security/ldap/populator/UserDetailsServiceLdapAuthoritiesPopulator.java b/core/src/main/java/org/springframework/security/ldap/populator/UserDetailsServiceLdapAuthoritiesPopulator.java index 9f9401bed3..20dd79d0ea 100644 --- a/core/src/main/java/org/springframework/security/ldap/populator/UserDetailsServiceLdapAuthoritiesPopulator.java +++ b/core/src/main/java/org/springframework/security/ldap/populator/UserDetailsServiceLdapAuthoritiesPopulator.java @@ -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(); } } diff --git a/core/src/test/java/org/springframework/security/ldap/populator/UserDetailsServiceLdapAuthoritiesPopulatorTests.java b/core/src/test/java/org/springframework/security/ldap/populator/UserDetailsServiceLdapAuthoritiesPopulatorTests.java new file mode 100644 index 0000000000..e103b77382 --- /dev/null +++ b/core/src/test/java/org/springframework/security/ldap/populator/UserDetailsServiceLdapAuthoritiesPopulatorTests.java @@ -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()); + } +} diff --git a/core/src/test/java/org/springframework/security/userdetails/MockUserDetailsService.java b/core/src/test/java/org/springframework/security/userdetails/MockUserDetailsService.java new file mode 100644 index 0000000000..33d17d3768 --- /dev/null +++ b/core/src/test/java/org/springframework/security/userdetails/MockUserDetailsService.java @@ -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); + } +} diff --git a/core/src/test/java/org/springframework/security/userdetails/decorator/StatusCheckingUserDetailsServiceTests.java b/core/src/test/java/org/springframework/security/userdetails/decorator/StatusCheckingUserDetailsServiceTests.java index e156828292..56825057ec 100644 --- a/core/src/test/java/org/springframework/security/userdetails/decorator/StatusCheckingUserDetailsServiceTests.java +++ b/core/src/test/java/org/springframework/security/userdetails/decorator/StatusCheckingUserDetailsServiceTests.java @@ -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); - } - } }