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.ldap.search;
17  
18  import org.acegisecurity.ldap.AbstractLdapServerTestCase;
19  import org.acegisecurity.ldap.DefaultInitialDirContextFactory;
20  
21  import org.acegisecurity.userdetails.UsernameNotFoundException;
22  import org.acegisecurity.userdetails.ldap.LdapUserDetails;
23  
24  import org.springframework.dao.IncorrectResultSizeDataAccessException;
25  
26  
27  /**
28   * Tests for FilterBasedLdapUserSearch.
29   *
30   * @author Luke Taylor
31   * @version $Id: FilterBasedLdapUserSearchTests.java 1928 2007-07-24 17:01:36Z luke_t $
32   */
33  public class FilterBasedLdapUserSearchTests extends AbstractLdapServerTestCase {
34      //~ Instance fields ================================================================================================
35  
36      private DefaultInitialDirContextFactory dirCtxFactory;
37  
38      //~ Constructors ===================================================================================================
39  
40      public FilterBasedLdapUserSearchTests(String string) {
41          super(string);
42      }
43  
44      public FilterBasedLdapUserSearchTests() {
45      }
46  
47      //~ Methods ========================================================================================================
48  
49      public void onSetUp() {
50          dirCtxFactory = getInitialCtxFactory();
51          dirCtxFactory.setManagerDn(MANAGER_USER);
52          dirCtxFactory.setManagerPassword(MANAGER_PASSWORD);
53      }
54  
55      public void testBasicSearch() {
56          FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(uid={0})", dirCtxFactory);
57          locator.setSearchSubtree(false);
58          locator.setSearchTimeLimit(0);
59          locator.setDerefLinkFlag(false);
60  
61          LdapUserDetails bob = locator.searchForUser("bob");
62          assertEquals("bob", bob.getUsername());
63  
64          // name is wrong with embedded apacheDS
65  //        assertEquals("uid=bob,ou=people,dc=acegisecurity,dc=org", bob.getDn());
66      }
67  
68      // Try some funny business with filters.
69      public void testExtraFilterPartToExcludeBob() throws Exception {
70          FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people",
71                  "(&(cn=*)(!(|(uid={0})(uid=marissa))))", dirCtxFactory);
72  
73          // Search for bob, get back ben...
74          LdapUserDetails ben = locator.searchForUser("bob");
75          String cn = (String) ben.getAttributes().get("cn").get();
76          assertEquals("Ben Alex", cn);
77  
78  //        assertEquals("uid=ben,ou=people,"+ROOT_DN, ben.getDn());
79      }
80  
81      public void testFailsOnMultipleMatches() {
82          FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(cn=*)", dirCtxFactory);
83  
84          try {
85              locator.searchForUser("Ignored");
86              fail("Expected exception for multiple search matches.");
87          } catch (IncorrectResultSizeDataAccessException expected) {}
88      }
89  
90      public void testSearchForInvalidUserFails() {
91          FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(uid={0})", dirCtxFactory);
92  
93          try {
94              locator.searchForUser("Joe");
95              fail("Expected UsernameNotFoundException for non-existent user.");
96          } catch (UsernameNotFoundException expected) {}
97      }
98  
99      public void testSubTreeSearchSucceeds() {
100         // Don't set the searchBase, so search from the root.
101         FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("", "(cn={0})", dirCtxFactory);
102         locator.setSearchSubtree(true);
103 
104         LdapUserDetails ben = locator.searchForUser("Ben Alex");
105         assertEquals("Ben Alex", ben.getUsername());
106 
107 //        assertEquals("uid=ben,ou=people,dc=acegisecurity,dc=org", ben.getDn());
108     }
109 }