diff --git a/apps/registration/forms.py b/apps/registration/forms.py index 18d76eb..b533e73 100644 --- a/apps/registration/forms.py +++ b/apps/registration/forms.py @@ -22,6 +22,15 @@ class SignupForm(UserCreationForm): ], ) + def clean_email(self): + """ + Ensure that the email address is unique. + """ + email = self.data["email"] + if User.objects.filter(email=email).exists(): + self.add_error("email", _("This email address is already used.")) + return email + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields["first_name"].required = True diff --git a/apps/registration/tests.py b/apps/registration/tests.py index e43aabc..f732f16 100644 --- a/apps/registration/tests.py +++ b/apps/registration/tests.py @@ -119,6 +119,20 @@ class TestRegistration(TestCase): self.assertRedirects(response, reverse("registration:email_validation_sent"), 302, 200) self.assertTrue(User.objects.filter(email="toto@example.com").exists()) + # Email is already used + response = self.client.post(reverse("registration:signup"), data=dict( + last_name="Toto", + first_name="Toto", + email="toto@example.com", + password1="azertyuiopazertyuiop", + password2="azertyuiopazertyuiop", + role="participant", + student_class=12, + school="God", + give_contact_to_animath=False, + )) + self.assertEqual(response.status_code, 200) + response = self.client.get(reverse("registration:email_validation_sent")) self.assertEqual(response.status_code, 200)