def test_get_readonly_fields(self): asrsnippet = ASRSnippetFactory() request = self.factory.get('/') admin = ASRSnippetAdmin(ASRSnippet, AdminSite()) # Not Super User request.user = UserFactory(is_superuser=False) readonly_fields = admin.get_readonly_fields(request, asrsnippet) self.assertTrue('for_qa' in readonly_fields) # SuperUser request.user = UserFactory(is_superuser=True) readonly_fields = admin.get_readonly_fields(request, asrsnippet) self.assertTrue('for_qa' not in readonly_fields)
def test_get_queryset(self): snippets = ASRSnippetFactory.create_batch(2) qa_snippets = ASRSnippetFactory.create_batch(2, for_qa=True) request = self.factory.get('/') admin = ASRSnippetAdmin(ASRSnippet, AdminSite()) # Not Super User request.user = UserFactory(is_superuser=False) queryset = admin.get_queryset(request) self.assertEqual(set(snippets), set(queryset.all())) # SuperUser request.user = UserFactory(is_superuser=True) queryset = admin.get_queryset(request) self.assertEqual(set(snippets + qa_snippets), set(queryset.all()))
def test_action_delete_job(self): to_get_deleted = [ JobFactory.create(status=Job.DRAFT), JobFactory.create(status=Job.DRAFT), ] to_remain = [ JobFactory(status=Job.CANCELED), JobFactory(status=Job.COMPLETED), JobFactory(status=Job.PUBLISHED), ] queryset = Job.objects.filter(id__in=[x.id for x in to_get_deleted + to_remain]) request = Mock() request.user = UserFactory.create() with patch.multiple('snippets.base.admin.adminmodels.messages', warning=DEFAULT_MOCK, success=DEFAULT_MOCK) as message_mocks: JobAdmin(Job, None).action_delete_job(request, queryset) self.assertEqual( set(Job.objects.all()), set(to_remain) ) self.assertTrue(message_mocks['warning'].called) self.assertTrue(message_mocks['success'].called)
def test_action_cancel_job(self): to_get_canceled = [ JobFactory.create(status=Job.PUBLISHED), JobFactory.create(status=Job.SCHEDULED), ] already_cancelled = JobFactory(status=Job.CANCELED) completed = JobFactory(status=Job.COMPLETED) JobFactory.create_batch(2, status=Job.DRAFT) queryset = Job.objects.filter(id__in=[ to_get_canceled[0].id, to_get_canceled[1].id, already_cancelled.id, completed.id, ]) request = Mock() request.user = UserFactory.create() with patch.multiple('snippets.base.admin.adminmodels.messages', warning=DEFAULT_MOCK, success=DEFAULT_MOCK) as message_mocks: JobAdmin(Job, None).action_cancel_job(request, queryset) self.assertEqual( set(Job.objects.filter(status=Job.CANCELED)), set(to_get_canceled + [already_cancelled]) ) self.assertTrue(message_mocks['warning'].called) self.assertTrue(message_mocks['success'].called)
def test_duplicate(self): user = UserFactory.create() snippet = ASRSnippetFactory.create(status=STATUS_CHOICES['Approved'], ) duplicate_snippet = snippet.duplicate(user) snippet.refresh_from_db() for attr in ['id', 'creator', 'created', 'modified', 'name', 'uuid']: self.assertNotEqual(getattr(snippet, attr), getattr(duplicate_snippet, attr)) self.assertEqual(duplicate_snippet.status, STATUS_CHOICES['Draft']) self.assertNotEqual(snippet.template_ng.pk, duplicate_snippet.template_ng.pk)
def _dup_test(self, snippet): user = UserFactory.create() snippet.client_match_rules.add(*ClientMatchRuleFactory.create_batch(3)) snippet_copy = snippet.duplicate(user) self.assertEqual(snippet_copy.published, False) self.assertNotEqual(snippet_copy.id, snippet.id) self.assertEqual(snippet_copy.locales.count(), 1) self.assertTrue( snippet_copy.locales.all()[0] == snippet.locales.all()[0]) self.assertEqual(set(snippet_copy.client_match_rules.all()), set(snippet.client_match_rules.all())) self.assertNotEqual(snippet_copy.creator, snippet.creator)
def test_duplicate(self): user = UserFactory.create() job = JobFactory.create(status=Job.PUBLISHED, metric_impressions=500, metric_clicks=500, metric_blocks=500, completed_on=datetime.utcnow()) duplicate_job = job.duplicate(user) job.refresh_from_db() for attr in ['id', 'creator', 'created', 'modified', 'uuid']: self.assertNotEqual(getattr(job, attr), getattr(duplicate_job, attr)) self.assertEqual(duplicate_job.status, Job.DRAFT) self.assertEqual(duplicate_job.metric_impressions, 0) self.assertEqual(duplicate_job.metric_clicks, 0) self.assertEqual(duplicate_job.metric_blocks, 0) self.assertEqual(duplicate_job.completed_on, None)
def setUp(self): self.factory = RequestFactory() self.model_admin = ASRSnippetAdmin(ASRSnippet, None) self.model_admin.admin_site = Mock() self.user = UserFactory()