Files
notifications-admin/tests/app/main/test_validators.py
David McDonald a6cac27957 Allow straight single quote in sms sender names
This is so we can allow the sender name 'UC' for DWP.

Note, this is specifically only straight single quotes and not curly
quotes or double quotes. Curly quotes are not supported in the GSM
character set (https://en.wikipedia.org/wiki/GSM_03.38). There is
currently no defined user ask to support double quotes in sms sender
names.

I have tested this by sending a message through both Firetext and MMG to
make sure they both support the single quote character in SMS sender
names.

DWP also have had no particular issues using the SMS sender name with
their existing system in the past either.
2021-07-27 09:26:16 +01:00

222 lines
6.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from unittest.mock import Mock
import pytest
from wtforms import ValidationError
from app.main.forms import RegisterUserForm, ServiceSmsSenderForm
from app.main.validators import (
MustContainAlphanumericCharacters,
NoCommasInPlaceHolders,
OnlySMSCharacters,
ValidGovEmail,
)
@pytest.mark.parametrize('password', [
'govuknotify', '11111111', 'kittykat', 'blackbox'
])
def test_should_raise_validation_error_for_password(
client,
mock_get_user_by_email,
password,
):
form = RegisterUserForm()
form.name.data = 'test'
form.email_address.data = 'teset@example.gov.uk'
form.mobile_number.data = '441231231231'
form.password.data = password
form.validate()
assert 'Choose a password thats harder to guess' in form.errors['password']
def test_valid_email_not_in_valid_domains(
client,
mock_get_organisations,
):
form = RegisterUserForm(email_address="test@test.com", mobile_number='441231231231')
assert not form.validate()
assert "Enter a public sector email address" in form.errors['email_address'][0]
def test_valid_email_in_valid_domains(
client
):
form = RegisterUserForm(
name="test",
email_address="test@my.gov.uk",
mobile_number='4407888999111',
password='an uncommon password')
form.validate()
assert form.errors == {}
def test_invalid_email_address_error_message(
client,
mock_get_organisations,
):
form = RegisterUserForm(
name="test",
email_address="test.com",
mobile_number='4407888999111',
password='1234567890')
assert not form.validate()
form = RegisterUserForm(
name="test",
email_address="test.com",
mobile_number='4407888999111',
password='1234567890')
assert not form.validate()
def _gen_mock_field(x):
return Mock(data=x)
@pytest.mark.parametrize("email", [
'test@gov.uk',
'test@GOV.UK',
'test@gov.uK',
'test@test.test.gov.uk',
'test@test.gov.uk',
'test@nhs.uk',
'test@gov.nhs.uk',
'test@nhs.net',
'test@gov.nhs.net',
'test@nhs.scot',
'test@police.uk',
'test@gov.police.uk',
'test@GOV.PoliCe.uk',
'test@cjsm.net',
'test@example.ac.uk',
'test@example.sch.uk',
])
def test_valid_list_of_white_list_email_domains(
client,
email,
):
email_domain_validators = ValidGovEmail()
email_domain_validators(None, _gen_mock_field(email))
@pytest.mark.parametrize("email", [
'test@ukgov.uk',
'test@gov.uk.uk',
'test@gov.test.uk',
'test@ukmod.uk',
'test@mod.uk.uk',
'test@mod.test.uk',
'test@ukddc-mod.org',
'test@ddc-mod.org.uk',
'test@ddc-mod.uk.org',
'test@ukgov.scot',
'test@gov.scot.uk',
'test@gov.test.scot',
'test@ukparliament.uk',
'test@parliament.uk.uk',
'test@parliament.test.uk',
'test@uknhs.uk',
'test@nhs.uk.uk',
'test@uknhs.net',
'test@nhs.net.uk',
'test@nhs.test.net',
'test@ukpolice.uk',
'test@police.uk.uk',
'test@police.test.uk',
'test@ucds.com',
'test@123bl.uk',
])
def test_invalid_list_of_white_list_email_domains(
client,
email,
mock_get_organisations,
):
email_domain_validators = ValidGovEmail()
with pytest.raises(ValidationError):
email_domain_validators(None, _gen_mock_field(email))
def test_for_commas_in_placeholders(
client
):
with pytest.raises(ValidationError) as error:
NoCommasInPlaceHolders()(None, _gen_mock_field('Hello ((name,date))'))
assert str(error.value) == 'You cannot put commas between double brackets'
NoCommasInPlaceHolders()(None, _gen_mock_field('Hello ((name))'))
@pytest.mark.parametrize('msg', ['The quick brown fox', 'Thé “quick” bröwn fox\u200B'])
def test_sms_character_validation(client, msg):
OnlySMSCharacters(template_type='sms')(None, _gen_mock_field(msg))
@pytest.mark.parametrize('data, err_msg', [
(
'∆ abc 📲 def 📵 ghi',
(
'You cannot use ∆, 📲 or 📵 in text messages. '
'They will not show up properly on everyones phones.'
)
),
(
'📵',
(
'You cannot use 📵 in text messages. '
'It will not show up properly on everyones phones.'
)
),
])
def test_non_sms_character_validation(data, err_msg, client):
with pytest.raises(ValidationError) as error:
OnlySMSCharacters(template_type='sms')(None, _gen_mock_field(data))
assert str(error.value) == err_msg
@pytest.mark.parametrize("string", [".", "A.", ".8...."])
def test_if_string_does_not_contain_alphanumeric_characters_raises(string):
with pytest.raises(ValidationError) as error:
MustContainAlphanumericCharacters()(None, _gen_mock_field(string))
assert str(error.value) == "Must include at least two alphanumeric characters"
@pytest.mark.parametrize("string", [".A8", "AB.", ".42...."])
def test_if_string_contains_alphanumeric_characters_does_not_raise(string):
MustContainAlphanumericCharacters()(None, _gen_mock_field(string))
@pytest.mark.parametrize(
"sms_sender,error_expected,error_message",
[
('', True, 'Cannot be empty'),
('22', True, 'Enter 3 characters or more'),
('333', False, None),
('elevenchars', False, None), # 11 chars
('twelvecharas', True, 'Enter 11 characters or fewer'), # 12 chars
('###', True, 'Use letters and numbers only'),
('00111222333', True, 'Cannot start with 00'),
('UK_GOV', False, None), # Underscores are allowed
('UK.GOV', False, None), # Full stops are allowed
("'UC'", False, None), # Straight single quotes are allowed
]
)
def test_sms_sender_form_validation(
client,
mock_get_user_by_email,
sms_sender,
error_expected,
error_message
):
form = ServiceSmsSenderForm()
form.sms_sender.data = sms_sender
form.validate()
if error_expected:
assert form.errors
assert error_message == form.errors['sms_sender'][0]
else:
assert not form.errors