def describe_client(): client = Client() client.conn = MagicMock() client.basedn = 'dc=test,dc=org' distinguished_name = 'cn=test.user,ou=People,dc=test,dc=org' object_class = ['inetOrgPerson'] attributes = {'givenName': 'Test', 'sn': 'User'} def it_adds_object_to_ldap(): client.conn.reset_mock() client.add(distinguished_name, object_class, attributes) client.conn.add.assert_called_once_with(distinguished_name, object_class, attributes) def describe_client_search(): client.conn.search = MagicMock() def it_searches_with_default_filter_and_attributes(): pytest.xfail(reason='ideopathic unreliable results') search_filter = ["(objectclass=*)"] filterstr = "(&{})".format(''.join(search_filter)) attributes = ['*'] client.conn.reset_mock() client.conn.search.reset_mock() client.search(None) client.conn.search.assert_any_call(search_base=client.basedn, search_filter=filterstr, search_scope=ldap3.SUBTREE, attributes=attributes) def it_searches_with_default_attributes_and_custom_filter(): pytest.xfail(reason='ideopathic unreliable results') search_filter = ["(objectclass=top)"] filterstr = "(&{})".format(''.join(search_filter)) attributes = ['*'] client.conn.reset_mock() client.conn.search.reset_mock() client.search(search_filter) client.conn.search.assert_any_call(search_base=client.basedn, search_filter=filterstr, search_scope=ldap3.SUBTREE, attributes=attributes) def it_searches_with_default_filter_and_custom_attributes(): pytest.xfail(reason='ideopathic unreliable results') search_filter = ["(objectclass=*)"] filterstr = "(&{})".format(''.join(search_filter)) attributes = ['uid', 'gid'] client.conn.reset_mock() client.conn.search.reset_mock() client.search(None, attributes) client.conn.search.assert_any_call(search_base=client.basedn, search_filter=filterstr, search_scope=ldap3.SUBTREE, attributes=attributes) def describe_get_max_id(): def it_gets_id_of_service_user(): client.search = MagicMock(return_value=[]) assert client.get_max_id('user', 'service') == 20000 def it_gets_id_of_normal_user(): client.search = MagicMock(return_value=[]) assert client.get_max_id('user', 'user') == 10000 def it_raises_error_on_bad_user_type(): client.search = MagicMock(return_value=[]) with pytest.raises(ldap_tools.exceptions.InvalidResult, message=("Unknown object type")): client.get_max_id('user', 'foo')
def describe_group(): # TODO: change this to use test_key.py format group_name = 'testGroup' group_type = 'user' username = '******' ldap_attributes = {'cn': b'testGroup', 'gidnumber': 99999} group_objectclass = ["(objectclass=posixGroup)"] runner = CliRunner() client = Client() client.prepare_connection = MagicMock() client.load_ldap_config = MagicMock() client.prepare_connection() client.basedn = 'dc=test,dc=org' client.server = ldap3.Server('my_fake_server') client.conn = ldap3.Connection(client.server, user='******'.format( client.basedn), password='******', client_strategy=ldap3.MOCK_SYNC) def describe_creates_group(): def describe_commandline(): def it_calls_the_api(mocker): # noqa: F811 mocker.patch('ldap_tools.group.API.create', return_value=None) mocker.patch('ldap_tools.client.Client.prepare_connection', return_value=None) runner.invoke( GroupCli.group, ['create', '--group', group_name, '--type', group_type]) ldap_tools.group.API.create.assert_called_once_with( group_name, group_type) def it_raises_exception_on_bad_type(mocker): # noqa: F811 mocker.patch('ldap_tools.group.API.create', return_value=None) mocker.patch('ldap_tools.client.Client.prepare_connection', return_value=None) runner.invoke( GroupCli.group, ['create', '--group', group_name, '--type', group_type]) assert pytest.raises(click.BadOptionUsage) def describe_api(): def it_calls_the_client(): client.add = MagicMock() client.get_max_id = MagicMock(return_value=99999) group_objectclass = ['top', 'posixGroup'] group_api = GroupApi(client) # mocker.patch.object( # group_api, '_API__ldap_attr', return_value=ldap_attributes) group_api.create(group_name, group_type) client.add.assert_called_once_with( 'cn={},ou=Group,{}'.format(group_name, client.basedn), group_objectclass, ldap_attributes) def describe_deletes_group(): def describe_commandline(): group_api = GroupApi(client) group_api.delete = MagicMock() def it_calls_the_api(mocker): # noqa: F811 mocker.patch('ldap_tools.group.API.delete', return_value=None) mocker.patch('ldap_tools.client.Client.prepare_connection', return_value=None) # We have to force here, otherwise, we get stuck # on an interactive prompt runner.invoke(GroupCli.group, ['delete', '--group', group_name, '--force']) ldap_tools.group.API.delete.assert_called_once_with(group_name) def it_exits_on_no_force_no_confirm(mocker): # noqa: F811 mocker.patch('ldap_tools.group.API.delete', return_value=None) mocker.patch('ldap_tools.client.Client.prepare_connection', return_value=None) mocker.patch('click.confirm', return_value=False) # We have to force here, otherwise, we get stuck # on an interactive prompt result = runner.invoke(GroupCli.group, ['delete', '--group', group_name]) assert result.output == "Deletion of {} aborted\n".format( group_name) def describe_api(): def it_calls_the_client(): client.delete = MagicMock() group_api = GroupApi(client) group_api.delete(group_name) client.delete.assert_called_once_with( 'cn={},ou=Group,{}'.format(group_name, client.basedn)) def describe_adds_user(): def describe_commandline(): def it_calls_the_api(mocker): # noqa: F811 mocker.patch('ldap_tools.group.API.add_user', return_value=None) mocker.patch('ldap_tools.client.Client.prepare_connection', return_value=None) group_api = GroupApi(client) group_api.add_user = MagicMock() runner.invoke(GroupCli.group, [ 'add_user', '--group', group_name, '--username', username ]) ldap_tools.group.API.add_user.assert_called_once_with( group_name, username) def describe_api(): def it_calls_the_client(mocker): # noqa: F811 client.modify = MagicMock() group_api = GroupApi(client) group_api.lookup_id = MagicMock(return_value=99999) group_api.add_user(group_name, username) client.modify.assert_called_once_with( 'cn={},ou=Group,{}'.format(group_name, client.basedn), {'memberUid': [(ldap3.MODIFY_ADD, [username])]}) def describe_removes_user(): def describe_commandline(): def it_calls_the_api(mocker): # noqa: F811 mocker.patch('ldap_tools.group.API.remove_user', return_value=None) mocker.patch('ldap_tools.client.Client.prepare_connection', return_value=None) group_api = GroupApi(client) group_api.remove_user = MagicMock() runner.invoke(GroupCli.group, [ 'remove_user', '--group', group_name, '--username', username ]) ldap_tools.group.API.remove_user.assert_called_once_with( group_name, username) def describe_api(): def it_calls_the_client(): client.modify = MagicMock() group_api = GroupApi(client) group_api.lookup_id = MagicMock(return_value=99999) group_api.remove_user(group_name, username) client.modify.assert_called_once_with( 'cn={},ou=Group,{}'.format(group_name, client.basedn), {'memberUid': [(ldap3.MODIFY_DELETE, [username])]}) def describe_indexes_groups(): def describe_commandline(): def it_calls_the_api(mocker): # noqa: F811 mocker.patch('ldap_tools.group.API.index', return_value=None) mocker.patch('ldap_tools.client.Client.prepare_connection', return_value=None) group_api = GroupApi(client) group_api.index = MagicMock() runner.invoke(GroupCli.group, ['index']) ldap_tools.group.API.index.assert_called_once_with() def describe_api(): def it_calls_the_client(): client.search = MagicMock() # client.prepare_connection = MagicMock() # client.load_ldap_config = MagicMock() group_api = GroupApi(client) group_api.index() client.search.assert_called_once_with(group_objectclass) def describe_lookup_id(): def describe_commandline(): pass # not implemented def describe_api(): def it_searches_with_correct_filter(): pytest.xfail( reason='Need to learn how to mock list.gidNumber.value') client.search = MagicMock(return_value=['foo']) group_api = GroupApi(client) group_api.lookup_id(group_name) search_filter = [ "(cn={})".format(group_name), "(objectclass=posixGroup)" ] client.search.assert_called_once_with(search_filter, ['gidNumber']) def it_raises_exception_on_no_groups_found(): client.search = MagicMock() group_api = GroupApi(client) with pytest.raises(ldap_tools.exceptions.NoGroupsFound, message=("No Groups Returned by LDAP.")): group_api.lookup_id(group_name) def it_raises_exception_on_multiple_results(): client.search = MagicMock(return_value=['foo', 'bar']) group_api = GroupApi(client) with pytest.raises( ldap_tools.exceptions.TooManyResults, message=( "Multiple groups found. Please narrow your search." )): group_api.lookup_id(group_name)