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.
This commit is contained in:
Jan Dittberner 2016-02-01 23:15:57 +00:00
parent 23015af083
commit 67ccdbb7c8

View file

@ -10,7 +10,14 @@ from celery.exceptions import Reject
from mockldap import MockLdap 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): class LdapTaskTestCase(TestCase):
@ -65,42 +72,35 @@ class LdapTaskTestCase(TestCase):
def test_create_ldap_group(self): def test_create_ldap_group(self):
dn = create_ldap_group('test', 5000, 'test group') dn = create_ldap_group('test', 5000, 'test group')
self.assertEqual('cn=test,%s' % settings.GROUP_BASE_DN, dn) self.assertEqual('cn=test,%s' % settings.GROUP_BASE_DN, dn)
self.assertEquals( self.assertEqual(self.ldapobj.methods_called(), [
self.ldapobj.methods_called(), 'initialize', 'simple_bind_s', 'search_s', 'add_s'
['initialize', 'simple_bind_s', 'search_s', 'add_s'] ])
)
def test_create_ldap_group_existing(self): def test_create_ldap_group_existing(self):
dn = create_ldap_group('existing', 4711, 'existing test group') dn = create_ldap_group('existing', 4711, 'existing test group')
self.assertEqual('cn=existing,%s' % settings.GROUP_BASE_DN, dn) self.assertEqual('cn=existing,%s' % settings.GROUP_BASE_DN, dn)
self.assertEquals( self.assertEqual(self.ldapobj.methods_called(), [
self.ldapobj.methods_called(), [ 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s',
'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s'
'search_s', 'search_s' ])
]
)
def test_create_ldap_group_existing_modify(self): def test_create_ldap_group_existing_modify(self):
dn = create_ldap_group('existing', 4711, 'change existing test group') dn = create_ldap_group('existing', 4711, 'change existing test group')
self.assertEqual('cn=existing,%s' % settings.GROUP_BASE_DN, dn) self.assertEqual('cn=existing,%s' % settings.GROUP_BASE_DN, dn)
self.assertEquals( self.assertEqual(self.ldapobj.methods_called(), [
self.ldapobj.methods_called(), [
'initialize', 'simple_bind_s', 'search_s', 'search_s', 'initialize', 'simple_bind_s', 'search_s', 'search_s',
'search_s', 'search_s', 'modify_s' 'search_s', 'search_s', 'modify_s'
] ])
)
def test_create_ldap_user(self): def test_create_ldap_user(self):
dn = create_ldap_user( dn = create_ldap_user(
'test', 5000, 4711, 'Test User', '/home/test', '/bin/bash', 'test', 5000, 4711, 'Test User', '/home/test', '/bin/bash',
'secret') 'secret')
self.assertEqual('uid=test,%s' % settings.USER_BASE_DN, dn) self.assertEqual('uid=test,%s' % settings.USER_BASE_DN, dn)
self.assertEquals( self.assertEqual(self.ldapobj.methods_called(), [
self.ldapobj.methods_called(), [ 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s',
'initialize', 'simple_bind_s', 'search_s', 'search_s', 'add_s', 'search_s', 'search_s', 'modify_s'
'search_s', 'add_s', 'search_s', 'search_s', 'modify_s' ])
]
)
def test_create_ldap_user_invalid_group(self): def test_create_ldap_user_invalid_group(self):
with self.assertRaises(Reject): with self.assertRaises(Reject):
@ -109,20 +109,17 @@ class LdapTaskTestCase(TestCase):
'secret') 'secret')
self.assertEqual(self.ldapobj.methods_called(), [ 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): def test_create_ldap_user_no_password(self):
dn = create_ldap_user( dn = create_ldap_user(
'test', 5000, 4711, 'Test User', '/home/test', '/bin/bash', 'test', 5000, 4711, 'Test User', '/home/test', '/bin/bash',
None) None)
self.assertEqual('uid=test,%s' % settings.USER_BASE_DN, dn) self.assertEqual('uid=test,%s' % settings.USER_BASE_DN, dn)
self.assertEquals( self.assertEqual(self.ldapobj.methods_called(), [
self.ldapobj.methods_called(), [ 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s',
'initialize', 'simple_bind_s', 'search_s', 'search_s', 'add_s', 'search_s', 'search_s', 'modify_s'
'search_s', 'add_s', 'search_s', 'search_s', 'modify_s' ])
]
)
def test_create_ldap_user_existing(self): def test_create_ldap_user_existing(self):
dn = create_ldap_user( dn = create_ldap_user(
@ -130,9 +127,87 @@ class LdapTaskTestCase(TestCase):
'/bin/bash', 'secret' '/bin/bash', 'secret'
) )
self.assertEqual('uid=existing,%s' % settings.USER_BASE_DN, dn) self.assertEqual('uid=existing,%s' % settings.USER_BASE_DN, dn)
self.assertEquals( self.assertEqual(self.ldapobj.methods_called(), [
self.ldapobj.methods_called(), [ 'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s',
'initialize', 'simple_bind_s', 'search_s', 'search_s', 'search_s', 'search_s', 'search_s', 'modify_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'
])