def test_locale(self): snippet1 = ASRSnippetFactory.create(locales=['xx', 'de']) snippet2 = ASRSnippetFactory.create(locales=['fr']) ASRSnippetFactory.create(locales=['de']) filtr = ASRSnippetFilter(QueryDict(query_string='locale=xx,fr'), queryset=models.ASRSnippet.objects.all()) self.assertEqual(set([snippet1, snippet2]), set(filtr.qs))
def test_locale(self): snippet1 = ASRSnippetFactory.create(locales=['xx', 'de']) snippet2 = ASRSnippetFactory.create(locales=['fr']) ASRSnippetFactory.create(locales=['de']) filtr = ASRSnippetFilter(QueryDict(query_string='locale=xx,fr'), queryset=models.ASRSnippet.objects.all()) self.assertEqual(set([snippet1, snippet2]), set(filtr.qs))
def test_only_scheduled_false(self): snippet1 = ASRSnippetFactory.create(publish_start=None, publish_end=None) ASRSnippetFactory.create(publish_end=datetime(2019, 2, 2)) filtr = ASRSnippetFilter( QueryDict(query_string='only_scheduled=false'), queryset=models.ASRSnippet.objects.all()) self.assertEqual(set([snippet1]), set(filtr.qs))
def test_base(self): snippet1, snippet2 = ASRSnippetFactory.create_batch(2) snippet3 = ASRSnippetFactory.create(publish_start=datetime(2019, 1, 1)) snippet4 = ASRSnippetFactory.create(publish_end=datetime(2019, 2, 2)) filtr = ASRSnippetFilter(QueryDict(), queryset=models.ASRSnippet.objects.all()) self.assertEqual(set([snippet1, snippet2, snippet3, snippet4]), set(filtr.qs))
def test_match_client_match_locale(self): params = {} snippet = ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['en-US']) ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['fr']) self._assert_client_matches_snippets(params, [snippet])
def test_match_client_match_locale(self): params = {} snippet = ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['en-us']) ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['fr']) self._assert_client_matches_snippets(params, [snippet])
def test_only_scheduled_true(self): snippet1 = ASRSnippetFactory.create(publish_end=datetime(2019, 2, 2)) snippet2 = ASRSnippetFactory.create(publish_start=datetime(2019, 2, 2)) snippet3 = ASRSnippetFactory.create(publish_start=datetime(2019, 2, 2), publish_end=datetime(2019, 2, 3)) ASRSnippetFactory.create(publish_start=None, publish_end=None) filtr = ASRSnippetFilter(QueryDict(query_string='only_scheduled=true'), queryset=models.ASRSnippet.objects.all()) self.assertEqual(set([snippet1, snippet2, snippet3]), set(filtr.qs))
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_asrsnippet(self): nightly_snippets = ASRSnippetFactory.create_batch( 2, targets=[TargetFactory(on_release=False, on_nightly=True)]) ASRSnippetFactory.create_batch(2, targets=[TargetFactory(on_release=False, on_beta=True)]) filtr = ChannelFilter(None, {'channel': 'on_nightly'}, ASRSnippet, ASRSnippetAdmin) result = filtr.queryset(None, ASRSnippet.objects.all()) self.assertTrue(result.count(), 2) self.assertEqual(set(result.all()), set(nightly_snippets))
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_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_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_match_client_invalid_locale(self): """ If client sends invalid locale return snippets with no locales specified. """ params = {'locale': 'foo'} snippet = ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=[]) ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['en-us']) self._assert_client_matches_snippets(params, [snippet])
def test_match_client_multiple_locales(self): """ If there are multiple locales that should match the client's locale, include all of them. """ params = {'locale': 'es-mx'} snippet_1 = ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['es']) snippet_2 = ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['es-mx']) self._assert_client_matches_snippets(params, [snippet_1, snippet_2])
def test_match_client_match_channel_partially(self): """ Client channels like "release-cck-mozilla14" should match "release". """ params = {'channel': 'release-cck-mozilla14'} snippet = ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['en-US']) ASRSnippetFactory.create( targets=[TargetFactory(on_release=False, on_startpage_6=True)], locales=['en-US']) self._assert_client_matches_snippets(params, [snippet])
def test_asrsnippet(self): nightly_snippets = ASRSnippetFactory.create_batch( 2, targets=[TargetFactory(on_release=False, on_nightly=True)]) ASRSnippetFactory.create_batch( 2, targets=[TargetFactory(on_release=False, on_beta=True)]) filtr = ChannelFilter(None, {'channel': 'on_nightly'}, ASRSnippet, ASRSnippetAdmin) result = filtr.queryset(None, ASRSnippet.objects.all()) self.assertTrue(result.count(), 2) self.assertEqual(set(result.all()), set(nightly_snippets))
def test_match_client_match_channel_partially(self): """ Client channels like "release-cck-mozilla14" should match "release". """ params = {'channel': 'release-cck-mozilla14'} snippet = ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['en-us']) ASRSnippetFactory.create( targets=[TargetFactory(on_release=False, on_startpage_6=True)], locales=['en-us']) self._assert_client_matches_snippets(params, [snippet])
def test_match_client_multiple_locales(self): """ If there are multiple locales that should match the client's locale, include all of them. """ params = {'locale': 'es-mx'} snippet_1 = ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['es']) snippet_2 = ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['es-mx']) self._assert_client_matches_snippets(params, [snippet_1, snippet_2])
def test_match_client_invalid_locale(self): """ If client sends invalid locale return snippets with no locales specified. """ params = {'locale': 'foo'} snippet = ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=[]) ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['en-us']) self._assert_client_matches_snippets(params, [snippet])
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_get_preview_url_rtl(self): snippet = ASRSnippetFactory.create() snippet.locale.rtl = True expected_result = 'about:newtab?theme=dark&dir=rtl&endpoint=http://example.com' expected_result += reverse('asr-preview', kwargs={'uuid': snippet.uuid}) self.assertEqual(snippet.get_preview_url(dark=True), expected_result)
def test_render(self): snippet = ASRSnippetFactory.create(template_relation__text=( 'snippet id [[snippet_id]] and with ' 'campaign [[campaign_slug]] and ' '<a href="https://example.com/[[snippet_id]]/foo">link</a> in ' '[[channels]] channels'), ) snippet.template_ng.TARGETING = 'true' generated_result = snippet.render() expected_result = { 'template': snippet.template_ng.code_name, 'template_version': snippet.template_ng.version, 'targeting': 'true', 'content': { 'text': ('snippet id {} and with campaign [[campaign_slug]] and ' '<link0>link</link0> in [[channels]] channels').format( snippet.id), 'links': { 'link0': { 'url': 'https://example.com/{}/foo'.format(snippet.id), } }, 'tall': False, 'icon': snippet.template_ng.icon.url, 'do_not_autoblock': False, 'block_button_text': 'Remove this', }, } self.assertEqual(generated_result, expected_result)
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_base(self): not_scheduled = ASRSnippetFactory.create() scheduled1 = ASRSnippetFactory.create(publish_start=datetime(2019, 1, 1)) scheduled2 = ASRSnippetFactory.create(publish_start=datetime(2019, 1, 1), publish_end=datetime(2019, 1, 2)) filtr = ScheduledFilter(None, {'is_scheduled': 'yes'}, ASRSnippet, ASRSnippetAdmin) result = filtr.queryset(None, ASRSnippet.objects.all()) self.assertTrue(result.count(), 2) self.assertEqual(set(result.all()), set([scheduled1, scheduled2])) filtr = ScheduledFilter(None, {'is_scheduled': 'no'}, ASRSnippet, ASRSnippetAdmin) result = filtr.queryset(None, ASRSnippet.objects.all()) self.assertTrue(result.count(), 1) self.assertEqual(set(result.all()), set([not_scheduled]))
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_default_is_same_as_nightly(self): """ Make sure that default channel follows nightly. """ # Snippets matching nightly (and therefor should match default). nightly_snippet = ASRSnippetFactory.create(targets=[TargetFactory(on_nightly=True)]) # Snippets that don't match nightly ASRSnippetFactory.create(targets=[TargetFactory(on_beta=True)]) nightly_client = self._build_client(channel='nightly') nightly_snippets = ASRSnippet.objects.match_client(nightly_client) default_client = self._build_client(channel='default') default_snippets = ASRSnippet.objects.match_client(default_client) # Assert that both the snippets returned from nightly and from default # are the same snippets. Just `nightly_snippet` in this case. self.assertEqual(set([nightly_snippet]), set(nightly_snippets)) self.assertEqual(set([nightly_snippet]), set(default_snippets))
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_default_is_same_as_nightly(self): """ Make sure that default channel follows nightly. """ # Snippets matching nightly (and therefor should match default). nightly_snippet = ASRSnippetFactory.create(targets=[TargetFactory(on_nightly=True)]) # Snippets that don't match nightly ASRSnippetFactory.create(targets=[TargetFactory(on_beta=True)]) nightly_client = self._build_client(channel='nightly') nightly_snippets = ASRSnippet.objects.match_client(nightly_client) default_client = self._build_client(channel='default') default_snippets = ASRSnippet.objects.match_client(default_client) # Assert that both the snippets returned from nightly and from default # are the same snippets. Just `nightly_snippet` in this case. self.assertEqual(set([nightly_snippet]), set(nightly_snippets)) self.assertEqual(set([nightly_snippet]), set(default_snippets))
def test_match_client_multiple_locales_distinct(self): """ If a snippet has multiple locales and a client matches more than one of them, the snippet should only be included in the queryset once. """ params = {'locale': 'es-mx'} snippet = ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['es', 'es-mx']) self._assert_client_matches_snippets(params, [snippet])
def test_match_client_multiple_locales_distinct(self): """ If a snippet has multiple locales and a client matches more than one of them, the snippet should only be included in the queryset once. """ params = {'locale': 'es-mx'} snippet = ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['es', 'es-mx']) self._assert_client_matches_snippets(params, [snippet])
def test_base(self): not_scheduled = ASRSnippetFactory.create() scheduled1 = ASRSnippetFactory.create( publish_start=datetime(2019, 1, 1)) scheduled2 = ASRSnippetFactory.create(publish_start=datetime( 2019, 1, 1), publish_end=datetime(2019, 1, 2)) filtr = ScheduledFilter(None, {'is_scheduled': 'yes'}, ASRSnippet, ASRSnippetAdmin) result = filtr.queryset(None, ASRSnippet.objects.all()) self.assertTrue(result.count(), 2) self.assertEqual(set(result.all()), set([scheduled1, scheduled2])) filtr = ScheduledFilter(None, {'is_scheduled': 'no'}, ASRSnippet, ASRSnippetAdmin) result = filtr.queryset(None, ASRSnippet.objects.all()) self.assertTrue(result.count(), 1) self.assertEqual(set(result.all()), set([not_scheduled]))
def test_channels(self): snippet = ASRSnippetFactory.create(targets=[ TargetFactory.create(on_release=True), TargetFactory.create(on_beta=True, on_nightly=True), TargetFactory.create(on_release=False, on_esr=False, on_aurora=False, on_beta=False, on_nightly=False), ]) self.assertTrue(snippet.channels, set(['release', 'beta', 'nightly']))
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_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 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_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_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_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_analytics_export(self): snippet = ASRSnippetFactory.create( name='test-snippet', campaign__name='test-campaign', category__name='test-category', template_relation__text= ('This is the <b>bold body</b> with a <a href="https://example.com">link</a>.' ), ) expected_data = { 'id': snippet.id, 'name': 'test-snippet', 'campaign': 'test-campaign', 'category': 'test-category', 'url': 'https://example.com', 'body': 'This is the bold body with a link.' } self.assertEqual(expected_data, snippet.analytics_export())
def test_match_client_base(self): client_match_rule_pass_1 = ClientMatchRuleFactory(channel='nightly') client_match_rule_pass_2 = ClientMatchRuleFactory(channel='/(beta|nightly)/') client_match_rule_fail = ClientMatchRuleFactory(channel='release') # Matching snippets. snippet_1 = ASRSnippetFactory.create( targets=[ TargetFactory(on_release=False, on_nightly=True, client_match_rules=[client_match_rule_pass_1]) ]) snippet_2 = ASRSnippetFactory.create( targets=[ TargetFactory(on_release=False, on_beta=True, on_nightly=True, client_match_rules=[client_match_rule_pass_2]) ]) snippet_3 = ASRSnippetFactory.create( targets=[TargetFactory(on_release=False, on_nightly=True)]) # Not matching snippets. ASRSnippetFactory.create(targets=[TargetFactory(on_release=False, on_beta=True)]) ASRSnippetFactory.create( targets=[ TargetFactory(on_nightly=True, client_match_rules=[client_match_rule_fail]) ]) ASRSnippetFactory.create( targets=[ TargetFactory(on_nightly=True, client_match_rules=[client_match_rule_fail, client_match_rule_pass_2]) ]) client = self._build_client(channel='nightly') snippets = ASRSnippet.objects.match_client(client) self.assertEqual(set(snippets), set([snippet_1, snippet_2, snippet_3]))
def test_match_client_base(self): client_match_rule_pass_1 = ClientMatchRuleFactory(channel='nightly') client_match_rule_pass_2 = ClientMatchRuleFactory(channel='/(beta|nightly)/') client_match_rule_fail = ClientMatchRuleFactory(channel='release') # Matching snippets. snippet_1 = ASRSnippetFactory.create( targets=[ TargetFactory(on_release=False, on_nightly=True, client_match_rules=[client_match_rule_pass_1]) ]) snippet_2 = ASRSnippetFactory.create( targets=[ TargetFactory(on_release=False, on_beta=True, on_nightly=True, client_match_rules=[client_match_rule_pass_2]) ]) snippet_3 = ASRSnippetFactory.create( targets=[TargetFactory(on_release=False, on_nightly=True)]) # Not matching snippets. ASRSnippetFactory.create(targets=[TargetFactory(on_release=False, on_beta=True)]) ASRSnippetFactory.create( targets=[ TargetFactory(on_nightly=True, client_match_rules=[client_match_rule_fail]) ]) ASRSnippetFactory.create( targets=[ TargetFactory(on_nightly=True, client_match_rules=[client_match_rule_fail, client_match_rule_pass_2]) ]) client = self._build_client(channel='nightly') snippets = ASRSnippet.objects.match_client(client) self.assertEqual(set(snippets), set([snippet_1, snippet_2, snippet_3]))
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_render_preview_only(self): snippet = ASRSnippetFactory.create(template_relation__text=( 'snippet id *[[snippet_id]]* ' '*[[campaign_slug]]* *[[channels]]* *[[job_id]]*')) generated_result = snippet.render(preview=True) expected_result = { 'id': 'preview-{}'.format(snippet.id), 'template': snippet.template_ng.code_name, 'template_version': snippet.template_ng.version, 'targeting': '', 'content': { 'do_not_autoblock': True, # snippet_id, campaign_slug and channels must be replaced with empty string. 'text': 'snippet id ** ** ** **', 'links': {}, 'tall': False, 'icon': snippet.template_ng.icon.url, 'block_button_text': 'Remove this', } } self.assertEqual(generated_result, expected_result)
def test_match_client_not_matching_locale(self): params = {'locale': 'en-US'} ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=[]) self._assert_client_matches_snippets(params, [])
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])
def test_base(self): snippet1, snippet2 = ASRSnippetFactory.create_batch(2) snippet3 = ASRSnippetFactory.create(publish_start=datetime(2019, 1, 1)) snippet4 = ASRSnippetFactory.create(publish_end=datetime(2019, 2, 2)) filtr = ASRSnippetFilter(QueryDict(), queryset=models.ASRSnippet.objects.all()) self.assertEqual(set([snippet1, snippet2, snippet3, snippet4]), set(filtr.qs))
def setUp(self): self.snippet1, self.snippet2 = ASRSnippetFactory.create_batch(2)
def test_name(self): snippet1 = ASRSnippetFactory.create(name='foo bar foo') ASRSnippetFactory.create(name='foo lala foo') filtr = ASRSnippetFilter(QueryDict(query_string='name=bar'), queryset=models.ASRSnippet.objects.all()) self.assertEqual(set([snippet1]), set(filtr.qs))
def test_match_client_not_matching_startpage(self): params = {'startpage_version': '0'} snippet = ASRSnippetFactory.create( targets=[TargetFactory(on_release=True, on_startpage_6=True)], locales=['en-us']) self._assert_client_matches_snippets(params, [snippet])
def test_match_client_not_matching_name(self): params = {'name': 'unicorn'} snippet = ASRSnippetFactory.create() self._assert_client_matches_snippets(params, [snippet])
def test_only_scheduled_all(self): snippet1, snippet2 = ASRSnippetFactory.create_batch(2) snippet3 = ASRSnippetFactory.create(publish_end=datetime(2019, 2, 2)) filtr = ASRSnippetFilter(QueryDict(query_string='only_scheduled=all'), queryset=models.ASRSnippet.objects.all()) self.assertEqual(set([snippet1, snippet2, snippet3]), set(filtr.qs))