From 1c1a1e2cadcfcd2d3ccf27da955c6176c24c49e7 Mon Sep 17 00:00:00 2001 From: Gaurav Pandey Date: Sat, 16 Oct 2021 23:22:57 +0530 Subject: [PATCH] api: Remove encoding of string in update_user endpoint. * Remove unnecessary json_validator for full_name parameter. * Update frontend to pass the right parameter. * Update documentation and note the change. Fixes #18409. --- static/js/settings_users.js | 2 +- templates/zerver/api/changelog.md | 5 +++++ version.py | 2 +- zerver/openapi/zulip.yaml | 11 ++++++----- zerver/tests/test_audit_log.py | 2 +- zerver/tests/test_users.py | 16 ++++++++-------- zerver/views/users.py | 2 +- 7 files changed, 23 insertions(+), 17 deletions(-) diff --git a/static/js/settings_users.js b/static/js/settings_users.js index 97c0b6989c..6449f4f608 100644 --- a/static/js/settings_users.js +++ b/static/js/settings_users.js @@ -574,7 +574,7 @@ function handle_human_form(tbody, status_field) { const url = "/json/users/" + encodeURIComponent(user_id); const data = { - full_name: JSON.stringify(full_name.val()), + full_name: full_name.val(), role: JSON.stringify(role), profile_data: JSON.stringify(profile_data), }; diff --git a/templates/zerver/api/changelog.md b/templates/zerver/api/changelog.md index 876e1f5b9a..a858719979 100644 --- a/templates/zerver/api/changelog.md +++ b/templates/zerver/api/changelog.md @@ -11,6 +11,11 @@ below features are supported. ## Changes in Zulip 5.0 +**Feature level 106** + +* [`PATCH /user/{user_id}`](/api/update-user): Removed unnecessary JSON-encoding of string + parameter `full_name`. + **Feature level 105** * [`POST /register`](/api/register-queue), [`PATCH diff --git a/version.py b/version.py index 1261a7a328..ae8cbf3c07 100644 --- a/version.py +++ b/version.py @@ -33,7 +33,7 @@ DESKTOP_WARNING_VERSION = "5.4.3" # Changes should be accompanied by documentation explaining what the # new level means in templates/zerver/api/changelog.md, as well as # "**Changes**" entries in the endpoint's documentation in `zulip.yaml`. -API_FEATURE_LEVEL = 105 +API_FEATURE_LEVEL = 106 # Bump the minor PROVISION_VERSION to indicate that folks should provision # only when going from an old version of the code to a newer version. Bump diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml index 87a8de2053..66845c33d4 100644 --- a/zerver/openapi/zulip.yaml +++ b/zerver/openapi/zulip.yaml @@ -8277,11 +8277,12 @@ paths: in: query description: | The user's full name. - content: - application/json: - schema: - type: string - example: NewName + + **Changes**: Removed unnecessary JSON-encoding of this parameter in + Zulip 5.0 (feature level 106). + schema: + type: string + example: NewName required: false - name: role in: query diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.py index 6665044161..afc5f1b2c7 100644 --- a/zerver/tests/test_audit_log.py +++ b/zerver/tests/test_audit_log.py @@ -206,7 +206,7 @@ class TestRealmAuditLog(ZulipTestCase): start = timezone_now() new_name = "George Hamletovich" self.login("iago") - req = dict(full_name=orjson.dumps(new_name).decode()) + req = dict(full_name=new_name) result = self.client_patch("/json/users/{}".format(self.example_user("hamlet").id), req) self.assertTrue(result.status_code == 200) query = RealmAuditLog.objects.filter( diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py index 2767078411..85868d0ef4 100644 --- a/zerver/tests/test_users.py +++ b/zerver/tests/test_users.py @@ -377,7 +377,7 @@ class PermissionTest(ZulipTestCase): new_name = "new name" self.login("iago") hamlet = self.example_user("hamlet") - req = dict(full_name=orjson.dumps(new_name).decode()) + req = dict(full_name=new_name) result = self.client_patch(f"/json/users/{hamlet.id}", req) self.assert_json_success(result) hamlet = self.example_user("hamlet") @@ -385,21 +385,21 @@ class PermissionTest(ZulipTestCase): def test_non_admin_cannot_change_full_name(self) -> None: self.login("hamlet") - req = dict(full_name=orjson.dumps("new name").decode()) + req = dict(full_name="new name") result = self.client_patch("/json/users/{}".format(self.example_user("othello").id), req) self.assert_json_error(result, "Insufficient permission") def test_admin_cannot_set_long_full_name(self) -> None: new_name = "a" * (UserProfile.MAX_NAME_LENGTH + 1) self.login("iago") - req = dict(full_name=orjson.dumps(new_name).decode()) + req = dict(full_name=new_name) result = self.client_patch("/json/users/{}".format(self.example_user("hamlet").id), req) self.assert_json_error(result, "Name too long!") def test_admin_cannot_set_short_full_name(self) -> None: new_name = "a" self.login("iago") - req = dict(full_name=orjson.dumps(new_name).decode()) + req = dict(full_name=new_name) result = self.client_patch("/json/users/{}".format(self.example_user("hamlet").id), req) self.assert_json_error(result, "Name too short!") @@ -407,7 +407,7 @@ class PermissionTest(ZulipTestCase): # Name of format "Alice|999" breaks in Markdown new_name = "iago|72" self.login("iago") - req = dict(full_name=orjson.dumps(new_name).decode()) + req = dict(full_name=new_name) result = self.client_patch("/json/users/{}".format(self.example_user("hamlet").id), req) self.assert_json_error(result, "Invalid format!") @@ -415,21 +415,21 @@ class PermissionTest(ZulipTestCase): # Adding characters after r'|d+' doesn't break Markdown new_name = "Hello- 12iago|72k" self.login("iago") - req = dict(full_name=orjson.dumps(new_name).decode()) + req = dict(full_name=new_name) result = self.client_patch("/json/users/{}".format(self.example_user("hamlet").id), req) self.assert_json_success(result) def test_not_allowed_format_complex(self) -> None: new_name = "Hello- 12iago|72" self.login("iago") - req = dict(full_name=orjson.dumps(new_name).decode()) + req = dict(full_name=new_name) result = self.client_patch("/json/users/{}".format(self.example_user("hamlet").id), req) self.assert_json_error(result, "Invalid format!") def test_admin_cannot_set_full_name_with_invalid_characters(self) -> None: new_name = "Opheli*" self.login("iago") - req = dict(full_name=orjson.dumps(new_name).decode()) + req = dict(full_name=new_name) result = self.client_patch("/json/users/{}".format(self.example_user("hamlet").id), req) self.assert_json_error(result, "Invalid characters in name!") diff --git a/zerver/views/users.py b/zerver/views/users.py index 9c6b638bd0..51e2246393 100644 --- a/zerver/views/users.py +++ b/zerver/views/users.py @@ -163,7 +163,7 @@ def update_user_backend( request: HttpRequest, user_profile: UserProfile, user_id: int, - full_name: Optional[str] = REQ(default=None, json_validator=check_string), + full_name: Optional[str] = REQ(default=None), role: Optional[int] = REQ( default=None, json_validator=check_int_in(