From c7951f8667f725c4419a9a6f0cdea687bc927c78 Mon Sep 17 00:00:00 2001 From: Ben Thorner Date: Wed, 30 Mar 2022 17:38:45 +0100 Subject: [PATCH] Split out email and letter branding request tests In response to: [^1]. I've named the file to distinguish it from the admin endpoints to set branding. [^1]: https://github.com/alphagov/notifications-admin/pull/4196#discussion_r838734482 --- .../test_email_branding_requests.py | 528 ++++++++++++ .../test_letter_branding_requests.py | 243 ++++++ .../service_settings/test_service_settings.py | 754 ------------------ 3 files changed, 771 insertions(+), 754 deletions(-) create mode 100644 tests/app/main/views/service_settings/test_email_branding_requests.py create mode 100644 tests/app/main/views/service_settings/test_letter_branding_requests.py diff --git a/tests/app/main/views/service_settings/test_email_branding_requests.py b/tests/app/main/views/service_settings/test_email_branding_requests.py new file mode 100644 index 000000000..63474a4c8 --- /dev/null +++ b/tests/app/main/views/service_settings/test_email_branding_requests.py @@ -0,0 +1,528 @@ +from unittest.mock import ANY, PropertyMock + +import pytest +from flask import url_for +from notifications_utils.clients.zendesk.zendesk_client import ( + NotifySupportTicket, +) + +from app.utils.branding import NHS_EMAIL_BRANDING_ID +from tests import sample_uuid +from tests.conftest import ORGANISATION_ID, SERVICE_ONE_ID, normalize_spaces + + +@pytest.mark.parametrize('organisation_type, expected_options', ( + ('nhs_central', [ + ('nhs', 'NHS'), + ('something_else', 'Something else'), + ]), + ('other', [ + ('something_else', 'Something else'), + ]) +)) +def test_email_branding_request_page_when_no_branding_is_set( + service_one, + client_request, + mocker, + mock_get_email_branding, + mock_get_letter_branding_by_id, + organisation_type, + expected_options, +): + service_one['email_branding'] = None + service_one['organisation_type'] = organisation_type + + mocker.patch( + 'app.models.service.Service.email_branding_id', + new_callable=PropertyMock, + return_value=None, + ) + + page = client_request.get( + '.email_branding_request', service_id=SERVICE_ONE_ID + ) + + assert mock_get_email_branding.called is False + assert page.find('iframe')['src'] == url_for('main.email_template', branding_style='__NONE__') + assert mock_get_letter_branding_by_id.called is False + + button_text = normalize_spaces(page.select_one('.page-footer button').text) + + assert [ + ( + radio['value'], + page.select_one('label[for={}]'.format(radio['id'])).text.strip() + ) + for radio in page.select('input[type=radio]') + ] == expected_options + + assert button_text == 'Continue' + + +def test_email_branding_request_page_shows_branding_if_set( + mocker, + service_one, + client_request, + mock_get_email_branding, + mock_get_service_organisation, +): + mocker.patch( + 'app.models.service.Service.email_branding_id', + new_callable=PropertyMock, + return_value='some-random-branding', + ) + + page = client_request.get( + '.email_branding_request', service_id=SERVICE_ONE_ID + ) + assert page.find('iframe')['src'] == url_for('main.email_template', branding_style='some-random-branding') + + +def test_email_branding_request_page_back_link( + client_request, +): + page = client_request.get( + '.email_branding_request', service_id=SERVICE_ONE_ID + ) + + back_link = page.select('a[class=govuk-back-link]') + assert back_link[0].attrs['href'] == url_for('.service_settings', service_id=SERVICE_ONE_ID) + + +@pytest.mark.parametrize('data, org_type, endpoint', ( + ( + { + 'options': 'govuk', + }, + 'central', + 'main.email_branding_govuk', + ), + ( + { + 'options': 'govuk_and_org', + }, + 'central', + 'main.email_branding_govuk_and_org', + ), + ( + { + 'options': 'organisation', + }, + 'central', + 'main.email_branding_organisation', + ), + ( + { + 'options': 'something_else', + }, + 'central', + 'main.email_branding_something_else', + ), + ( + { + 'options': 'nhs', + }, + 'nhs_local', + 'main.email_branding_nhs', + ), +)) +def test_email_branding_request_submit( + client_request, + service_one, + mocker, + mock_get_email_branding, + organisation_one, + data, + org_type, + endpoint, +): + organisation_one['organisation_type'] = org_type + service_one['email_branding'] = sample_uuid() + service_one['organisation'] = organisation_one + + mocker.patch( + 'app.organisations_client.get_organisation', + return_value=organisation_one, + ) + + client_request.post( + '.email_branding_request', + service_id=SERVICE_ONE_ID, + _data=data, + _expected_status=302, + _expected_redirect=url_for( + endpoint, + service_id=SERVICE_ONE_ID, + _external=True, + ) + ) + + +def test_email_branding_request_submit_when_no_radio_button_is_selected( + client_request, + service_one, + mock_get_email_branding, +): + service_one['email_branding'] = sample_uuid() + + page = client_request.post( + '.email_branding_request', service_id=SERVICE_ONE_ID, + _data={'options': ''}, + _follow_redirects=True, + ) + assert page.h1.text == 'Change email branding' + assert normalize_spaces(page.select_one('.error-message').text) == 'Select an option' + + +@pytest.mark.parametrize('endpoint, expected_heading', [ + ('main.email_branding_govuk_and_org', 'Before you request new branding'), + ('main.email_branding_organisation', 'When you request new branding'), +]) +def test_email_branding_description_pages_for_org_branding( + client_request, + mocker, + service_one, + organisation_one, + mock_get_email_branding, + endpoint, + expected_heading, +): + service_one['email_branding'] = sample_uuid() + service_one['organisation'] = organisation_one + + mocker.patch( + 'app.organisations_client.get_organisation', + return_value=organisation_one, + ) + + page = client_request.get( + endpoint, + service_id=SERVICE_ONE_ID, + ) + assert page.h1.text == expected_heading + assert normalize_spaces(page.select_one('.page-footer button').text) == 'Request new branding' + + +@pytest.mark.parametrize('endpoint, service_org_type, branding_preview_id', [ + ('main.email_branding_govuk', 'central', '__NONE__'), + ('main.email_branding_nhs', 'nhs_local', NHS_EMAIL_BRANDING_ID), +]) +def test_email_branding_govuk_and_nhs_pages( + client_request, + mocker, + service_one, + organisation_one, + mock_get_email_branding, + endpoint, + service_org_type, + branding_preview_id, +): + organisation_one['organisation_type'] = service_org_type + service_one['email_branding'] = sample_uuid() + service_one['organisation'] = organisation_one + + mocker.patch( + 'app.organisations_client.get_organisation', + return_value=organisation_one, + ) + + page = client_request.get( + endpoint, + service_id=SERVICE_ONE_ID, + ) + assert page.h1.text == 'Check your new branding' + assert 'Emails from service one will look like this' in normalize_spaces(page.text) + assert page.find('iframe')['src'] == url_for('main.email_template', branding_style=branding_preview_id) + assert normalize_spaces(page.select_one('.page-footer button').text) == 'Use this branding' + + +def test_email_branding_something_else_page(client_request, service_one): + # expect to have a "NHS" option as well as the + # fallback, so back button goes to choices page + service_one['organisation_type'] = 'nhs_central' + + page = client_request.get( + 'main.email_branding_something_else', + service_id=SERVICE_ONE_ID, + ) + assert normalize_spaces(page.h1.text) == 'Describe the branding you want' + assert page.select_one('textarea')['name'] == ('something_else') + assert normalize_spaces(page.select_one('.page-footer button').text) == 'Request new branding' + assert page.select_one('.govuk-back-link')['href'] == url_for( + 'main.email_branding_request', service_id=SERVICE_ONE_ID, + ) + + +def test_get_email_branding_something_else_page_is_only_option(client_request, service_one): + # should only have a "something else" option + # so back button goes back to settings page + service_one['organisation_type'] = 'other' + + page = client_request.get( + 'main.email_branding_something_else', + service_id=SERVICE_ONE_ID, + ) + assert page.select_one('.govuk-back-link')['href'] == url_for( + 'main.service_settings', service_id=SERVICE_ONE_ID, + ) + + +@pytest.mark.parametrize('endpoint', [ + ('main.email_branding_govuk'), + ('main.email_branding_govuk_and_org'), + ('main.email_branding_nhs'), + ('main.email_branding_organisation'), +]) +def test_email_branding_pages_give_404_if_selected_branding_not_allowed( + client_request, + endpoint, +): + # The only email branding allowed is 'something_else', so trying to visit any of the other + # endpoints gives a 404 status code. + client_request.get( + endpoint, + service_id=SERVICE_ONE_ID, + _expected_status=404 + ) + + +def test_email_branding_govuk_submit( + mocker, + client_request, + service_one, + organisation_one, + no_reply_to_email_addresses, + mock_get_email_branding, + single_sms_sender, + mock_update_service, +): + mocker.patch( + 'app.organisations_client.get_organisation', + return_value=organisation_one, + ) + mocker.patch( + 'app.models.service.Service.organisation_id', + new_callable=PropertyMock, + return_value=ORGANISATION_ID, + ) + service_one['email_branding'] = sample_uuid() + + page = client_request.post( + '.email_branding_govuk', + service_id=SERVICE_ONE_ID, + _follow_redirects=True, + ) + + mock_update_service.assert_called_once_with( + SERVICE_ONE_ID, + email_branding=None, + ) + assert page.h1.text == 'Settings' + assert normalize_spaces(page.select_one('.banner-default').text) == 'You’ve updated your email branding' + + +def test_email_branding_govuk_and_org_submit( + mocker, + client_request, + service_one, + organisation_one, + no_reply_to_email_addresses, + mock_get_email_branding, + single_sms_sender, +): + mocker.patch( + 'app.organisations_client.get_organisation', + return_value=organisation_one, + ) + mocker.patch( + 'app.models.service.Service.organisation_id', + new_callable=PropertyMock, + return_value=ORGANISATION_ID, + ) + service_one['email_branding'] = sample_uuid() + + mock_create_ticket = mocker.spy(NotifySupportTicket, '__init__') + mock_send_ticket_to_zendesk = mocker.patch( + 'app.main.views.service_settings.zendesk_client.send_ticket_to_zendesk', + autospec=True, + ) + + page = client_request.post( + '.email_branding_govuk_and_org', + service_id=SERVICE_ONE_ID, + _follow_redirects=True, + ) + + mock_create_ticket.assert_called_once_with( + ANY, + message='\n'.join([ + 'Organisation: organisation one', + 'Service: service one', + 'http://localhost/services/596364a0-858e-42c8-9062-a8fe822260eb', + '', + '---', + 'Current branding: Organisation name', + 'Branding requested: GOV.UK and organisation one\n', + ]), + subject='Email branding request - service one', + ticket_type='question', + user_name='Test User', + user_email='test@user.gov.uk', + org_id=ORGANISATION_ID, + org_type='central', + service_id=SERVICE_ONE_ID + ) + mock_send_ticket_to_zendesk.assert_called_once() + assert normalize_spaces(page.select_one('.banner-default').text) == ( + 'Thanks for your branding request. We’ll get back to you ' + 'within one working day.' + ) + + +def test_email_branding_nhs_submit( + mocker, + client_request, + service_one, + organisation_one, + no_reply_to_email_addresses, + mock_get_email_branding, + single_sms_sender, + mock_update_service, +): + service_one['email_branding'] = sample_uuid() + service_one['organisation_type'] = 'nhs_local' + + page = client_request.post( + '.email_branding_nhs', + service_id=SERVICE_ONE_ID, + _follow_redirects=True, + ) + + mock_update_service.assert_called_once_with( + SERVICE_ONE_ID, + email_branding=NHS_EMAIL_BRANDING_ID, + ) + assert page.h1.text == 'Settings' + assert normalize_spaces(page.select_one('.banner-default').text) == 'You’ve updated your email branding' + + +def test_email_branding_organisation_submit( + mocker, + client_request, + service_one, + organisation_one, + no_reply_to_email_addresses, + mock_get_email_branding, + single_sms_sender, +): + mocker.patch( + 'app.organisations_client.get_organisation', + return_value=organisation_one, + ) + mocker.patch( + 'app.models.service.Service.organisation_id', + new_callable=PropertyMock, + return_value=ORGANISATION_ID, + ) + service_one['email_branding'] = sample_uuid() + + mock_create_ticket = mocker.spy(NotifySupportTicket, '__init__') + mock_send_ticket_to_zendesk = mocker.patch( + 'app.main.views.service_settings.zendesk_client.send_ticket_to_zendesk', + autospec=True, + ) + + page = client_request.post( + '.email_branding_organisation', + service_id=SERVICE_ONE_ID, + _follow_redirects=True, + ) + + mock_create_ticket.assert_called_once_with( + ANY, + message='\n'.join([ + 'Organisation: organisation one', + 'Service: service one', + 'http://localhost/services/596364a0-858e-42c8-9062-a8fe822260eb', + '', + '---', + 'Current branding: Organisation name', + 'Branding requested: organisation one\n', + ]), + subject='Email branding request - service one', + ticket_type='question', + user_name='Test User', + user_email='test@user.gov.uk', + org_id=ORGANISATION_ID, + org_type='central', + service_id=SERVICE_ONE_ID + ) + mock_send_ticket_to_zendesk.assert_called_once() + assert normalize_spaces(page.select_one('.banner-default').text) == ( + 'Thanks for your branding request. We’ll get back to you ' + 'within one working day.' + ) + + +def test_email_branding_something_else_submit( + client_request, + mocker, + service_one, + no_reply_to_email_addresses, + mock_get_email_branding, + single_sms_sender, +): + service_one['email_branding'] = sample_uuid() + service_one['organisation_type'] = 'nhs_local' + + mock_create_ticket = mocker.spy(NotifySupportTicket, '__init__') + mock_send_ticket_to_zendesk = mocker.patch( + 'app.main.views.service_settings.zendesk_client.send_ticket_to_zendesk', + autospec=True, + ) + + page = client_request.post( + '.email_branding_something_else', + service_id=SERVICE_ONE_ID, + _data={'something_else': 'Homer Simpson'}, + _follow_redirects=True, + ) + + mock_create_ticket.assert_called_once_with( + ANY, + message='\n'.join([ + 'Organisation: Can’t tell (domain is user.gov.uk)', + 'Service: service one', + 'http://localhost/services/596364a0-858e-42c8-9062-a8fe822260eb', + '', + '---', + 'Current branding: Organisation name', + 'Branding requested: Something else\n', + 'Homer Simpson\n' + ]), + subject='Email branding request - service one', + ticket_type='question', + user_name='Test User', + user_email='test@user.gov.uk', + org_id=None, + org_type='nhs_local', + service_id=SERVICE_ONE_ID + ) + mock_send_ticket_to_zendesk.assert_called_once() + assert normalize_spaces(page.select_one('.banner-default').text) == ( + 'Thanks for your branding request. We’ll get back to you ' + 'within one working day.' + ) + + +def test_email_branding_something_else_submit_shows_error_if_textbox_is_empty( + client_request, +): + page = client_request.post( + '.email_branding_something_else', + service_id=SERVICE_ONE_ID, + _data={'something_else': ''}, + _follow_redirects=True, + ) + assert normalize_spaces(page.h1.text) == 'Describe the branding you want' + assert normalize_spaces(page.select_one('.govuk-error-message').text) == 'Error: Cannot be empty' diff --git a/tests/app/main/views/service_settings/test_letter_branding_requests.py b/tests/app/main/views/service_settings/test_letter_branding_requests.py new file mode 100644 index 000000000..949a13687 --- /dev/null +++ b/tests/app/main/views/service_settings/test_letter_branding_requests.py @@ -0,0 +1,243 @@ +from unittest.mock import ANY, PropertyMock + +import pytest +from flask import url_for +from notifications_utils.clients.zendesk.zendesk_client import ( + NotifySupportTicket, +) + +from tests import organisation_json, sample_uuid +from tests.conftest import ( + ORGANISATION_ID, + SERVICE_ONE_ID, + TEMPLATE_ONE_ID, + normalize_spaces, +) + + +@pytest.mark.parametrize('organisation_type, expected_options', ( + ('nhs_central', [ + ('nhs', 'NHS'), + ('something_else', 'Something else'), + ]), + ('other', None), +)) +def test_letter_branding_request_page_when_no_branding_is_set( + service_one, + client_request, + mock_get_email_branding, + mock_get_letter_branding_by_id, + organisation_type, + expected_options, +): + service_one['letter_branding'] = None + service_one['organisation_type'] = organisation_type + + page = client_request.get( + '.letter_branding_request', service_id=SERVICE_ONE_ID + ) + + assert mock_get_email_branding.called is False + assert mock_get_letter_branding_by_id.called is False + + button_text = normalize_spaces(page.select_one('.page-footer button').text) + assert button_text == 'Request new branding' + + if expected_options: + assert [ + ( + radio['value'], + page.select_one('label[for={}]'.format(radio['id'])).text.strip() + ) + for radio in page.select('input[type=radio]') + ] == expected_options + assert page.select_one( + '.conditional-radios-panel#panel-something-else textarea' + )['name'] == ( + 'something_else' + ) + else: + assert page.select_one( + 'textarea' + )['name'] == ( + 'something_else' + ) + assert not page.select('.conditional-radios-panel') + + +@pytest.mark.parametrize('from_template,back_link_url', [ + (None, '/services/{}/service-settings'.format(SERVICE_ONE_ID),), + (TEMPLATE_ONE_ID, '/services/{}/templates/{}'.format(SERVICE_ONE_ID, TEMPLATE_ONE_ID),) +]) +def test_letter_branding_request_page_back_link( + client_request, + from_template, + back_link_url, +): + if from_template: + page = client_request.get( + '.letter_branding_request', service_id=SERVICE_ONE_ID, from_template=from_template + ) + else: + page = client_request.get( + '.letter_branding_request', service_id=SERVICE_ONE_ID + ) + + back_link = page.select('a[class=govuk-back-link]') + assert back_link[0].attrs['href'] == back_link_url + + +@pytest.mark.parametrize('org_name, expected_organisation', ( + (None, 'Can’t tell (domain is user.gov.uk)'), + ('Test Organisation', 'Test Organisation'), +)) +def test_letter_branding_request_submit( + client_request, + service_one, + mocker, + mock_get_letter_branding_by_id, + no_reply_to_email_addresses, + no_letter_contact_blocks, + single_sms_sender, + org_name, + expected_organisation, +): + service_one['letter_branding'] = sample_uuid() + organisation_id = ORGANISATION_ID if org_name else None + + mocker.patch( + 'app.models.service.Service.organisation_id', + new_callable=PropertyMock, + return_value=organisation_id, + ) + mocker.patch( + 'app.organisations_client.get_organisation', + return_value=organisation_json(name=org_name), + ) + + mock_create_ticket = mocker.spy(NotifySupportTicket, '__init__') + mock_send_ticket_to_zendesk = mocker.patch( + 'app.main.views.service_settings.zendesk_client.send_ticket_to_zendesk', + autospec=True, + ) + + page = client_request.post( + '.letter_branding_request', service_id=SERVICE_ONE_ID, + _data={ + 'options': 'something_else', + 'something_else': 'Homer Simpson', + }, + _follow_redirects=True, + ) + + mock_create_ticket.assert_called_once_with( + ANY, + message='\n'.join([ + 'Organisation: {}', + 'Service: service one', + 'http://localhost/services/596364a0-858e-42c8-9062-a8fe822260eb', + '', + '---', + 'Current branding: HM Government', + 'Branding requested: Something else\n\nHomer Simpson\n', + ]).format(expected_organisation), + subject='Letter branding request - service one', + ticket_type='question', + user_name='Test User', + user_email='test@user.gov.uk', + org_id=organisation_id, + org_type='central', + service_id=SERVICE_ONE_ID + ) + mock_send_ticket_to_zendesk.assert_called_once() + assert normalize_spaces(page.select_one('.banner-default').text) == ( + 'Thanks for your branding request. We’ll get back to you ' + 'within one working day.' + ) + + +@pytest.mark.parametrize('data, error_message', ( + ({'options': 'something_else'}, 'Cannot be empty'), # no data in 'something_else' textbox + ({'options': ''}, 'Select an option'), # no radio button selected +)) +def test_letter_branding_request_submit_when_form_has_missing_data( + client_request, + mocker, + service_one, + organisation_one, + data, + error_message, + mock_get_letter_branding_by_id, +): + mocker.patch( + 'app.organisations_client.get_organisation', + return_value=organisation_one, + ) + service_one['letter_branding'] = sample_uuid() + service_one['organisation'] = organisation_one + + page = client_request.post( + '.letter_branding_request', service_id=SERVICE_ONE_ID, + _data=data, + _follow_redirects=True, + ) + assert page.h1.text == 'Change letter branding' + assert normalize_spaces(page.select_one('.error-message').text) == error_message + + +@pytest.mark.parametrize('from_template', [ + None, + TEMPLATE_ONE_ID +]) +def test_letter_branding_request_submit_redirects_if_from_template_is_set( + client_request, + service_one, + mocker, + from_template, + +): + mocker.patch('app.main.views.service_settings.zendesk_client.send_ticket_to_zendesk', autospec=True) + data = {'options': 'something_else', 'something_else': 'Homer Simpson'} + + if from_template: + client_request.post( + '.letter_branding_request', service_id=SERVICE_ONE_ID, from_template=from_template, + _data=data, + _expected_redirect=url_for( + 'main.view_template', service_id=SERVICE_ONE_ID, template_id=from_template, _external=True + ) + ) + else: + client_request.post( + '.letter_branding_request', service_id=SERVICE_ONE_ID, + _data=data, + _expected_redirect=url_for('main.service_settings', service_id=SERVICE_ONE_ID, _external=True) + ) + + +def test_letter_branding_submit_when_something_else_is_only_option( + client_request, + service_one, + mocker, + mock_get_letter_branding_by_id, +): + mock_create_ticket = mocker.spy(NotifySupportTicket, '__init__') + mocker.patch( + 'app.main.views.service_settings.zendesk_client.send_ticket_to_zendesk', + autospec=True, + ) + + client_request.post( + '.letter_branding_request', + service_id=SERVICE_ONE_ID, + _data={ + 'something_else': 'Homer Simpson', + }, + ) + + assert ( + 'Current branding: no\n' + 'Branding requested: Something else\n' + '\n' + 'Homer Simpson' + ) in mock_create_ticket.call_args_list[0][1]['message'] diff --git a/tests/app/main/views/service_settings/test_service_settings.py b/tests/app/main/views/service_settings/test_service_settings.py index 2b1053863..40a4bf35a 100644 --- a/tests/app/main/views/service_settings/test_service_settings.py +++ b/tests/app/main/views/service_settings/test_service_settings.py @@ -14,7 +14,6 @@ from notifications_utils.clients.zendesk.zendesk_client import ( ) import app -from app.utils.branding import NHS_EMAIL_BRANDING_ID from tests import ( find_element_by_tag_and_partial_text, invite_json, @@ -4779,759 +4778,6 @@ def test_service_settings_links_to_branding_request_page_for_emails( assert len(page.find_all('a', attrs={'href': expected_href})) == 1 -@pytest.mark.parametrize('organisation_type, expected_options', ( - ('nhs_central', [ - ('nhs', 'NHS'), - ('something_else', 'Something else'), - ]), - ('other', [ - ('something_else', 'Something else'), - ]) -)) -def test_email_branding_request_page_when_no_branding_is_set( - service_one, - client_request, - mocker, - mock_get_email_branding, - mock_get_letter_branding_by_id, - organisation_type, - expected_options, -): - service_one['email_branding'] = None - service_one['organisation_type'] = organisation_type - - mocker.patch( - 'app.models.service.Service.email_branding_id', - new_callable=PropertyMock, - return_value=None, - ) - - page = client_request.get( - '.email_branding_request', service_id=SERVICE_ONE_ID - ) - - assert mock_get_email_branding.called is False - assert page.find('iframe')['src'] == url_for('main.email_template', branding_style='__NONE__') - assert mock_get_letter_branding_by_id.called is False - - button_text = normalize_spaces(page.select_one('.page-footer button').text) - - assert [ - ( - radio['value'], - page.select_one('label[for={}]'.format(radio['id'])).text.strip() - ) - for radio in page.select('input[type=radio]') - ] == expected_options - - assert button_text == 'Continue' - - -def test_email_branding_request_page_shows_branding_if_set( - mocker, - service_one, - client_request, - mock_get_email_branding, - mock_get_service_organisation, -): - mocker.patch( - 'app.models.service.Service.email_branding_id', - new_callable=PropertyMock, - return_value='some-random-branding', - ) - - page = client_request.get( - '.email_branding_request', service_id=SERVICE_ONE_ID - ) - assert page.find('iframe')['src'] == url_for('main.email_template', branding_style='some-random-branding') - - -def test_email_branding_request_page_back_link( - client_request, -): - page = client_request.get( - '.email_branding_request', service_id=SERVICE_ONE_ID - ) - - back_link = page.select('a[class=govuk-back-link]') - assert back_link[0].attrs['href'] == url_for('.service_settings', service_id=SERVICE_ONE_ID) - - -@pytest.mark.parametrize('data, org_type, endpoint', ( - ( - { - 'options': 'govuk', - }, - 'central', - 'main.email_branding_govuk', - ), - ( - { - 'options': 'govuk_and_org', - }, - 'central', - 'main.email_branding_govuk_and_org', - ), - ( - { - 'options': 'organisation', - }, - 'central', - 'main.email_branding_organisation', - ), - ( - { - 'options': 'something_else', - }, - 'central', - 'main.email_branding_something_else', - ), - ( - { - 'options': 'nhs', - }, - 'nhs_local', - 'main.email_branding_nhs', - ), -)) -def test_email_branding_request_submit( - client_request, - service_one, - mocker, - mock_get_email_branding, - organisation_one, - data, - org_type, - endpoint, -): - organisation_one['organisation_type'] = org_type - service_one['email_branding'] = sample_uuid() - service_one['organisation'] = organisation_one - - mocker.patch( - 'app.organisations_client.get_organisation', - return_value=organisation_one, - ) - - client_request.post( - '.email_branding_request', - service_id=SERVICE_ONE_ID, - _data=data, - _expected_status=302, - _expected_redirect=url_for( - endpoint, - service_id=SERVICE_ONE_ID, - _external=True, - ) - ) - - -def test_email_branding_request_submit_when_no_radio_button_is_selected( - client_request, - service_one, - mock_get_email_branding, -): - service_one['email_branding'] = sample_uuid() - - page = client_request.post( - '.email_branding_request', service_id=SERVICE_ONE_ID, - _data={'options': ''}, - _follow_redirects=True, - ) - assert page.h1.text == 'Change email branding' - assert normalize_spaces(page.select_one('.error-message').text) == 'Select an option' - - -@pytest.mark.parametrize('endpoint, expected_heading', [ - ('main.email_branding_govuk_and_org', 'Before you request new branding'), - ('main.email_branding_organisation', 'When you request new branding'), -]) -def test_email_branding_description_pages_for_org_branding( - client_request, - mocker, - service_one, - organisation_one, - mock_get_email_branding, - endpoint, - expected_heading, -): - service_one['email_branding'] = sample_uuid() - service_one['organisation'] = organisation_one - - mocker.patch( - 'app.organisations_client.get_organisation', - return_value=organisation_one, - ) - - page = client_request.get( - endpoint, - service_id=SERVICE_ONE_ID, - ) - assert page.h1.text == expected_heading - assert normalize_spaces(page.select_one('.page-footer button').text) == 'Request new branding' - - -@pytest.mark.parametrize('endpoint, service_org_type, branding_preview_id', [ - ('main.email_branding_govuk', 'central', '__NONE__'), - ('main.email_branding_nhs', 'nhs_local', NHS_EMAIL_BRANDING_ID), -]) -def test_email_branding_govuk_and_nhs_pages( - client_request, - mocker, - service_one, - organisation_one, - mock_get_email_branding, - endpoint, - service_org_type, - branding_preview_id, -): - organisation_one['organisation_type'] = service_org_type - service_one['email_branding'] = sample_uuid() - service_one['organisation'] = organisation_one - - mocker.patch( - 'app.organisations_client.get_organisation', - return_value=organisation_one, - ) - - page = client_request.get( - endpoint, - service_id=SERVICE_ONE_ID, - ) - assert page.h1.text == 'Check your new branding' - assert 'Emails from service one will look like this' in normalize_spaces(page.text) - assert page.find('iframe')['src'] == url_for('main.email_template', branding_style=branding_preview_id) - assert normalize_spaces(page.select_one('.page-footer button').text) == 'Use this branding' - - -def test_email_branding_something_else_page(client_request, service_one): - # expect to have a "NHS" option as well as the - # fallback, so back button goes to choices page - service_one['organisation_type'] = 'nhs_central' - - page = client_request.get( - 'main.email_branding_something_else', - service_id=SERVICE_ONE_ID, - ) - assert normalize_spaces(page.h1.text) == 'Describe the branding you want' - assert page.select_one('textarea')['name'] == ('something_else') - assert normalize_spaces(page.select_one('.page-footer button').text) == 'Request new branding' - assert page.select_one('.govuk-back-link')['href'] == url_for( - 'main.email_branding_request', service_id=SERVICE_ONE_ID, - ) - - -def test_get_email_branding_something_else_page_is_only_option(client_request, service_one): - # should only have a "something else" option - # so back button goes back to settings page - service_one['organisation_type'] = 'other' - - page = client_request.get( - 'main.email_branding_something_else', - service_id=SERVICE_ONE_ID, - ) - assert page.select_one('.govuk-back-link')['href'] == url_for( - 'main.service_settings', service_id=SERVICE_ONE_ID, - ) - - -@pytest.mark.parametrize('endpoint', [ - ('main.email_branding_govuk'), - ('main.email_branding_govuk_and_org'), - ('main.email_branding_nhs'), - ('main.email_branding_organisation'), -]) -def test_email_branding_pages_give_404_if_selected_branding_not_allowed( - client_request, - endpoint, -): - # The only email branding allowed is 'something_else', so trying to visit any of the other - # endpoints gives a 404 status code. - client_request.get( - endpoint, - service_id=SERVICE_ONE_ID, - _expected_status=404 - ) - - -def test_email_branding_govuk_submit( - mocker, - client_request, - service_one, - organisation_one, - mock_get_service_settings_page_common, - no_reply_to_email_addresses, - mock_get_email_branding, - single_sms_sender, - mock_update_service, -): - mocker.patch( - 'app.organisations_client.get_organisation', - return_value=organisation_one, - ) - mocker.patch( - 'app.models.service.Service.organisation_id', - new_callable=PropertyMock, - return_value=ORGANISATION_ID, - ) - service_one['email_branding'] = sample_uuid() - - page = client_request.post( - '.email_branding_govuk', - service_id=SERVICE_ONE_ID, - _follow_redirects=True, - ) - - mock_update_service.assert_called_once_with( - SERVICE_ONE_ID, - email_branding=None, - ) - assert page.h1.text == 'Settings' - assert normalize_spaces(page.select_one('.banner-default').text) == 'You’ve updated your email branding' - - -def test_email_branding_govuk_and_org_submit( - mocker, - client_request, - service_one, - organisation_one, - mock_get_service_settings_page_common, - no_reply_to_email_addresses, - mock_get_email_branding, - single_sms_sender, -): - mocker.patch( - 'app.organisations_client.get_organisation', - return_value=organisation_one, - ) - mocker.patch( - 'app.models.service.Service.organisation_id', - new_callable=PropertyMock, - return_value=ORGANISATION_ID, - ) - service_one['email_branding'] = sample_uuid() - - mock_create_ticket = mocker.spy(NotifySupportTicket, '__init__') - mock_send_ticket_to_zendesk = mocker.patch( - 'app.main.views.service_settings.zendesk_client.send_ticket_to_zendesk', - autospec=True, - ) - - page = client_request.post( - '.email_branding_govuk_and_org', - service_id=SERVICE_ONE_ID, - _follow_redirects=True, - ) - - mock_create_ticket.assert_called_once_with( - ANY, - message='\n'.join([ - 'Organisation: organisation one', - 'Service: service one', - 'http://localhost/services/596364a0-858e-42c8-9062-a8fe822260eb', - '', - '---', - 'Current branding: Organisation name', - 'Branding requested: GOV.UK and organisation one\n', - ]), - subject='Email branding request - service one', - ticket_type='question', - user_name='Test User', - user_email='test@user.gov.uk', - org_id=ORGANISATION_ID, - org_type='central', - service_id=SERVICE_ONE_ID - ) - mock_send_ticket_to_zendesk.assert_called_once() - assert normalize_spaces(page.select_one('.banner-default').text) == ( - 'Thanks for your branding request. We’ll get back to you ' - 'within one working day.' - ) - - -def test_email_branding_nhs_submit( - mocker, - client_request, - service_one, - organisation_one, - mock_get_service_settings_page_common, - no_reply_to_email_addresses, - mock_get_email_branding, - single_sms_sender, - mock_update_service, -): - service_one['email_branding'] = sample_uuid() - service_one['organisation_type'] = 'nhs_local' - - page = client_request.post( - '.email_branding_nhs', - service_id=SERVICE_ONE_ID, - _follow_redirects=True, - ) - - mock_update_service.assert_called_once_with( - SERVICE_ONE_ID, - email_branding=NHS_EMAIL_BRANDING_ID, - ) - assert page.h1.text == 'Settings' - assert normalize_spaces(page.select_one('.banner-default').text) == 'You’ve updated your email branding' - - -def test_email_branding_organisation_submit( - mocker, - client_request, - service_one, - organisation_one, - mock_get_service_settings_page_common, - no_reply_to_email_addresses, - mock_get_email_branding, - single_sms_sender, -): - mocker.patch( - 'app.organisations_client.get_organisation', - return_value=organisation_one, - ) - mocker.patch( - 'app.models.service.Service.organisation_id', - new_callable=PropertyMock, - return_value=ORGANISATION_ID, - ) - service_one['email_branding'] = sample_uuid() - - mock_create_ticket = mocker.spy(NotifySupportTicket, '__init__') - mock_send_ticket_to_zendesk = mocker.patch( - 'app.main.views.service_settings.zendesk_client.send_ticket_to_zendesk', - autospec=True, - ) - - page = client_request.post( - '.email_branding_organisation', - service_id=SERVICE_ONE_ID, - _follow_redirects=True, - ) - - mock_create_ticket.assert_called_once_with( - ANY, - message='\n'.join([ - 'Organisation: organisation one', - 'Service: service one', - 'http://localhost/services/596364a0-858e-42c8-9062-a8fe822260eb', - '', - '---', - 'Current branding: Organisation name', - 'Branding requested: organisation one\n', - ]), - subject='Email branding request - service one', - ticket_type='question', - user_name='Test User', - user_email='test@user.gov.uk', - org_id=ORGANISATION_ID, - org_type='central', - service_id=SERVICE_ONE_ID - ) - mock_send_ticket_to_zendesk.assert_called_once() - assert normalize_spaces(page.select_one('.banner-default').text) == ( - 'Thanks for your branding request. We’ll get back to you ' - 'within one working day.' - ) - - -def test_email_branding_something_else_submit( - client_request, - mocker, - service_one, - mock_get_service_settings_page_common, - no_reply_to_email_addresses, - mock_get_email_branding, - single_sms_sender, -): - service_one['email_branding'] = sample_uuid() - service_one['organisation_type'] = 'nhs_local' - - mock_create_ticket = mocker.spy(NotifySupportTicket, '__init__') - mock_send_ticket_to_zendesk = mocker.patch( - 'app.main.views.service_settings.zendesk_client.send_ticket_to_zendesk', - autospec=True, - ) - - page = client_request.post( - '.email_branding_something_else', - service_id=SERVICE_ONE_ID, - _data={'something_else': 'Homer Simpson'}, - _follow_redirects=True, - ) - - mock_create_ticket.assert_called_once_with( - ANY, - message='\n'.join([ - 'Organisation: Can’t tell (domain is user.gov.uk)', - 'Service: service one', - 'http://localhost/services/596364a0-858e-42c8-9062-a8fe822260eb', - '', - '---', - 'Current branding: Organisation name', - 'Branding requested: Something else\n', - 'Homer Simpson\n' - ]), - subject='Email branding request - service one', - ticket_type='question', - user_name='Test User', - user_email='test@user.gov.uk', - org_id=None, - org_type='nhs_local', - service_id=SERVICE_ONE_ID - ) - mock_send_ticket_to_zendesk.assert_called_once() - assert normalize_spaces(page.select_one('.banner-default').text) == ( - 'Thanks for your branding request. We’ll get back to you ' - 'within one working day.' - ) - - -def test_email_branding_something_else_submit_shows_error_if_textbox_is_empty( - client_request, -): - page = client_request.post( - '.email_branding_something_else', - service_id=SERVICE_ONE_ID, - _data={'something_else': ''}, - _follow_redirects=True, - ) - assert normalize_spaces(page.h1.text) == 'Describe the branding you want' - assert normalize_spaces(page.select_one('.govuk-error-message').text) == 'Error: Cannot be empty' - - -@pytest.mark.parametrize('organisation_type, expected_options', ( - ('nhs_central', [ - ('nhs', 'NHS'), - ('something_else', 'Something else'), - ]), - ('other', None), -)) -def test_letter_branding_request_page_when_no_branding_is_set( - service_one, - client_request, - mock_get_email_branding, - mock_get_letter_branding_by_id, - organisation_type, - expected_options, -): - service_one['letter_branding'] = None - service_one['organisation_type'] = organisation_type - - page = client_request.get( - '.letter_branding_request', service_id=SERVICE_ONE_ID - ) - - assert mock_get_email_branding.called is False - assert mock_get_letter_branding_by_id.called is False - - button_text = normalize_spaces(page.select_one('.page-footer button').text) - assert button_text == 'Request new branding' - - if expected_options: - assert [ - ( - radio['value'], - page.select_one('label[for={}]'.format(radio['id'])).text.strip() - ) - for radio in page.select('input[type=radio]') - ] == expected_options - assert page.select_one( - '.conditional-radios-panel#panel-something-else textarea' - )['name'] == ( - 'something_else' - ) - else: - assert page.select_one( - 'textarea' - )['name'] == ( - 'something_else' - ) - assert not page.select('.conditional-radios-panel') - - -@pytest.mark.parametrize('from_template,back_link_url', [ - (None, '/services/{}/service-settings'.format(SERVICE_ONE_ID),), - (TEMPLATE_ONE_ID, '/services/{}/templates/{}'.format(SERVICE_ONE_ID, TEMPLATE_ONE_ID),) -]) -def test_letter_branding_request_page_back_link( - client_request, - from_template, - back_link_url, -): - if from_template: - page = client_request.get( - '.letter_branding_request', service_id=SERVICE_ONE_ID, from_template=from_template - ) - else: - page = client_request.get( - '.letter_branding_request', service_id=SERVICE_ONE_ID - ) - - back_link = page.select('a[class=govuk-back-link]') - assert back_link[0].attrs['href'] == back_link_url - - -@pytest.mark.parametrize('org_name, expected_organisation', ( - (None, 'Can’t tell (domain is user.gov.uk)'), - ('Test Organisation', 'Test Organisation'), -)) -def test_letter_branding_request_submit( - client_request, - service_one, - mocker, - mock_get_service_settings_page_common, - mock_get_letter_branding_by_id, - no_reply_to_email_addresses, - no_letter_contact_blocks, - single_sms_sender, - org_name, - expected_organisation, -): - service_one['letter_branding'] = sample_uuid() - organisation_id = ORGANISATION_ID if org_name else None - - mocker.patch( - 'app.models.service.Service.organisation_id', - new_callable=PropertyMock, - return_value=organisation_id, - ) - mocker.patch( - 'app.organisations_client.get_organisation', - return_value=organisation_json(name=org_name), - ) - - mock_create_ticket = mocker.spy(NotifySupportTicket, '__init__') - mock_send_ticket_to_zendesk = mocker.patch( - 'app.main.views.service_settings.zendesk_client.send_ticket_to_zendesk', - autospec=True, - ) - - page = client_request.post( - '.letter_branding_request', service_id=SERVICE_ONE_ID, - _data={ - 'options': 'something_else', - 'something_else': 'Homer Simpson', - }, - _follow_redirects=True, - ) - - mock_create_ticket.assert_called_once_with( - ANY, - message='\n'.join([ - 'Organisation: {}', - 'Service: service one', - 'http://localhost/services/596364a0-858e-42c8-9062-a8fe822260eb', - '', - '---', - 'Current branding: HM Government', - 'Branding requested: Something else\n\nHomer Simpson\n', - ]).format(expected_organisation), - subject='Letter branding request - service one', - ticket_type='question', - user_name='Test User', - user_email='test@user.gov.uk', - org_id=organisation_id, - org_type='central', - service_id=SERVICE_ONE_ID - ) - mock_send_ticket_to_zendesk.assert_called_once() - assert normalize_spaces(page.select_one('.banner-default').text) == ( - 'Thanks for your branding request. We’ll get back to you ' - 'within one working day.' - ) - - -@pytest.mark.parametrize('data, error_message', ( - ({'options': 'something_else'}, 'Cannot be empty'), # no data in 'something_else' textbox - ({'options': ''}, 'Select an option'), # no radio button selected -)) -def test_letter_branding_request_submit_when_form_has_missing_data( - client_request, - mocker, - service_one, - organisation_one, - data, - error_message, - mock_get_letter_branding_by_id, -): - mocker.patch( - 'app.organisations_client.get_organisation', - return_value=organisation_one, - ) - service_one['letter_branding'] = sample_uuid() - service_one['organisation'] = organisation_one - - page = client_request.post( - '.letter_branding_request', service_id=SERVICE_ONE_ID, - _data=data, - _follow_redirects=True, - ) - assert page.h1.text == 'Change letter branding' - assert normalize_spaces(page.select_one('.error-message').text) == error_message - - -@pytest.mark.parametrize('from_template', [ - None, - TEMPLATE_ONE_ID -]) -def test_letter_branding_request_submit_redirects_if_from_template_is_set( - client_request, - service_one, - mocker, - mock_get_service_settings_page_common, - from_template, - -): - mocker.patch('app.main.views.service_settings.zendesk_client.send_ticket_to_zendesk', autospec=True) - data = {'options': 'something_else', 'something_else': 'Homer Simpson'} - - if from_template: - client_request.post( - '.letter_branding_request', service_id=SERVICE_ONE_ID, from_template=from_template, - _data=data, - _expected_redirect=url_for( - 'main.view_template', service_id=SERVICE_ONE_ID, template_id=from_template, _external=True - ) - ) - else: - client_request.post( - '.letter_branding_request', service_id=SERVICE_ONE_ID, - _data=data, - _expected_redirect=url_for('main.service_settings', service_id=SERVICE_ONE_ID, _external=True) - ) - - -def test_letter_branding_submit_when_something_else_is_only_option( - client_request, - service_one, - mocker, - mock_get_service_settings_page_common, - mock_get_letter_branding_by_id, -): - mock_create_ticket = mocker.spy(NotifySupportTicket, '__init__') - mocker.patch( - 'app.main.views.service_settings.zendesk_client.send_ticket_to_zendesk', - autospec=True, - ) - - client_request.post( - '.letter_branding_request', - service_id=SERVICE_ONE_ID, - _data={ - 'something_else': 'Homer Simpson', - }, - ) - - assert ( - 'Current branding: no\n' - 'Branding requested: Something else\n' - '\n' - 'Homer Simpson' - ) in mock_create_ticket.call_args_list[0][1]['message'] - - def test_service_settings_links_to_branding_request_page_for_letters( mocker, service_one,