api: Fix encoding of strings in user settings endpoints.

* Remove unnecessary json_validator for string parameters.
* Remove unnecessary JSON encoding in frontend calls.  Structurally,
  JavaScript does correct encoding without explicit JSON encoding.

Fixes part of #18035.
This commit is contained in:
Gaurav Pandey 2021-05-04 21:41:59 +05:30 committed by Tim Abbott
parent 232c7107eb
commit 36ad9b7d0e
4 changed files with 18 additions and 11 deletions

View File

@ -90,7 +90,7 @@ export function set_up() {
const $link = $(e.target).closest("a[data-code]"); const $link = $(e.target).closest("a[data-code]");
const setting_value = $link.attr("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"); const new_language = $link.attr("data-name");
$("#default_language_name").text(new_language); $("#default_language_name").text(new_language);

View File

@ -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 = {}; const data = {};
data[setting] = JSON.stringify(setting_data); data[setting] = value;
settings_ui.do_settings_change( settings_ui.do_settings_change(
channel.patch, channel.patch,
"/json/settings/notifications", "/json/settings/notifications",

View File

@ -173,17 +173,17 @@ class ChangeSettingsTest(ZulipTestCase):
user_profile = self.example_user("hamlet") user_profile = self.example_user("hamlet")
self.login_user(user_profile) 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'") 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) self.assert_json_success(json_result)
# refetch user_profile object to correctly handle caching # refetch user_profile object to correctly handle caching
user_profile = self.example_user("hamlet") user_profile = self.example_user("hamlet")
self.assertEqual(getattr(user_profile, param), "ding") 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) self.assert_json_success(json_result)
# refetch user_profile object to correctly handle caching # refetch user_profile object to correctly handle caching
@ -353,7 +353,11 @@ class ChangeSettingsTest(ZulipTestCase):
invalid_value: Any = 100 invalid_value: Any = 100
else: else:
invalid_value = "invalid_" + setting_name 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) result = self.client_patch("/json/settings/display", data)
self.assert_json_success(result) self.assert_json_success(result)
@ -362,7 +366,10 @@ class ChangeSettingsTest(ZulipTestCase):
# Test to make sure invalid settings are not accepted # Test to make sure invalid settings are not accepted
# and saved in the db. # 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) result = self.client_patch("/json/settings/display", data)
# the json error for multiple word setting names (ex: default_language) # the json error for multiple word setting names (ex: default_language)

View File

@ -42,7 +42,7 @@ from zerver.lib.request import JsonableError
from zerver.lib.response import json_error, json_success from zerver.lib.response import json_error, json_success
from zerver.lib.send_email import FromAddress, send_email from zerver.lib.send_email import FromAddress, send_email
from zerver.lib.upload import upload_avatar_image 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 zerver.models import UserProfile, avatar_changes_disabled, name_changes_disabled
from zproject.backends import check_password_strength, email_belongs_to_ldap 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 json_validator=check_int_in(UserProfile.COLOR_SCHEME_CHOICES), default=None
), ),
translate_emoticons: Optional[bool] = REQ(json_validator=check_bool, 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( default_view: Optional[str] = REQ(
json_validator=check_string_in(default_view_options), default=None 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 json_validator=check_bool, default=None
), ),
wildcard_mentions_notify: Optional[bool] = REQ(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_desktop_notifications: Optional[bool] = REQ(json_validator=check_bool, default=None),
enable_sounds: 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( enable_offline_email_notifications: Optional[bool] = REQ(