def test_lists_live_articles_in_stream(api_client): # Create the articles with freeze_time('2012-01-14 12:00:02'): article_a = ArticlePageFactory(article_title='Article A', article_teaser='Descriptive text', article_body_text='Body text', last_published_at=timezone.now(), slug='article-a') with freeze_time('2012-01-14 12:00:02'): article_b = ArticlePageFactory(article_title='Article B', article_teaser='Descriptive text', article_body_text='Body text', last_published_at=timezone.now(), slug='article-b') with freeze_time('2012-01-14 12:00:01'): article_c = ArticlePageFactory(article_title='Article C', article_teaser='Descriptive text', article_body_text='Body text', last_published_at=timezone.now(), slug='article-c') with freeze_time('2012-01-14 12:00:01'): ArticlePageFactory(article_title='Article D', article_teaser='Descriptive text', article_body_text='Body text', last_published_at=timezone.now(), slug='article-d', live=False) sender = auth_sender() response = api_client.get( URL, content_type='', HTTP_AUTHORIZATION=sender.request_header, HTTP_X_FORWARDED_FOR='1.2.3.4, 123.123.123.123', ) items = response.json()['orderedItems'] id_prefix = 'dit:cms:Article:' assert len(items) == 3 assert article_attribute(items[0], 'name') == 'Article C' assert article_attribute(items[0], 'id') == id_prefix + str(article_c.id) assert article_attribute(items[0], 'summary') == 'Descriptive text' assert article_attribute(items[0], 'content') == 'Body text' assert article_attribute(items[0], 'url') == \ environ["APP_URL_EXPORT_READINESS"] + '/article-c/' assert items[0]['published'] == '2012-01-14T12:00:01+00:00' assert article_attribute(items[1], 'name') == 'Article A' assert article_attribute(items[1], 'id') == id_prefix + str(article_a.id) assert items[1]['published'] == '2012-01-14T12:00:02+00:00' assert article_attribute(items[2], 'name') == 'Article B' assert article_attribute(items[2], 'id') == id_prefix + str(article_b.id) assert items[2]['published'] == '2012-01-14T12:00:02+00:00'
def test_editors_cannot_publish_child_pages(root_page): env = two_branches_with_users(root_page) draft_page = ArticlePageFactory( parent=env.landing_1, live=False ) revision = draft_page.save_revision( user=env.editor_1, submitted_for_moderation=True ) resp = env.editor_1_client.post( reverse('wagtailadmin_pages:approve_moderation', args=[revision.pk]) ) assert resp.status_code == status.HTTP_403_FORBIDDEN
def test_pagination(api_client, django_assert_num_queries): """The requests are paginated, ending on a article without a next key """ """ create 50 articles. Second set should appear in feed first. """ with freeze_time('2012-01-14 12:00:02'): for i in range(0, 25): ArticlePageFactory(article_title='article_' + str(i), article_teaser='Descriptive text', article_body_text='Body text', last_published_at=timezone.now(), slug='article-' + str(i)) with freeze_time('2012-01-14 12:00:01'): for i in range(25, 50): ArticlePageFactory(article_title='article_' + str(i), article_teaser='Descriptive text', article_body_text='Body text', last_published_at=timezone.now(), slug='article-' + str(i)) items = [] next_url = URL num_pages = 0 """ One query to pull items 0 -> 24, Two queries to pull items 25 -> 49 due to filter being used, No queries on final blank page """ # TODO: Improve performance of page.url, full_url, full_path # Since page.url needs to get the slugs of the article's parent # pages it is doing a TON of queries each time this endpoint is hit with django_assert_num_queries(53): while next_url: num_pages += 1 sender = auth_sender(url=next_url) response = api_client.get( next_url, content_type='', HTTP_AUTHORIZATION=sender.request_header, HTTP_X_FORWARDED_FOR='1.2.3.4, 123.123.123.123', ) response_json = response.json() items += response_json['orderedItems'] next_url = \ response_json['next'] if 'next' in response_json else \ None assert num_pages == 3 assert len(items) == 50 assert len(set([item['id'] for item in items])) == 50 # All unique assert article_attribute(items[49], 'name') == 'article_24'
def test_moderators_and_admins_can_publish_child_pages( branch_factory, root_page ): branch = branch_factory.get(root_page) draft_page = ArticlePageFactory(parent=branch.listing, live=False) revision = draft_page.save_revision( user=branch.user, submitted_for_moderation=True, ) resp = branch.client.post( reverse('wagtailadmin_pages:approve_moderation', args=[revision.pk]) ) assert resp.status_code == status.HTTP_302_FOUND assert resp.url == '/admin/'
def test_url_methods_use_tree_based_routing(root_page): # Checks that the full_path and full_url methods call get_tree_based_url # when uses_tree_based_routing is True domestic_homepage = HomePageFactory(parent=root_page) domestic_page_one = TopicLandingPageFactory( parent=domestic_homepage, slug='topic') domestic_page_two = ArticleListingPageFactory( parent=domestic_page_one, slug='list') domestic_page_three = ArticlePageFactory( parent=domestic_page_two, slug='article') Site.objects.all().delete() site = Site.objects.create( site_name='Great Domestic', hostname='domestic.trade.great', port=8007, root_page=domestic_homepage, ) RoutingSettings.objects.create( site=site, root_path_prefix='/domestic/c', include_port_in_urls=False, ) domestic_page_three.get_tree_based_url = mock.MagicMock( side_effect=domestic_page_three.get_tree_based_url ) # First check without tree based routing domestic_page_three.uses_tree_based_routing = False assert domestic_page_three.full_path == '/topic/list/article/' assert ( domestic_page_three.full_url == 'http://exred.trade.great:8007/topic/list/article/' ) domestic_page_three.get_tree_based_url.assert_not_called() domestic_page_three.get_tree_based_url.reset_mock() # Now check with tree based routing domestic_page_three.uses_tree_based_routing = True assert domestic_page_three.full_path == '/domestic/c/topic/list/article/' assert ( domestic_page_three.full_url == 'http://domestic.trade.great/domestic/c/topic/list/article/' ) domestic_page_three.get_tree_based_url.assert_called()
def test_related_article_page_serializer_has_pages(ParentPage, Serializer, root_page, rf): related_page_one = ArticlePageFactory(parent=root_page, slug='one') related_page_two = ArticlePageFactory(parent=root_page, slug='two') related_page_three = ArticlePageFactory(parent=root_page, slug='three') article = ParentPage( parent=root_page, slug='article-slug', related_page_one=related_page_one, related_page_two=related_page_two, related_page_three=related_page_three, ) serializer = Serializer(instance=article, context={'request': rf.get('/')}) assert len(serializer.data['related_pages']) == 3
def test_population_and_value_getting(root_page, django_assert_num_queries): domestic_homepage = HomePageFactory(parent=root_page) topic_page = TopicLandingPageFactory(parent=domestic_homepage, slug='topic') article_list_page = ArticleListingPageFactory(parent=topic_page, slug='list') article_page = ArticlePageFactory(parent=article_list_page, slug='article') Site.objects.all().delete() site = Site.objects.create( site_name='Great Domestic', hostname='domestic.trade.great', root_page=domestic_homepage, ) # Trigger population of site root paths cache Site.get_site_root_paths() # Prefetch content type for this page root_page.specific_class # With the two potential queries above out of the way, # population should only use as single datatbase query with django_assert_num_queries(1): result = PageIDCache.populate() # Check result looks as expected assert result == { 'by-path': { f'{site.id}:/': domestic_homepage.id, f'{site.id}:/topic/': topic_page.id, f'{site.id}:/topic/list/': article_list_page.id, f'{site.id}:/topic/list/article/': article_page.id }, 'by-slug': { 'EXPORT_READINESS:great-domestic-home': domestic_homepage.id, 'EXPORT_READINESS:topic': topic_page.id, 'EXPORT_READINESS:list': article_list_page.id, 'EXPORT_READINESS:article': article_page.id, }, } # Check get_for_path() result_1 = PageIDCache.get_for_path('/', site.id) assert result_1 == domestic_homepage.id result_2 = PageIDCache.get_for_path('/topic/list/article/', site.id) assert result_2 == article_page.id # Check invalid get_for_path() assert PageIDCache.get_for_path('123', 99) is None # Check get_for_slug() result_1 = PageIDCache.get_for_slug('topic', 'EXPORT_READINESS') assert result_1 == topic_page.id result_2 = PageIDCache.get_for_slug('article', 'EXPORT_READINESS') assert result_2 == article_page.id # Check invalid get_for_slug() assert PageIDCache.get_for_slug('abc', 'IMPORT_NOT_READINESS') is None
def test_get_tree_based_url(root_page): domestic_homepage = HomePageFactory(parent=root_page) domestic_page_one = TopicLandingPageFactory( parent=domestic_homepage, slug='topic') domestic_page_two = ArticleListingPageFactory( parent=domestic_page_one, slug='list') domestic_page_three = ArticlePageFactory( parent=domestic_page_two, slug='article') Site.objects.all().delete() site = Site.objects.create( site_name='Great Domestic', hostname='domestic.trade.great', port=8007, root_page=domestic_homepage, ) routing_settings = RoutingSettings.objects.create( site=site, root_path_prefix='/domestic/c', include_port_in_urls=False, ) assert domestic_page_two.get_tree_based_url() == '/domestic/c/topic/list/' assert ( domestic_page_three.get_tree_based_url() == '/domestic/c/topic/list/article/' ) # Test include_site_url assert ( domestic_page_two.get_tree_based_url(include_site_url=True) == 'http://domestic.trade.great/domestic/c/topic/list/' ) # Test RoutingSettings.include_port_in_urls routing_settings.include_port_in_urls = True routing_settings.save() assert ( domestic_page_two.get_tree_based_url(include_site_url=True) == 'http://domestic.trade.great:8007/domestic/c/topic/list/' )
def test_page_paths(root_page): invest_app = InvestAppFactory(parent=root_page) invest_page_one = SectorLandingPageFactory(parent=invest_app) invest_page_two = SectorPageFactory(slug='foo', parent=invest_page_one) invest_page_three = SectorPageFactory(slug='bar', parent=invest_page_two) assert invest_page_two.full_path == '/industries/foo/' assert invest_page_three.full_path == '/industries/foo/bar/' fas_app = FindASupplierAppFactory(parent=root_page) fas_industry_landing_page = IndustryLandingPageFactory(parent=fas_app) fas_industry_one = IndustryPageFactory( slug='foo', parent=fas_industry_landing_page) fas_industry_two = IndustryPageFactory( slug='bar', parent=fas_industry_landing_page) fas_industry_article = IndustryArticlePageFactory( slug='article', parent=fas_industry_two) assert fas_industry_one.full_path == '/industries/foo/' assert fas_industry_two.full_path == '/industries/bar/' assert fas_industry_article.full_path == '/industry-articles/article/' domestic_homepage = HomePageFactory(parent=root_page) domestic_page_one = TopicLandingPageFactory( parent=domestic_homepage, slug='topic') domestic_page_two = ArticleListingPageFactory( parent=domestic_page_one, slug='list') domestic_page_three = ArticlePageFactory( parent=domestic_page_two, slug='article') assert domestic_page_two.full_path == '/topic/list/' assert domestic_page_three.full_path == '/topic/list/article/' domestice_site_policy = SitePolicyPagesFactory(parent=domestic_homepage) domestic_cookies_one = PrivacyAndCookiesPageFactory( slug='privacy', parent=domestice_site_policy) domestic_cookies_two = PrivacyAndCookiesPageFactory( slug='cookies', parent=domestic_cookies_one) assert domestic_cookies_one.full_path == '/privacy/' assert domestic_cookies_two.full_path == '/privacy/cookies/' international_homepage = InternationalHomePageFactory(parent=root_page) international_page_one = InternationalTopicLandingPageFactory( parent=international_homepage, slug='topic') international_page_two = InternationalArticleListingPageFactory( parent=international_page_one, slug='list') international_page_three = InternationalArticlePageFactory( parent=international_page_two, slug='article') assert international_page_two.full_path == '/topic/list/' assert international_page_three.full_path == '/topic/list/article/'
def article_page(): return ArticlePageFactory()