launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #06767
[Merge] lp:~rvb/maas/maas-fix-950804 into lp:maas
Raphaël Badin has proposed merging lp:~rvb/maas/maas-fix-950804 into lp:maas.
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
Related bugs:
Bug #950804 in MAAS: "Same email address for multiple users"
https://bugs.launchpad.net/maas/+bug/950804
For more details, see:
https://code.launchpad.net/~rvb/maas/maas-fix-950804/+merge/98232
Make sure each email is unique to a maas instance.
--
https://code.launchpad.net/~rvb/maas/maas-fix-950804/+merge/98232
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/maas-fix-950804 into lp:maas.
=== modified file 'src/maasserver/forms.py'
--- src/maasserver/forms.py 2012-03-15 13:58:32 +0000
+++ src/maasserver/forms.py 2012-03-19 16:43:20 +0000
@@ -127,7 +127,19 @@
return node
-class ProfileForm(ModelForm):
+class UniqueEmailMixin:
+
+ def clean_email(self):
+ """Validate that the supplied email address is unique for the
+ site.
+ """
+ if User.objects.filter(email__iexact=self.cleaned_data['email']):
+ raise forms.ValidationError(
+ "This email address is already in use.")
+ return self.cleaned_data['email']
+
+
+class ProfileForm(UniqueEmailMixin, ModelForm):
# We use the field 'last_name' to store the user's full name (and
# don't display Django's 'first_name' field).
last_name = forms.CharField(
@@ -138,7 +150,7 @@
fields = ('last_name', 'email')
-class NewUserCreationForm(UserCreationForm):
+class NewUserCreationForm(UniqueEmailMixin, UserCreationForm):
is_superuser = forms.BooleanField(
label="MAAS administrator", required=False)
@@ -170,7 +182,7 @@
return user
-class EditUserForm(UserChangeForm):
+class EditUserForm(UniqueEmailMixin, UserChangeForm):
# Override the default label.
is_superuser = forms.BooleanField(
label="MAAS administrator", required=False)
=== modified file 'src/maasserver/tests/test_forms.py'
--- src/maasserver/tests/test_forms.py 2012-03-13 05:34:38 +0000
+++ src/maasserver/tests/test_forms.py 2012-03-19 16:43:20 +0000
@@ -16,9 +16,11 @@
from django.http import QueryDict
from maasserver.forms import (
ConfigForm,
+ EditUserForm,
HostnameFormField,
NewUserCreationForm,
NodeWithMACAddressesForm,
+ ProfileForm,
validate_hostname,
)
from maasserver.models import (
@@ -188,8 +190,51 @@
form.errors['hostname'])
+class TestProfileForm(TestCase):
+
+ def test_email_must_be_unique(self):
+ email = '%s@xxxxxxxxxxx' % factory.getRandomString()
+ user = factory.make_user(email=email)
+ form = ProfileForm(instance=user, data={'email': email})
+ self.assertFalse(form.is_valid())
+ self.assertIn('email', form._errors)
+ self.assertEqual(
+ ["This email address is already in use."], form._errors['email'])
+
+
+class TestEditUserForm(TestCase):
+
+ def test_email_must_be_unique(self):
+ email = '%s@xxxxxxxxxxx' % factory.getRandomString()
+ user = factory.make_user(email=email)
+ form = EditUserForm(instance=user, data={'email': email})
+ self.assertFalse(form.is_valid())
+ self.assertIn('email', form._errors)
+ self.assertEqual(
+ ["This email address is already in use."], form._errors['email'])
+
+ def test_email_must_be_unique_ignores_case(self):
+ email = '%s@xxxxxxxxxxx' % factory.getRandomString()
+ user = factory.make_user(email=email)
+ form = EditUserForm(instance=user, data={'email': email.upper()})
+ self.assertFalse(form.is_valid())
+ self.assertIn('email', form._errors)
+
+
class TestNewUserCreationForm(TestCase):
+ def test_email_must_be_unique(self):
+ email = '%s@xxxxxxxxxxx' % factory.getRandomString()
+ factory.make_user(email=email)
+ form = NewUserCreationForm(
+ {
+ 'email': email,
+ })
+ self.assertFalse(form.is_valid())
+ self.assertIn('email', form._errors)
+ self.assertEqual(
+ ["This email address is already in use."], form._errors['email'])
+
def test_fields_order(self):
form = NewUserCreationForm()