Use email fields for feedback form

Otherwise we can end up collecting invalid email addresses…

This required some refactoring to allow our email fields to be optional
(but not by default).
This commit is contained in:
Chris Hill-Scott
2018-12-07 12:23:12 +00:00
parent 7c4b292770
commit 184a9fa605
3 changed files with 42 additions and 5 deletions

View File

@@ -110,15 +110,18 @@ class MultiCheckboxField(SelectMultipleField):
option_widget = CheckboxInput() option_widget = CheckboxInput()
def email_address(label='Email address', gov_user=True): def email_address(label='Email address', gov_user=True, required=True):
validators = [ validators = [
Length(min=5, max=255), ValidEmail(),
DataRequired(message='Cant be empty'),
ValidEmail()
] ]
if gov_user: if gov_user:
validators.append(ValidGovEmail()) validators.append(ValidGovEmail())
if required:
validators.append(DataRequired(message='Cant be empty'))
return EmailField(label, validators) return EmailField(label, validators)
@@ -561,7 +564,7 @@ class SupportType(StripWhitespaceForm):
class Feedback(StripWhitespaceForm): class Feedback(StripWhitespaceForm):
name = StringField('Name') name = StringField('Name')
email_address = StringField('Email address') email_address = email_address(label='Email address', gov_user=False, required=False)
feedback = TextAreaField('Your message', validators=[DataRequired(message="Cant be empty")]) feedback = TextAreaField('Your message', validators=[DataRequired(message="Cant be empty")])

View File

@@ -38,6 +38,10 @@ class CsvFileValidator:
class ValidGovEmail: class ValidGovEmail:
def __call__(self, form, field): def __call__(self, form, field):
if field.data == '':
return
from flask import url_for from flask import url_for
message = ( message = (
'Enter a government email address.' 'Enter a government email address.'
@@ -53,10 +57,15 @@ class ValidEmail(Email):
super().__init__('Enter a valid email address') super().__init__('Enter a valid email address')
def __call__(self, form, field): def __call__(self, form, field):
if field.data == '':
return
try: try:
validate_email_address(field.data) validate_email_address(field.data)
except InvalidEmailError: except InvalidEmailError:
raise ValidationError(self.message) raise ValidationError(self.message)
return super().__call__(form, field) return super().__call__(form, field)

View File

@@ -17,6 +17,7 @@ from tests.conftest import (
mock_get_services, mock_get_services,
mock_get_services_with_no_services, mock_get_services_with_no_services,
mock_get_services_with_one_service, mock_get_services_with_one_service,
normalize_spaces,
) )
@@ -224,6 +225,30 @@ def test_email_address_required_for_problems(
assert isinstance(page.find('span', {'class': 'error-message'}), expected_error) assert isinstance(page.find('span', {'class': 'error-message'}), expected_error)
@freeze_time('2016-12-12 12:00:00.000000')
@pytest.mark.parametrize('ticket_type', (
PROBLEM_TICKET_TYPE, QUESTION_TICKET_TYPE
))
def test_email_address_must_be_valid_if_provided_to_support_form(
client,
mocker,
ticket_type,
):
response = client.post(
url_for('main.feedback', ticket_type=ticket_type),
data={
'feedback': 'blah',
'email_address': 'not valid',
},
)
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert normalize_spaces(page.select_one('span.error-message').text) == (
'Enter a valid email address'
)
@pytest.mark.parametrize('ticket_type, severe, is_in_business_hours, is_urgent, is_p1', [ @pytest.mark.parametrize('ticket_type, severe, is_in_business_hours, is_urgent, is_p1', [
# business hours, always urgent, never p1 # business hours, always urgent, never p1