def test_match_client_match_locale(self): params = {} snippet = SnippetFactory.create(on_release=True, on_startpage_4=True, locale_set=[SnippetLocale(locale='en-US')]) SnippetFactory.create(on_release=True, on_startpage_4=True, locale_set=[SnippetLocale(locale='fr')]) self._assert_client_matches_snippets(params, [snippet])
def test_match_client(self): params = {} snippet = SnippetFactory.create(on_release=True, on_startpage_4=True, locales=['en-US']) SnippetFactory.create(on_release=False, on_startpage_4=True, locales=['en-US']) self._assert_client_matches_snippets(params, [snippet])
def test_match_client_match_locale(self): params = {} snippet = SnippetFactory.create(on_release=True, on_startpage_4=True, locale_set=[SnippetLocale(locale='en-US')]) SnippetFactory.create(on_release=True, on_startpage_4=True, locale_set=[SnippetLocale(locale='fr')]) self._assert_client_matches_snippets(params, [snippet])
def test_match_client(self): params = {} snippet = SnippetFactory.create(on_release=True, on_startpage_4=True, locales=['en-us']) SnippetFactory.create(on_release=False, on_startpage_4=True, locales=['en-us']) self._assert_client_matches_snippets(params, [snippet])
def test_match_client_invalid_locale(self): """ If client sends invalid locale return snippets with no locales specified. """ params = {'locale': 'foo'} snippet = SnippetFactory.create(on_release=True, on_startpage_4=True, locales=[]) SnippetFactory.create(on_release=True, on_startpage_4=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 = SnippetFactory.create(on_release=True, on_startpage_4=True, locales=['es']) snippet_2 = SnippetFactory.create(on_release=True, on_startpage_4=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 = SnippetFactory.create(on_release=True, on_startpage_4=True, locales=[]) SnippetFactory.create(on_release=True, on_startpage_4=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 = SnippetFactory.create(on_release=True, on_startpage_4=True, locales=['es']) snippet_2 = SnippetFactory.create(on_release=True, on_startpage_4=True, locales=['es-mx']) self._assert_client_matches_snippets(params, [snippet_1, snippet_2])
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 = SnippetFactory.create( on_nightly=True, client_match_rules=[client_match_rule_pass_1]) snippet_2 = SnippetFactory.create( on_beta=True, on_nightly=True, client_match_rules=[client_match_rule_pass_2]) snippet_3 = SnippetFactory.create(on_nightly=True) # Not matching snippets. SnippetFactory.create(on_beta=True) SnippetFactory.create(on_nightly=True, client_match_rules=[client_match_rule_fail]) SnippetFactory.create(on_nightly=True, client_match_rules=[ client_match_rule_fail, client_match_rule_pass_2 ]) client = self._build_client(channel='nightly') snippets = Snippet.objects.match_client(client) self.assertEqual(set(snippets), set([snippet_1, snippet_2, snippet_3]))
def test_match_client_match_channel_partially(self): """ Client channels like "release-cck-mozilla14" should match "release". """ params = {'channel': 'release-cck-mozilla14'} snippet = SnippetFactory.create(on_release=True, on_startpage_4=True, locale_set=[SnippetLocale(locale='en-US')]) SnippetFactory.create(on_release=False, on_startpage_4=True, locale_set=[SnippetLocale(locale='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 = SnippetFactory.create(on_release=True, on_startpage_4=True, locale_set=[SnippetLocale(locale='en-US')]) SnippetFactory.create(on_release=False, on_startpage_4=True, locale_set=[SnippetLocale(locale='en-US')]) self._assert_client_matches_snippets(params, [snippet])
def test_base(self): snippets = SnippetFactory.create_batch(2) jsonsnippets = JSONSnippetFactory.create_batch(2) SnippetFactory.create(disabled=True) JSONSnippetFactory.create(disabled=True) response = views.ActiveSnippetsView.as_view()(self.request) eq_(response.get('content-type'), 'application/json') data = json.loads(response.content) eq_(set([snippets[0].id, snippets[1].id, jsonsnippets[0].id, jsonsnippets[1].id]), set([x['id'] for x in data]))
def test_base(self): snippets = SnippetFactory.create_batch(2) jsonsnippets = JSONSnippetFactory.create_batch(2) SnippetFactory.create(disabled=True) JSONSnippetFactory.create(disabled=True) response = views.ActiveSnippetsView.as_view()(self.request) eq_(response.get('content-type'), 'application/json') data = json.loads(response.content) eq_( set([ snippets[0].id, snippets[1].id, jsonsnippets[0].id, jsonsnippets[1].id ]), set([x['id'] for x in data]))
def test_encode_snippet(self): encoder = ActiveSnippetsEncoder() now = datetime.now() data = { 'id': 99, 'publish_start': now, 'name': 'Foo bar', 'countries': ['us', 'gr'] } snippet = SnippetFactory.create(**data) result = encoder.default(snippet) eq_( result, { 'id': 99, 'name': 'Foo bar', 'type': 'Desktop Snippet', 'template': snippet.template.name, 'publish_start': now, 'publish_end': None, 'on_release': True, 'on_beta': False, 'on_aurora': False, 'on_nightly': False, 'locales': ['en-us'], 'countries': ['gr', 'us'], 'weight': 100 })
def test_valid_disabled_snippet_authenticated(self): """Test disabled snippet returns 200 to authenticated users.""" snippet = SnippetFactory.create(published=False) User.objects.create_superuser('admin', '*****@*****.**', 'asdf') self.client.login(username='******', password='******') response = self.client.get(reverse('base.show', kwargs={'snippet_id': snippet.id})) self.assertEqual(response.status_code, 200)
def test_base(self): # Matching snippets. snippet_1 = SnippetFactory.create(on_nightly=True) # Matching but disabled snippet. SnippetFactory.create(on_nightly=True, disabled=True) # Snippet that doesn't match. SnippetFactory.create(on_nightly=False), params = self.client_params response = self.client.get('/{0}/'.format('/'.join(params))) snippets_json = json.dumps([snippet_1.to_dict()]) eq_(snippets_json, response.context['snippets_json']) eq_(response.context['locale'], 'en-US')
def test_no_locales(self): """ If the form being saved has no locale field, do not alter the snippet's locale. """ en_us = SnippetLocale(locale='en-us') fr = SnippetLocale(locale='fr') snippet = SnippetFactory.create(locale_set=[en_us, fr]) data = { 'name': 'test', 'data': '{}', 'template': snippet.template.id, 'priority': 0, 'weight': 100, } # FormClass has no locale field. class FormClass(ModelForm): class Meta: model = Snippet form = FormClass(data, instance=snippet) self._save_model(snippet, form) snippet = Snippet.objects.get(pk=snippet.pk) locales = (l.locale for l in snippet.locale_set.all()) eq_(set(locales), set(('en-us', 'fr')))
def test_base(self): # Matching snippets. snippet_1 = SnippetFactory.create(on_nightly=True) # Matching but disabled snippet. SnippetFactory.create(on_nightly=True, disabled=True) # Snippet that doesn't match. SnippetFactory.create(on_nightly=False), snippets_ok = [snippet_1] params = self.client_params response = self.client.get('/{0}/'.format('/'.join(params))) eq_(set(snippets_ok), set(response.context['snippets'])) eq_(response.context['locale'], 'en-US')
def test_base(self): # Matching snippets. snippet_1 = SnippetFactory.create(on_nightly=True) # Matching but disabled snippet. SnippetFactory.create(on_nightly=True, disabled=True) # Snippet that doesn't match. SnippetFactory.create(on_nightly=False), params = self.client_params response = self.client.get('/{0}/'.format('/'.join(params))) snippets_json = json.dumps([snippet_1.to_dict()]) eq_(snippets_json, response.context['snippets_json']) eq_(response.context['locale'], 'en-US')
def test_no_locales(self): """ If the form being saved has no locale field, do not alter the snippet's locale. """ en_us = SnippetLocale(locale='en-us') fr = SnippetLocale(locale='fr') snippet = SnippetFactory.create(locale_set=[en_us, fr]) data = { 'name': 'test', 'data': '{}', 'template': snippet.template.id, 'priority': 0, 'weight': 100, } # FormClass has no locale field. class FormClass(ModelForm): class Meta: model = Snippet fields = ('name', 'data', 'template', 'priority', 'weight') form = FormClass(data, instance=snippet) self._save_model(snippet, form) snippet = Snippet.objects.get(pk=snippet.pk) locales = (l.locale for l in snippet.locale_set.all()) self.assertEqual(set(locales), set(('en-us', 'fr')))
def test_base(self): # Matching snippets. snippet_1 = SnippetFactory.create(on_nightly=True) # Matching but disabled snippet. SnippetFactory.create(on_nightly=True, disabled=True) # Snippet that doesn't match. SnippetFactory.create(on_nightly=False), snippets_ok = [snippet_1] params = self.client_params response = self.client.get('/{0}/'.format('/'.join(params))) eq_(set(snippets_ok), set(response.context['snippets'])) eq_(response.context['locale'], 'en-US')
def test_valid_disabled_snippet_authenticated(self): """Test disabled snippet returns 200 to authenticated users.""" snippet = SnippetFactory.create(disabled=True) User.objects.create_superuser('admin', '*****@*****.**', 'asdf') self.client.login(username='******', password='******') response = self.client.get(reverse('base.show', kwargs={'snippet_id': snippet.id})) eq_(response.status_code, 200)
def test_render_snippet_id(self): """Include the snippet ID in the template context when rendering.""" snippet = SnippetFactory.create( template__code='<p>{{ snippet_id }}</p>') snippet.template.render = Mock() snippet.render() snippet.template.render.assert_called_with({'snippet_id': snippet.id})
def test_publish_date_filters(self, mock_datetime): """ If it is currently outside of the publish times for a snippet, it should not be included in the response. """ mock_datetime.utcnow.return_value = datetime(2013, 4, 5) # Passing snippets. snippet_no_dates = SnippetFactory.create(on_release=True) snippet_after_start_date = SnippetFactory.create( on_release=True, publish_start=datetime(2013, 3, 6) ) snippet_before_end_date = SnippetFactory.create( on_release=True, publish_end=datetime(2013, 6, 6) ) snippet_within_range = SnippetFactory.create( on_release=True, publish_start=datetime(2013, 3, 6), publish_end=datetime(2013, 6, 6) ) # Failing snippets. SnippetFactory.create( # Before start date. on_release=True, publish_start=datetime(2013, 5, 6) ) SnippetFactory.create( # After end date. on_release=True, publish_end=datetime(2013, 3, 6) ) SnippetFactory.create( # Outside range. on_release=True, publish_start=datetime(2013, 6, 6), publish_end=datetime(2013, 7, 6) ) params = ('4', 'Firefox', '23.0a1', '20130510041606', 'Darwin_Universal-gcc3', 'en-US', 'release', 'Darwin%2010.8.0', 'default', 'default_version') response = self.client.get('/{0}/'.format('/'.join(params))) expected = set([snippet_no_dates, snippet_after_start_date, snippet_before_end_date, snippet_within_range]) eq_(expected, set(response.context['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 = SnippetFactory.create(on_nightly=True) # Snippets that don't match nightly SnippetFactory.create(on_beta=True) nightly_client = self._build_client(channel='nightly') nightly_snippets = Snippet.cached_objects.match_client(nightly_client) default_client = self._build_client(channel='default') default_snippets = Snippet.cached_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. eq_(set([nightly_snippet]), set(nightly_snippets)) eq_(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 = SnippetFactory.create(on_nightly=True) # Snippets that don't match nightly SnippetFactory.create(on_beta=True) nightly_client = self._build_client(channel='nightly') nightly_snippets = Snippet.cached_objects.match_client(nightly_client) default_client = self._build_client(channel='default') default_snippets = Snippet.cached_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. eq_(set([nightly_snippet]), set(nightly_snippets)) eq_(set([nightly_snippet]), set(default_snippets))
def test_esr_is_same_as_release(self): """ Make sure that esr channel follows release. """ # Snippets matching nightly (and therefor should match default). release_snippet = SnippetFactory.create(on_release=True) # Snippets that don't match nightly SnippetFactory.create(on_release=False, on_beta=True) release_client = self._build_client(channel='release') release_snippets = Snippet.objects.match_client(release_client) esr_client = self._build_client(channel='esr') esr_snippets = Snippet.objects.match_client(esr_client) # Assert that both the snippets returned from release and from esr # are the same snippets. Just `release_snippet` in this case. self.assertEqual(set([release_snippet]), set(release_snippets)) self.assertEqual(set([release_snippet]), set(esr_snippets))
def test_base(self): # Matching snippets. snippet_1 = SnippetFactory.create(on_nightly=True) # Matching but disabled snippet. SnippetFactory.create(on_nightly=True, disabled=True) # Snippet that doesn't match. SnippetFactory.create(on_nightly=False), snippets_ok = [snippet_1] params = ('4', 'Firefox', '23.0a1', '20130510041606', 'Darwin_Universal-gcc3', 'en-US', 'nightly', 'Darwin%2010.8.0', 'default', 'default_version') response = self.client.get('/{0}/'.format('/'.join(params))) eq_(set(snippets_ok), set(response.context['snippets'])) eq_(response.context['locale'], 'en-US')
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 = SnippetFactory.create(on_release=True, on_startpage_4=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 = SnippetFactory.create(on_release=True, on_startpage_4=True, locales=['es', 'es-mx']) self._assert_client_matches_snippets(params, [snippet])
def test_render_campaign(self): template = SnippetTemplateFactory.create() template.render = Mock() template.render.return_value = '<a href="asdf">qwer</a>' data = '{"url": "asdf", "text": "qwer"}' snippet = SnippetFactory.create(template=template, data=data, campaign='foo') expected = Markup('<div data-snippet-id="{id}" data-weight="100" ' 'data-campaign="foo" class="snippet-metadata">' '<a href="asdf">qwer</a></div>'.format(id=snippet.id)) self.assertEqual(snippet.render().strip(), expected)
def test_render_data_with_snippet_id(self): """ Any strings included in the template context should have the substring "[[snippet_id]]" replaced with the ID of the snippet. """ snippet = SnippetFactory.create( template__code='<p>{{ code }}</p>', data='{"code": "snippet id [[snippet_id]]", "foo": true}') snippet.template.render = Mock() snippet.render() snippet.template.render.assert_called_with({'code': 'snippet id {0}'.format(snippet.id), 'snippet_id': snippet.id, 'foo': True})
def test_match_client_multiple_locale_matches(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. """ es_mx = SnippetLocale(locale='es-mx') es = SnippetLocale(locale='es') snippet = SnippetFactory.create(locale_set=[es_mx, es]) client = self._build_client(locale='es-MX') snippets = Snippet.cached_objects.match_client(client) # Filter out any snippets that aren't the one we made, and ensure there's only one. eq_(len([s for s in snippets if s.pk == snippet.pk]), 1)
def test_to_dict(self): snippet = SnippetFactory.create(weight=60, campaign='foo-campaign', countries=['gr', 'it']) snippet.render = Mock() snippet.render.return_value = 'rendered' data = { 'code': 'rendered', 'campaign': 'foo-campaign', 'weight': 60, 'countries': [u'gr', u'it'], 'exclude_from_search_engines': [], 'id': snippet.id, } self.assertEqual(data, snippet.to_dict())
def test_render_campaign(self): template = SnippetTemplateFactory.create() template.render = Mock() template.render.return_value = '<a href="asdf">qwer</a>' data = '{"url": "asdf", "text": "qwer"}' snippet = SnippetFactory.create(template=template, data=data, campaign='foo') expected = Markup( '<div data-snippet-id="{id}" data-weight="100" ' 'data-campaign="foo" class="snippet-metadata">' '<a href="asdf">qwer</a></div>'.format(id=snippet.id)) self.assertEqual(snippet.render().strip(), expected)
def test_render_no_country(self): """ If the snippet isn't geolocated, don't include the data-country attribute. """ template = SnippetTemplateFactory.create() template.render = Mock() template.render.return_value = '<a href="asdf">qwer</a>' data = '{"url": "asdf", "text": "qwer"}' snippet = SnippetFactory.create(template=template, data=data) expected = ('<div data-snippet-id="{0}" data-weight="100">' '<a href="asdf">qwer</a></div>'.format(snippet.id)) eq_(snippet.render().strip(), expected)
def test_render_no_country(self): """ If the snippet isn't geolocated, don't include the data-country attribute. """ template = SnippetTemplateFactory.create() template.render = Mock() template.render.return_value = '<a href="asdf">qwer</a>' data = '{"url": "asdf", "text": "qwer"}' snippet = SnippetFactory.create(template=template, data=data) expected = ('<div data-snippet-id="{0}" data-weight="100">' '<a href="asdf">qwer</a></div>' .format(snippet.id)) eq_(snippet.render().strip(), expected)
def test_render_multiple_countries(self): """ Include multiple countries in data-countries """ template = SnippetTemplateFactory.create() template.render = Mock() template.render.return_value = '<a href="asdf">qwer</a>' data = '{"url": "asdf", "text": "qwer"}' snippet = SnippetFactory.create(template=template, data=data, countries=['us', 'el']) expected = Markup( '<div data-snippet-id="{0}" data-weight="100" data-campaign="" ' 'class="snippet-metadata" data-countries="el,us">' '<a href="asdf">qwer</a></div>'.format(snippet.id)) self.assertEqual(snippet.render().strip(), expected)
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 = SnippetFactory.create(on_nightly=True, client_match_rules=[client_match_rule_pass_1]) snippet_2 = SnippetFactory.create(on_beta=True, on_nightly=True, client_match_rules=[client_match_rule_pass_2]) snippet_3 = SnippetFactory.create(on_nightly=True) # Not matching snippets. SnippetFactory.create(on_beta=True) SnippetFactory.create(on_nightly=True, client_match_rules=[client_match_rule_fail]) SnippetFactory.create(on_nightly=True, client_match_rules=[client_match_rule_fail, client_match_rule_pass_2]) client = self._build_client(channel='nightly') snippets = Snippet.cached_objects.match_client(client) eq_(set(snippets), set([snippet_1, snippet_2, snippet_3]))
def test_render(self): template = SnippetTemplateFactory.create() template.render = Mock() template.render.return_value = '<a href="asdf">qwer</a>' data = '{"url": "asdf", "text": "qwer"}' snippet = SnippetFactory.create(template=template, data=data, country='us', weight=60) expected = ('<div data-snippet-id="{id}" data-weight="60" class="snippet-metadata" ' 'data-country="us"><a href="asdf">qwer</a></div>'.format(id=snippet.id)) eq_(snippet.render().strip(), expected) template.render.assert_called_with({ 'url': 'asdf', 'text': 'qwer', 'snippet_id': snippet.id })
def test_render(self): template = SnippetTemplateFactory.create() template.render = Mock() template.render.return_value = '<a href="asdf">qwer</a>' data = '{"url": "asdf", "text": "qwer"}' snippet = SnippetFactory.create(template=template, data=data, country='us', weight=60) expected = ('<div data-snippet-id="{0}" data-weight="60" data-country="us">' '<a href="asdf">qwer</a></div>'.format(snippet.id)) eq_(snippet.render().strip(), expected) template.render.assert_called_with({ 'url': 'asdf', 'text': 'qwer', 'snippet_id': snippet.id })
def test_render_data_with_snippet_id(self): """ Any strings included in the template context should have the substring "[[snippet_id]]" replaced with the ID of the snippet. """ snippet = SnippetFactory.create( template__code='<p>{{ code }}</p>', data='{"code": "snippet id [[snippet_id]]", "foo": true}') snippet.template.render = Mock() snippet.render() snippet.template.render.assert_called_with({ 'code': 'snippet id {0}'.format(snippet.id), 'snippet_id': snippet.id, 'foo': True })
def test_render_multiple_countries(self): """ Include multiple countries in data-countries """ template = SnippetTemplateFactory.create() template.render = Mock() template.render.return_value = '<a href="asdf">qwer</a>' data = '{"url": "asdf", "text": "qwer"}' snippet = SnippetFactory.create(template=template, data=data, countries=['us', 'el']) expected = Markup( '<div data-snippet-id="{0}" data-weight="100" data-campaign="" ' 'class="snippet-metadata" data-countries="el,us">' '<a href="asdf">qwer</a></div>'.format(snippet.id)) self.assertEqual(snippet.render().strip(), expected)
def test_encode_snippet(self): encoder = ActiveSnippetsEncoder() now = datetime.now() data = {'id': 99, 'publish_start': now, 'name': 'Foo bar', 'countries': ['us', 'gr']} snippet = SnippetFactory.create(**data) result = encoder.default(snippet) self.assertEqual(result, {'id': 99, 'name': 'Foo bar', 'type': 'Desktop Snippet', 'template': snippet.template.name, 'publish_start': now, 'publish_end': None, 'on_release': True, 'on_beta': False, 'on_aurora': False, 'on_nightly': False, 'locales': [u'en-us'], 'countries': [u'us', u'gr'], 'weight': 100 })
def test_render_exclude_search_engines(self): """ If the snippet must get excluded from search engines, include the data-exclude-from-search-engines attribute. """ template = SnippetTemplateFactory.create() template.render = Mock() template.render.return_value = '<a href="asdf">qwer</a>' data = '{"url": "asdf", "text": "qwer"}' snippet = SnippetFactory.create(template=template, data=data) search_providers = SearchProviderFactory.create_batch(2) snippet.exclude_from_search_providers.add(*search_providers) engines = ','.join(map(lambda x: x.identifier, search_providers)) expected = Markup( '<div data-snippet-id="{id}" data-weight="100" data-campaign="" ' 'class="snippet-metadata" data-exclude-from-search-engines="{engines}">' '<a href="asdf">qwer</a></div>'.format(id=snippet.id, engines=engines)) self.assertEqual(snippet.render().strip(), expected)
def test_filter_by_available(self): snippet_match_1 = SnippetFactory.create() snippet_match_2 = (SnippetFactory.create(publish_start=datetime(2012, 05, 15, 0, 0))) # Snippet that starts later. SnippetFactory.create(publish_start=datetime(2012, 07, 01, 0, 0)) # Snippet that ended. SnippetFactory.create(publish_end=datetime(2012, 05, 01, 0, 0)) with patch('snippets.base.managers.datetime') as datetime_mock: datetime_mock.utcnow.return_value = datetime(2012, 06, 01, 0, 0) matching_snippets = self.manager.all().filter_by_available() eq_(set([snippet_match_1, snippet_match_2]), set(matching_snippets))
def test_filter_by_available(self): snippet_match_1 = SnippetFactory.create() snippet_match_2 = (SnippetFactory.create(publish_start=datetime(2012, 5, 15, 0, 0))) # Snippet that starts later. SnippetFactory.create(publish_start=datetime(2012, 7, 1, 0, 0)) # Snippet that ended. SnippetFactory.create(publish_end=datetime(2012, 5, 1, 0, 0)) with patch('snippets.base.managers.datetime') as datetime_mock: datetime_mock.utcnow.return_value = datetime(2012, 6, 1, 0, 0) matching_snippets = self.manager.all().filter_by_available() self.assertEqual(set([snippet_match_1, snippet_match_2]), set(matching_snippets))
def test_render_exclude_search_engines(self): """ If the snippet must get excluded from search engines, include the data-exclude-from-search-engines attribute. """ template = SnippetTemplateFactory.create() template.render = Mock() template.render.return_value = '<a href="asdf">qwer</a>' data = '{"url": "asdf", "text": "qwer"}' snippet = SnippetFactory.create(template=template, data=data) search_providers = SearchProviderFactory.create_batch(2) snippet.exclude_from_search_providers.add(*search_providers) engines = ','.join(map(lambda x: x.identifier, search_providers)) expected = Markup( '<div data-snippet-id="{id}" data-weight="100" data-campaign="" ' 'class="snippet-metadata" data-exclude-from-search-engines="{engines}">' '<a href="asdf">qwer</a></div>'.format(id=snippet.id, engines=engines)) self.assertEqual(snippet.render().strip(), expected)
def test_to_dict(self): snippet = SnippetFactory.create(weight=60, campaign='foo-campaign', countries=['gr', 'it'], client_options={'foo': 'bar'}) snippet.render = Mock() snippet.render.return_value = 'rendered' data = { 'code': 'rendered', 'client_options': { 'foo': 'bar' }, 'campaign': 'foo-campaign', 'weight': 60, 'countries': [u'gr', u'it'], 'exclude_from_search_engines': [], 'id': snippet.id, 'name': snippet.name, } self.assertEqual(data, snippet.to_dict())
def test_save_model_locales(self): """ save_model should delete any locales that were removed from the snippet and save any locales that were added. """ en_us = SnippetLocale(locale='en-us') fr = SnippetLocale(locale='fr') snippet = SnippetFactory.create(locale_set=[en_us, fr]) data = { 'name': 'test', 'data': '{}', 'template': snippet.template.id, 'locales': ['en-us', 'de'], 'priority': 0, 'weight': 100, } form = SnippetAdminForm(data, instance=snippet) self._save_model(snippet, form) snippet = Snippet.objects.get(pk=snippet.pk) locales = (l.locale for l in snippet.locale_set.all()) self.assertEqual(set(locales), set(('en-us', 'de')))
def test_save_model_locales(self): """ save_model should delete any locales that were removed from the snippet and save any locales that were added. """ en_us = SnippetLocale(locale='en-us') fr = SnippetLocale(locale='fr') snippet = SnippetFactory.create(locale_set=[en_us, fr]) data = { 'name': 'test', 'data': '{}', 'template': snippet.template.id, 'locales': ['en-us', 'de'], 'priority': 0, 'weight': 100, } form = SnippetAdminForm(data, instance=snippet) self._save_model(snippet, form) snippet = Snippet.objects.get(pk=snippet.pk) locales = (l.locale for l in snippet.locale_set.all()) eq_(set(locales), set(('en-us', 'de')))
def test_filter(self): SnippetFactory.create(on_nightly=True) response = self.client.get(urlparams(reverse('base.index'), on_nightly=2)) eq_(response.status_code, 200) eq_(response.context['snippets'].paginator.count, 1)
def setUp(self): for i in range(10): SnippetFactory.create()
def test_valid_disabled_snippet_unauthenticated(self): """Test disabled snippet returns 404 to unauthenticated users.""" snippet = SnippetFactory.create(disabled=True) response = self.client.get(reverse('base.show', kwargs={'snippet_id': snippet.id})) eq_(response.status_code, 404)
def test_valid_snippet(self): """Test show of snippet.""" snippet = SnippetFactory.create() response = self.client.get(reverse('base.show', kwargs={'snippet_id': snippet.id})) eq_(response.status_code, 200)
def test_filter_by_available(self): snippet_match_1 = SnippetFactory.create() snippet_match_2 = (SnippetFactory.create(publish_start=datetime(2012, 05, 15, 0, 0)))
def test_filter(self): SnippetFactory.create(on_nightly=True) response = self.client.get( urlparams(reverse('base.index'), on_nightly=2)) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['snippets'].paginator.count, 1)
def setUp(self): for i in range(10): SnippetFactory.create()
def test_uuid_snippet(self): snippet = SnippetFactory.create(disabled=True) response = self.client.get( reverse('base.show_uuid', kwargs={'snippet_id': snippet.uuid})) self.assertEqual(response.status_code, 200)
def test_valid_disabled_snippet_unauthenticated(self): """Test disabled snippet returns 404 to unauthenticated users.""" snippet = SnippetFactory.create(disabled=True) response = self.client.get( reverse('base.show', kwargs={'snippet_id': snippet.id})) self.assertEqual(response.status_code, 404)