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

Updates `json_change_settings` so that the default value for the `email`,
`full_name`, `new_password` and `old_password` parameters is `None` instead
of an empty string, which also makes the type annotation `Optional[str]`.

Also, updates tests for email and full name changes to include an empty
string as one of the tested invalid values.
This commit is contained in:
Lauryn Menard 2022-08-08 01:39:32 +02:00 committed by Tim Abbott
parent 7661df20a9
commit df3b8c590f
3 changed files with 42 additions and 36 deletions

View File

@ -244,7 +244,9 @@ class EmailChangeTestCase(ZulipTestCase):
) )
def test_post_invalid_email(self) -> None: def test_post_invalid_email(self) -> None:
data = {"email": "hamlet-new"} invalid_emails = ["", "hamlet-new"]
for email in invalid_emails:
data = {"email": email}
self.login("hamlet") self.login("hamlet")
url = "/json/settings" url = "/json/settings"
result = self.client_patch(url, data) result = self.client_patch(url, data)

View File

@ -121,8 +121,10 @@ class ChangeSettingsTest(ZulipTestCase):
json_result = self.client_patch("/json/settings", dict(full_name="x" * 1000)) json_result = self.client_patch("/json/settings", dict(full_name="x" * 1000))
self.assert_json_error(json_result, "Name too long!") self.assert_json_error(json_result, "Name too long!")
# Now try a too-short name # Now try too-short names
json_result = self.client_patch("/json/settings", dict(full_name="x")) 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!") self.assert_json_error(json_result, "Name too short!")
def test_illegal_characters_in_name_changes(self) -> None: def test_illegal_characters_in_name_changes(self) -> None:

View File

@ -147,10 +147,10 @@ def check_settings_values(
def json_change_settings( def json_change_settings(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
full_name: str = REQ(default=""), full_name: Optional[str] = REQ(default=None),
email: str = REQ(default=""), email: Optional[str] = REQ(default=None),
old_password: str = REQ(default=""), old_password: Optional[str] = REQ(default=None),
new_password: str = REQ(default=""), new_password: Optional[str] = REQ(default=None),
twenty_four_hour_time: Optional[bool] = REQ(json_validator=check_bool, 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), dense_mode: Optional[bool] = REQ(json_validator=check_bool, default=None),
starred_message_counts: 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 notification_sound, email_notifications_batching_period_seconds, default_language
) )
if new_password != "": if new_password is not None:
return_data: Dict[str, Any] = {} return_data: Dict[str, Any] = {}
if email_belongs_to_ldap(user_profile.realm, user_profile.delivery_email): if email_belongs_to_ldap(user_profile.realm, user_profile.delivery_email):
raise JsonableError(_("Your Zulip password is managed in LDAP")) raise JsonableError(_("Your Zulip password is managed in LDAP"))
@ -270,8 +270,10 @@ def json_change_settings(
request.session.save() request.session.save()
result: Dict[str, Any] = {} result: Dict[str, Any] = {}
if email is not None:
new_email = email.strip() new_email = email.strip()
if user_profile.delivery_email != new_email and new_email != "": if user_profile.delivery_email != new_email:
if user_profile.realm.email_changes_disabled and not user_profile.is_realm_admin: if user_profile.realm.email_changes_disabled and not user_profile.is_realm_admin:
raise JsonableError(_("Email address changes are disabled in this organization.")) raise JsonableError(_("Email address changes are disabled in this organization."))
@ -299,7 +301,7 @@ def json_change_settings(
do_start_email_change_process(user_profile, new_email) do_start_email_change_process(user_profile, new_email)
if user_profile.full_name != full_name and full_name.strip() != "": 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: 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 # Failingly silently is fine -- they can't do it through the UI, so
# they'd have to be trying to break the rules. # they'd have to be trying to break the rules.