<commit_before>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);
    }
}
<commit_msg>add test for wrong attributes<commit_after>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());
    }
}

{'fraction_non_alphanumeric': 0.1286953443087272, 'fraction_numerical': 0.007788989201628607, 'mean_word_length': 4.290262172284645, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1115996', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1755, 'n_words': 261}