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.authenticator;
017:
018: import org.acegisecurity.AcegiMessageSource;
019: import org.acegisecurity.BadCredentialsException;
020: import org.acegisecurity.GrantedAuthorityImpl;
021:
022: import org.acegisecurity.ldap.AbstractLdapServerTestCase;
023:
024: import org.acegisecurity.userdetails.ldap.LdapUserDetails;
025: import org.acegisecurity.userdetails.ldap.LdapUserDetailsImpl;
026: import org.acegisecurity.userdetails.ldap.LdapUserDetailsMapper;
027:
028: /**
029: * Tests for {@link BindAuthenticator}.
030: *
031: * @author Luke Taylor
032: * @version $Id: BindAuthenticatorTests.java 1496 2006-05-23 13:38:33Z benalex $
033: */
034: public class BindAuthenticatorTests extends AbstractLdapServerTestCase {
035: //~ Instance fields ================================================================================================
036:
037: private BindAuthenticator authenticator;
038:
039: //~ Methods ========================================================================================================
040:
041: public void onSetUp() {
042: authenticator = new BindAuthenticator(getInitialCtxFactory());
043: authenticator.setMessageSource(new AcegiMessageSource());
044: }
045:
046: public void testAuthenticationWithCorrectPasswordSucceeds() {
047: authenticator
048: .setUserDnPatterns(new String[] { "uid={0},ou=people" });
049:
050: LdapUserDetails user = authenticator.authenticate("bob",
051: "bobspassword");
052: assertEquals("bob", user.getUsername());
053: }
054:
055: public void testAuthenticationWithInvalidUserNameFails() {
056: authenticator
057: .setUserDnPatterns(new String[] { "uid={0},ou=people" });
058:
059: try {
060: authenticator.authenticate("nonexistentsuser",
061: "bobspassword");
062: fail("Shouldn't be able to bind with invalid username");
063: } catch (BadCredentialsException expected) {
064: }
065: }
066:
067: public void testAuthenticationWithUserSearch() throws Exception {
068: LdapUserDetailsImpl.Essence userEssence = new LdapUserDetailsImpl.Essence();
069: userEssence.setDn("uid=bob,ou=people,dc=acegisecurity,dc=org");
070:
071: authenticator.setUserSearch(new MockUserSearch(userEssence
072: .createUserDetails()));
073: authenticator.afterPropertiesSet();
074: authenticator.authenticate("bob", "bobspassword");
075: }
076:
077: public void testAuthenticationWithWrongPasswordFails() {
078: authenticator
079: .setUserDnPatterns(new String[] { "uid={0},ou=people" });
080:
081: try {
082: authenticator.authenticate("bob", "wrongpassword");
083: fail("Shouldn't be able to bind with wrong password");
084: } catch (BadCredentialsException expected) {
085: }
086: }
087:
088: // TODO: Create separate tests for base class
089: public void testRoleRetrieval() {
090: authenticator
091: .setUserDnPatterns(new String[] { "uid={0},ou=people" });
092:
093: LdapUserDetailsMapper userMapper = new LdapUserDetailsMapper();
094: userMapper.setRoleAttributes(new String[] { "uid" });
095:
096: authenticator.setUserDetailsMapper(userMapper);
097:
098: LdapUserDetails user = authenticator.authenticate("bob",
099: "bobspassword");
100:
101: assertEquals(1, user.getAuthorities().length);
102: assertEquals(new GrantedAuthorityImpl("ROLE_BOB"), user
103: .getAuthorities()[0]);
104: }
105:
106: public void testUserDnPatternReturnsCorrectDn() {
107: authenticator
108: .setUserDnPatterns(new String[] { "cn={0},ou=people" });
109: assertEquals("cn=Joe,ou=people,"
110: + getInitialCtxFactory().getRootDn(), authenticator
111: .getUserDns("Joe").get(0));
112: }
113: }
|