diff --git a/app/main/forms.py b/app/main/forms.py index ae5fd6004..911b0531e 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -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): diff --git a/app/main/validators.py b/app/main/validators.py index 3bf5b19fe..9434c39c4 100644 --- a/app/main/validators.py +++ b/app/main/validators.py @@ -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 everyone’s phones.'.format( + 'You cannot use {} in {}. {} will not show up properly on everyone’s 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') ) ) diff --git a/tests/app/main/test_validators.py b/tests/app/main/test_validators.py index d0ab5e4f9..a92cf19d3 100644 --- a/tests/app/main/test_validators.py +++ b/tests/app/main/test_validators.py @@ -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 diff --git a/tests/app/main/views/test_templates.py b/tests/app/main/views/test_templates.py index 53bdd1987..bc07a0c49 100644 --- a/tests/app/main/views/test_templates.py +++ b/tests/app/main/views/test_templates.py @@ -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