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.

user-settings: Make default `None` for name, email and password changes.

Lauryn Menarddf3b8c590f2b
diff --git a/zerver/tests/test_email_change.py b/zerver/tests/test_email_change.py
index f69cea4d7c2..8ddd62c15db 100644
--- a/zerver/tests/test_email_change.py
+++ b/zerver/tests/test_email_change.py
@@ -244,11 +244,13 @@ class EmailChangeTestCase(ZulipTestCase):
)
def test_post_invalid_email(self) -> None:
- data = {"email": "hamlet-new"}
- self.login("hamlet")
- url = "/json/settings"
- result = self.client_patch(url, data)
- self.assert_in_response("Invalid address", result)
+ invalid_emails = ["", "hamlet-new"]
+ for email in invalid_emails:
+ data = {"email": email}
+ self.login("hamlet")
+ url = "/json/settings"
+ result = self.client_patch(url, data)
+ self.assert_in_response("Invalid address", result)
def test_post_same_email(self) -> None:
data = {"email": self.example_email("hamlet")}
diff --git a/zerver/tests/test_settings.py b/zerver/tests/test_settings.py
index 4701273ac33..cca3aeb802d 100644
--- a/zerver/tests/test_settings.py
+++ b/zerver/tests/test_settings.py
@@ -121,9 +121,11 @@ class ChangeSettingsTest(ZulipTestCase):
json_result = self.client_patch("/json/settings", dict(full_name="x" * 1000))
self.assert_json_error(json_result, "Name too long!")
- # Now try a too-short name
- json_result = self.client_patch("/json/settings", dict(full_name="x"))
- self.assert_json_error(json_result, "Name too short!")
+ # Now try too-short names
+ short_names = ["", "x"]
+ for name in short_names:
+ json_result = self.client_patch("/json/settings", dict(full_name=name))
+ self.assert_json_error(json_result, "Name too short!")
def test_illegal_characters_in_name_changes(self) -> None:
self.login("hamlet")
diff --git a/zerver/views/user_settings.py b/zerver/views/user_settings.py
index e12e9af1a2d..8865f2d7f93 100644
--- a/zerver/views/user_settings.py
+++ b/zerver/views/user_settings.py
@@ -147,10 +147,10 @@ def check_settings_values(
def json_change_settings(
request: HttpRequest,
user_profile: UserProfile,
- full_name: str = REQ(default=""),
- email: str = REQ(default=""),
- old_password: str = REQ(default=""),
- new_password: str = REQ(default=""),
+ full_name: Optional[str] = REQ(default=None),
+ email: Optional[str] = REQ(default=None),
+ old_password: Optional[str] = REQ(default=None),
+ new_password: Optional[str] = REQ(default=None),
twenty_four_hour_time: Optional[bool] = REQ(json_validator=check_bool, default=None),
dense_mode: Optional[bool] = REQ(json_validator=check_bool, default=None),
starred_message_counts: Optional[bool] = REQ(json_validator=check_bool, default=None),
@@ -227,7 +227,7 @@ def json_change_settings(
notification_sound, email_notifications_batching_period_seconds, default_language
)
- if new_password != "":
+ if new_password is not None:
return_data: Dict[str, Any] = {}
if email_belongs_to_ldap(user_profile.realm, user_profile.delivery_email):
raise JsonableError(_("Your Zulip password is managed in LDAP"))
@@ -270,36 +270,38 @@ def json_change_settings(
request.session.save()
result: Dict[str, Any] = {}
- new_email = email.strip()
- if user_profile.delivery_email != new_email and new_email != "":
- if user_profile.realm.email_changes_disabled and not user_profile.is_realm_admin:
- raise JsonableError(_("Email address changes are disabled in this organization."))
-
- error = validate_email_is_valid(
- new_email,
- get_realm_email_validator(user_profile.realm),
- )
- if error:
- raise JsonableError(error)
- try:
- validate_email_not_already_in_realm(
- user_profile.realm,
+ if email is not None:
+ new_email = email.strip()
+ if user_profile.delivery_email != new_email:
+ if user_profile.realm.email_changes_disabled and not user_profile.is_realm_admin:
+ raise JsonableError(_("Email address changes are disabled in this organization."))
+
+ error = validate_email_is_valid(
new_email,
- verbose=False,
+ get_realm_email_validator(user_profile.realm),
)
- except ValidationError as e:
- raise JsonableError(e.message)
-
- ratelimited, time_until_free = RateLimitedUser(
- user_profile, domain="email_change_by_user"
- ).rate_limit()
- if ratelimited:
- raise RateLimited(time_until_free)
-
- do_start_email_change_process(user_profile, new_email)
-
- if user_profile.full_name != full_name and full_name.strip() != "":
+ if error:
+ raise JsonableError(error)
+
+ try:
+ validate_email_not_already_in_realm(
+ user_profile.realm,
+ new_email,
+ verbose=False,
+ )
+ except ValidationError as e:
+ raise JsonableError(e.message)
+
+ ratelimited, time_until_free = RateLimitedUser(
+ user_profile, domain="email_change_by_user"
+ ).rate_limit()
+ if ratelimited:
+ raise RateLimited(time_until_free)
+
+ do_start_email_change_process(user_profile, new_email)
+
+ if full_name is not None and user_profile.full_name != full_name:
if name_changes_disabled(user_profile.realm) and not user_profile.is_realm_admin:
# Failingly silently is fine -- they can't do it through the UI, so
# they'd have to be trying to break the rules.