package org.acegisecurity.ldap.search;

import org.acegisecurity.ldap.AbstractLdapServerTestCase;
import org.acegisecurity.ldap.DefaultInitialDirContextFactory;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.acegisecurity.userdetails.ldap.LdapUserDetails;
import org.springframework.dao.IncorrectResultSizeDataAccessException;

/**
 * Tests for FilterBasedLdapUserSearch.
 * 
 * @author Luke Taylor
 * @version $Id$
 */
public class FilterBasedLdapUserSearchTests extends AbstractLdapServerTestCase {
    private DefaultInitialDirContextFactory dirCtxFactory;

    public void onSetUp() {
        dirCtxFactory = getInitialCtxFactory();
        dirCtxFactory.setManagerDn(MANAGER_USER);
        dirCtxFactory.setManagerPassword(MANAGER_PASSWORD);
    }

    public FilterBasedLdapUserSearchTests(String string) {
        super(string);
    }

    public FilterBasedLdapUserSearchTests() {
        super();
    }

    public void testBasicSearch() throws Exception {
        FilterBasedLdapUserSearch locator =
                new FilterBasedLdapUserSearch("ou=people", "(uid={0})", dirCtxFactory);
        LdapUserDetails bob = locator.searchForUser("bob");
        locator.setSearchSubtree(false);
        locator.setSearchTimeLimit(0);
        // name is wrong with embedded apacheDS
//        assertEquals("uid=bob,ou=people,"+ROOT_DN, bob.getDn());
    }

    public void testSubTreeSearchSucceeds() throws Exception {
        // Don't set the searchBase, so search from the root.
        FilterBasedLdapUserSearch locator =
                new FilterBasedLdapUserSearch("", "(cn={0})", dirCtxFactory);
        locator.setSearchSubtree(true);

        LdapUserDetails ben = locator.searchForUser("Ben Alex");
//        assertEquals("uid=ben,ou=people,"+ROOT_DN, bob.getDn());
    }

    public void testSearchForInvalidUserFails() {
        FilterBasedLdapUserSearch locator =
                new FilterBasedLdapUserSearch("ou=people", "(uid={0})", dirCtxFactory);

        try {
            locator.searchForUser("Joe");
            fail("Expected UsernameNotFoundException for non-existent user.");
        } catch (UsernameNotFoundException expected) {
        }
    }

    public void testFailsOnMultipleMatches() {
        FilterBasedLdapUserSearch locator =
                new FilterBasedLdapUserSearch("ou=people", "(cn=*)", dirCtxFactory);

        try {
            locator.searchForUser("Ignored");
            fail("Expected exception for multiple search matches.");
        } catch (IncorrectResultSizeDataAccessException expected) {
        }
    }

    // Try some funny business with filters.

    public void testExtraFilterPartToExcludeBob() throws Exception {
        FilterBasedLdapUserSearch locator =
                new FilterBasedLdapUserSearch("ou=people",
                        "(&(cn=*)(!(|(uid={0})(uid=marissa))))",
                        dirCtxFactory);

        // Search for bob, get back ben...
        LdapUserDetails ben = locator.searchForUser("bob");
        String cn = (String)ben.getAttributes().get("cn").get();
        assertEquals("Ben Alex", cn);
//        assertEquals("uid=ben,ou=people,"+ROOT_DN, ben.getDn());
    }
}

@@ -967,33 +967,16 @@
 Search()
- throws Exception
  %7B%0A     
@@ -1106,68 +1106,8 @@
 y);%0A
-        LdapUserDetails bob = locator.searchForUser(%22bob%22);%0A
     
@@ -1182,16 +1182,118 @@
 mit(0);%0A
+        locator.setDerefLinkFlag(false);%0A%0A        LdapUserDetails bob = locator.searchForUser(%22bob%22);%0A
         
@@ -1370,33 +1370,48 @@
 b,ou=people,
-%22+ROOT_DN
+dc=acegisecurity,dc=org%22
 , bob.getDn(
@@ -1464,33 +1464,16 @@
 cceeds()
- throws Exception
  %7B%0A     
@@ -1803,22 +1803,37 @@
 ple,
-%22+ROOT_DN, bob
+dc=acegisecurity,dc=org%22, ben
 .get

Add call for setDerefLinkFlag