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.

api: Fix encoding of strings in user settings endpoints.

Gaurav Pandey36ad9b7d0e89
diff --git a/static/js/settings_display.js b/static/js/settings_display.js
index 72f62ad005e..4eb39b855cd 100644
--- a/static/js/settings_display.js
+++ b/static/js/settings_display.js
@@ -90,7 +90,7 @@ export function set_up() {
const $link = $(e.target).closest("a[data-code]");
const setting_value = $link.attr("data-code");
- const data = {default_language: JSON.stringify(setting_value)};
+ const data = {default_language: setting_value};
const new_language = $link.attr("data-name");
$("#default_language_name").text(new_language);
diff --git a/static/js/settings_notifications.js b/static/js/settings_notifications.js
index 59894742626..57cbd55340d 100644
--- a/static/js/settings_notifications.js
+++ b/static/js/settings_notifications.js
@@ -42,9 +42,9 @@ function rerender_ui() {
}
}
-function change_notification_setting(setting, setting_data, status_element) {
+function change_notification_setting(setting, value, status_element) {
const data = {};
- data[setting] = JSON.stringify(setting_data);
+ data[setting] = value;
settings_ui.do_settings_change(
channel.patch,
"/json/settings/notifications",
diff --git a/zerver/tests/test_settings.py b/zerver/tests/test_settings.py
index 4f8b78af3da..9649c8a8bbd 100644
--- a/zerver/tests/test_settings.py
+++ b/zerver/tests/test_settings.py
@@ -173,17 +173,17 @@ class ChangeSettingsTest(ZulipTestCase):
user_profile = self.example_user("hamlet")
self.login_user(user_profile)
- json_result = self.client_patch(pattern, {param: orjson.dumps("invalid").decode()})
+ json_result = self.client_patch(pattern, {param: "invalid"})
self.assert_json_error(json_result, "Invalid notification sound 'invalid'")
- json_result = self.client_patch(pattern, {param: orjson.dumps("ding").decode()})
+ json_result = self.client_patch(pattern, {param: "ding"})
self.assert_json_success(json_result)
# refetch user_profile object to correctly handle caching
user_profile = self.example_user("hamlet")
self.assertEqual(getattr(user_profile, param), "ding")
- json_result = self.client_patch(pattern, {param: orjson.dumps("zulip").decode()})
+ json_result = self.client_patch(pattern, {param: "zulip"})
self.assert_json_success(json_result)
# refetch user_profile object to correctly handle caching
@@ -353,7 +353,11 @@ class ChangeSettingsTest(ZulipTestCase):
invalid_value: Any = 100
else:
invalid_value = "invalid_" + setting_name
- data = {setting_name: orjson.dumps(test_value).decode()}
+
+ if setting_name == "default_language":
+ data = {setting_name: test_value}
+ else:
+ data = {setting_name: orjson.dumps(test_value).decode()}
result = self.client_patch("/json/settings/display", data)
self.assert_json_success(result)
@@ -362,7 +366,10 @@ class ChangeSettingsTest(ZulipTestCase):
# Test to make sure invalid settings are not accepted
# and saved in the db.
- data = {setting_name: orjson.dumps(invalid_value).decode()}
+ if setting_name == "default_language":
+ data = {setting_name: invalid_value}
+ else:
+ data = {setting_name: orjson.dumps(invalid_value).decode()}
result = self.client_patch("/json/settings/display", data)
# the json error for multiple word setting names (ex: default_language)
diff --git a/zerver/views/user_settings.py b/zerver/views/user_settings.py
index 7ae93420b5c..f24753d6d56 100644
--- a/zerver/views/user_settings.py
+++ b/zerver/views/user_settings.py
@@ -42,7 +42,7 @@ from zerver.lib.request import JsonableError
from zerver.lib.response import json_error, json_success
from zerver.lib.send_email import FromAddress, send_email
from zerver.lib.upload import upload_avatar_image
-from zerver.lib.validator import check_bool, check_int, check_int_in, check_string, check_string_in
+from zerver.lib.validator import check_bool, check_int, check_int_in, check_string_in
from zerver.models import UserProfile, avatar_changes_disabled, name_changes_disabled
from zproject.backends import check_password_strength, email_belongs_to_ldap
@@ -197,7 +197,7 @@ def update_display_settings_backend(
json_validator=check_int_in(UserProfile.COLOR_SCHEME_CHOICES), default=None
),
translate_emoticons: Optional[bool] = REQ(json_validator=check_bool, default=None),
- default_language: Optional[str] = REQ(json_validator=check_string, default=None),
+ default_language: Optional[str] = REQ(default=None),
default_view: Optional[str] = REQ(
json_validator=check_string_in(default_view_options), default=None
),
@@ -243,7 +243,7 @@ def json_change_notify_settings(
json_validator=check_bool, default=None
),
wildcard_mentions_notify: Optional[bool] = REQ(json_validator=check_bool, default=None),
- notification_sound: Optional[str] = REQ(json_validator=check_string, default=None),
+ notification_sound: Optional[str] = REQ(default=None),
enable_desktop_notifications: Optional[bool] = REQ(json_validator=check_bool, default=None),
enable_sounds: Optional[bool] = REQ(json_validator=check_bool, default=None),
enable_offline_email_notifications: Optional[bool] = REQ(