001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.providers.ldap.populator;
017:
018: import org.acegisecurity.GrantedAuthority;
019:
020: import org.acegisecurity.ldap.AbstractLdapServerTestCase;
021:
022: import org.acegisecurity.userdetails.ldap.LdapUserDetailsImpl;
023:
024: import java.util.HashSet;
025: import java.util.Set;
026:
027: import javax.naming.directory.BasicAttributes;
028:
029: /**
030: *
031: DOCUMENT ME!
032: *
033: * @author Luke Taylor
034: * @version $Id: DefaultLdapAuthoritiesPopulatorTests.java 1496 2006-05-23 13:38:33Z benalex $
035: */
036: public class DefaultLdapAuthoritiesPopulatorTests extends
037: AbstractLdapServerTestCase {
038: //~ Methods ========================================================================================================
039:
040: public void onSetUp() {
041: getInitialCtxFactory().setManagerDn(MANAGER_USER);
042: getInitialCtxFactory().setManagerPassword(MANAGER_PASSWORD);
043: }
044:
045: // public void testUserAttributeMappingToRoles() {
046: // DefaultLdapAuthoritiesPopulator populator = new DefaultLdapAuthoritiesPopulator();
047: // populator.setUserRoleAttributes(new String[] {"userRole", "otherUserRole"});
048: // populator.getUserRoleAttributes();
049: //
050: // Attributes userAttrs = new BasicAttributes();
051: // BasicAttribute attr = new BasicAttribute("userRole", "role1");
052: // attr.add("role2");
053: // userAttrs.put(attr);
054: // attr = new BasicAttribute("otherUserRole", "role3");
055: // attr.add("role2"); // duplicate
056: // userAttrs.put(attr);
057: //
058: // LdapUserDetailsImpl.Essence user = new LdapUserDetailsImpl.Essence();
059: // user.setDn("Ignored");
060: // user.setUsername("Ignored");
061: // user.setAttributes(userAttrs);
062: //
063: // GrantedAuthority[] authorities =
064: // populator.getGrantedAuthorities(user.createUserDetails());
065: // assertEquals("User should have three roles", 3, authorities.length);
066: // }
067: public void testDefaultRoleIsAssignedWhenSet() {
068: DefaultLdapAuthoritiesPopulator populator = new DefaultLdapAuthoritiesPopulator(
069: getInitialCtxFactory(), "ou=groups");
070: populator.setDefaultRole("ROLE_USER");
071:
072: LdapUserDetailsImpl.Essence user = new LdapUserDetailsImpl.Essence();
073: user.setDn("cn=notfound");
074: user.setUsername("notfound");
075: user.setAttributes(new BasicAttributes());
076:
077: GrantedAuthority[] authorities = populator
078: .getGrantedAuthorities(user.createUserDetails());
079: assertEquals(1, authorities.length);
080: assertEquals("ROLE_USER", authorities[0].getAuthority());
081: }
082:
083: public void testGroupSearchReturnsExpectedRoles() {
084: DefaultLdapAuthoritiesPopulator populator = new DefaultLdapAuthoritiesPopulator(
085: getInitialCtxFactory(), "ou=groups");
086: populator.setRolePrefix("ROLE_");
087: populator.setGroupRoleAttribute("ou");
088: populator.setSearchSubtree(true);
089: populator.setSearchSubtree(false);
090: populator.setConvertToUpperCase(true);
091: populator.setGroupSearchFilter("(member={0})");
092:
093: LdapUserDetailsImpl.Essence user = new LdapUserDetailsImpl.Essence();
094: user.setUsername("ben");
095: user.setDn("uid=ben,ou=people,dc=acegisecurity,dc=org");
096: user.setAttributes(new BasicAttributes());
097:
098: GrantedAuthority[] authorities = populator
099: .getGrantedAuthorities(user.createUserDetails());
100:
101: assertEquals("Should have 2 roles", 2, authorities.length);
102:
103: Set roles = new HashSet();
104: roles.add(authorities[0].toString());
105: roles.add(authorities[1].toString());
106: assertTrue(roles.contains("ROLE_DEVELOPER"));
107: assertTrue(roles.contains("ROLE_MANAGER"));
108: }
109:
110: public void testUseOfUsernameParameterReturnsExpectedRoles() {
111: DefaultLdapAuthoritiesPopulator populator = new DefaultLdapAuthoritiesPopulator(
112: getInitialCtxFactory(), "ou=groups");
113: populator.setGroupRoleAttribute("ou");
114: populator.setConvertToUpperCase(true);
115: populator.setGroupSearchFilter("(ou={1})");
116:
117: LdapUserDetailsImpl.Essence user = new LdapUserDetailsImpl.Essence();
118: user.setUsername("manager");
119: user.setDn("uid=ben,ou=people,dc=acegisecurity,dc=org");
120:
121: GrantedAuthority[] authorities = populator
122: .getGrantedAuthorities(user.createUserDetails());
123: assertEquals("Should have 1 role", 1, authorities.length);
124: assertTrue(authorities[0].equals("ROLE_MANAGER"));
125: }
126: }
|