Improve RegisterUserForm

This commit is contained in:
Matt Wright
2012-08-16 19:05:42 -04:00
parent 1c31728a26
commit adb550a9f2
2 changed files with 24 additions and 2 deletions
+16 -1
View File
@@ -21,6 +21,14 @@ from .exceptions import UserNotFoundError
_datastore = LocalProxy(lambda: app.extensions['security'].datastore)
def unique_user_email(form, field):
try:
_datastore.find_user(email=field.data)
raise ValidationError('%s is already associated with an account' % field.data)
except UserNotFoundError:
pass
def valid_user_email(form, field):
try:
_datastore.find_user(email=field.data)
@@ -41,6 +49,13 @@ class UserEmailFormMixin():
valid_user_email])
class UniqueEmailFormMixin():
email = TextField("Email Address",
validators=[Required(message="Email not provided"),
Email(message="Invalid email address"),
unique_user_email])
class PasswordFormMixin():
password = PasswordField("Password",
validators=[Required(message="Password not provided")])
@@ -102,7 +117,7 @@ class LoginForm(Form, EmailFormMixin, PasswordFormMixin):
class RegisterForm(Form,
EmailFormMixin,
UniqueEmailFormMixin,
PasswordFormMixin,
PasswordConfirmFormMixin):
"""The default register form"""
+8 -1
View File
@@ -237,9 +237,16 @@ class ConfiguredSecurityTests(SecurityTest):
password='password',
password_confirm='password')
r = self.client.post('/register', data=data, follow_redirects=True)
r = self._post('/register', data=data, follow_redirects=True)
self.assertIn('Post Register', r.data)
def test_register_existing_email(self):
data = dict(email='matt@lp.com',
password='password',
password_confirm='password')
r = self._post('/register', data=data, follow_redirects=True)
self.assertIn('matt@lp.com is already associated with an account', r.data)
def test_unauthorized(self):
self.authenticate("joe@lp.com", endpoint="/custom_auth")
r = self._get("/admin", follow_redirects=True)