Make error message specific to template type

This commit is contained in:
Chris Hill-Scott
2020-07-03 15:46:00 +01:00
parent f3115c0cea
commit e832d18002
4 changed files with 42 additions and 11 deletions

View File

@@ -777,11 +777,12 @@ class BaseTemplateForm(StripWhitespaceForm):
class SMSTemplateForm(BaseTemplateForm):
def validate_template_content(self, field):
OnlySMSCharacters()(None, field)
OnlySMSCharacters(template_type='sms')(None, field)
class BroadcastTemplateForm(SMSTemplateForm):
pass
def validate_template_content(self, field):
OnlySMSCharacters(template_type='broadcast')(None, field)
class LetterAddressForm(StripWhitespaceForm):

View File

@@ -88,12 +88,21 @@ class NoEmbeddedImagesInSVG:
class OnlySMSCharacters:
def __init__(self, *args, template_type, **kwargs):
self._template_type = template_type
super().__init__(*args, **kwargs)
def __call__(self, form, field):
non_sms_characters = sorted(list(SanitiseSMS.get_non_compatible_characters(field.data)))
if non_sms_characters:
raise ValidationError(
'You cannot use {} in text messages. {} will not show up properly on everyones phones.'.format(
'You cannot use {} in {}. {} will not show up properly on everyones phones.'.format(
formatted_list(non_sms_characters, conjunction='or', before_each='', after_each=''),
{
'broadcast': 'broadcasts',
'sms': 'text messages',
}.get(self._template_type),
('It' if len(non_sms_characters) == 1 else 'They')
)
)

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

@@ -1994,14 +1994,20 @@ def test_can_create_email_template_with_emoji(
assert mock_create_service_template.called is True
@pytest.mark.parametrize('template_type', (
'sms', 'broadcast'
@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(
@@ -2017,22 +2023,37 @@ def test_should_not_create_sms_or_broadcast_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', (
'sms', 'broadcast'
@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,
@@ -2047,7 +2068,7 @@ def test_should_not_update_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_update_service_template.called is False