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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_render_preview_only(self): snippet = ASRSnippetFactory.create( template_relation__text='snippet id [[snippet_id]]', targets=[TargetFactory(jexl_expr='foo == bar')]) 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, 'campaign': 'preview-{}'.format(snippet.campaign.slug), 'weight': 100, 'content': { 'do_not_autoblock': True, 'text': 'snippet id {}'.format(snippet.id), 'links': {}, 'tall': False, 'icon': snippet.template_ng.icon.url, 'block_button_text': 'Remove this', } } self.assertEqual(generated_result, expected_result)
def test_render(self): snippet = ASRSnippetFactory.create( template_relation__text=( 'snippet id [[snippet_id]] and with ' '<a href="https://example.com/[[snippet_id]]/foo">link</a>'), targets=[ TargetFactory(jexl_expr='foo == bar'), TargetFactory(jexl_expr='lalo == true') ]) self.maxDiff = None generated_result = snippet.render() expected_result = { 'id': str(snippet.id), 'template': snippet.template_ng.code_name, 'template_version': snippet.template_ng.version, 'campaign': snippet.campaign.slug, 'weight': snippet.weight, 'content': { 'text': 'snippet id {} and with <link0>link</link0>'.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', }, 'targeting': 'foo == bar && lalo == true' } self.assertEqual(generated_result, expected_result)
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))
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_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_get_preview_url_admin(self): snippet = ASRSnippetFactory.create() expected_result = 'about:newtab?theme=light&dir=ltr&endpoint=http://admin.example.com' expected_result += reverse('asr-preview', kwargs={'uuid': snippet.uuid}) self.assertEqual(snippet.get_preview_url(), expected_result)
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_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_name(self): params = {'name': 'unicorn'} snippet = ASRSnippetFactory.create() self._assert_client_matches_snippets(params, [snippet])
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_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))
def test_get_admin_url(self): snippet = ASRSnippetFactory.create() self.assertTrue( snippet.get_admin_url().startswith('http://example.com')) self.assertTrue(snippet.get_admin_url(full=False).startswith('/'))
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_match_client_not_matching_name(self): params = {'name': 'unicorn'} snippet = ASRSnippetFactory.create() self._assert_client_matches_snippets(params, [snippet])
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_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_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])