def test_subtemplate(self): snippet = ASRSnippetFactory() subtemplate = snippet.template_relation.subtemplate self.assertTrue(type(subtemplate) is SimpleTemplate) # Test subtemplate when checking from an object that inherits Template subtemplate = snippet.template_relation.subtemplate.subtemplate self.assertTrue(type(subtemplate) is SimpleTemplate)
def test_base(self): snippet = ASRSnippetFactory() url = reverse('asr-preview', kwargs={'uuid': snippet.uuid}) with patch('snippets.base.views.ASRSnippet.render') as render_mock: render_mock.return_value = 'foo' response = self.client.get(url) render_mock.assert_called_with(preview=True) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'application/json')
def test_modified_date_updates_when_target_updates(self): new_target = TargetFactory() snippet = ASRSnippetFactory() snippet.targets.add(new_target) old_modified = snippet.modified new_target.name = 'new name' new_target.save() snippet.refresh_from_db() new_modified = snippet.modified self.assertNotEqual(old_modified, new_modified)
def test_modified_date_updates_when_campaign_updates(self): snippet = ASRSnippetFactory() old_modified = snippet.modified campaign = snippet.campaign campaign.name = 'new name' campaign.save() snippet.refresh_from_db() new_modified = snippet.modified self.assertNotEqual(old_modified, new_modified)
def test_modified_date_updates_when_icon_updates(self): snippet = ASRSnippetFactory() old_modified = snippet.modified template = snippet.template_ng template.icon = IconFactory() template.save() snippet.refresh_from_db() new_modified = snippet.modified self.assertNotEqual(old_modified, new_modified)
def test_get_readonly_fields(self): asrsnippet = ASRSnippetFactory() request = self.factory.get('/') admin = ASRSnippetAdmin(ASRSnippet, AdminSite()) request.user = UserFactory # No obj readonly_fields = admin.get_readonly_fields(request, None) self.assertTrue('status' in readonly_fields) # With obj readonly_fields = admin.get_readonly_fields(request, asrsnippet) self.assertTrue('status' not in readonly_fields)
def test_base(self): snippet_without_end_date = SnippetFactory(published=True, publish_end=None) snippet_that_has_ended = SnippetFactory(published=True, publish_end=datetime.utcnow()) snippet_ending_in_the_future = SnippetFactory( published=True, publish_end=datetime.utcnow() + timedelta(days=1)) call_command('disable_snippets_past_publish_date', stdout=Mock()) snippet_without_end_date.refresh_from_db() snippet_that_has_ended.refresh_from_db() snippet_ending_in_the_future.refresh_from_db() self.assertFalse(snippet_that_has_ended.published) self.assertTrue(snippet_without_end_date.published) self.assertTrue(snippet_ending_in_the_future.published) asrsnippet_without_end_date = ASRSnippetFactory( status=STATUS_CHOICES['Published'], publish_end=None) asrsnippet_that_has_ended = ASRSnippetFactory( status=STATUS_CHOICES['Published'], publish_end=datetime.utcnow()) asrsnippet_ending_in_the_future = ASRSnippetFactory( status=STATUS_CHOICES['Published'], publish_end=datetime.utcnow() + timedelta(days=1)) call_command('disable_snippets_past_publish_date', stdout=Mock()) asrsnippet_without_end_date.refresh_from_db() asrsnippet_that_has_ended.refresh_from_db() asrsnippet_ending_in_the_future.refresh_from_db() self.assertEqual(asrsnippet_that_has_ended.status, STATUS_CHOICES['Approved']) self.assertEqual(asrsnippet_without_end_date.status, STATUS_CHOICES['Published']) self.assertEqual(asrsnippet_ending_in_the_future.status, STATUS_CHOICES['Published'])
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_modified_date_updates_when_icon_updates(self): snippet = ASRSnippetFactory() # Must refresh from db to get the actual datetime stored in db which # may be different by milliseconds from the original python datetime. snippet.refresh_from_db() old_modified = snippet.modified template = snippet.template_ng template.icon = IconFactory() template.save() snippet.refresh_from_db() new_modified = snippet.modified self.assertNotEqual(old_modified, new_modified)
def test_process_rendered_data(self): data = { 'foo': '', 'bar': 'bar', } expected_data = { 'bar': 'bar', 'links': {}, } snippet = ASRSnippetFactory() with patch('snippets.base.models.util.fluent_link_extractor', wraps=fluent_link_extractor) as fluent_link_extractor_mock: processed_data = snippet.template_ng._process_rendered_data(data) self.assertTrue(fluent_link_extractor_mock.called) self.assertEqual(processed_data, expected_data)
def test_process_rendered_data(self): data = {'foo': '', 'bar': 'bar', 'button_url': 'special:about:logins'} expected_data = { 'bar': 'bar', 'links': {}, 'button_action': 'OPEN_ABOUT_PAGE', 'button_action_args': 'logins', 'button_entrypoint_name': 'entryPoint', 'button_entrypoint_value': 'snippet', } snippet = ASRSnippetFactory() with patch('snippets.base.models.util.fluent_link_extractor', wraps=fluent_link_extractor) as fluent_link_extractor_mock: processed_data = snippet.template_ng._process_rendered_data(data) self.assertTrue(fluent_link_extractor_mock.called) self.assertEqual(processed_data, expected_data)
def test_add_utm_params(self): snippet = ASRSnippetFactory( template_relation__text=( 'This is a <a href="https://www.example.com/?utm_medium=SI">' 'linked test</a> and <a href="https://example.com">' 'another link</a> without any params'), template_relation__button_url= 'https://www.mozilla.org/foo/bar/?lala=lolo', ) snippet.template_ng.add_utm_params() self.assertEqual(snippet.template_ng.text, ( 'This is a <a href="https://www.example.com/?utm_medium=SI' '&utm_source=desktop-snippet&utm_campaign=[[campaign_slug]]' '&utm_term=[[job_id]]&utm_content=[[channels]]">linked test</a> and ' '<a href="https://example.com/?utm_source=desktop-snippet&utm_medium=snippet' '&utm_campaign=[[campaign_slug]]&utm_term=[[job_id]]&utm_content=[[channels]]">' 'another link</a> without any params')) self.assertEqual(snippet.template_ng.button_url, ( 'https://www.mozilla.org/foo/bar/?lala=lolo&utm_source=desktop-snippet' '&utm_medium=snippet&utm_campaign=[[campaign_slug]]&utm_term=[[job_id]]' '&utm_content=[[channels]]'))
def test_make_published(self): to_be_published = ASRSnippetFactory.create_batch( 2, status=STATUS_CHOICES['Draft']) already_published = ASRSnippetFactory( status=STATUS_CHOICES['Published']) ASRSnippetFactory.create_batch(2, status=STATUS_CHOICES['Draft']) queryset = ASRSnippet.objects.filter(id__in=[ to_be_published[0].id, to_be_published[1].id, already_published.id ]) with patch( 'snippets.base.admin.adminmodels.messages.warning') as warning: with patch('snippets.base.admin.adminmodels.messages.success' ) as success: ASRSnippetAdmin(ASRSnippet, None).make_published(None, queryset) self.assertEqual( set(ASRSnippet.objects.filter(status=STATUS_CHOICES['Published'])), set(to_be_published + [already_published])) self.assertTrue(warning.called) self.assertTrue(success.called)
def test_publish_permission_check_asr(self): user = User.objects.create_user(username='******', email='*****@*****.**', password='******') perm_beta = Permission.objects.get(codename='publish_on_beta', content_type__model='asrsnippet') user.user_permissions.add(perm_beta) perm_nightly = Permission.objects.get(codename='publish_on_nightly', content_type__model='asrsnippet') user.user_permissions.add(perm_nightly) target_release = TargetFactory(on_release=True, on_beta=False, on_esr=False, on_nightly=False, on_aurora=False) target_beta = TargetFactory(on_release=False, on_beta=True, on_esr=False, on_nightly=False, on_aurora=False) target_nightly = TargetFactory(on_release=False, on_beta=False, on_esr=False, on_nightly=True, on_aurora=False) asrsnippet = ASRSnippetFactory(targets=[]) data = { 'name': 'Test', 'weight': 100, } # User should get an error trying to publish on Release new_data = data.copy() new_data['status'] = STATUS_CHOICES['Published'] new_data['targets'] = [target_release] form = ASRSnippetAdminForm(new_data, instance=asrsnippet) form.current_user = user self.assertFalse(form.is_valid()) self.assertTrue( 'You are not allowed to edit or publish on Release channel.' in form.errors['__all__'][0]) # User should get an error trying to edit or publish on Release even though Beta # is selected too. new_data = data.copy() new_data['status'] = STATUS_CHOICES['Published'] new_data['targets'] = [target_release, target_beta] form = ASRSnippetAdminForm(new_data, instance=asrsnippet) form.current_user = user self.assertFalse(form.is_valid()) self.assertTrue( 'You are not allowed to edit or publish on Release channel.' in form.errors['__all__'][0]) # Form is valid if user tries to edit or publish on Beta. new_data = data.copy() new_data['status'] = STATUS_CHOICES['Published'] new_data['targets'] = [target_beta] form = ASRSnippetAdminForm(new_data, instance=asrsnippet) form.current_user = user self.assertTrue(form.is_valid()) # Form is valid if user tries to publish or edit on Beta and Nightly. new_data = data.copy() new_data['status'] = STATUS_CHOICES['Published'] new_data['targets'] = [target_beta, target_nightly] form = ASRSnippetAdminForm(new_data, instance=asrsnippet) form.current_user = user self.assertTrue(form.is_valid()) # Form is invalid if user tries edit published Snippet on Release. instance = ASRSnippetFactory.create( status=STATUS_CHOICES['Published'], targets=[TargetFactory(on_release=True)]) new_data = data.copy() new_data['targets'] = [target_release, target_beta, target_nightly] form = ASRSnippetAdminForm(new_data, instance=instance) form.current_user = user self.assertFalse(form.is_valid()) self.assertTrue( 'You are not allowed to edit or publish on Release channel.' in form.errors['__all__'][0]) # User cannot unset Release channel and save. instance = ASRSnippetFactory.create( status=STATUS_CHOICES['Published'], targets=[TargetFactory(on_release=True)]) new_data = data.copy() new_data['targets'] = [target_beta, target_nightly] form = ASRSnippetAdminForm(new_data, instance=instance) form.current_user = user self.assertFalse(form.is_valid()) self.assertTrue( 'You are not allowed to edit or publish on Release channel.' in form.errors['__all__'][0]) # User can un-publish if they have permission on all channels. instance = ASRSnippetFactory.create(status=STATUS_CHOICES['Published'], targets=[ TargetFactory(on_release=False, on_beta=True, on_nightly=True) ]) new_data = data.copy() new_data['status'] = STATUS_CHOICES['Approved'] new_data['targets'] = [target_beta, target_nightly] form = ASRSnippetAdminForm(new_data, instance=instance) form.current_user = user self.assertTrue(form.is_valid()) # User cannot un-publish if they don't have permission on all channels. instance = ASRSnippetFactory.create( status=STATUS_CHOICES['Published'], targets=[TargetFactory(on_release=True, on_nightly=True)]) new_data = data.copy() new_data['status'] = STATUS_CHOICES['Approved'] new_data['targets'] = [target_release, target_nightly] form = ASRSnippetAdminForm(new_data, instance=instance) form.current_user = user self.assertFalse(form.is_valid()) self.assertTrue( 'You are not allowed to edit or publish on Release channel.' in form.errors['__all__'][0])