From 94aacf1398cfaf3c5c176800e665898e88ce3aa1 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Tue, 2 Feb 2016 18:31:37 +0000 Subject: [PATCH] Add tests for delete tasks This commit raises coverage for ldaptasks.tasks to 100% by adding tests for delete_ldap_group, delete_ldap_group_if_empty and delete_ldap_user tasks. --- gvaldap/ldaptasks/tests/test_tasks.py | 89 +++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/gvaldap/ldaptasks/tests/test_tasks.py b/gvaldap/ldaptasks/tests/test_tasks.py index 125b6a5..330604f 100644 --- a/gvaldap/ldaptasks/tests/test_tasks.py +++ b/gvaldap/ldaptasks/tests/test_tasks.py @@ -15,6 +15,9 @@ from ldaptasks.tasks import ( add_ldap_user_to_group, create_ldap_group, create_ldap_user, + delete_ldap_group, + delete_ldap_group_if_empty, + delete_ldap_user, remove_ldap_user_from_group, set_ldap_user_password, ) @@ -211,3 +214,89 @@ class LdapTaskTestCase(TestCase): self.assertEqual(self.ldapobj.methods_called(), [ 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s' ]) + + def test_delete_ldap_user_existing(self): + res = delete_ldap_user('existing') + self.assertTrue(res) + self.assertNotIn( + 'uid=existing,' + settings.USER_BASE_DN, self.ldapobj.directory) + self.assertNotIn('memberUid', self.ldapobj.directory[ + 'cn=existing,' + settings.GROUP_BASE_DN]) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s', + 'search_s', 'search_s', 'search_s', 'modify_s', 'delete_s' + ]) + + def test_delete_ldap_user_missing(self): + res = delete_ldap_user('missing') + self.assertFalse(res) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s' + ]) + + def test_delete_ldap_user_no_group(self): + self.ldapobj.directory[ + 'uid=existing,' + settings.USER_BASE_DN]['gidNumber'] = ['5000'] + res = delete_ldap_user('existing') + self.assertTrue(res) + self.assertNotIn( + 'uid=existing,' + settings.USER_BASE_DN, self.ldapobj.directory) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s', + 'delete_s' + ]) + + def test_delete_ldap_user_not_in_group(self): + del self.ldapobj.directory[ + 'cn=existing,' + settings.GROUP_BASE_DN]['memberUid'] + res = delete_ldap_user('existing') + self.assertTrue(res) + self.assertNotIn( + 'uid=existing,' + settings.USER_BASE_DN, self.ldapobj.directory) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s', + 'search_s', 'delete_s' + ]) + + def test_delete_ldap_group_if_empty_nonempty(self): + res = delete_ldap_group_if_empty('existing') + self.assertFalse(res) + self.assertIn( + 'cn=existing,' + settings.GROUP_BASE_DN, self.ldapobj.directory) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s' + ]) + + def test_delete_ldap_group_if_empty_missing(self): + res = delete_ldap_group_if_empty('missing') + self.assertFalse(res) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s' + ]) + + def test_delete_ldap_group_if_empty_empty(self): + del self.ldapobj.directory[ + 'cn=existing,' + settings.GROUP_BASE_DN]['memberUid'] + res = delete_ldap_group_if_empty('existing') + self.assertTrue(res) + self.assertNotIn( + 'cn=existing,' + settings.GROUP_BASE_DN, self.ldapobj.directory) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'delete_s' + ]) + + def test_delete_ldap_group_existing(self): + res = delete_ldap_group('existing') + self.assertTrue(res) + self.assertNotIn( + 'cn=existing,' + settings.GROUP_BASE_DN, self.ldapobj.directory) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'delete_s' + ]) + + def test_delete_ldap_group_missing(self): + res = delete_ldap_group('missing') + self.assertFalse(res) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s' + ])