def test_basepage_can_exist_under(root_page): page = InternationalArticlePageFactory(parent=root_page) assert isinstance(page, BasePage) dummy_ctype = ContentType.objects.create(app_label='blah', model='blah') test_parent = Page(slug='basic', title='Page') test_parent.content_type = dummy_ctype assert page.can_exist_under(test_parent) is False
def test_rebuild_all_cache_task(mock_cache_populator): article1 = InternationalArticlePageFactory(live=True) article2 = InternationalArticlePageFactory(live=True) InternationalArticlePageFactory(live=False) cache.rebuild_all_cache() assert mock_cache_populator.populate_async.call_count == 5 # contains home page assert mock_cache_populator.populate_async.call_args_list[-2] == call( article1) assert mock_cache_populator.populate_async.call_args_list[-1] == call( article2)
def test_meta_field_draft(international_root_page, rf): article_page = InternationalArticlePageFactory( parent=international_root_page, slug='test-slug', ) serializer = InternationalArticlePageSerializer( instance=article_page, context={'is_draft': True} ) assert serializer.data['meta']['url'] == article_page.get_url(is_draft=True)
def test_meta_field_slug_translation(international_root_page, rf): article_page = InternationalArticlePageFactory( parent=international_root_page, slug='test-slug', ) with translation.override('de'): serializer = InternationalArticlePageSerializer( instance=article_page, context={'request': rf.get('/')} ) data = serializer.data['meta'] assert data == { 'draft_token': None, 'languages': [('en-gb', 'English')], 'url': 'http://great.gov.uk/international/content/test-slug/', 'localised_urls': [ ( 'en-gb', 'http://great.gov.uk/international/content/test-slug/' ) ], 'slug': 'test-slug', 'pk': article_page.pk, }
def test_lookup_by_path(international_root_page, page, admin_client): # Creating a semi-realistic page structure and moving page into it parent_page = InternationalArticlePageFactory( parent=international_root_page) page.move(target=parent_page, pos='last-child') cache.rebuild_all_cache() # to lookup page, the path should include the parent's slug and # the page's slug, but NOT that of app_root_page path = '/'.join([parent_page.slug, page.slug]) response = admin_client.get( reverse('api:lookup-by-path', kwargs={ 'site_id': '1', 'path': path })) assert response.status_code == 200 assert response.json()['id'] == page.id # paths are normalised by the view, so the presence of extra '/' # characters on either end of the value shouldn't hinder matching dodgy_path = '///' + path + '///' response = admin_client.get( reverse('api:lookup-by-path', kwargs={ 'site_id': '1', 'path': dodgy_path })) assert response.status_code == 200 assert response.json()['id'] == page.id
def test_get_site_creates_routing_settings_if_none_exist(root_page, django_assert_num_queries): page = InternationalArticlePageFactory(parent=root_page, slug='industry') site = Site.objects.create(hostname='example.gov', root_page=page) # running this first so that the query doesn't count toward # the total query count (the value is usually cached) page._get_site_root_paths() with django_assert_num_queries(2): # 1 query to get the site, 1 to create routing settings result_site = page.get_site() # Check the correct site was returned assert result_site == site # This attribute is set to reference the newly created object, # so this shouldn't result in another query result_site.routingsettings
def test_campaign_page_serializer_has_related_pages(international_root_page, rf): related_page_one = InternationalArticlePageFactory( parent=international_root_page, slug='one') related_page_two = InternationalArticlePageFactory( parent=international_root_page, slug='two') related_page_three = InternationalArticlePageFactory( parent=international_root_page, slug='three') article = InternationalCampaignPageFactory( parent=international_root_page, slug='article-slug', related_page_one=related_page_one, related_page_two=related_page_two, related_page_three=related_page_three) serializer = InternationalCampaignPageSerializer( instance=article, context={'request': rf.get('/')}) assert len(serializer.data['related_pages']) == 3
def test_markdown_to_html_field_without_slug_hyperlinks(international_root_page, rf): article_page = InternationalArticlePageFactory( parent=international_root_page, slug='test-slug', ) article_page.hero_text_en_gb = ( '[hyperlink](slug:{slug})'.format(slug=article_page.slug) ) class TestSerializer(Serializer): hero_text_en_gb = fields.MarkdownToHTMLField() serializer = TestSerializer( instance=article_page, context={'request': rf.get('/')} ) assert serializer.data == { 'hero_text_en_gb': ( '<p><a>hyperlink</a></p>' ) }
def test_base_page_serializer(rf, international_root_page): page = InternationalArticlePageFactory(slug='test-slug', parent=international_root_page) serializer = InternationalArticlePageSerializer( instance=page, context={'request': rf.get('/')}) assert serializer.data['id'] == page.id assert serializer.data['seo_title'] == page.seo_title assert serializer.data['search_description'] == page.search_description assert 'meta' in serializer.data # meta content tested in another test assert serializer.data['full_url'] == page.full_url assert serializer.data['full_path'] == page.full_path assert serializer.data['title'] == page.title assert serializer.data['page_type'] == 'InternationalArticlePage'
def test_cache_etags_mismatch(admin_client, international_root_page): service_name = cms.GREAT_INTERNATIONAL # given there exists a page that is cached page = InternationalArticlePageFactory.create( parent=international_root_page, live=True) # when the page is retrieved url = reverse('api:lookup-by-slug', kwargs={'slug': page.slug}) admin_client.get(url, {'service_name': service_name}) # then exposing the same etag in subsequent responses results in 304 response_two = admin_client.get( url, {'service_name': service_name}, HTTP_IF_NONE_MATCH='something-123', ) assert isinstance(response_two, CachedResponse) assert response_two.status_code == 200 assert response_two.content
def test_cache_miss_slow_database_read(admin_client, international_root_page): class SlowSerializer(Serializer): def to_representation(self, instance): time.sleep(3) return {} service_name = cms.GREAT_INTERNATIONAL page = InternationalArticlePageFactory.create( parent=international_root_page, live=True) url = reverse('api:lookup-by-slug', kwargs={'slug': page.slug}) # given the page is very slow to read with mock.patch.dict(serializer_mapping.MODELS_SERIALIZERS_MAPPING, {page.__class__: SlowSerializer}): response = admin_client.get(url, {'service_name': service_name}) # then the response results in 501 assert response.status_code == 501
def test_cache_etags_match(admin_client, international_root_page): service_name = cms.GREAT_INTERNATIONAL # given there exists a page that is cached page = InternationalArticlePageFactory.create( parent=international_root_page, live=True) url = reverse('api:lookup-by-slug', kwargs={'slug': page.slug}) admin_client.get(url, {'service_name': service_name}) # and the cached page is retrieved response_two = admin_client.get(url, {'service_name': service_name}) # then exposing the same etag in subsequent responses results in 304 response_three = admin_client.get( url, {'service_name': service_name}, HTTP_IF_NONE_MATCH=response_two['ETag'], ) assert response_three.status_code == 304 assert response_three.content == b''
def test_meta_field(rf, international_root_page): article_page = InternationalArticlePageFactory( parent=international_root_page, slug='test-slug', ) serializer = InternationalArticlePageSerializer( instance=article_page, context={'request': rf.get('/')} ) assert serializer.data['meta'] == { 'draft_token': None, 'languages': [('en-gb', 'English')], 'url': 'http://great.gov.uk/international/content/test-slug/', 'localised_urls': [ ( 'en-gb', 'http://great.gov.uk/international/content/test-slug/' ) ], 'slug': 'test-slug', 'pk': article_page.pk }
def test_get_site_returns_none_when_page_not_routable(root_page): Site.objects.all().delete() # ensures pages are not routable page = InternationalArticlePageFactory(parent=root_page, slug='industry') result = page.get_site() assert result is None