From 67ccdbb7c8b00c648c0d68b4dcad851f9e6dfd5e Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Mon, 1 Feb 2016 23:15:57 +0000 Subject: [PATCH] Add tests for ldaptasks functions This commit adds tests for ldaptasks.set_ldap_user_password, ldaptasks.add_ldap_user_to_group and ldaptasks.remove_ldap_user_from_group. --- gvaldap/ldaptasks/tests/test_tasks.py | 151 +++++++++++++++++++------- 1 file changed, 113 insertions(+), 38 deletions(-) diff --git a/gvaldap/ldaptasks/tests/test_tasks.py b/gvaldap/ldaptasks/tests/test_tasks.py index 3b601f0..125b6a5 100644 --- a/gvaldap/ldaptasks/tests/test_tasks.py +++ b/gvaldap/ldaptasks/tests/test_tasks.py @@ -10,7 +10,14 @@ from celery.exceptions import Reject from mockldap import MockLdap -from ldaptasks.tasks import create_ldap_group, create_ldap_user +from ldapentities.models import LdapUser +from ldaptasks.tasks import ( + add_ldap_user_to_group, + create_ldap_group, + create_ldap_user, + remove_ldap_user_from_group, + set_ldap_user_password, +) class LdapTaskTestCase(TestCase): @@ -65,42 +72,35 @@ class LdapTaskTestCase(TestCase): def test_create_ldap_group(self): dn = create_ldap_group('test', 5000, 'test group') self.assertEqual('cn=test,%s' % settings.GROUP_BASE_DN, dn) - self.assertEquals( - self.ldapobj.methods_called(), - ['initialize', 'simple_bind_s', 'search_s', 'add_s'] - ) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'add_s' + ]) def test_create_ldap_group_existing(self): dn = create_ldap_group('existing', 4711, 'existing test group') self.assertEqual('cn=existing,%s' % settings.GROUP_BASE_DN, dn) - self.assertEquals( - self.ldapobj.methods_called(), [ - 'initialize', 'simple_bind_s', 'search_s', 'search_s', - 'search_s', 'search_s' - ] - ) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s', + 'search_s' + ]) def test_create_ldap_group_existing_modify(self): dn = create_ldap_group('existing', 4711, 'change existing test group') self.assertEqual('cn=existing,%s' % settings.GROUP_BASE_DN, dn) - self.assertEquals( - self.ldapobj.methods_called(), [ - 'initialize', 'simple_bind_s', 'search_s', 'search_s', - 'search_s', 'search_s', 'modify_s' - ] - ) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s', + 'search_s', 'search_s', 'modify_s' + ]) def test_create_ldap_user(self): dn = create_ldap_user( 'test', 5000, 4711, 'Test User', '/home/test', '/bin/bash', 'secret') self.assertEqual('uid=test,%s' % settings.USER_BASE_DN, dn) - self.assertEquals( - self.ldapobj.methods_called(), [ - 'initialize', 'simple_bind_s', 'search_s', 'search_s', - 'search_s', 'add_s', 'search_s', 'search_s', 'modify_s' - ] - ) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s', + 'add_s', 'search_s', 'search_s', 'modify_s' + ]) def test_create_ldap_user_invalid_group(self): with self.assertRaises(Reject): @@ -108,21 +108,18 @@ class LdapTaskTestCase(TestCase): 'test', 5000, 5000, 'Test User', '/home/test', '/bin/bash', 'secret') self.assertEqual(self.ldapobj.methods_called(), [ - 'initialize', 'simple_bind_s', 'search_s', 'search_s' - ] - ) + 'initialize', 'simple_bind_s', 'search_s', 'search_s' + ]) def test_create_ldap_user_no_password(self): dn = create_ldap_user( 'test', 5000, 4711, 'Test User', '/home/test', '/bin/bash', None) self.assertEqual('uid=test,%s' % settings.USER_BASE_DN, dn) - self.assertEquals( - self.ldapobj.methods_called(), [ - 'initialize', 'simple_bind_s', 'search_s', 'search_s', - 'search_s', 'add_s', 'search_s', 'search_s', 'modify_s' - ] - ) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s', + 'add_s', 'search_s', 'search_s', 'modify_s' + ]) def test_create_ldap_user_existing(self): dn = create_ldap_user( @@ -130,9 +127,87 @@ class LdapTaskTestCase(TestCase): '/bin/bash', 'secret' ) self.assertEqual('uid=existing,%s' % settings.USER_BASE_DN, dn) - self.assertEquals( - self.ldapobj.methods_called(), [ - 'initialize', 'simple_bind_s', 'search_s', 'search_s', - 'search_s', 'search_s', 'search_s', 'search_s', 'modify_s' - ] - ) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s', + 'search_s', 'search_s', 'search_s', 'modify_s' + ]) + + def test_set_ldap_user_password_existing(self): + res = set_ldap_user_password('existing', 'newpassword') + self.assertTrue(res) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s', + 'search_s', 'modify_s' + ]) + + def test_set_ldap_user_password_missing(self): + res = set_ldap_user_password('missing', 'newpassword') + self.assertFalse(res) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s' + ]) + + def test_add_ldap_user_to_group_existing(self): + res = add_ldap_user_to_group('existing', 'existing') + self.assertTrue(res) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s', + 'search_s' + ]) + + def test_add_ldap_user_to_group_new_user(self): + create_ldap_group('test', 5000, 'test group') + res = add_ldap_user_to_group('existing', 'test') + self.assertTrue(res) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'add_s', 'search_s', + 'search_s', 'search_s', 'search_s', 'search_s', 'search_s', + 'modify_s' + ]) + + def test_add_ldap_user_to_group_no_group(self): + res = add_ldap_user_to_group('existing', 'test') + self.assertFalse(res) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s' + ]) + + def test_add_ldap_user_to_group_no_user(self): + with self.assertRaises(LdapUser.DoesNotExist): + add_ldap_user_to_group('test', 'existing') + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s' + ]) + + def test_remove_ldap_user_from_group_existing(self): + res = remove_ldap_user_from_group('existing', 'existing') + self.assertTrue(res) + 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' + ]) + + def test_remove_ldap_user_from_group_not_in_group(self): + create_ldap_group('test', 5000, 'test group') + res = remove_ldap_user_from_group('existing', 'test') + self.assertFalse(res) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'add_s', 'search_s', + 'search_s', 'search_s', 'search_s' + ]) + + def test_remove_ldap_user_from_group_no_group(self): + res = remove_ldap_user_from_group('existing', 'test') + self.assertFalse(res) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s' + ]) + + def test_remove_ldap_user_from_group_no_user(self): + res = remove_ldap_user_from_group('test', 'existing') + self.assertFalse(res) + self.assertEqual(self.ldapobj.methods_called(), [ + 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s' + ])