Files
notifications-admin/tests/app/main/test_placeholder_form.py
Ben Thorner 5bfce61bcf Rename "app_" fixture to "notify_admin"
This naming was introduced in 2016 without explanation [1]. I find it
confusing because:

- It's reminiscent of "_app", which is a Python convention indicating
the variable is internal, so maybe avoid using it.

- It suggests there's some other "app" fixture I should be using (there
isn't, though).

The Python style guide describes using an underscore suffix to avoid
clashes with inbuilt names [1], which is sort of applicable if we need
to import the "app" module [2]. However, we can also avoid clashes by
choosing a different name, without the strange underscore.

[1]: 3b1d521c10
[2]: 78824f54fd/tests/app/main/views/test_forgot_password.py (L5)
2021-05-19 11:44:20 +01:00

72 lines
2.6 KiB
Python

import pytest
from app.main.forms import get_placeholder_form_instance
def test_form_class_not_mutated(notify_admin):
with notify_admin.test_request_context(
method='POST',
data={'placeholder_value': ''}
):
form1 = get_placeholder_form_instance('name', {}, 'sms')
form2 = get_placeholder_form_instance('city', {}, 'sms')
assert not form1.validate_on_submit()
assert not form2.validate_on_submit()
assert str(form1.placeholder_value.label) == '<label for="placeholder_value">name</label>'
assert str(form2.placeholder_value.label) == '<label for="placeholder_value">city</label>'
@pytest.mark.parametrize('service_can_send_international_sms, placeholder_name, template_type, value, expected_error', [
(False, 'email address', 'email', '', 'Cannot be empty'),
(False, 'email address', 'email', '12345', 'Enter a valid email address'),
(False, 'email address', 'email', '“bad”@email-address.com', 'Enter a valid email address'),
(False, 'email address', 'email', 'test@example.com', None),
(False, 'email address', 'email', 'test@example.gov.uk', None),
(False, 'phone number', 'sms', '', 'Cannot be empty'),
(False, 'phone number', 'sms', '+1-2345-678890', 'Not a UK mobile number'),
(False, 'phone number', 'sms', '07900900123', None),
(False, 'phone number', 'sms', '+44(0)7900 900-123', None),
(True, 'phone number', 'sms', '+123', 'Not enough digits'),
(True, 'phone number', 'sms', '+44(0)7900 900-123', None),
(True, 'phone number', 'sms', '+1-2345-678890', None),
(False, 'anything else', 'sms', '', 'Cannot be empty'),
(False, 'anything else', 'email', '', 'Cannot be empty'),
(True, 'phone number', 'sms', 'invalid', 'Must not contain letters or symbols'),
(True, 'phone number', 'email', 'invalid', None),
(True, 'phone number', 'letter', 'invalid', None),
(True, 'email address', 'sms', 'invalid', None),
])
def test_validates_recipients(
notify_admin,
placeholder_name,
template_type,
value,
service_can_send_international_sms,
expected_error,
):
with notify_admin.test_request_context(
method='POST',
data={'placeholder_value': value}
):
form = get_placeholder_form_instance(
placeholder_name,
{},
template_type,
allow_international_phone_numbers=service_can_send_international_sms,
)
if expected_error:
assert not form.validate_on_submit()
assert form.placeholder_value.errors[0] == expected_error
else:
assert form.validate_on_submit()