complete coverage for osusers.models

- add tests for AdditionalGroups methods save, delete and __str__
- add TaskResultTest.test_update_taskstatus_finished
This commit is contained in:
Jan Dittberner 2014-06-01 16:30:14 +02:00
parent c270a6087e
commit 842e207acc

View file

@ -31,25 +31,51 @@ class TestCaseWithCeleryTasks(TestCase):
class AdditionalGroupTest(TestCaseWithCeleryTasks): class AdditionalGroupTest(TestCaseWithCeleryTasks):
def test_clean_primary_group(self): def setUp(self):
group1 = Group.objects.create(groupname='test1', gid=1000) self.group1 = Group.objects.create(groupname='test1', gid=1000)
user = User.objects.create( self.user = User.objects.create(
username='test', uid=1000, group=group1, username='test', uid=1000, group=self.group1,
homedir='/home/test', shell='/bin/bash') homedir='/home/test', shell='/bin/bash')
testsubj = AdditionalGroup(user=user, group=group1)
def test_clean_primary_group(self):
testsubj = AdditionalGroup(user=self.user, group=self.group1)
with self.assertRaises(ValidationError) as cm: with self.assertRaises(ValidationError) as cm:
testsubj.clean() testsubj.clean()
self.assertEqual( self.assertEqual(
cm.exception.message, CANNOT_USE_PRIMARY_GROUP_AS_ADDITIONAL) cm.exception.message, CANNOT_USE_PRIMARY_GROUP_AS_ADDITIONAL)
def test_clean_other_group(self): def test_clean_other_group(self):
group1 = Group(groupname='test1', gid=1000) group2 = Group.objects.create(groupname='test2', gid=1001)
group2 = Group(groupname='test2', gid=1001) testsubj = AdditionalGroup(user=self.user, group=group2)
user = User(username='test', uid=1000, group=group1,
homedir='/home/test', shell='/bin/bash')
testsubj = AdditionalGroup(user=user, group=group2)
testsubj.clean() testsubj.clean()
def test_save(self):
group2 = Group.objects.create(groupname='test2', gid=1001)
GroupTaskResult.objects.all().delete()
addgroup = AdditionalGroup(user=self.user, group=group2)
addgroup.save()
taskres = GroupTaskResult.objects.all()
self.assertTrue(len(taskres), 1)
self.assertEqual(taskres[0].task_name, 'add_ldap_user_to_group')
self.assertEqual(taskres[0].group, group2)
def test_delete(self):
group2 = Group.objects.create(groupname='test2', gid=1001)
addgroup = AdditionalGroup.objects.create(user=self.user, group=group2)
DeleteTaskResult.objects.all().delete()
addgroup.delete()
taskres = DeleteTaskResult.objects.all()
self.assertTrue(len(taskres), 1)
self.assertEqual(taskres[0].task_name, 'remove_ldap_user_from_group')
self.assertEqual(taskres[0].modeltype, 'usergroup')
self.assertEqual(taskres[0].modelname, 'test (1000) in test2 (1001)')
self.assertEqual(len(AdditionalGroup.objects.all()), 0)
def test___str__(self):
group2 = Group.objects.create(groupname='test2', gid=1001)
addgroup = AdditionalGroup.objects.create(user=self.user, group=group2)
self.assertEqual(str(addgroup), 'test (1000) in test2 (1001)')
@override_settings(OSUSER_MINGID=10000) @override_settings(OSUSER_MINGID=10000)
class GroupManagerTest(TestCase): class GroupManagerTest(TestCase):
@ -177,6 +203,19 @@ class TaskResultTest(TestCase):
mymock.ready.assert_called_with() mymock.ready.assert_called_with()
self.assertTrue(tr.is_finished) self.assertTrue(tr.is_finished)
@patch('osusers.models.AsyncResult')
def test_update_taskstatus_finished(self, asyncres):
mock = MagicMock(task_id=TEST_TASK_UUID, task_name=TEST_TASK_NAME)
mock.ready.return_value = True
mock.state = 'SUCCESS'
mock.result = TEST_RESULT
tr = DeleteTaskResult.objects.create(mock, TEST_TASK_NAME)
self.assertTrue(tr.is_finished)
mymock = asyncres(TEST_TASK_UUID)
tr.update_taskstatus()
self.assertFalse(mymock.ready.called)
self.assertTrue(tr.is_finished)
TEST_RESULT = MagicMock() TEST_RESULT = MagicMock()
TEST_RESULT.task_id = TEST_TASK_UUID TEST_RESULT.task_id = TEST_TASK_UUID