mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-05-05 08:31:00 -04:00
This involves three changes which broke our code. To validate email addresses, the optional dependency `email-validator` must be installed<sup>1</sup>. But since we don’t use WTForms’ email validation, we shouldn’t need to subclass it – it can just be its own self contained thing. Then we don’t need to add the extra dependency. When rendering textareas, and extra `\r\n` is inserted at the beginning <sup>2</sup>. Browsers will strip this when displaying the textbox and submitting the form, but some of our tests need updating to account for this. The error message for when you don’t choose an option from some radio buttons has now changed. Rather than just accepting WTForms’ new message, this commit makes the error messages like the examples from the Design System<sup>3</sup>. By default it will say ‘Select an option’, but by passing in an extra parameter (`thing`) it can be customised to be more specific, for example ‘Select a type of organisation’. *** 1. https://github.com/wtforms/wtforms/pull/429 2. https://github.com/wtforms/wtforms/issues/238 3. https://design-system.service.gov.uk/components/radios/#error-messages
93 lines
3.3 KiB
Python
93 lines
3.3 KiB
Python
import pytest
|
|
|
|
from app.main.forms import ServiceContactDetailsForm
|
|
|
|
|
|
def test_form_fails_validation_with_no_radio_buttons_selected(app_):
|
|
with app_.test_request_context(method='POST', data={}):
|
|
form = ServiceContactDetailsForm()
|
|
|
|
assert not form.validate_on_submit()
|
|
assert len(form.errors) == 1
|
|
assert form.errors['contact_details_type'] == ['Select an option']
|
|
|
|
|
|
@pytest.mark.parametrize('selected_radio_button, selected_text_box, text_box_data', [
|
|
('email_address', 'url', 'http://www.example.com'),
|
|
('phone_number', 'url', 'http://www.example.com'),
|
|
('url', 'email_address', 'user@example.com'),
|
|
('phone_number', 'email_address', 'user@example.com'),
|
|
('url', 'phone_number', '0207 123 4567'),
|
|
('email_address', 'phone_number', '0207 123 4567'),
|
|
])
|
|
def test_form_fails_validation_when_radio_button_selected_and_text_box_filled_in_do_not_match(
|
|
app_,
|
|
selected_radio_button,
|
|
selected_text_box,
|
|
text_box_data
|
|
):
|
|
data = {'contact_details_type': selected_radio_button, selected_text_box: text_box_data}
|
|
|
|
with app_.test_request_context(method='POST', data=data):
|
|
form = ServiceContactDetailsForm()
|
|
|
|
assert not form.validate_on_submit()
|
|
assert len(form.errors) == 1
|
|
assert form.errors[selected_radio_button] == ['This field is required.']
|
|
|
|
|
|
@pytest.mark.parametrize('selected_field, url, email_address, phone_number', [
|
|
('url', 'http://www.example.com', 'invalid-email.com', 'phone'),
|
|
('email_address', 'www.invalid-url.com', 'me@example.com', 'phone'),
|
|
('phone_number', 'www.invalid-url.com', 'invalid-email.com', '0207 123 4567'),
|
|
])
|
|
def test_form_only_validates_the_field_which_matches_the_selected_radio_button(
|
|
app_,
|
|
selected_field,
|
|
url,
|
|
email_address,
|
|
phone_number,
|
|
):
|
|
data = {'contact_details_type': selected_field,
|
|
'url': url,
|
|
'email_address': email_address,
|
|
'phone_number': phone_number}
|
|
|
|
with app_.test_request_context(method='POST', data=data):
|
|
form = ServiceContactDetailsForm()
|
|
|
|
assert form.validate_on_submit()
|
|
|
|
|
|
def test_form_url_validation_fails_with_invalid_url_field(app_):
|
|
data = {'contact_details_type': 'url', 'url': 'www.example.com'}
|
|
|
|
with app_.test_request_context(method='POST', data=data):
|
|
form = ServiceContactDetailsForm()
|
|
|
|
assert not form.validate_on_submit()
|
|
assert len(form.errors) == 1
|
|
assert len(form.errors['url']) == 1
|
|
|
|
|
|
def test_form_email_validation_fails_with_invalid_email_address_field(app_):
|
|
data = {'contact_details_type': 'email_address', 'email_address': '1@co'}
|
|
|
|
with app_.test_request_context(method='POST', data=data):
|
|
form = ServiceContactDetailsForm()
|
|
|
|
assert not form.validate_on_submit()
|
|
assert len(form.errors) == 1
|
|
assert len(form.errors['email_address']) == 2
|
|
|
|
|
|
def test_form_phone_number_validation_fails_with_invalid_phone_number_field(app_):
|
|
data = {'contact_details_type': 'phone_number', 'phone_number': '1235 A'}
|
|
|
|
with app_.test_request_context(method='POST', data=data):
|
|
form = ServiceContactDetailsForm()
|
|
|
|
assert not form.validate_on_submit()
|
|
assert len(form.errors) == 1
|
|
assert form.errors['phone_number'] == ['Must be a valid phone number']
|