Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
registration: Handle race conditions with duplicate emails.
Alex Vandiver89b7f9cbd0e3
diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.pyindex 02bb1b64808..2858699cd36 100644--- a/zerver/tests/test_signup.py+++ b/zerver/tests/test_signup.py@@ -11,6 +11,7 @@ from django.conf import settingsfrom django.contrib.auth.views import PasswordResetConfirmViewfrom django.contrib.contenttypes.models import ContentTypefrom django.core.exceptions import ValidationError+from django.db.utils import IntegrityErrorfrom django.http import HttpResponse, HttpResponseBasefrom django.template.response import TemplateResponsefrom django.test import Client, override_settings@@ -2379,6 +2380,38 @@ class UserSignUpTest(ZulipTestCase):["Enter your account details to complete registration."], result)+ def test_signup_with_email_address_race(self) -> None:+ """+ The check for if an email is in use can race with other user+ creation; it is caught by database uniqueness rules. Verify+ that that is transformed into a redirect to log into the+ account.+ """+ email = "[email protected]"+ password = "newpassword"++ self.client_post("/accounts/home/", {"email": email})+ confirmation_url = self.get_confirmation_url_from_outbox(email)+ self.client_get(confirmation_url)+ self.client_post(+ "/accounts/register/",+ {+ "password": password,+ "key": find_key_by_email(email),+ "terms": True,+ "full_name": "New Guy",+ "from_confirmation": "1",+ },+ )+ with patch("zerver.actions.create_user.create_user", side_effect=IntegrityError):+ result = self.submit_reg_form_for_user(email, "easy", full_name="New Guy")+ self.assertEqual(result.status_code, 302)+ self.assertTrue(+ result["Location"].endswith(+ f"/accounts/login/?email={urllib.parse.quote(email)}&already_registered=1"+ )+ )+def test_signup_with_weak_password(self) -> None:"""Check if signing up without a full name redirects to a registrationdiff --git a/zerver/views/registration.py b/zerver/views/registration.pyindex b63895e541f..7dc154e6a4f 100644--- a/zerver/views/registration.py+++ b/zerver/views/registration.py@@ -11,6 +11,7 @@ from django.contrib.sessions.backends.base import SessionBasefrom django.core import validatorsfrom django.core.exceptions import ValidationErrorfrom django.db.models import Q+from django.db.utils import IntegrityErrorfrom django.http import HttpRequest, HttpResponse, HttpResponseRedirectfrom django.shortcuts import redirect, renderfrom django.template.response import TemplateResponse@@ -573,24 +574,30 @@ def registration_helper(# make it respect invited_as_admin / is_realm_admin.if user_profile is None:- user_profile = do_create_user(- email,- password,- realm,- full_name,- prereg_user=prereg_user,- prereg_realm=prereg_realm,- role=role,- tos_version=settings.TERMS_OF_SERVICE_VERSION,- timezone=timezone,- default_language=get_default_language_for_new_user(realm, request=request),- default_stream_groups=default_stream_groups,- source_profile=source_profile,- realm_creation=realm_creation,- acting_user=None,- enable_marketing_emails=enable_marketing_emails,- email_address_visibility=email_address_visibility,- )+ try:+ user_profile = do_create_user(+ email,+ password,+ realm,+ full_name,+ prereg_user=prereg_user,+ prereg_realm=prereg_realm,+ role=role,+ tos_version=settings.TERMS_OF_SERVICE_VERSION,+ timezone=timezone,+ default_language=get_default_language_for_new_user(realm, request=request),+ default_stream_groups=default_stream_groups,+ source_profile=source_profile,+ realm_creation=realm_creation,+ acting_user=None,+ enable_marketing_emails=enable_marketing_emails,+ email_address_visibility=email_address_visibility,+ )+ except IntegrityError:+ # Race condition making the user, leading to a+ # duplicate email address. Redirect them to the login+ # form.+ return redirect_to_email_login_url(email)if realm_creation:# Because for realm creation, registration happens on the