<commit_before>package org.synyx.urlaubsverwaltung.security;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.ldap.core.DirContextOperations;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


/**
 * @author  <NAME> - <EMAIL>
 */
public class LdapUserMapperTest {

    private static final String IDENTIFIER_ATTRIBUTE = "uid";
    private static final String FIRST_NAME_ATTRIBUTE = "givenName";
    private static final String LAST_NAME_ATTRIBUTE = "sn";
    private static final String MAIL_ADDRESS_ATTRIBUTE = "mail";
    private static final String MEMBER_OF_ATTRIBUTE = "memberOf";

    private static final String MEMBER_OF_FILTER = "CN=mygroup,DC=mydomain,DC=com";

    private LdapUserMapper ldapUserMapper;

    @Before
    public void setUp() {

        ldapUserMapper = new LdapUserMapper(IDENTIFIER_ATTRIBUTE, FIRST_NAME_ATTRIBUTE, LAST_NAME_ATTRIBUTE,
                MAIL_ADDRESS_ATTRIBUTE, MEMBER_OF_FILTER);
    }


    // Map user from attributes ----------------------------------------------------------------------------------------

    @Test
    public void ensureThrowsIfTryingToCreateLdapUserFromAttributesWithInvalidIdentifierAttribute()
        throws NamingException {

        Attributes attributes = mock(Attributes.class);
        when(attributes.get(IDENTIFIER_ATTRIBUTE)).thenReturn(null);

        try {
            ldapUserMapper.mapFromAttributes(attributes);
            Assert.fail("Should throw on empty username!");
        } catch (InvalidSecurityConfigurationException ex) {
            // Expected
        }

        verify(attributes, Mockito.atLeastOnce()).get(IDENTIFIER_ATTRIBUTE);
        verify(attributes, never()).get(FIRST_NAME_ATTRIBUTE);
        verify(attributes, never()).get(LAST_NAME_ATTRIBUTE);
        verify(attributes, never()).get(MAIL_ADDRESS_ATTRIBUTE);
    }


    @Test
    public void ensureCreatesLdapUserFromAttributesWithOnlyUsernameGiven() throws NamingException {

        Attributes attributes = mock(Attributes.class);
        when(attributes.get(IDENTIFIER_ATTRIBUTE))
            .thenReturn(new BasicAttribute(IDENTIFIER_ATTRIBUTE, "username"));
        when(attributes.get(FIRST_NAME_ATTRIBUTE)).thenReturn(null);
        when(attributes.get(LAST_NAME_ATTRIBUTE)).thenReturn(null);
        when(attributes.get(MAIL_ADDRESS_ATTRIBUTE)).thenReturn(null);

        LdapUser ldapUser = ldapUserMapper.mapFromAttributes(attributes);

        verify(attributes).get(IDENTIFIER_ATTRIBUTE);
        verify(attributes).get(FIRST_NAME_ATTRIBUTE);
        verify(attributes).get(LAST_NAME_ATTRIBUTE);
        verify(attributes).get(MAIL_ADDRESS_ATTRIBUTE);

        Assert.assertEquals("Wrong username", "username", ldapUser.getUsername());
        Assert.assertFalse("First name should be empty", ldapUser.getFirstName().isPresent());
        Assert.assertFalse("Last name should be empty", ldapUser.getLastName().isPresent());
        Assert.assertFalse("Email should be empty", ldapUser.getEmail().isPresent());
    }


    @Test
    public void ensureCreatesLdapUserFromAttributes() throws NamingException {

        Attributes attributes = mock(Attributes.class);
        when(attributes.get(IDENTIFIER_ATTRIBUTE))
            .thenReturn(new BasicAttribute(IDENTIFIER_ATTRIBUTE, "geralt"));
        when(attributes.get(FIRST_NAME_ATTRIBUTE))
            .thenReturn(new BasicAttribute(FIRST_NAME_ATTRIBUTE, "Geralt"));
        when(attributes.get(LAST_NAME_ATTRIBUTE))
            .thenReturn(new BasicAttribute(LAST_NAME_ATTRIBUTE, "<NAME>"));
        when(attributes.get(MAIL_ADDRESS_ATTRIBUTE))
            .thenReturn(new BasicAttribute(MAIL_ADDRESS_ATTRIBUTE, "<EMAIL>"));

        LdapUser ldapUser = ldapUserMapper.mapFromAttributes(attributes);

        verify(attributes).get(IDENTIFIER_ATTRIBUTE);
        verify(attributes).get(FIRST_NAME_ATTRIBUTE);
        verify(attributes).get(LAST_NAME_ATTRIBUTE);
        verify(attributes).get(MAIL_ADDRESS_ATTRIBUTE);

        Assert.assertEquals("Wrong username", "geralt", ldapUser.getUsername());
        Assert.assertEquals("Wrong first name", "Geralt", ldapUser.getFirstName().get());
        Assert.assertEquals("Wrong last name", "<NAME>", ldapUser.getLastName().get());
        Assert.assertEquals("Wrong email", "<EMAIL>", ldapUser.getEmail().get());
    }


    // Map user from context -------------------------------------------------------------------------------------------

    @Test
    public void ensureThrowsIfTryingToCreateLdapUserFromContextWithInvalidIdentifierAttribute() throws NamingException,
        UnsupportedMemberAffiliationException {

        DirContextOperations ctx = mock(DirContextOperations.class);
        when(ctx.getStringAttribute(IDENTIFIER_ATTRIBUTE)).thenReturn(null);

        try {
            ldapUserMapper.mapFromContext(ctx);
            Assert.fail("Should throw on empty username!");
        } catch (InvalidSecurityConfigurationException ex) {
            // Expected
        }

        verify(ctx, Mockito.atLeastOnce()).getStringAttribute(IDENTIFIER_ATTRIBUTE);
        verify(ctx, never()).getStringAttribute(LAST_NAME_ATTRIBUTE);
        verify(ctx, never()).getStringAttribute(FIRST_NAME_ATTRIBUTE);
        verify(ctx, never()).getStringAttribute(MAIL_ADDRESS_ATTRIBUTE);
    }


    @Test
    public void ensureCreatesLdapUserFromContext() throws NamingException, UnsupportedMemberAffiliationException {

        DirContextOperations ctx = mock(DirContextOperations.class);
        when(ctx.getStringAttribute(IDENTIFIER_ATTRIBUTE)).thenReturn("rick");
        when(ctx.getStringAttribute(FIRST_NAME_ATTRIBUTE)).thenReturn("Rick");
        when(ctx.getStringAttribute(LAST_NAME_ATTRIBUTE)).thenReturn("Grimes");
        when(ctx.getStringAttribute(MAIL_ADDRESS_ATTRIBUTE)).thenReturn("<EMAIL>");
        when(ctx.getStringAttributes(MEMBER_OF_ATTRIBUTE)).thenReturn(new String[] { MEMBER_OF_FILTER });

        LdapUser ldapUser = ldapUserMapper.mapFromContext(ctx);

        verify(ctx, Mockito.atLeastOnce()).getStringAttribute(IDENTIFIER_ATTRIBUTE);
        verify(ctx, Mockito.atLeastOnce()).getStringAttribute(LAST_NAME_ATTRIBUTE);
        verify(ctx, Mockito.atLeastOnce()).getStringAttribute(FIRST_NAME_ATTRIBUTE);
        verify(ctx, Mockito.atLeastOnce()).getStringAttribute(MAIL_ADDRESS_ATTRIBUTE);
<commit_msg>use static import for Mockito.atLeastOnce()<commit_after>package org.synyx.urlaubsverwaltung.security;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.ldap.core.DirContextOperations;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;

import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


/**
 * @author  <NAME> - <EMAIL>
 */
public class LdapUserMapperTest {

    private static final String IDENTIFIER_ATTRIBUTE = "uid";
    private static final String FIRST_NAME_ATTRIBUTE = "givenName";
    private static final String LAST_NAME_ATTRIBUTE = "sn";
    private static final String MAIL_ADDRESS_ATTRIBUTE = "mail";
    private static final String MEMBER_OF_ATTRIBUTE = "memberOf";

    private static final String MEMBER_OF_FILTER = "CN=mygroup,DC=mydomain,DC=com";

    private LdapUserMapper ldapUserMapper;

    @Before
    public void setUp() {

        ldapUserMapper = new LdapUserMapper(IDENTIFIER_ATTRIBUTE, FIRST_NAME_ATTRIBUTE, LAST_NAME_ATTRIBUTE,
                MAIL_ADDRESS_ATTRIBUTE, MEMBER_OF_FILTER);
    }


    // Map user from attributes ----------------------------------------------------------------------------------------

    @Test
    public void ensureThrowsIfTryingToCreateLdapUserFromAttributesWithInvalidIdentifierAttribute()
        throws NamingException {

        Attributes attributes = mock(Attributes.class);
        when(attributes.get(IDENTIFIER_ATTRIBUTE)).thenReturn(null);

        try {
            ldapUserMapper.mapFromAttributes(attributes);
            Assert.fail("Should throw on empty username!");
        } catch (InvalidSecurityConfigurationException ex) {
            // Expected
        }

        verify(attributes, atLeastOnce()).get(IDENTIFIER_ATTRIBUTE);
        verify(attributes, never()).get(FIRST_NAME_ATTRIBUTE);
        verify(attributes, never()).get(LAST_NAME_ATTRIBUTE);
        verify(attributes, never()).get(MAIL_ADDRESS_ATTRIBUTE);
    }


    @Test
    public void ensureCreatesLdapUserFromAttributesWithOnlyUsernameGiven() throws NamingException {

        Attributes attributes = mock(Attributes.class);
        when(attributes.get(IDENTIFIER_ATTRIBUTE))
            .thenReturn(new BasicAttribute(IDENTIFIER_ATTRIBUTE, "username"));
        when(attributes.get(FIRST_NAME_ATTRIBUTE)).thenReturn(null);
        when(attributes.get(LAST_NAME_ATTRIBUTE)).thenReturn(null);
        when(attributes.get(MAIL_ADDRESS_ATTRIBUTE)).thenReturn(null);

        LdapUser ldapUser = ldapUserMapper.mapFromAttributes(attributes);

        verify(attributes).get(IDENTIFIER_ATTRIBUTE);
        verify(attributes).get(FIRST_NAME_ATTRIBUTE);
        verify(attributes).get(LAST_NAME_ATTRIBUTE);
        verify(attributes).get(MAIL_ADDRESS_ATTRIBUTE);

        Assert.assertEquals("Wrong username", "username", ldapUser.getUsername());
        Assert.assertFalse("First name should be empty", ldapUser.getFirstName().isPresent());
        Assert.assertFalse("Last name should be empty", ldapUser.getLastName().isPresent());
        Assert.assertFalse("Email should be empty", ldapUser.getEmail().isPresent());
    }


    @Test
    public void ensureCreatesLdapUserFromAttributes() throws NamingException {

        Attributes attributes = mock(Attributes.class);
        when(attributes.get(IDENTIFIER_ATTRIBUTE))
            .thenReturn(new BasicAttribute(IDENTIFIER_ATTRIBUTE, "geralt"));
        when(attributes.get(FIRST_NAME_ATTRIBUTE))
            .thenReturn(new BasicAttribute(FIRST_NAME_ATTRIBUTE, "Geralt"));
        when(attributes.get(LAST_NAME_ATTRIBUTE))
            .thenReturn(new BasicAttribute(LAST_NAME_ATTRIBUTE, "<NAME>"));
        when(attributes.get(MAIL_ADDRESS_ATTRIBUTE))
            .thenReturn(new BasicAttribute(MAIL_ADDRESS_ATTRIBUTE, "<EMAIL>"));

        LdapUser ldapUser = ldapUserMapper.mapFromAttributes(attributes);

        verify(attributes).get(IDENTIFIER_ATTRIBUTE);
        verify(attributes).get(FIRST_NAME_ATTRIBUTE);
        verify(attributes).get(LAST_NAME_ATTRIBUTE);
        verify(attributes).get(MAIL_ADDRESS_ATTRIBUTE);

        Assert.assertEquals("Wrong username", "geralt", ldapUser.getUsername());
        Assert.assertEquals("Wrong first name", "Geralt", ldapUser.getFirstName().get());
        Assert.assertEquals("Wrong last name", "<NAME>", ldapUser.getLastName().get());
        Assert.assertEquals("Wrong email", "<EMAIL>", ldapUser.getEmail().get());
    }


    // Map user from context -------------------------------------------------------------------------------------------

    @Test
    public void ensureThrowsIfTryingToCreateLdapUserFromContextWithInvalidIdentifierAttribute() throws NamingException,
        UnsupportedMemberAffiliationException {

        DirContextOperations ctx = mock(DirContextOperations.class);
        when(ctx.getStringAttribute(IDENTIFIER_ATTRIBUTE)).thenReturn(null);

        try {
            ldapUserMapper.mapFromContext(ctx);
            Assert.fail("Should throw on empty username!");
        } catch (InvalidSecurityConfigurationException ex) {
            // Expected
        }

        verify(ctx, atLeastOnce()).getStringAttribute(IDENTIFIER_ATTRIBUTE);
        verify(ctx, never()).getStringAttribute(LAST_NAME_ATTRIBUTE);
        verify(ctx, never()).getStringAttribute(FIRST_NAME_ATTRIBUTE);
        verify(ctx, never()).getStringAttribute(MAIL_ADDRESS_ATTRIBUTE);
    }


    @Test
    public void ensureCreatesLdapUserFromContext() throws NamingException, UnsupportedMemberAffiliationException {

        DirContextOperations ctx = mock(DirContextOperations.class);
        when(ctx.getStringAttribute(IDENTIFIER_ATTRIBUTE)).thenReturn("rick");
        when(ctx.getStringAttribute(FIRST_NAME_ATTRIBUTE)).thenReturn("Rick");
        when(ctx.getStringAttribute(LAST_NAME_ATTRIBUTE)).thenReturn("Grimes");
        when(ctx.getStringAttribute(MAIL_ADDRESS_ATTRIBUTE)).thenReturn("<EMAIL>");
        when(ctx.getStringAttributes(MEMBER_OF_ATTRIBUTE)).thenReturn(new String[] { MEMBER_OF_FILTER });

        LdapUser ldapUser = ldapUserMapper.mapFromContext(ctx);

        verify(ctx, atLeastOnce()).getStringAttribute(IDENTIFIER_ATTRIBUTE);
        verify(ctx, atLeastOnce()).getStringAttribute(LAST_NAME_ATTRIBUTE);
        verify(ctx, atLeastOnce()).getStringAttribute(FIRST_NAME_ATTRIBUTE);
        verify(ctx, atLeastOnce()).getStringAttribute(MAIL_ADDRESS_ATTRIBUTE);

{'fraction_non_alphanumeric': 0.12821479749604042, 'fraction_numerical': 0.0, 'mean_word_length': 3.6954674220963173, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3111915', 'n_tokens_mistral': 3580, 'n_tokens_neox': 3617, 'n_words': 555}