1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.providers.ldap.authenticator;
17  
18  import org.acegisecurity.AcegiMessageSource;
19  import org.acegisecurity.BadCredentialsException;
20  import org.acegisecurity.GrantedAuthorityImpl;
21  
22  import org.acegisecurity.ldap.AbstractLdapServerTestCase;
23  
24  import org.acegisecurity.userdetails.ldap.LdapUserDetails;
25  import org.acegisecurity.userdetails.ldap.LdapUserDetailsImpl;
26  import org.acegisecurity.userdetails.ldap.LdapUserDetailsMapper;
27  
28  
29  /**
30   * Tests for {@link BindAuthenticator}.
31   *
32   * @author Luke Taylor
33   * @version $Id: BindAuthenticatorTests.java 1496 2006-05-23 13:38:33Z benalex $
34   */
35  public class BindAuthenticatorTests extends AbstractLdapServerTestCase {
36      //~ Instance fields ================================================================================================
37  
38      private BindAuthenticator authenticator;
39  
40      //~ Methods ========================================================================================================
41  
42      public void onSetUp() {
43          authenticator = new BindAuthenticator(getInitialCtxFactory());
44          authenticator.setMessageSource(new AcegiMessageSource());
45      }
46  
47      public void testAuthenticationWithCorrectPasswordSucceeds() {
48          authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
49  
50          LdapUserDetails user = authenticator.authenticate("bob", "bobspassword");
51          assertEquals("bob", user.getUsername());
52      }
53  
54      public void testAuthenticationWithInvalidUserNameFails() {
55          authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
56  
57          try {
58              authenticator.authenticate("nonexistentsuser", "bobspassword");
59              fail("Shouldn't be able to bind with invalid username");
60          } catch (BadCredentialsException expected) {}
61      }
62  
63      public void testAuthenticationWithUserSearch() throws Exception {
64          LdapUserDetailsImpl.Essence userEssence = new LdapUserDetailsImpl.Essence();
65          userEssence.setDn("uid=bob,ou=people,dc=acegisecurity,dc=org");
66  
67          authenticator.setUserSearch(new MockUserSearch(userEssence.createUserDetails()));
68          authenticator.afterPropertiesSet();
69          authenticator.authenticate("bob", "bobspassword");
70      }
71  
72      public void testAuthenticationWithWrongPasswordFails() {
73          authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
74  
75          try {
76              authenticator.authenticate("bob", "wrongpassword");
77              fail("Shouldn't be able to bind with wrong password");
78          } catch (BadCredentialsException expected) {}
79      }
80  
81      // TODO: Create separate tests for base class
82      public void testRoleRetrieval() {
83          authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
84  
85          LdapUserDetailsMapper userMapper = new LdapUserDetailsMapper();
86          userMapper.setRoleAttributes(new String[] {"uid"});
87  
88          authenticator.setUserDetailsMapper(userMapper);
89  
90          LdapUserDetails user = authenticator.authenticate("bob", "bobspassword");
91  
92          assertEquals(1, user.getAuthorities().length);
93          assertEquals(new GrantedAuthorityImpl("ROLE_BOB"), user.getAuthorities()[0]);
94      }
95  
96      public void testUserDnPatternReturnsCorrectDn() {
97          authenticator.setUserDnPatterns(new String[] {"cn={0},ou=people"});
98          assertEquals("cn=Joe,ou=people," + getInitialCtxFactory().getRootDn(), authenticator.getUserDns("Joe").get(0));
99      }
100 }