Merge pull request #3498 from alphagov/add-broadcast-template

Allow adding broadcast templates
This commit is contained in:
Chris Hill-Scott
2020-07-06 09:06:16 +01:00
committed by GitHub
18 changed files with 285 additions and 87 deletions

View File

@@ -154,7 +154,7 @@ def test_for_commas_in_placeholders(
@pytest.mark.parametrize('msg', ['The quick brown fox', 'Thé “quick” bröwn fox\u200B'])
def test_sms_character_validation(client, msg):
OnlySMSCharacters()(None, _gen_mock_field(msg))
OnlySMSCharacters(template_type='sms')(None, _gen_mock_field(msg))
@pytest.mark.parametrize('data, err_msg', [
@@ -175,7 +175,7 @@ def test_sms_character_validation(client, msg):
])
def test_non_sms_character_validation(data, err_msg, client):
with pytest.raises(ValidationError) as error:
OnlySMSCharacters()(None, _gen_mock_field(data))
OnlySMSCharacters(template_type='sms')(None, _gen_mock_field(data))
assert str(error.value) == err_msg

View File

@@ -14,7 +14,8 @@ letters_urls = [
([], 403)
])
def test_letters_access_restricted(
platform_admin_client,
client_request,
platform_admin_user,
mocker,
permissions,
response_code,
@@ -23,12 +24,12 @@ def test_letters_access_restricted(
service_one,
):
service_one['permissions'] = permissions
mocker.patch('app.service_api_client.get_service', return_value={"data": service_one})
response = platform_admin_client.get(url(service_id=service_one['id']))
assert response.status_code == response_code
client_request.login(platform_admin_user)
client_request.get_url(
url(service_id=service_one['id']),
_follow_redirects=True,
_expected_status=response_code,
)
@pytest.mark.parametrize('url', letters_urls)

View File

@@ -280,6 +280,7 @@ def test_should_not_allow_files_to_be_uploaded_without_the_correct_permission(
service_id=SERVICE_ONE_ID,
template_id=template_id,
_follow_redirects=True,
_expected_status=403,
)
assert page.select('main p')[0].text.strip() == "Sending text messages has been disabled for your service."
@@ -312,10 +313,12 @@ def test_example_spreadsheet(
def test_example_spreadsheet_for_letters(
client_request,
service_one,
mocker,
mock_get_service_letter_template_with_placeholders,
fake_uuid,
):
service_one['permissions'] += ['letter']
mocker.patch('app.main.views.send.get_page_count_for_letter', return_value=1)
page = client_request.get(
@@ -600,6 +603,7 @@ def test_upload_csv_file_with_bad_postal_address_shows_check_page_with_errors(
mock_get_jobs,
fake_uuid,
):
service_one['permissions'] += ['letter']
mocker.patch('app.main.views.send.get_page_count_for_letter', return_value=9)
mocker.patch(
'app.main.views.send.s3download',
@@ -658,7 +662,7 @@ def test_upload_csv_file_with_international_letters_permission_shows_appropriate
mock_get_jobs,
fake_uuid,
):
service_one['permissions'] += ['international_letters']
service_one['permissions'] += ['letter', 'international_letters']
mocker.patch('app.main.views.send.get_page_count_for_letter', return_value=9)
mocker.patch(
'app.main.views.send.s3download',
@@ -1328,6 +1332,7 @@ def test_send_one_off_does_not_send_without_the_correct_permissions(
service_id=SERVICE_ONE_ID,
template_id=template_id,
_follow_redirects=True,
_expected_status=403,
)
assert page.select('main p')[0].text.strip() == "Sending text messages has been disabled for your service."

View File

@@ -674,6 +674,41 @@ def test_should_be_able_to_view_a_template_with_links(
)
def test_view_broadcast_template(
client_request,
service_one,
mock_get_broadcast_template,
mock_get_template_folders,
active_user_with_permissions,
fake_uuid,
):
page = client_request.get(
'.view_template',
service_id=SERVICE_ONE_ID,
template_id=fake_uuid,
_test_page_title=False,
)
assert [
(link.text.strip(), link['href'])
for link in page.select('.pill-separate-item')
] == [
('Edit', url_for(
'.edit_service_template',
service_id=SERVICE_ONE_ID,
template_id=fake_uuid,
)),
]
assert (
normalize_spaces(page.select_one('.template-container').text)
) == (
normalize_spaces(page.select_one('.broadcast-message-wrapper').text)
) == (
'This is a test'
)
def test_should_show_template_id_on_template_page(
client_request,
mock_get_service_template,
@@ -1241,9 +1276,9 @@ def test_cant_copy_template_from_non_member_service(
assert mock_get_service_email_template.call_args_list == []
@pytest.mark.parametrize('endpoint, data, expected_error', (
@pytest.mark.parametrize('service_permissions, data, expected_error', (
(
'main.choose_template',
['letter'],
{
'operation': 'add-new-template',
'add_template_by_template_type': 'email',
@@ -1251,30 +1286,47 @@ def test_cant_copy_template_from_non_member_service(
"Sending emails has been disabled for your service."
),
(
'main.choose_template',
['email'],
{
'operation': 'add-new-template',
'add_template_by_template_type': 'sms',
},
"Sending text messages has been disabled for your service."
),
(
['sms'],
{
'operation': 'add-new-template',
'add_template_by_template_type': 'letter',
},
"Sending letters has been disabled for your service."
),
(
['letter'],
{
'operation': 'add-new-template',
'add_template_by_template_type': 'broadcast',
},
"Sending broadcasts has been disabled for your service."
),
))
def test_should_not_allow_creation_of_template_through_form_without_correct_permission(
client_request,
service_one,
mock_get_service_templates,
mock_get_template_folders,
endpoint,
service_permissions,
data,
expected_error,
fake_uuid,
):
service_one['permissions'] = []
service_one['permissions'] = service_permissions
page = client_request.post(
endpoint,
'main.choose_template',
service_id=SERVICE_ONE_ID,
_data=data,
_follow_redirects=True,
_expected_status=403,
)
assert normalize_spaces(page.select('main p')[0].text) == expected_error
assert page.select(".govuk-back-link")[0].text == "Back"
@@ -1284,24 +1336,31 @@ def test_should_not_allow_creation_of_template_through_form_without_correct_perm
)
@pytest.mark.parametrize('type_of_template', ['email', 'sms'])
@pytest.mark.parametrize('method', ('get', 'post'))
@pytest.mark.parametrize('type_of_template, expected_error', [
('email', 'Sending emails has been disabled for your service.'),
('sms', 'Sending text messages has been disabled for your service.'),
('letter', 'Sending letters has been disabled for your service.'),
('broadcast', 'Sending broadcasts has been disabled for your service.'),
])
def test_should_not_allow_creation_of_a_template_without_correct_permission(
client_request,
service_one,
mocker,
method,
type_of_template,
expected_error,
):
service_one['permissions'] = []
template_description = {'sms': 'text messages', 'email': 'emails'}
page = client_request.get(
page = getattr(client_request, method)(
'.add_service_template',
service_id=SERVICE_ONE_ID,
template_type=type_of_template,
_follow_redirects=True,
_expected_status=403,
)
assert page.select('main p')[0].text.strip() == \
"Sending {} has been disabled for your service.".format(template_description[type_of_template])
assert page.select('main p')[0].text.strip() == expected_error
assert page.select(".govuk-back-link")[0].text == "Back"
assert page.select(".govuk-back-link")[0]['href'] == url_for(
'.choose_template',
@@ -1419,6 +1478,7 @@ def test_should_not_allow_template_edits_without_correct_permission(
service_id=SERVICE_ONE_ID,
template_id=fake_uuid,
_follow_redirects=True,
_expected_status=403,
)
assert page.select('main p')[0].text.strip() == "Sending text messages has been disabled for your service."
@@ -1934,15 +1994,26 @@ def test_can_create_email_template_with_emoji(
assert mock_create_service_template.called is True
def test_should_not_create_sms_template_with_emoji(
@pytest.mark.parametrize('template_type, expected_error', (
('sms', (
'You cannot use 🍜 in text messages.'
)),
('broadcast', (
'You cannot use 🍜 in broadcasts.'
)),
))
def test_should_not_create_sms_or_broadcast_template_with_emoji(
client_request,
service_one,
mock_create_service_template,
template_type,
expected_error,
):
service_one['permissions'] += [template_type]
page = client_request.post(
'.add_service_template',
service_id=SERVICE_ONE_ID,
template_type='sms',
template_type=template_type,
_data={
'name': "new name",
'template_content': "here are some noodles 🍜",
@@ -1952,16 +2023,37 @@ def test_should_not_create_sms_template_with_emoji(
},
_expected_status=200,
)
assert "You cannot use 🍜 in text messages." in page.text
assert expected_error in page.text
assert mock_create_service_template.called is False
@pytest.mark.parametrize('template_type, expected_error', (
('sms', (
'You cannot use 🍔 in text messages.'
)),
('broadcast', (
'You cannot use 🍔 in broadcasts.'
)),
))
def test_should_not_update_sms_template_with_emoji(
mocker,
client_request,
service_one,
mock_get_service_template,
mock_update_service_template,
fake_uuid,
template_type,
expected_error,
):
service_one['permissions'] += [template_type]
return mocker.patch(
'app.service_api_client.get_service_template',
return_value=template_json(
SERVICE_ONE_ID,
fake_uuid,
type_=template_type,
),
)
page = client_request.post(
'.edit_service_template',
service_id=SERVICE_ONE_ID,
@@ -1971,19 +2063,26 @@ def test_should_not_update_sms_template_with_emoji(
'name': "new name",
'template_content': "here's a burger 🍔",
'service': SERVICE_ONE_ID,
'template_type': 'sms',
'template_type': template_type,
'process_type': 'normal'
},
_expected_status=200,
)
assert "You cannot use 🍔 in text messages." in page.text
assert expected_error in page.text
assert mock_update_service_template.called is False
def test_should_create_sms_template_without_downgrading_unicode_characters(
@pytest.mark.parametrize('template_type', (
'sms', 'broadcast'
))
def test_should_create_sms_or_broadcast_template_without_downgrading_unicode_characters(
client_request,
mock_create_service_template
service_one,
mock_create_service_template,
template_type,
):
service_one['permissions'] += [template_type]
msg = 'here:\tare some “fancy quotes” and non\u200Bbreaking\u200Bspaces'
client_request.post(
@@ -1993,7 +2092,7 @@ def test_should_create_sms_template_without_downgrading_unicode_characters(
_data={
'name': "new name",
'template_content': msg,
'template_type': 'sms',
'template_type': template_type,
'service': SERVICE_ONE_ID,
'process_type': 'normal'
},