def test_breadcrumbs_mixin(mock_get_page, client, settings): settings.FEATURE_FLAGS['EXPORT_JOURNEY_ON'] = False url = reverse('create-an-export-plan-article', kwargs={'slug': 'foo'}) mock_get_page.return_value = create_response( status_code=200, json_body={'page_type': 'ArticlePage'}) response = client.get(url) breadcrumbs = response.context_data['breadcrumbs'] assert breadcrumbs == [ { 'url': '/advice/', 'label': 'Advice' }, { 'url': '/advice/create-an-export-plan/', 'label': 'Create an export plan' }, { 'url': '/advice/create-an-export-plan/foo/', 'label': 'Foo' }, ]
def test_article_article_detail_page_no_related_content( mock_get_page, client, settings ): settings.FEATURE_FLAGS['PROTOTYPE_PAGES_ON'] = True test_article_page_no_related_content = { "title": "Test article admin title", "article_title": "Test article", "article_teaser": "Test teaser", "article_image": {"url": "foobar.png"}, "article_body_text": "<p>Lorem ipsum</p>", "related_pages": [], "full_path": "/advice/manage-legal-and-ethical-compliance/foo/", "last_published_at": "2018-10-09T16:25:13.142357Z", "meta": { "slug": "foo", }, "page_type": "ArticlePage", } url = reverse( 'manage-legal-and-ethical-compliance-article', kwargs={'slug': 'foo'} ) mock_get_page.return_value = create_response( status_code=200, json_body=test_article_page_no_related_content ) response = client.get(url) assert response.status_code == 200 assert response.template_name == ['article/article_detail.html'] assert 'Related content' not in str(response.content)
def test_landing_page_header_footer(mock_get_page, client, settings): settings.FEATURE_FLAGS['PROTOTYPE_PAGES_ON'] = True settings.FEATURE_FLAGS['EXPORT_JOURNEY_ON'] = False url = reverse('landing-page') page = { 'news_title': 'News', 'news_description': '<p>Lorem ipsum</p>', 'articles': [], } mock_get_page.return_value = create_response(status_code=200, json_body=page) response = client.get(url) assert response.status_code == 200 assert '/static/js/home' in str(response.content) assert 'Create an export plan' in str(response.content) soup = BeautifulSoup(response.content, 'html.parser') assert soup.find(id="header-dit-logo")
def test_article_advice_page(mock_get_page, client, settings): settings.FEATURE_FLAGS['PROTOTYPE_PAGES_ON'] = True url = reverse('advice', kwargs={'slug': 'advice'}) mock_get_page.return_value = create_response( status_code=200, json_body=test_topic_page ) response = client.get(url) assert response.status_code == 200 assert response.template_name == ['article/topic_list.html'] assert test_topic_page['title'] not in str(response.content) assert test_topic_page['landing_page_title'] in str(response.content) assert 'Asia market information' in str(response.content) assert 'Africa market information' not in str(response.content) assert 'markets.jpg' in str(response.content) assert 'asia.jpg' in str(response.content) assert 'africa.jpg' not in str(response.content) assert '01 October 2018' in str(response.content)
def test_breadcrumbs_mixin(mock_get_page, client, settings): url = reverse('article-detail', kwargs={ 'topic': 'topic', 'list': 'bar', 'slug': 'foo'}) mock_get_page.return_value = create_response( status_code=200, json_payload={ 'page_type': 'InternationalArticlePage', 'meta': { 'slug': 'foo', 'languages': [('en-gb', 'English')], }, } ) response = client.get(url) breadcrumbs = response.context_data['breadcrumbs'] assert breadcrumbs == [ { 'url': '/international/', 'label': 'International' }, { 'url': '/international/topic/', 'label': 'Topic' }, { 'url': '/international/topic/bar/', 'label': 'Bar' }, { 'url': '/international/topic/bar/foo/', 'label': 'Foo' }, ]
def test_article_detail_page_related_content(mock_get_page, client, settings): article_page = { 'title': 'Test article admin title', 'article_title': 'Test article', 'article_teaser': 'Test teaser', 'article_image': { 'url': 'foobar.png' }, 'article_body_text': '<p>Lorem ipsum</p>', 'related_pages': [ { 'article_title': 'Related article 1', 'article_teaser': 'Related article 1 teaser', 'article_image_thumbnail': { 'url': 'related_article_one.jpg' }, 'full_path': '/test-list/test-one/', 'meta': { 'slug': 'test-one', } }, { 'article_title': 'Related article 2', 'article_teaser': 'Related article 2 teaser', 'article_image_thumbnail': { 'url': 'related_article_two.jpg' }, 'full_path': '/test-list/test-two/', 'meta': { 'slug': 'test-two', } }, ], 'meta': { 'languages': [('en-gb', 'English')], 'slug': 'foo', }, 'page_type': 'InternationalArticlePage', } url = reverse('article-detail', kwargs={ 'topic': 'topic', 'list': 'bar', 'slug': 'foo' }) mock_get_page.return_value = create_response(status_code=200, json_payload=article_page) response = client.get(url) assert response.status_code == 200 assert response.template_name == ['core/article_detail.html'] assert 'Related content' in str(response.content) soup = BeautifulSoup(response.content, 'html.parser') assert soup.find(id='related-article-test-one-link' ).attrs['href'] == '/international/test-list/test-one/' assert soup.find(id='related-article-test-two-link' ).attrs['href'] == '/international/test-list/test-two/' assert soup.find(id='related-article-test-one').select( 'h3')[0].text == 'Related article 1' assert soup.find(id='related-article-test-two').select( 'h3')[0].text == 'Related article 2'
def test_companies_house_search_api_error(mock_search, client): mock_search.return_value = create_response(400) url = reverse('api-internal-companies-house-search') with pytest.raises(requests.HTTPError): client.get(url, data={'term': 'thing'})
def auth_backend(): patch = mock.patch( 'directory_sso_api_client.sso_api_client.user.get_session_user', return_value=create_response(404)) yield patch.start() patch.stop()
def mock_update_company(): patch = mock.patch.object(api_client.company, 'profile_update', return_value=create_response()) yield patch.start() patch.stop()
def test_edit_page_submmit_error(client, mock_update_company, url, data, user): client.force_login(user) mock_update_company.return_value = create_response(status_code=400) with pytest.raises(HTTPError): client.post(url, data)
def mock_request_collaboration(client): patch = mock.patch.object(helpers.api_client.company, 'request_collaboration', return_value=create_response(200)) yield patch.start() patch.stop()
def test_list_officers(mock_client_get, api_client): expected = { "active_count": "integer", "etag": "string", "inactive_count": "integer", "items": [{ "address": { "address_line_1": "string", "address_line_2": "string", "care_of": "string", "country": "string", "locality": "string", "po_box": "string", "postal_code": "string", "premises": "string", "region": "string" }, "appointed_on": "date", "country_of_residence": "string", "date_of_birth": { "day": "integer", "month": "integer", "year": "integer" }, "former_names": [{ "forenames": "string", "surname": "string" }], "identification": { "identification_type": "string", "legal_authority": "string", "legal_form": "string", "place_registered": "string", "registration_number": "string" }, "links": { "officer": { "appointments": "string" } }, "name": "string", "nationality": "string", "occupation": "string", "officer_role": "string", "resigned_on": "date" }], "items_per_page": "integer", "kind": "string", "links": { "self": "string" }, "resigned_count": "integer", "start_index": "integer", "total_results": "integer" } mock_client_get.return_value = create_response(json_body=expected) url = reverse('api:company-officers', kwargs={'company_number': '11006939'}) response = api_client.get(url) assert response.status_code == status.HTTP_200_OK assert response.json() == expected
def test_article_detail_page_related_content( mock_get_page, client, settings ): settings.FEATURE_FLAGS['PROTOTYPE_PAGES_ON'] = True article_page = { "title": "Test article admin title", "article_title": "Test article", "article_teaser": "Test teaser", "article_image": {"url": "foobar.png"}, "article_body_text": "<p>Lorem ipsum</p>", "related_pages": [ { "article_title": "Related article 1", "article_teaser": "Related article 1 teaser", "article_image_thumbnail": {"url": "related_article_one.jpg"}, "full_path": "/markets/test/test-one", "meta": { "slug": "test-one", } }, { "article_title": "Related article 2", "article_teaser": "Related article 2 teaser", "article_image_thumbnail": {"url": "related_article_two.jpg"}, "full_path": "/markets/test/test-two", "meta": { "slug": "test-two", } }, ], "full_path": "/markets/foo/bar/", "last_published_at": "2018-10-09T16:25:13.142357Z", "meta": { "slug": "bar", }, "page_type": "ArticlePage", } url = reverse( 'manage-legal-and-ethical-compliance-article', kwargs={'slug': 'foo'} ) mock_get_page.return_value = create_response( status_code=200, json_body=article_page ) response = client.get(url) assert response.status_code == 200 assert response.template_name == ['article/article_detail.html'] assert 'Related content' in str(response.content) soup = BeautifulSoup(response.content, 'html.parser') assert soup.find( id='related-article-test-one-link' ).attrs['href'] == '/markets/test/test-one' assert soup.find( id='related-article-test-two-link' ).attrs['href'] == '/markets/test/test-two' assert soup.find( id='related-article-test-one' ).select('h3')[0].text == 'Related article 1' assert soup.find( id='related-article-test-two' ).select('h3')[0].text == 'Related article 2'
def mock_user_has_company(): patch = mock.patch.object(helpers.api_client.company, 'retrieve_private_profile', return_value=create_response(404)) yield patch.start() patch.stop()
def mock_claim_company(client): patch = mock.patch.object(helpers.api_client.enrolment, 'claim_prepeveried_company', return_value=create_response(200)) yield patch.start() patch.stop()
def mock_enrolment_send(client): patch = mock.patch.object(helpers.api_client.enrolment, 'send_form', return_value=create_response(201)) yield patch.start() patch.stop()
def test_marketing_campaign_campaign_page_all_fields(mock_get_page, client, settings): url = reverse('campaign', kwargs={'slug': 'test-page'}) mock_get_page.return_value = create_response( status_code=200, json_payload=campaign_page_all_fields) response = client.get(url) assert response.status_code == 200 assert response.template_name == ['core/campaign.html'] soup = BeautifulSoup(response.content, 'html.parser') assert ('<p class="body-text">Selling point two content</p>') in str( response.content) assert ('<p class="body-text">Selling point three content</p>') in str( response.content) hero_section = soup.find(id='campaign-hero') exp_style = "background-image: url('{}')".format( campaign_page_all_fields['campaign_hero_image']['url']) assert hero_section.attrs['style'] == exp_style assert soup.find( id='selling-points-icon-two' ).attrs['src'] == campaign_page_all_fields['selling_point_two_icon']['url'] assert soup.find(id='selling-points-icon-three').attrs[ 'src'] == campaign_page_all_fields['selling_point_three_icon']['url'] assert soup.find(id='section-one-contact-button').attrs[ 'href'] == campaign_page_all_fields['section_one_contact_button_url'] assert soup.find( id='section-one-contact-button' ).text == campaign_page_all_fields['section_one_contact_button_text'] assert soup.find(id='section-two-contact-button').attrs[ 'href'] == campaign_page_all_fields['section_two_contact_button_url'] assert soup.find( id='section-two-contact-button' ).text == campaign_page_all_fields['section_two_contact_button_text'] related_page_one = soup.find(id='related-page-article-1') assert related_page_one.find('a').text == 'Related article 1' assert related_page_one.find('p').text == 'Related article description 1' assert related_page_one.find('a').attrs['href'] == ( '/international/advice/finance/article-1/') assert related_page_one.find('img').attrs['src'] == ( 'article1_image_thumbnail.jpg') related_page_two = soup.find(id='related-page-article-2') assert related_page_two.find('a').text == 'Related article 2' assert related_page_two.find('p').text == 'Related article description 2' assert related_page_two.find('a').attrs['href'] == ( '/international/advice/finance/article-2/') assert related_page_two.find('img').attrs['src'] == ( 'article2_image_thumbnail.jpg') related_page_three = soup.find(id='related-page-article-3') assert related_page_three.find('a').text == 'Related article 3' assert related_page_three.find('p').text == 'Related article description 3' assert related_page_three.find('a').attrs['href'] == ( '/international/advice/finance/article-3/') assert related_page_three.find('img').attrs['src'] == ( 'article3_image_thumbnail.jpg')
def test_get_company_profile_not_ok(mock_get_company_profile): mock_get_company_profile.return_value = create_response(status_code=400) with pytest.raises(HTTPError): helpers.get_companies_house_profile('123456')
def test_get_company_profile_not_ok(mock_get_company_profile): mock_get_company_profile.return_value = create_response(400) with pytest.raises(HTTPError): helpers.get_company_profile('123456', {})
def mock_validate_company_number(client): patch = mock.patch.object(helpers.api_client.company, 'validate_company_number', return_value=create_response(200)) yield patch.start() patch.stop()
def test_get_company_admin_not_ok(mock_get_collaborator_list): mock_get_collaborator_list.return_value = create_response(status_code=400) with pytest.raises(HTTPError): helpers.get_company_admins('123456')
def mock_retrive_articles_read(): mock = patch( 'api_client.api_client.exportreadiness.bulk_create_article_read') mock.return_value = create_response(200, json_body=[]) yield mock.start() mock.stop()
def mock_collaborator_role_update(): patch = mock.patch.object(api_client.company, 'collaborator_role_update', return_value=create_response()) yield patch.start() patch.stop()
def test_retrieve_company_profile_mixin_not_ok(company_profile): company_profile.return_value = create_response(status_code=502) user = models.SSOUser() with pytest.raises(HTTPError): user.company
def mock_case_study_create(): patch = mock.patch.object(api_client.company, 'case_study_create', return_value=create_response(201)) yield patch.start() patch.stop()
def test_get_mobile_number_with_no_details(user, company_profile): # Inherit the company_profile fixture and overwrite the company # information user.mobile_phone_number = "" company_profile.return_value = create_response({}) assert user.get_mobile_number() == ''
def test_high_potential_opportunity_form_submmit_cms_retrieval_ok( mock_lookup_by_path, mock_save, mock_action_class, settings, rf, captcha_stub): mock_lookup_by_path.return_value = create_response( status_code=200, json_payload={ 'opportunity_list': [{ 'pdf_document': 'http://www.example.com/a', 'heading': 'some great opportunity', 'meta': { 'slug': 'rail' } }], 'page_type': 'InvestHighPotentialOpportunityFormPage', }) mock_save.return_value = create_response(status_code=200) settings.HPO_GOV_NOTIFY_AGENT_EMAIL_ADDRESS = '*****@*****.**' url = reverse('high-potential-opportunity-request-form') request = rf.post(url, data={ 'full_name': 'Jim Example', 'role_in_company': 'Chief chief', 'email_address': '*****@*****.**', 'phone_number': '555', 'company_name': 'Example corp', 'website_url': 'example.com', 'country': choices.COUNTRY_CHOICES[1][0], 'company_size': '1 - 10', 'opportunities': [ 'http://www.example.com/a', ], 'comment': 'hello', 'terms_agreed': True, 'g-recaptcha-response': captcha_stub, }) utm_data = { 'campaign_source': 'test_source', 'campaign_medium': 'test_medium', 'campaign_name': 'test_campaign', 'campaign_term': 'test_term', 'campaign_content': 'test_content' } request.utm = utm_data request.session = {} response = views.HighPotentialOpportunityFormView.as_view()( request, path='/invest/high-potential-opportunities/contact/success/', ) assert response.status_code == 302 assert response.url == reverse( 'high-potential-opportunity-request-form-success') assert mock_action_class.call_args_list[0] == call( email_address=settings.HPO_GOV_NOTIFY_AGENT_EMAIL_ADDRESS, template_id=settings.HPO_GOV_NOTIFY_AGENT_TEMPLATE_ID, form_url=url, sender={ 'email_address': '*****@*****.**', 'country_code': 'AL' }) assert mock_action_class.call_args_list[1] == call( email_address='*****@*****.**', template_id=settings.HPO_GOV_NOTIFY_USER_TEMPLATE_ID, form_url=url, email_reply_to_id=settings.HPO_GOV_NOTIFY_USER_REPLY_TO_ID, )
def test_retrieve_company_profile_mixin_success(company_profile): company_profile.return_value = create_response({'key': 'value'}) user = models.SSOUser() assert user.company == {'key': 'value'}
def test_address_lookup_not_ok(mock_get, client): mock_get.return_value = create_response(500) url = reverse('api:postcode-search') with pytest.raises(requests.HTTPError): client.get(url, data={'postcode': '21313'})
def login(): started.return_value = create_response(200, { 'id': '123', 'email': '*****@*****.**' })