Working tests, hopefully all code changes done.

This commit is contained in:
Nicholas Staples
2016-01-27 12:22:32 +00:00
parent ce81a6f540
commit 6959d695d3
57 changed files with 1143 additions and 1578 deletions

View File

@@ -11,8 +11,7 @@ from wtforms import (
)
from wtforms.validators import DataRequired, Email, Length, Regexp
from app.main.validators import Blacklist, ValidateUserCodes, CsvFileValidator
from app.main.dao import verify_codes_dao
from app.main.validators import Blacklist, CsvFileValidator
from app.main.encryption import check_hash
@@ -84,16 +83,14 @@ def sms_code():
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')])
message='Text message confirmation code must be 5 digits')])
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')])
Regexp(regex=verify_code, message='Email confirmation code must be 5 digits')])
class LoginForm(Form):
@@ -125,31 +122,45 @@ class RegisterUserForm(Form):
class TwoFactorForm(Form):
def __init__(self, user_codes, *args, **kwargs):
def __init__(self, validate_code_func, *args, **kwargs):
'''
Keyword arguments:
user_codes -- List of user code objects which have the fields
(code_type, expiry_datetime, code)
validate_code_func -- Validates the code with the API.
'''
self.user_codes = user_codes
self.validate_code_func = validate_code_func
super(TwoFactorForm, self).__init__(*args, **kwargs)
sms_code = sms_code()
def validate_sms_code(self, field):
is_valid, reason = self.validate_code_func(field.data)
if not is_valid:
raise ValidationError(reason)
class VerifyForm(Form):
def __init__(self, user_codes, *args, **kwargs):
def __init__(self, validate_code_func, *args, **kwargs):
'''
Keyword arguments:
user_codes -- List of user code objects which have the fields
(code_type, expiry_datetime, code)
validate_code_func -- Validates the code with the API.
'''
self.user_codes = user_codes
self.validate_code_func = validate_code_func
super(VerifyForm, self).__init__(*args, **kwargs)
sms_code = sms_code()
email_code = email_code()
def _validate_code(self, cde, code_type):
is_valid, reason = self.validate_code_func(cde, code_type)
if not is_valid:
raise ValidationError(reason)
def validate_email_code(self, field):
self._validate_code(field.data, 'email')
def validate_sms_code(self, field):
self._validate_code(field.data, 'sms')
class EmailNotReceivedForm(Form):
email_address = email_address()
@@ -218,9 +229,18 @@ class NewPasswordForm(Form):
class ChangePasswordForm(Form):
def __init__(self, validate_password_func, *args, **kwargs):
self.validate_password_func = validate_password_func
super(ChangePasswordForm, self).__init__(*args, **kwargs)
old_password = password('Current password')
new_password = password('New password')
def validate_old_password(self, field):
if not self.validate_password_func(field.data):
raise ValidationError('Invalid password')
class CsvUploadForm(Form):
file = FileField('File to upload', validators=[DataRequired(
@@ -232,20 +252,50 @@ class ChangeNameForm(Form):
class ChangeEmailForm(Form):
def __init__(self, validate_email_func, *args, **kwargs):
self.validate_email_func = validate_email_func
super(ChangeEmailForm, self).__init__(*args, **kwargs)
email_address = email_address()
def validate_email_address(self, field):
is_valid = self.validate_email_func(field.data)
if not is_valid:
raise ValidationError("The email address is already in use")
class ConfirmEmailForm(Form):
def __init__(self, validate_code_func, *args, **kwargs):
self.validate_code_func = validate_code_func
super(ConfirmEmailForm, self).__init__(*args, **kwargs)
email_code = email_code()
def validate_email_code(self, field):
is_valid, msg = self.validate_code_func(field.data)
if not is_valid:
raise ValidationError(msg)
class ChangeMobileNumberForm(Form):
mobile_number = mobile_number()
class ConfirmMobileNumberForm(Form):
def __init__(self, validate_code_func, *args, **kwargs):
self.validate_code_func = validate_code_func
super(ConfirmMobileNumberForm, self).__init__(*args, **kwargs)
sms_code = sms_code()
def validate_sms_code(self, field):
is_valid, msg = self.validate_code_func(field.data)
if not is_valid:
raise ValidationError(msg)
class CreateKeyForm(Form):
def __init__(self, existing_key_names=[], *args, **kwargs):