[extensions-web] auth: ensure that email is unique



commit 106e73c378656fc2fb802837d39a36684e3ec4e9
Author: Yuri Konotopov <ykonotopov gnome org>
Date:   Tue Nov 29 23:43:20 2016 +0300

    auth: ensure that email is unique

 sweettooth/auth/forms.py |    4 ++--
 sweettooth/auth/tests.py |   33 +++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)
---
diff --git a/sweettooth/auth/forms.py b/sweettooth/auth/forms.py
index 373a9ab..92e2182 100644
--- a/sweettooth/auth/forms.py
+++ b/sweettooth/auth/forms.py
@@ -2,7 +2,7 @@
 from django import forms
 from django.contrib.auth import forms as auth_forms
 from django.utils.translation import ugettext_lazy as _
-from registration.forms import RegistrationForm
+from registration.forms import RegistrationFormUniqueEmail
 
 class PlainOutputForm(object):
     def as_plain(self):
@@ -33,7 +33,7 @@ class InlineAuthenticationForm(PlainOutputForm, AutoFocusForm,
 class AuthenticationForm(AutoFocusForm, auth_forms.AuthenticationForm):
     pass
 
-class RegistrationForm(RegistrationForm):
+class RegistrationForm(RegistrationFormUniqueEmail):
     # Copies the standard setting from the django.contrib.auth.forms
     username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[\w.@+-]+$',
         help_text = _("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."),
diff --git a/sweettooth/auth/tests.py b/sweettooth/auth/tests.py
new file mode 100644
index 0000000..4e91eb0
--- /dev/null
+++ b/sweettooth/auth/tests.py
@@ -0,0 +1,33 @@
+from registration import validators
+from registration.tests.base import RegistrationTestCase
+
+from forms import AutoFocusRegistrationForm
+from django.contrib.auth import get_user_model
+from django.utils.six import text_type
+
+User = get_user_model()
+
+# registration/tests/test_forms.py
+class AuthTests(RegistrationTestCase):
+    def test_email_uniqueness(self):
+        User.objects.create(
+            username='bob',
+            email=self.valid_data['email'],
+            password=self.valid_data['password1']
+        )
+
+        form = AutoFocusRegistrationForm(
+            data=self.valid_data.copy()
+        )
+        self.assertFalse(form.is_valid())
+        self.assertEqual(
+            form.errors['email'],
+            [text_type(validators.DUPLICATE_EMAIL)]
+        )
+
+        data = self.valid_data.copy()
+        data.update(email='bob example com')
+        form = AutoFocusRegistrationForm(
+            data=data
+        )
+        self.assertTrue(form.is_valid())


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]