<commit_before>import mock
import csv
import textwrap
import StringIO
from ldap.ldapobject import LDAPObject
from aedash_connector.input import from_csv, from_ldap


def test_csv_input():
    """
    Test CSV Input

    Provide test input data via StringIO
    :return: None
    """
    test_input = StringIO.StringIO(
        textwrap.dedent("""\
        firstname	lastname	email	groups
        Test	User1	<EMAIL>	group1,group2\n"""))

    users = from_csv(csv.DictReader(test_input, delimiter='\t'))
    assert users.next() == {'firstname': 'Test', 'lastname': 'User1', 'disable': False,
                            'email': '<EMAIL>', 'groups': ['group1', 'group2']}


def test_ldap_input():
    """
    Test LDAP Input

    Create a mock ldap query function to provide test data
    :return: None
    """

    def mock_search(*args, **kwargs):
        return [(None, {
            'givenName': ['Test'],
            'sn': ['User1'],
            'sAMAccountName': ['user'],
            'memberOf': ['CN=group1,', 'CN=group2,'],
        })]

    con = mock.create_autospec(LDAPObject)
    con.search_s = mock_search
    users = from_ldap(con, "example.com")
    assert users.next() == {'firstname': 'Test', 'lastname': 'User1', 'disable': False,
                            'email': '<EMAIL>', 'groups': ['group1', 'group2']}<commit_msg>add country to test cases (and update ldap test case to reflect group-based queries)<commit_after>import mock
import csv
import textwrap
import StringIO
from ldap.ldapobject import LDAPObject
from aedash_connector.input import from_csv, from_ldap


def test_csv_input():
    """
    Test CSV Input

    Provide test input data via StringIO
    :return: None
    """
    test_input = StringIO.StringIO(
        textwrap.dedent("""\
        firstname	lastname	email	groups	country
        Test	User1	<EMAIL>	group1,group2	US\n"""))

    users = from_csv(csv.DictReader(test_input, delimiter='\t'))
    assert users[0] == {'firstname': 'Test', 'lastname': 'User1', 'disable': False,
                        'email': '<EMAIL>', 'groups': ['group1', 'group2'], 'country': 'US'}


def test_ldap_input():
    """
    Test LDAP Input

    Create a mock ldap query function to provide test data
    :return: None
    """

    def mock_search_s(*args, **kwargs):
        return [(None, {'distinguishedName': ['CN=group1,CN=Users,DC=example,DC=com']})]

    def mock_search_ext(*args, **kwargs):
        return 0

    def mock_result3(*args, **kwargs):
        rtype = None
        rmsgid = None
        serverctrls = []
        rdata = [(None, {
            'givenName': ['Test'],
            'sn': ['User1'],
            'sAMAccountName': ['user'],
            'memberOf': ['CN=group1,', 'CN=group2,'],
            'c': ['US'],
        })]
        return rtype, rdata, rmsgid, serverctrls

    con = mock.create_autospec(LDAPObject)
    con.search_s = mock_search_s
    con.search_ext = mock_search_ext
    con.result3 = mock_result3
    users = from_ldap(con, "example.com", ['group1'], "DC=example,DC=com", "(objectClass=user)")
    assert users[0] == {'firstname': 'Test', 'lastname': 'User1', 'disable': False,
                        'email': '<EMAIL>', 'groups': ['group1', 'group2'], 'country': 'US'}
{'fraction_non_alphanumeric': 0.15322829780661107, 'fraction_numerical': 0.009885696632684585, 'mean_word_length': 2.8319526627218936, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3237985', 'n_tokens_mistral': 1060, 'n_tokens_neox': 1026, 'n_words': 265}