From afc5066e36b51bd1f59319f7f968a9101d14e721 Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Wed, 26 Apr 2023 15:57:30 +0530 Subject: [PATCH] registration: Fix "Resend" link not working for realm creation. The "Resend" link for realm creation was not working correctly because it is implemented by basically submiting the registration form again which results in resending the email but all the required parameters were not passed to the form after recent changes in the realm creation flow. This commit fixes it by passing all the required parameters - email, realm name, realm type and realm subdomain, when submitting form again by clicking on the "resend" link. Fixes #25249. --- templates/zerver/accounts_send_confirm.html | 3 ++ zerver/tests/test_management_commands.py | 3 +- zerver/tests/test_signup.py | 24 +++++++------- zerver/views/registration.py | 36 ++++++++++++++++++--- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/templates/zerver/accounts_send_confirm.html b/templates/zerver/accounts_send_confirm.html index 780f0f2254..5ae4350907 100644 --- a/templates/zerver/accounts_send_confirm.html +++ b/templates/zerver/accounts_send_confirm.html @@ -28,6 +28,9 @@ page can be easily identified in it's respective JavaScript file -->
{{ csrf_input }}   +   +   +  
{% else %}
diff --git a/zerver/tests/test_management_commands.py b/zerver/tests/test_management_commands.py index abfa251e84..772580a53b 100644 --- a/zerver/tests/test_management_commands.py +++ b/zerver/tests/test_management_commands.py @@ -338,7 +338,8 @@ class TestGenerateRealmCreationLink(ZulipTestCase): ) self.assertEqual(result.status_code, 302) self.assertEqual( - f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}", result["Location"] + f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}&realm_name={urllib.parse.quote_plus(realm_name)}&realm_type=10&realm_subdomain={string_id}", + result["Location"], ) result = self.client_get(result["Location"]) self.assert_in_response("Check your email", result) diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index 2b5abb28dd..0122d98df5 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -758,7 +758,9 @@ class PasswordResetTest(ZulipTestCase): result = self.client_get("/accounts/send_confirm/?email=alice@example.com") self.assert_in_success_response(["/accounts/home/"], result) - result = self.client_get("/accounts/new/send_confirm/?email=alice@example.com") + result = self.client_get( + "/accounts/new/send_confirm/?email=alice@example.com&realm_name=Zulip+test&realm_type=10&realm_subdomain=zuliptest" + ) self.assert_in_success_response(["/new/"], result) def test_password_reset_for_soft_deactivated_user(self) -> None: @@ -1266,7 +1268,7 @@ class RealmCreationTest(ZulipTestCase): self.assertEqual(result.status_code, 302) self.assertTrue( result["Location"].endswith( - f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}" + f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}&realm_name={urllib.parse.quote_plus(org_name)}&realm_type=10&realm_subdomain={string_id}" ) ) result = self.client_get(result["Location"]) @@ -1401,7 +1403,7 @@ class RealmCreationTest(ZulipTestCase): self.assertEqual(result.status_code, 302) self.assertTrue( result["Location"].endswith( - f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}" + f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}&realm_name={urllib.parse.quote_plus(realm_name)}&realm_type=10&realm_subdomain={string_id}" ) ) result = self.client_get(result["Location"]) @@ -1447,7 +1449,7 @@ class RealmCreationTest(ZulipTestCase): self.assertEqual(result.status_code, 302) self.assertTrue( result["Location"].endswith( - f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}" + f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}&realm_name={urllib.parse.quote_plus(realm_name)}&realm_type=10&realm_subdomain={string_id}" ) ) result = self.client_get(result["Location"]) @@ -1496,7 +1498,7 @@ class RealmCreationTest(ZulipTestCase): self.assertEqual(result.status_code, 302) self.assertTrue( result["Location"].endswith( - f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}" + f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}&realm_name={urllib.parse.quote_plus(realm_name)}&realm_type=10&realm_subdomain={string_id}" ) ) result = self.client_get(result["Location"]) @@ -1556,7 +1558,7 @@ class RealmCreationTest(ZulipTestCase): self.assertEqual(result.status_code, 302) self.assertTrue( result["Location"].endswith( - f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}" + f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}&realm_name={urllib.parse.quote_plus(realm_name)}&realm_type=10&realm_subdomain={string_id}" ) ) result = self.client_get(result["Location"]) @@ -1601,7 +1603,7 @@ class RealmCreationTest(ZulipTestCase): self.assertEqual(result.status_code, 302) self.assertTrue( result["Location"].endswith( - f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}" + f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}&realm_name={urllib.parse.quote_plus(realm_name)}&realm_type=10&realm_subdomain={string_id}" ) ) result = self.client_get(result["Location"]) @@ -1642,7 +1644,7 @@ class RealmCreationTest(ZulipTestCase): self.assertEqual(result.status_code, 302) self.assertTrue( result["Location"].endswith( - f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}" + f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}&realm_name={urllib.parse.quote_plus(realm_name)}&realm_type=10&realm_subdomain={string_id}" ) ) result = self.client_get(result["Location"]) @@ -1687,7 +1689,7 @@ class RealmCreationTest(ZulipTestCase): self.assertEqual(result.status_code, 302) self.assertTrue( result["Location"].endswith( - f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}" + f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}&realm_name={urllib.parse.quote_plus(realm_name)}&realm_type=10&realm_subdomain={string_id}" ) ) result = self.client_get(result["Location"]) @@ -1741,7 +1743,7 @@ class RealmCreationTest(ZulipTestCase): self.assertEqual(result.status_code, 302) self.assertTrue( result["Location"].endswith( - f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}" + f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}&realm_name={urllib.parse.quote_plus(first_realm_name)}&realm_type=10&realm_subdomain={first_string_id}" ) ) result = self.client_get(result["Location"]) @@ -1755,7 +1757,7 @@ class RealmCreationTest(ZulipTestCase): self.assertEqual(result.status_code, 302) self.assertTrue( result["Location"].endswith( - f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}" + f"/accounts/new/send_confirm/?email={urllib.parse.quote(email)}&realm_name={urllib.parse.quote_plus(second_realm_name)}&realm_type=10&realm_subdomain={second_string_id}" ) ) result = self.client_get(result["Location"]) diff --git a/zerver/views/registration.py b/zerver/views/registration.py index a51ca7a358..44bc75674f 100644 --- a/zerver/views/registration.py +++ b/zerver/views/registration.py @@ -58,7 +58,13 @@ from zerver.lib.sessions import get_expirable_session_var from zerver.lib.subdomains import get_subdomain from zerver.lib.url_encoding import append_url_query_string from zerver.lib.users import get_accounts_for_email -from zerver.lib.validator import to_converted_or_fallback, to_non_negative_int, to_timezone_or_empty +from zerver.lib.validator import ( + check_capped_string, + check_int_in, + to_converted_or_fallback, + to_non_negative_int, + to_timezone_or_empty, +) from zerver.lib.zephyr import compute_mit_user_fullname from zerver.models import ( DisposableEmailError, @@ -756,7 +762,14 @@ def create_realm(request: HttpRequest, creation_key: Optional[str] = None) -> Ht if key_record is not None: key_record.delete() new_realm_send_confirm_url = reverse("new_realm_send_confirm") - query = urlencode({"email": email}) + query = urlencode( + { + "email": email, + "realm_name": realm_name, + "realm_type": realm_type, + "realm_subdomain": realm_subdomain, + } + ) url = append_url_query_string(new_realm_send_confirm_url, query) return HttpResponseRedirect(url) else: @@ -787,11 +800,26 @@ def signup_send_confirm(request: HttpRequest, email: str = REQ("email")) -> Http @add_google_analytics @has_request_variables -def new_realm_send_confirm(request: HttpRequest, email: str = REQ("email")) -> HttpResponse: +def new_realm_send_confirm( + request: HttpRequest, + email: str = REQ("email"), + realm_name: str = REQ(str_validator=check_capped_string(Realm.MAX_REALM_NAME_LENGTH)), + realm_type: int = REQ(json_validator=check_int_in(Realm.ORG_TYPE_IDS)), + realm_subdomain: str = REQ(str_validator=check_capped_string(Realm.MAX_REALM_SUBDOMAIN_LENGTH)), +) -> HttpResponse: return TemplateResponse( request, "zerver/accounts_send_confirm.html", - context={"email": email, "realm_creation": True}, + context={ + "email": email, + # Using "new_realm_name" key here since "realm_name" key is already present in + # the context provided by zulip_default_context and it is "None" during realm + # creation. + "new_realm_name": realm_name, + "realm_type": realm_type, + "realm_subdomain": realm_subdomain, + "realm_creation": True, + }, )