Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

registration: Encode source realm as an integer.

Siddharth Asthana4262c04db108
diff --git a/static/js/portico/signup.js b/static/js/portico/signup.js
index 15ccfd0a7d7..9a85d26892a 100644
--- a/static/js/portico/signup.js
+++ b/static/js/portico/signup.js
@@ -198,7 +198,7 @@ $(() => {
function update_full_name_section() {
if (
$("#source_realm_select").length &&
- $("#source_realm_select").find(":selected").val() !== "on"
+ $("#source_realm_select").find(":selected").val() !== ""
) {
$("#full_name_input_section").hide();
$("#profile_info_section").show();
diff --git a/templates/zerver/register.html b/templates/zerver/register.html
index ecbd0bcfef7..d4a67bad759 100644
--- a/templates/zerver/register.html
+++ b/templates/zerver/register.html
@@ -111,13 +111,13 @@ Form is validated both client-side using jquery-validate (see signup.js) and ser
</div>
<div id="source_realm_select_section" class="input-group m-10 inline-block">
<select class="select" name="source_realm" id="source_realm_select">
- <option value="on"
- {% if "source_realm" in form.data and form.data["source_realm"] == "on" %}selected {% endif %}>
+ <option value=""
+ {% if "source_realm" in form.data and form.data["source_realm"] == "" %}selected {% endif %}>
{{ _('Don&rsquo;t import settings') }}
</option>
{% for account in accounts %}
- <option value="{{ account.string_id }}" data-full-name="{{account.full_name}}" data-avatar="{{account.avatar}}"
- {% if ("source_realm" in form.data and account.string_id == form.data["source_realm"])
+ <option value="{{ account.realm_id }}" data-full-name="{{account.full_name}}" data-avatar="{{account.avatar}}"
+ {% if ("source_realm" in form.data and account.realm_id == form.data["source_realm"]|int)
or ("source_realm" not in form.data and loop.index0 == 0) %} selected {% endif %}>
{{ account.realm_name }}
</option>
diff --git a/zerver/lib/users.py b/zerver/lib/users.py
index a07cddedfe1..a0bb0117ed7 100644
--- a/zerver/lib/users.py
+++ b/zerver/lib/users.py
@@ -8,6 +8,7 @@ from django.core.exceptions import ValidationError
from django.db.models.query import QuerySet
from django.forms.models import model_to_dict
from django.utils.translation import gettext as _
+from typing_extensions import TypedDict
from zulip_bots.custom_exceptions import ConfigValidationError
from zerver.lib.avatar import avatar_url, get_avatar_field
@@ -269,7 +270,14 @@ def access_user_by_id(
return target
-def get_accounts_for_email(email: str) -> List[Dict[str, Optional[str]]]:
+class Accounts(TypedDict):
+ realm_name: str
+ realm_id: int
+ full_name: str
+ avatar: Optional[str]
+
+
+def get_accounts_for_email(email: str) -> List[Accounts]:
profiles = (
UserProfile.objects.select_related("realm")
.filter(
@@ -280,15 +288,17 @@ def get_accounts_for_email(email: str) -> List[Dict[str, Optional[str]]]:
)
.order_by("date_joined")
)
- return [
- {
- "realm_name": profile.realm.name,
- "string_id": profile.realm.string_id,
- "full_name": profile.full_name,
- "avatar": avatar_url(profile),
- }
- for profile in profiles
- ]
+ accounts: List[Accounts] = []
+ for profile in profiles:
+ accounts.append(
+ dict(
+ realm_name=profile.realm.name,
+ realm_id=profile.realm.id,
+ full_name=profile.full_name,
+ avatar=avatar_url(profile),
+ )
+ )
+ return accounts
def get_api_key(user_profile: UserProfile) -> str:
diff --git a/zerver/models.py b/zerver/models.py
index bc67b5f9d2d..b8b9e717fbe 100644
--- a/zerver/models.py
+++ b/zerver/models.py
@@ -760,6 +760,10 @@ def get_realm(string_id: str) -> Realm:
return Realm.objects.get(string_id=string_id)
+def get_realm_by_id(realm_id: int) -> Realm:
+ return Realm.objects.get(id=realm_id)
+
+
def name_changes_disabled(realm: Optional[Realm]) -> bool:
if realm is None:
return settings.NAME_CHANGES_DISABLED
@@ -2932,9 +2936,9 @@ def active_non_guest_user_ids(realm_id: int) -> List[int]:
return list(query)
-def get_source_profile(email: str, string_id: str) -> Optional[UserProfile]:
+def get_source_profile(email: str, realm_id: int) -> Optional[UserProfile]:
try:
- return get_user_by_delivery_email(email, get_realm(string_id))
+ return get_user_by_delivery_email(email, get_realm_by_id(realm_id))
except (Realm.DoesNotExist, UserProfile.DoesNotExist):
return None
diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py
index 115b4b46c15..f44e29f292e 100644
--- a/zerver/tests/test_signup.py
+++ b/zerver/tests/test_signup.py
@@ -747,7 +747,7 @@ class LoginTest(ZulipTestCase):
with queries_captured() as queries, cache_tries_captured() as cache_tries:
self.register(self.nonreg_email("test"), "test")
# Ensure the number of queries we make is not O(streams)
- self.assertEqual(len(queries), 71)
+ self.assertEqual(len(queries), 70)
# We can probably avoid a couple cache hits here, but there doesn't
# seem to be any O(N) behavior. Some of the cache hits are related
@@ -3611,7 +3611,7 @@ class UserSignUpTest(InviteUserBase):
result = self.client_get(confirmation_url, subdomain=subdomain)
self.assertEqual(result.status_code, 200)
result = self.submit_reg_form_for_user(
- email, password, source_realm="on", HTTP_HOST=subdomain + ".testserver"
+ email, password, source_realm="", HTTP_HOST=subdomain + ".testserver"
)
hamlet = get_user(self.example_email("hamlet"), realm)
@@ -3665,7 +3665,10 @@ class UserSignUpTest(InviteUserBase):
)
result = self.submit_reg_form_for_user(
- email, password, source_realm="zulip", HTTP_HOST=subdomain + ".testserver"
+ email,
+ password,
+ source_realm=str(hamlet_in_zulip.realm.id),
+ HTTP_HOST=subdomain + ".testserver",
)
hamlet_in_lear = get_user(email, lear_realm)
@@ -3727,6 +3730,38 @@ class UserSignUpTest(InviteUserBase):
)
self.assertEqual(result.status_code, 302)
+ def test_signup_using_invalid_subdomain_preserves_state_of_form(self) -> None:
+ """
+ Check that when we give invalid subdomain and submit the registration form
+ all the values in the form are preserved.
+ """
+ realm = get_realm("zulip")
+
+ password = "test"
+ email = self.example_email("iago")
+ realm_name = "Test"
+
+ result = self.client_post("/new/", {"email": email})
+ self.client_get(result["Location"])
+ confirmation_url = self.get_confirmation_url_from_outbox(email)
+ self.client_get(confirmation_url)
+ result = self.submit_reg_form_for_user(
+ email,
+ password,
+ realm_subdomain=realm.string_id,
+ realm_name=realm_name,
+ source_realm=str(realm.id),
+ )
+ self.assert_in_success_response(
+ [
+ "Subdomain unavailable. Please choose a different one.",
+ "selected >\n Zulip Dev\n",
+ 'name="password"\n value="test"',
+ 'value="Test"\n name="realm_name"',
+ ],
+ result,
+ )
+
def test_replace_subdomain_in_confirmation_link(self) -> None:
"""
Check that manually changing the subdomain in a registration
diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py
index d9f21f46afc..210f08e0f20 100644
--- a/zerver/tests/test_users.py
+++ b/zerver/tests/test_users.py
@@ -42,7 +42,7 @@ from zerver.lib.test_helpers import (
)
from zerver.lib.topic_mutes import add_topic_mute
from zerver.lib.upload import upload_avatar_image
-from zerver.lib.users import access_user_by_id, get_accounts_for_email, user_ids_to_users
+from zerver.lib.users import Accounts, access_user_by_id, get_accounts_for_email, user_ids_to_users
from zerver.models import (
CustomProfileField,
InvalidFakeEmailDomain,
@@ -1072,16 +1072,14 @@ class UserProfileTest(ZulipTestCase):
def test_get_accounts_for_email(self) -> None:
reset_emails_in_zulip_realm()
- def check_account_present_in_accounts(
- user: UserProfile, accounts: List[Dict[str, Optional[str]]]
- ) -> None:
+ def check_account_present_in_accounts(user: UserProfile, accounts: List[Accounts]) -> None:
for account in accounts:
realm = user.realm
if (
account["avatar"] == avatar_url(user)
and account["full_name"] == user.full_name
and account["realm_name"] == realm.name
- and account["string_id"] == realm.string_id
+ and account["realm_id"] == realm.id
):
return
raise AssertionError("Account not found")
@@ -1117,22 +1115,24 @@ class UserProfileTest(ZulipTestCase):
def test_get_source_profile(self) -> None:
reset_emails_in_zulip_realm()
- iago = get_source_profile("[email protected]", "zulip")
+ zulip_realm_id = get_realm("zulip").id
+ iago = get_source_profile("[email protected]", zulip_realm_id)
assert iago is not None
self.assertEqual(iago.email, "[email protected]")
self.assertEqual(iago.realm, get_realm("zulip"))
- iago = get_source_profile("[email protected]", "zulip")
+ iago = get_source_profile("[email protected]", zulip_realm_id)
assert iago is not None
self.assertEqual(iago.email, "[email protected]")
- cordelia = get_source_profile("[email protected]", "lear")
+ lear_realm_id = get_realm("lear").id
+ cordelia = get_source_profile("[email protected]", lear_realm_id)
assert cordelia is not None
self.assertEqual(cordelia.email, "[email protected]")
- self.assertIsNone(get_source_profile("[email protected]", "zulip"))
- self.assertIsNone(get_source_profile("[email protected]", "ZULIP"))
- self.assertIsNone(get_source_profile("[email protected]", "lear"))
+ self.assertIsNone(get_source_profile("[email protected]", zulip_realm_id))
+ self.assertIsNone(get_source_profile("[email protected]", 0))
+ self.assertIsNone(get_source_profile("[email protected]", lear_realm_id))
def test_copy_user_settings(self) -> None:
iago = self.example_user("iago")
diff --git a/zerver/views/registration.py b/zerver/views/registration.py
index dc6bf5ccd93..2e447a3457a 100644
--- a/zerver/views/registration.py
+++ b/zerver/views/registration.py
@@ -311,8 +311,10 @@ def accounts_register(request: HttpRequest) -> HttpResponse:
if "timezone" in request.POST and request.POST["timezone"] in pytz.all_timezones_set:
timezone = request.POST["timezone"]
- if "source_realm" in request.POST and request.POST["source_realm"] != "on":
- source_profile = get_source_profile(email, request.POST["source_realm"])
+ if "source_realm" in request.POST and request.POST["source_realm"] != "":
+ source_profile: Optional[UserProfile] = get_source_profile(
+ email, int(request.POST["source_realm_id"])
+ )
else:
source_profile = None