mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-19 14:09:20 -04:00
Take out the Canadian politeness.
Make the error message more consistent. Extracted common fields for the forms.
This commit is contained in:
@@ -1,28 +1,61 @@
|
|||||||
from datetime import datetime
|
|
||||||
from flask import session
|
|
||||||
|
|
||||||
from flask_wtf import Form
|
from flask_wtf import Form
|
||||||
from wtforms import StringField, PasswordField, ValidationError
|
from wtforms import StringField, PasswordField, ValidationError
|
||||||
from wtforms.validators import DataRequired, Email, Length, Regexp
|
from wtforms.validators import DataRequired, Email, Length, Regexp
|
||||||
from app.main.validators import Blacklist, ValidateUserCodes
|
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):
|
class LoginForm(Form):
|
||||||
email_address = StringField('Email address', validators=[
|
email_address = StringField('Email address', validators=[
|
||||||
Length(min=5, max=255),
|
Length(min=5, max=255),
|
||||||
DataRequired(message='Email cannot be empty'),
|
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=[
|
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):
|
class RegisterUserForm(Form):
|
||||||
def __init__(self, existing_email_addresses, existing_mobile_numbers, *args, **kwargs):
|
def __init__(self, existing_email_addresses, existing_mobile_numbers, *args, **kwargs):
|
||||||
self.existing_emails = existing_email_addresses
|
self.existing_emails = existing_email_addresses
|
||||||
@@ -31,19 +64,9 @@ class RegisterUserForm(Form):
|
|||||||
|
|
||||||
name = StringField('Full name',
|
name = StringField('Full name',
|
||||||
validators=[DataRequired(message='Name can not be empty')])
|
validators=[DataRequired(message='Name can not be empty')])
|
||||||
email_address = StringField('Email address', validators=[
|
email_address = email_address()
|
||||||
Length(min=5, max=255),
|
mobile_number = mobile_number()
|
||||||
DataRequired(message='Email cannot be empty'),
|
password = password()
|
||||||
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')])
|
|
||||||
|
|
||||||
def validate_email_address(self, field):
|
def validate_email_address(self, field):
|
||||||
# Validate email address is unique.
|
# Validate email address is unique.
|
||||||
@@ -59,7 +82,6 @@ class RegisterUserForm(Form):
|
|||||||
|
|
||||||
|
|
||||||
class TwoFactorForm(Form):
|
class TwoFactorForm(Form):
|
||||||
|
|
||||||
def __init__(self, user_codes, *args, **kwargs):
|
def __init__(self, user_codes, *args, **kwargs):
|
||||||
'''
|
'''
|
||||||
Keyword arguments:
|
Keyword arguments:
|
||||||
@@ -69,13 +91,10 @@ class TwoFactorForm(Form):
|
|||||||
self.user_codes = user_codes
|
self.user_codes = user_codes
|
||||||
super(TwoFactorForm, self).__init__(*args, **kwargs)
|
super(TwoFactorForm, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
sms_code = StringField('sms code', validators=[DataRequired(message='Enter verification code'),
|
sms_code = sms_code()
|
||||||
Regexp(regex=verify_code, message='Code must be 5 digits'),
|
|
||||||
ValidateUserCodes(code_type='sms')])
|
|
||||||
|
|
||||||
|
|
||||||
class VerifyForm(Form):
|
class VerifyForm(Form):
|
||||||
|
|
||||||
def __init__(self, user_codes, *args, **kwargs):
|
def __init__(self, user_codes, *args, **kwargs):
|
||||||
'''
|
'''
|
||||||
Keyword arguments:
|
Keyword arguments:
|
||||||
@@ -85,29 +104,16 @@ class VerifyForm(Form):
|
|||||||
self.user_codes = user_codes
|
self.user_codes = user_codes
|
||||||
super(VerifyForm, self).__init__(*args, **kwargs)
|
super(VerifyForm, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
sms_code = StringField("Text message confirmation code",
|
sms_code = sms_code()
|
||||||
validators=[DataRequired(message='SMS code can not be empty'),
|
email_code = email_code()
|
||||||
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')])
|
|
||||||
|
|
||||||
|
|
||||||
class EmailNotReceivedForm(Form):
|
class EmailNotReceivedForm(Form):
|
||||||
email_address = StringField('Email address', validators=[
|
email_address = email_address()
|
||||||
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')
|
|
||||||
])
|
|
||||||
|
|
||||||
|
|
||||||
class TextNotReceivedForm(Form):
|
class TextNotReceivedForm(Form):
|
||||||
mobile_number = StringField('Mobile phone number', validators=[
|
mobile_number = mobile_number()
|
||||||
DataRequired(message='Please enter your mobile number'),
|
|
||||||
Regexp(regex=mobile_number, message='Please enter a +44 mobile number')])
|
|
||||||
|
|
||||||
|
|
||||||
class AddServiceForm(Form):
|
class AddServiceForm(Form):
|
||||||
@@ -116,7 +122,7 @@ class AddServiceForm(Form):
|
|||||||
super(AddServiceForm, self).__init__(*args, **kwargs)
|
super(AddServiceForm, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
service_name = StringField(validators=[
|
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):
|
def validate_service_name(self, a):
|
||||||
if self.service_name.data in self.service_names:
|
if self.service_name.data in self.service_names:
|
||||||
@@ -124,16 +130,8 @@ class AddServiceForm(Form):
|
|||||||
|
|
||||||
|
|
||||||
class ForgotPasswordForm(Form):
|
class ForgotPasswordForm(Form):
|
||||||
email_address = StringField('Email address',
|
email_address = 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')
|
|
||||||
])
|
|
||||||
|
|
||||||
|
|
||||||
class NewPasswordForm(Form):
|
class NewPasswordForm(Form):
|
||||||
new_password = StringField('Create a password',
|
new_password = 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')])
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ def forgot_password():
|
|||||||
send_change_password_email(form.email_address.data)
|
send_change_password_email(form.email_address.data)
|
||||||
return render_template('views/password-reset-sent.html')
|
return render_template('views/password-reset-sent.html')
|
||||||
else:
|
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)
|
return render_template('views/forgot-password.html', form=form)
|
||||||
else:
|
else:
|
||||||
return render_template('views/forgot-password.html', form=form)
|
return render_template('views/forgot-password.html', form=form)
|
||||||
|
|||||||
@@ -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'})
|
client.post('/two-factor', data={'sms_code': '12345'})
|
||||||
response = client.post('/add-service', data={})
|
response = client.post('/add-service', data={})
|
||||||
assert response.status_code == 200
|
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)
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ def test_process_register_returns_400_when_mobile_number_is_invalid(notification
|
|||||||
'password': 'validPassword!'})
|
'password': 'validPassword!'})
|
||||||
|
|
||||||
assert response.status_code == 200
|
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,
|
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!'})
|
'password': 'validPassword!'})
|
||||||
|
|
||||||
assert response.status_code == 200
|
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):
|
def test_should_add_verify_codes_on_session(notifications_admin, notifications_admin_db, mocker, notify_db_session):
|
||||||
|
|||||||
Reference in New Issue
Block a user