from django.core.exceptions import ValidationError from django.test import TestCase from django.test.utils import override_settings from mock import patch, MagicMock from osusers.models import ( CANNOT_USE_PRIMARY_GROUP_AS_ADDITIONAL, AdditionalGroup, DeleteTaskResult, Group, GroupTaskResult, Shadow, User, ) @override_settings( CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND='memory', BROKER_BACKEND='memory' ) class TestCaseWithCeleryTasks(TestCase): pass class AdditionalGroupTest(TestCaseWithCeleryTasks): def test_clean_primary_group(self): group1 = Group.objects.create(groupname='test1', gid=1000) user = User.objects.create( username='test', uid=1000, group=group1, homedir='/home/test', shell='/bin/bash') testsubj = AdditionalGroup(user=user, group=group1) with self.assertRaises(ValidationError) as cm: testsubj.clean() self.assertEqual( cm.exception.message, CANNOT_USE_PRIMARY_GROUP_AS_ADDITIONAL) def test_clean_other_group(self): group1 = Group(groupname='test1', gid=1000) group2 = Group(groupname='test2', gid=1001) user = User(username='test', uid=1000, group=group1, homedir='/home/test', shell='/bin/bash') testsubj = AdditionalGroup(user=user, group=group2) testsubj.clean() @override_settings(OSUSER_MINGID=10000) class GroupManagerTest(TestCase): def test_get_next_gid_first(self): self.assertEqual(Group.objects.get_next_gid(), 10000) def test_get_next_gid_second(self): Group.objects.create(gid=10010, groupname='test') self.assertEqual(Group.objects.get_next_gid(), 10011) class GroupTest(TestCaseWithCeleryTasks): def test___str__(self): group = Group.objects.create(gid=10000, groupname='test') self.assertEqual(str(group), 'test (10000)') def test_save(self): group = Group(gid=10000, groupname='test') self.assertIs(group.save(), group) taskres = GroupTaskResult.objects.all() self.assertEqual(len(taskres), 1) self.assertEqual(taskres[0].group, group) self.assertEqual(taskres[0].task_name, 'create_ldap_group') def test_delete(self): group = Group.objects.create(gid=10000, groupname='test') self.assertEqual(len(Group.objects.all()), 1) self.assertEqual(len(GroupTaskResult.objects.all()), 1) group.delete() self.assertEqual(len(Group.objects.all()), 0) self.assertEqual(len(GroupTaskResult.objects.all()), 0) taskres = DeleteTaskResult.objects.all() self.assertEqual(len(taskres), 1) self.assertEqual(taskres[0].task_name, 'delete_ldap_group') self.assertEqual(taskres[0].modeltype, 'group') self.assertEqual(taskres[0].modelname, 'test') class ShadowTest(TestCaseWithCeleryTasks): def test___str__(self): group = Group.objects.create( groupname='test', gid=1000) user = User.objects.create( username='test', uid=1000, group=group, homedir='/home/test', shell='/bin/bash') shadow = Shadow(user=user) self.assertEqual(str(shadow), 'for user test (1000)') TEST_TASK_UUID = '3120f6a8-2665-4fa3-a785-79efd28bfe92' TEST_TASK_NAME = 'test.task' TEST_TASK_RESULT = '4ll y0ur b453 4r3 b3l0ng t0 u5' class TaskResultTest(TestCase): def test__set_result_fields_not_ready(self): mock = MagicMock(task_id=TEST_TASK_UUID, task_name=TEST_TASK_NAME) mock.ready.return_value = False tr = DeleteTaskResult.objects.create(mock, TEST_TASK_NAME) self.assertFalse(tr.is_finished) self.assertFalse(tr.is_success) self.assertEqual(tr.state, '') self.assertEqual(tr.result_body, '') def test__set_result_fields_ready(self): mock = MagicMock(task_id=TEST_TASK_UUID, task_name=TEST_TASK_NAME, state='SUCCESS', result=TEST_TASK_RESULT) mock.ready.return_value = True tr = DeleteTaskResult.objects.create(mock, TEST_TASK_NAME) self.assertTrue(tr.is_finished) self.assertTrue(tr.is_success) self.assertEqual(tr.state, 'SUCCESS') self.assertEqual(tr.result_body, TEST_TASK_RESULT) def test__set_result_fields_exception(self): mock = MagicMock(task_id=TEST_TASK_UUID, task_name=TEST_TASK_NAME, state='FAILURE', result=Exception('Fail')) mock.ready.return_value = True tr = DeleteTaskResult.objects.create(mock, TEST_TASK_NAME) self.assertTrue(tr.is_finished) self.assertFalse(tr.is_success) self.assertEqual(tr.state, 'FAILURE') self.assertEqual(tr.result_body, 'Fail') @patch('osusers.models.AsyncResult') def test_update_taskstatus_unfinished(self, asyncres): mock = MagicMock(task_id=TEST_TASK_UUID, task_name=TEST_TASK_NAME) mock.ready.return_value = False tr = DeleteTaskResult.objects.create(mock, TEST_TASK_NAME) self.assertFalse(tr.is_finished) mymock = asyncres(TEST_TASK_UUID) mymock.ready.return_value = True mymock.state = 'SUCCESS' mymock.result = TEST_RESULT tr.update_taskstatus() mymock.ready.assert_called_with() self.assertTrue(tr.is_finished) TEST_RESULT = MagicMock() TEST_RESULT.task_id = TEST_TASK_UUID TEST_RESULT.task_name = TEST_TASK_NAME TEST_RESULT.ready.return_value = False class TaskResultManagerTest(TestCase): def test_create(self): tr = DeleteTaskResult.objects.create(TEST_RESULT, TEST_TASK_NAME) self.assertIsInstance(tr, DeleteTaskResult) self.assertEqual(tr.task_uuid, TEST_TASK_UUID) self.assertEqual(tr.task_name, TEST_TASK_NAME) class UserTest(TestCaseWithCeleryTasks): pass