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.userdetails.ldap;
017:
018: import junit.framework.TestCase;
019:
020: import javax.naming.directory.BasicAttributes;
021: import javax.naming.directory.BasicAttribute;
022:
023: import org.acegisecurity.GrantedAuthorityImpl;
024:
025: /**
026: * Tests {@link LdapUserDetailsMapper}.
027: *
028: * @author Luke Taylor
029: * @version $Id$
030: */
031: public class LdapUserDetailsMapperTests extends TestCase {
032:
033: public void testMultipleRoleAttributeValuesAreMappedToAuthorities()
034: throws Exception {
035: LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
036: mapper.setConvertToUpperCase(false);
037: mapper.setRolePrefix("");
038:
039: mapper.setRoleAttributes(new String[] { "userRole" });
040:
041: BasicAttributes attrs = new BasicAttributes();
042: BasicAttribute roleAttribute = new BasicAttribute("userRole");
043: roleAttribute.add("X");
044: roleAttribute.add("Y");
045: roleAttribute.add("Z");
046: attrs.put(roleAttribute);
047:
048: LdapUserDetailsImpl.Essence user = (LdapUserDetailsImpl.Essence) mapper
049: .mapAttributes("cn=someName", attrs);
050:
051: assertEquals(3, user.getGrantedAuthorities().length);
052: }
053:
054: /**
055: * SEC-303. Non-retrieved role attribute causes NullPointerException
056: */
057: public void testNonRetrievedRoleAttributeIsIgnored()
058: throws Exception {
059: LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
060:
061: mapper.setRoleAttributes(new String[] { "userRole",
062: "nonRetrievedAttribute" });
063:
064: BasicAttributes attrs = new BasicAttributes();
065: attrs.put(new BasicAttribute("userRole", "x"));
066:
067: LdapUserDetailsImpl.Essence user = (LdapUserDetailsImpl.Essence) mapper
068: .mapAttributes("cn=someName", attrs);
069:
070: assertEquals(1, user.getGrantedAuthorities().length);
071: assertEquals("ROLE_X", user.getGrantedAuthorities()[0]
072: .getAuthority());
073: }
074:
075: public void testNonStringRoleAttributeIsIgnoredByDefault()
076: throws Exception {
077: LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
078:
079: mapper.setRoleAttributes(new String[] { "userRole" });
080:
081: BasicAttributes attrs = new BasicAttributes();
082: attrs.put(new BasicAttribute("userRole",
083: new GrantedAuthorityImpl("X")));
084:
085: LdapUserDetailsImpl.Essence user = (LdapUserDetailsImpl.Essence) mapper
086: .mapAttributes("cn=someName", attrs);
087:
088: assertEquals(0, user.getGrantedAuthorities().length);
089: }
090:
091: public void testPasswordAttributeIsMappedCorrectly()
092: throws Exception {
093: LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
094:
095: mapper.setPasswordAttributeName("myappsPassword");
096: BasicAttributes attrs = new BasicAttributes();
097: attrs.put(new BasicAttribute("myappsPassword", "mypassword"
098: .getBytes()));
099:
100: LdapUserDetails user = ((LdapUserDetailsImpl.Essence) mapper
101: .mapAttributes("cn=someName", attrs))
102: .createUserDetails();
103:
104: assertEquals("mypassword", user.getPassword());
105: }
106: }
|