示例#1
0
def test_api_falls_back_to_english_when_translations_unavailable(
        client, untranslated_page, site_with_untranslated_page_as_root,
        language_code):
    cache.rebuild_all_cache()
    # to be added as a query params to all requests
    languge_query_params = {'lang': language_code}

    # looking up by id
    url = reverse('api:api:pages:detail', kwargs={'pk': untranslated_page.pk})
    response = client.get(url, languge_query_params)
    assert response.status_code == 200
    assert response.json()['title'] == 'ENGLISH'

    # looking up by site_id + path
    # NOTE: path should be blank when you want a site root page
    url = reverse('api:lookup-by-path',
                  kwargs={
                      'path': '',
                      'site_id': site_with_untranslated_page_as_root.id
                  })
    response = client.get(url, languge_query_params)
    assert response.status_code == 200
    assert response.json()['title'] == 'ENGLISH'

    # looking up by service_name + slug
    url = reverse('api:lookup-by-slug',
                  kwargs={'slug': untranslated_page.slug})
    query_params = {'service_name': 'GREAT_INTERNATIONAL'}
    query_params.update(languge_query_params)
    response = client.get(url, query_params)
    assert response.status_code == 200
    assert response.json()['title'] == 'ENGLISH'
示例#2
0
def test_api_translations_are_loaded_when_available(
        client, translated_page, site_with_translated_page_as_root,
        language_code, expected_title):
    cache.rebuild_all_cache()

    # to be added as a query params to all requests
    languge_query_params = {'lang': language_code}

    # looking up by id
    url = reverse('api:api:pages:detail', kwargs={'pk': translated_page.pk})
    response = client.get(url, languge_query_params)
    assert response.status_code == 200
    assert response.json()['title'] == expected_title

    # looking up by path and site_id
    # NOTE: path should be blank when you want a site root page
    url = reverse('api:lookup-by-path',
                  kwargs={
                      'path': '',
                      'site_id': site_with_translated_page_as_root.id,
                  })
    response = client.get(url, languge_query_params)
    assert response.status_code == 200
    assert response.json()['title'] == expected_title

    # looking up by slug and service_name
    url = reverse('api:lookup-by-slug', kwargs={'slug': translated_page.slug})
    query_params = {'service_name': cms.GREAT_INTERNATIONAL}
    query_params.update(languge_query_params)
    response = client.get(url, query_params)
    assert response.status_code == 200
    assert response.json()['title'] == expected_title
示例#3
0
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
示例#4
0
def test_international_campaign_page(admin_client, international_root_page):
    campaign_page = factories.InternationalCampaignPageFactory(
        parent=international_root_page)
    cache.rebuild_all_cache()

    url = reverse('api:api:pages:detail', kwargs={'pk': campaign_page.pk})
    response = admin_client.get(url)
    assert response.status_code == 200
示例#5
0
def test_translations_exposed(translated_page, settings, client):
    cache.rebuild_all_cache()

    url = reverse('api:api:pages:detail', kwargs={'pk': translated_page.pk})

    response = client.get(url)

    expected = [[code, label] for code, label in settings.LANGUAGES_LOCALIZED]

    assert response.json()['meta']['languages'] == expected
示例#6
0
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)
示例#7
0
def test_lookup_by_slug(translated_page, admin_client):
    cache.rebuild_all_cache()

    url = reverse('api:lookup-by-slug',
                  kwargs={
                      'slug': translated_page.slug,
                  })

    response = admin_client.get(url, {'service_name': cms.GREAT_INTERNATIONAL})

    assert response.status_code == 200
    assert response.json()['id'] == translated_page.id
示例#8
0
def test_add_page_prepopulate(is_edit, expected_template,
                              international_root_page, translated_page,
                              admin_client, image, cluster_data):
    cache.rebuild_all_cache()
    cache.PageIDCache.populate()
    url = reverse('preload-add-page')
    model_as_dict = model_to_dict(translated_page,
                                  exclude=[
                                      'go_live_at',
                                      'expire_at',
                                      'slug',
                                  ])
    model_as_dict = {key: val for key, val in model_as_dict.items() if val}
    post_data = {
        **model_as_dict,
        '(image)hero_image': image.file.name,
        '(image)introduction_column_one_icon': image.file.name,
        '(image)introduction_column_two_icon': image.file.name,
        '(image)introduction_column_three_icon': image.file.name,
        'management-app_label': translated_page._meta.app_label,
        'management-model_name': translated_page._meta.model_name,
        'management-parent_path': international_root_page.get_url_parts()[2],
        'management-site_name': international_root_page.get_site().site_name,
        **cluster_data,
    }
    expected_data = {
        **model_as_dict,
        'hero_image': str(image.pk),
        'introduction_column_one_icon': str(image.pk),
        'introduction_column_two_icon': str(image.pk),
        'introduction_column_three_icon': str(image.pk),
    }
    if is_edit:
        post_data['management-path'] = expected_data[
            'path'] = translated_page.get_url_parts()[2]

    response = admin_client.post(url, clean_post_data(post_data))

    assert response.template_name == [expected_template]
    assert response.status_code == 200

    soup = BeautifulSoup(response.content, 'html.parser')
    for name, value in expected_data.items():
        element = soup.find(id='id_' + name)
        if not element or not value:
            continue
        if element.name == 'textarea':
            actual = element.contents[0].strip()
        elif element.name == 'select':
            actual = element.find_all('option', selected=True)[0].get('value')
        else:
            actual = element.get('value')
        assert str(actual) == str(value)
示例#9
0
def test_unserializable_page_requested(settings, client):
    page = ComponentsApp.objects.create(
        title_en_gb='the app',
        depth=2,
        path='/thing',
    )
    cache.rebuild_all_cache()

    url = reverse('api:api:pages:detail', kwargs={'pk': page.pk})

    response = client.get(url)

    assert response.status_code == 204
示例#10
0
def test_international_topic_landing_page_view_sectors_alphabetical_order(
        admin_client, international_root_page):
    landing_page = factories.InternationalTopicLandingPageFactory.create(
        parent=international_root_page)
    sector_page1 = factories.InternationalInvestmentSectorPageFactory.create(
        parent=landing_page, live=True, heading='acme')
    sector_page2 = factories.InternationalInvestmentSectorPageFactory.create(
        parent=landing_page, live=True, heading='foo')
    cache.rebuild_all_cache()

    url = reverse('api:api:pages:detail', kwargs={'pk': landing_page.pk})
    response = admin_client.get(url)
    assert response.status_code == 200
    assert [page['id'] for page in response.json()['child_pages']
            ] == [sector_page1.pk, sector_page2.pk]
示例#11
0
def test_international_topic_landing_page_view(admin_client,
                                               international_root_page):
    topic_landing_page = factories.InternationalTopicLandingPageFactory.create(
        parent=international_root_page, live=True)
    campaign_page = factories.InternationalCampaignPageFactory(
        parent=topic_landing_page, live=True)
    article_listing_page = factories.InternationalArticleListingPageFactory.create(
        parent=topic_landing_page, live=True)
    cache.rebuild_all_cache()

    url = reverse('api:api:pages:detail', kwargs={'pk': topic_landing_page.pk})
    response = admin_client.get(url)
    assert response.status_code == 200
    assert 'child_pages' in response.json()
    assert sorted([page['id'] for page in response.json()['child_pages']],
                  reverse=True) == [article_listing_page.pk, campaign_page.pk]
示例#12
0
def test_api_serves_drafts(client, page_with_reversion,
                           site_with_revised_page_as_root):
    cache.rebuild_all_cache()
    # For applying the draft token as a query param for each request
    param_name = permissions.DraftTokenPermisison.TOKEN_PARAM
    draft_query_params = {param_name: page_with_reversion.get_draft_token()}

    # first we'll get a non-draft response for comparison
    url = reverse('api:api:pages:detail',
                  kwargs={'pk': page_with_reversion.pk})
    response = client.get(url)
    assert response.status_code == 200
    data = response.json()
    assert data['title'] == 'published-title'
    assert data['meta']['url'] == page_with_reversion.get_url()

    # get draft version, looking up by id
    response = client.get(url, draft_query_params)
    assert response.status_code == 200
    data = response.json()
    assert data['title'] == 'draft-title'
    assert data['meta']['url'] == page_with_reversion.get_url(is_draft=True)

    # get draft version, looking up by site_id + path
    # NOTE: path should be blank when you want a site root page
    url = reverse('api:lookup-by-path',
                  kwargs={
                      'path': '',
                      'site_id': site_with_revised_page_as_root.id
                  })
    response = client.get(url, draft_query_params)
    assert response.status_code == 200
    data = response.json()
    assert data['title'] == 'draft-title'
    assert data['meta']['url'] == page_with_reversion.get_url(is_draft=True)

    # get draft version, looking up by service_name + slug
    url = reverse('api:lookup-by-slug',
                  kwargs={'slug': page_with_reversion.slug})
    query_params = {'service_name': cms.GREAT_INTERNATIONAL}
    query_params.update(draft_query_params)
    response = client.get(url, query_params)
    assert response.status_code == 200
    data = response.json()
    assert data['title'] == 'draft-title'
    assert data['meta']['url'] == page_with_reversion.get_url(is_draft=True)
示例#13
0
def test_international_homepage(admin_client, root_page):
    home_page = factories.InternationalHomePageFactory.create(parent=root_page)
    home_page.homepage_link_panels = [
        ('link_panel', {
            'title': 'panel one',
            'supporting_text': 'panel one supporting text',
            'link': {
                'external_link': 'http://example.com/one/',
            }
        }),
        ('link_panel', {
            'title': 'panel two',
            'supporting_text': 'panel two supporting text',
            'link': {
                'external_link': 'http://example.com/two/',
            }
        }),
    ]
    home_page.save()

    cache.rebuild_all_cache()

    url = reverse('api:api:pages:detail', kwargs={'pk': home_page.pk})
    response = admin_client.get(url)
    assert response.status_code == 200
    json_response = response.json()

    assert 'hero_title' in json_response

    homepage_link_panels = response.json()['homepage_link_panels']
    assert homepage_link_panels[0]['type'] == 'link_panel'
    assert homepage_link_panels[0]['value'] == {
        'title': 'panel one',
        'supporting_text': 'panel one supporting text',
        'link': 'http://example.com/one/',
    }
    assert homepage_link_panels[1]['type'] == 'link_panel'
    assert homepage_link_panels[1]['value'] == {
        'title': 'panel two',
        'supporting_text': 'panel two supporting text',
        'link': 'http://example.com/two/',
    }
示例#14
0
def test_international_trade_home_page_exposes_industries(
        admin_client, international_root_page):
    industry = factories.InternationalInvestmentSectorPageFactory(
        parent=international_root_page,
        live=True,
    )
    factories.InternationalInvestmentSectorPageFactory(
        parent=international_root_page,
        live=False,
    )
    homepage = factories.InternationalTradeHomePageFactory(
        live=True, parent=international_root_page)
    cache.rebuild_all_cache()

    url = reverse('api:api:pages:detail', kwargs={'pk': homepage.pk})
    response = admin_client.get(url)

    assert response.status_code == 200
    assert len(response.json()['industries']) == 1
    assert response.json()['industries'][0]['meta']['pk'] == industry.pk
示例#15
0
 def handle(self, *args, **options):
     rebuild_all_cache()