diff --git a/app/main/forms.py b/app/main/forms.py index 46321303d..96980aeb5 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -1,28 +1,61 @@ -from datetime import datetime -from flask import session - from flask_wtf import Form from wtforms import StringField, PasswordField, ValidationError from wtforms.validators import DataRequired, Email, Length, Regexp from app.main.validators import Blacklist, ValidateUserCodes +def email_address(): + gov_uk_email \ + = "(^[^@^\\s]+@[^@^\\.^\\s]+(\\.[^@^\\.^\\s]*)*.gov.uk)" + return StringField('Email address', validators=[ + Length(min=5, max=255), + DataRequired(message='Email cannot be empty'), + Email(message='Enter a valid email address'), + Regexp(regex=gov_uk_email, message='Enter a gov.uk email address')]) + + +def mobile_number(): + mobile_number_regex = "^\\+44[\\d]{10}$" + return StringField('Mobile phone number', + validators=[DataRequired(message='Mobile number can not be empty'), + Regexp(regex=mobile_number_regex, message='Enter a +44 mobile number')]) + + +def password(): + return PasswordField('Create a password', + validators=[DataRequired(message='Password can not be empty'), + Length(10, 255, message='Password must be at least 10 characters'), + Blacklist(message='That password is blacklisted, too common')]) + + +def sms_code(): + verify_code = '^\d{5}$' + return StringField('Text message confirmation code', + validators=[DataRequired(message='Text message confirmation code can not be empty'), + Regexp(regex=verify_code, + message='Text message confirmation code must be 5 digits'), + ValidateUserCodes(code_type='sms')]) + + +def email_code(): + verify_code = '^\d{5}$' + return StringField("Email confirmation code", + validators=[DataRequired(message='Email confirmation code can not be empty'), + Regexp(regex=verify_code, message='Email confirmation code must be 5 digits'), + ValidateUserCodes(code_type='email')]) + + class LoginForm(Form): email_address = StringField('Email address', validators=[ Length(min=5, max=255), DataRequired(message='Email cannot be empty'), - Email(message='Please enter a valid email address') + Email(message='Enter a valid email address') ]) password = PasswordField('Password', validators=[ - DataRequired(message='Please enter your password') + DataRequired(message='Enter your password') ]) -gov_uk_email = "(^[^@^\\s]+@[^@^\\.^\\s]+(\\.[^@^\\.^\\s]*)*.gov.uk)" -mobile_number = "^\\+44[\\d]{10}$" -verify_code = '^\d{5}$' - - class RegisterUserForm(Form): def __init__(self, existing_email_addresses, existing_mobile_numbers, *args, **kwargs): self.existing_emails = existing_email_addresses @@ -31,19 +64,9 @@ class RegisterUserForm(Form): name = StringField('Full name', validators=[DataRequired(message='Name can not be empty')]) - email_address = StringField('Email address', validators=[ - Length(min=5, max=255), - DataRequired(message='Email cannot be empty'), - Email(message='Please enter a valid email address'), - Regexp(regex=gov_uk_email, message='Please enter a gov.uk email address') - ]) - mobile_number = StringField('Mobile phone number', - validators=[DataRequired(message='Please enter your mobile number'), - Regexp(regex=mobile_number, message='Please enter a +44 mobile number')]) - password = PasswordField('Create a password', - validators=[DataRequired(message='Please enter your password'), - Length(10, 255, message='Password must be at least 10 characters'), - Blacklist(message='That password is blacklisted, too common')]) + email_address = email_address() + mobile_number = mobile_number() + password = password() def validate_email_address(self, field): # Validate email address is unique. @@ -59,7 +82,6 @@ class RegisterUserForm(Form): class TwoFactorForm(Form): - def __init__(self, user_codes, *args, **kwargs): ''' Keyword arguments: @@ -69,13 +91,10 @@ class TwoFactorForm(Form): self.user_codes = user_codes super(TwoFactorForm, self).__init__(*args, **kwargs) - sms_code = StringField('sms code', validators=[DataRequired(message='Enter verification code'), - Regexp(regex=verify_code, message='Code must be 5 digits'), - ValidateUserCodes(code_type='sms')]) + sms_code = sms_code() class VerifyForm(Form): - def __init__(self, user_codes, *args, **kwargs): ''' Keyword arguments: @@ -85,29 +104,16 @@ class VerifyForm(Form): self.user_codes = user_codes super(VerifyForm, self).__init__(*args, **kwargs) - sms_code = StringField("Text message confirmation code", - validators=[DataRequired(message='SMS code can not be empty'), - Regexp(regex=verify_code, message='Code must be 5 digits'), - ValidateUserCodes(code_type='sms')]) - email_code = StringField("Email confirmation code", - validators=[DataRequired(message='Email code can not be empty'), - Regexp(regex=verify_code, message='Code must be 5 digits'), - ValidateUserCodes(code_type='email')]) + sms_code = sms_code() + email_code = email_code() class EmailNotReceivedForm(Form): - email_address = StringField('Email address', validators=[ - Length(min=5, max=255), - DataRequired(message='Email cannot be empty'), - Email(message='Please enter a valid email address'), - Regexp(regex=gov_uk_email, message='Please enter a gov.uk email address') - ]) + email_address = email_address() class TextNotReceivedForm(Form): - mobile_number = StringField('Mobile phone number', validators=[ - DataRequired(message='Please enter your mobile number'), - Regexp(regex=mobile_number, message='Please enter a +44 mobile number')]) + mobile_number = mobile_number() class AddServiceForm(Form): @@ -116,7 +122,7 @@ class AddServiceForm(Form): super(AddServiceForm, self).__init__(*args, **kwargs) service_name = StringField(validators=[ - DataRequired(message='Please enter your service name')]) + DataRequired(message='Service name can not be empty')]) def validate_service_name(self, a): if self.service_name.data in self.service_names: @@ -124,16 +130,8 @@ class AddServiceForm(Form): class ForgotPasswordForm(Form): - email_address = StringField('Email address', - validators=[Length(min=5, max=255), - DataRequired(message='Email cannot be empty'), - Email(message='Please enter a valid email address'), - Regexp(regex=gov_uk_email, message='Please enter a gov.uk email address') - ]) + email_address = email_address() class NewPasswordForm(Form): - new_password = StringField('Create a password', - validators=[DataRequired(message='Please enter your password'), - Length(10, 255, message='Password must be at least 10 characters'), - Blacklist(message='That password is blacklisted, too common')]) + new_password = password() diff --git a/app/main/views/forgot_password.py b/app/main/views/forgot_password.py index 8413d7270..0488afac3 100644 --- a/app/main/views/forgot_password.py +++ b/app/main/views/forgot_password.py @@ -13,7 +13,7 @@ def forgot_password(): send_change_password_email(form.email_address.data) return render_template('views/password-reset-sent.html') else: - flash('The email address is not recognized. Try again.') + flash('The email address is not recognized. Enter the password you registered with.') return render_template('views/forgot-password.html', form=form) else: return render_template('views/forgot-password.html', form=form) diff --git a/tests/app/main/views/test_add_service.py b/tests/app/main/views/test_add_service.py index 5f199ac63..02506ace3 100644 --- a/tests/app/main/views/test_add_service.py +++ b/tests/app/main/views/test_add_service.py @@ -39,4 +39,4 @@ def test_should_return_form_errors_when_service_name_is_empty(notifications_admi client.post('/two-factor', data={'sms_code': '12345'}) response = client.post('/add-service', data={}) assert response.status_code == 200 - assert 'Please enter your service name' in response.get_data(as_text=True) + assert 'Service name can not be empty' in response.get_data(as_text=True) diff --git a/tests/app/main/views/test_register.py b/tests/app/main/views/test_register.py index 666e9f86d..cdf021adc 100644 --- a/tests/app/main/views/test_register.py +++ b/tests/app/main/views/test_register.py @@ -31,7 +31,7 @@ def test_process_register_returns_400_when_mobile_number_is_invalid(notification 'password': 'validPassword!'}) assert response.status_code == 200 - assert 'Please enter a +44 mobile number' in response.get_data(as_text=True) + assert 'Enter a +44 mobile number' in response.get_data(as_text=True) def test_should_return_400_when_email_is_not_gov_uk(notifications_admin, @@ -46,7 +46,7 @@ def test_should_return_400_when_email_is_not_gov_uk(notifications_admin, 'password': 'validPassword!'}) assert response.status_code == 200 - assert 'Please enter a gov.uk email address' in response.get_data(as_text=True) + assert 'Enter a gov.uk email address' in response.get_data(as_text=True) def test_should_add_verify_codes_on_session(notifications_admin, notifications_admin_db, mocker, notify_db_session):