package net.hackergarten.ldapauth;

import java.util.Map;

import junit.framework.TestCase;

/**
 * Implement some basic test using the public ldap from forumsys.
 *
 * see http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/
 */
public class LdapAuthenticatorTest extends TestCase {

    public void testGetUid() throws Exception {
        LdapAuthenticator ldapAuthenticator = new LdapAuthenticator("dc=example,dc=com", "ldap://ldap.forumsys.com:389/");
        ldapAuthenticator.setUidProperty("cn");
        String uid = ldapAuthenticator.getUid("read-only-admin");
        assertEquals("cn=read-only-admin,dc=example,dc=com",uid);
    }

    public void testGetUidForUnknownUserReturnsNull() throws Exception {
        LdapAuthenticator ldapAuthenticator = new LdapAuthenticator("dc=example,dc=com", "ldap://ldap.forumsys.com:389/");
        ldapAuthenticator.setUidProperty("cn");
        String uid = ldapAuthenticator.getUid("non-existent-user");
        assertNull(uid);
    }

    public void testBind() throws Exception {
        LdapAuthenticator ldapAuthenticator = new LdapAuthenticator("dc=example,dc=com", "ldap://ldap.forumsys.com:389/");
        boolean authenticated = ldapAuthenticator.testBind("cn=read-only-admin,dc=example,dc=com", "password");
        assertTrue(authenticated);
    }

    public void testBindFailsOnInvalidPassword() throws Exception {
        LdapAuthenticator ldapAuthenticator = new LdapAuthenticator("dc=example,dc=com", "ldap://ldap.forumsys.com:389/");
        boolean authenticated = ldapAuthenticator.testBind("cn=read-only-admin,dc=example,dc=com", "wrongPassword");
        assertFalse(authenticated);
    }

    public void testSearch() throws Exception{
        LdapAuthenticator ldapAuthenticator = new LdapAuthenticator("dc=example,dc=com", "ldap://ldap.forumsys.com:389/");
        ldapAuthenticator.setUidProperty("uid");
        ldapAuthenticator.setSearchAttributes("uid");
        Map<String, String> result = ldapAuthenticator.search("riemann");
        assertNotNull(result);
        assertEquals(1,result.size());
        assertEquals("riemann", ((Map.Entry) result.entrySet().toArray()[0]).getValue());
    }

    public void testSearchForUnknownUserReturnsNull() throws Exception {
        LdapAuthenticator ldapAuthenticator = new LdapAuthenticator("dc=example,dc=com", "ldap://ldap.forumsys.com:389/");
        ldapAuthenticator.setUidProperty("cn");
        Map<String, String> result = ldapAuthenticator.search("non-existent-user");
        assertNull(result);
    }

    public void testSearchForUserWithWrongAttrsReturnsEmptyMap() throws Exception {
        LdapAuthenticator ldapAuthenticator = new LdapAuthenticator("dc=example,dc=com", "ldap://ldap.forumsys.com:389/");
        ldapAuthenticator.setUidProperty("uid");
        ldapAuthenticator.setSearchAttributes("unknown,sample");
        Map<String, String> result = ldapAuthenticator.search("riemann");
        assertEquals(0,result.size());
    }
}

@@ -1949,28 +1949,39 @@
 ributes(%22uid
+,cn,sn,mail
 %22);%0A
-
         Map%3C
@@ -2098,9 +2098,9 @@
 als(
-1
+4
 ,res
@@ -2198,24 +2198,321 @@
 etValue());%0A
+        assertEquals(%22riemann@ldap.forumsys.com%22, ((Map.Entry) result.entrySet().toArray()%5B1%5D).getValue());%0A        assertEquals(%22Riemann%22, ((Map.Entry) result.entrySet().toArray()%5B2%5D).getValue());%0A        assertEquals(%22Bernhard Riemann%22, ((Map.Entry) result.entrySet().toArray()%5B3%5D).getValue());%0A
     %7D%0A%0A    p

extend test for search with some additional attributes