From 9c63976da583c3777f1dc7fc1cc4a17c8809e6ea Mon Sep 17 00:00:00 2001 From: akashaviator Date: Mon, 9 Mar 2020 01:43:11 +0530 Subject: [PATCH] api: Refactor get_members_backend in zerver/views/users.py. This refactors get_members_backend to return user data of a single user in the form of a dictionary (earlier being a list with a single dictionary). This also refactors it to return the data with an appropriate key (inside a dictionary), "user" or "members", according to the type of data being returned. Tweaked by tabbott to use somewhat less opaque code and simple OpenAPI descriptions. --- templates/zerver/api/get-all-users.md | 6 +++--- templates/zerver/api/get-user.md | 3 +-- zerver/openapi/zulip.yaml | 20 +++++++++----------- zerver/tests/test_realm.py | 12 ++++++------ zerver/tests/test_users.py | 14 +++++++------- zerver/views/users.py | 8 +++++++- 6 files changed, 33 insertions(+), 30 deletions(-) diff --git a/templates/zerver/api/get-all-users.md b/templates/zerver/api/get-all-users.md index 91daabd82d..36014128d8 100644 --- a/templates/zerver/api/get-all-users.md +++ b/templates/zerver/api/get-all-users.md @@ -54,9 +54,9 @@ You may pass the `client_gravatar` query parameter as follows: #### Return values -* `members`: A list of dictionaries where each dictionary contains information - about a particular user or bot. - * `email`: The email address of the user or bot.. +* `members`: A list of dictionaries, each containing the details for + one of the users in the organization. + * `email`: The email address of the user or bot. * `is_bot`: A boolean specifying whether the user is a bot or not. * `avatar_url`: URL to the user's gravatar. `None` if the `client_gravatar` query paramater was set to `True`. diff --git a/templates/zerver/api/get-user.md b/templates/zerver/api/get-user.md index a672f24e12..b10c3fc007 100644 --- a/templates/zerver/api/get-user.md +++ b/templates/zerver/api/get-user.md @@ -33,8 +33,7 @@ You may pass the `client_gravatar` or `include_custom_profile_fields` query para #### Return values -* `members`: A list with a single dictionary that contains information - about a particular user or bot. +* `user`: A dictionary that contains the requested user's details. * `email`: The email address of the user or bot. * `is_bot`: A boolean specifying whether the user is a bot or not. * `avatar_url`: URL to the user's gravatar. `None` if the `client_gravatar` diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml index 03e2cfee38..529bfa16f2 100644 --- a/zerver/openapi/zulip.yaml +++ b/zerver/openapi/zulip.yaml @@ -1006,9 +1006,8 @@ paths: - properties: members: type: array - description: A list of `member` objects, each containing - details of a user in the realm (like their email, name, - avatar, user type...). + description: A list of `user` objects, each containing + details about a user in the organization. - example: { "msg": "", @@ -1171,17 +1170,15 @@ paths: allOf: - $ref: '#/components/schemas/JsonSuccess' - properties: - members: - type: array - description: A list `member` containing a single object with - details of a user in the realm (like their email, name, - avatar, user type...). + user: + type: object + description: | + A dictionary containing the requested user's details. - example: { "msg": "", "result": "success", - "members": [ - { + "user": { "date_joined": "2019-10-20T07:50:53.729659+00:00", "full_name": "King Hamlet", "is_guest": false, @@ -1223,7 +1220,8 @@ paths: "is_active": true, "email": "hamlet@zulip.com" } - ] + + } patch: diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py index 1397205e23..580ef2cc2f 100644 --- a/zerver/tests/test_realm.py +++ b/zerver/tests/test_realm.py @@ -421,16 +421,16 @@ class RealmTest(ZulipTestCase): # Check normal user cannot access email result = self.api_get(cordelia.delivery_email, "/api/v1/users/%s" % (hamlet.id,)) self.assert_json_success(result) - self.assertEqual(result.json()['members'][0]['email'], + self.assertEqual(result.json()['user']['email'], 'user%s@zulip.testserver' % (hamlet.id,)) - self.assertEqual(result.json()['members'][0].get('delivery_email'), None) + self.assertEqual(result.json()['user'].get('delivery_email'), None) # Check administrator gets delivery_email with EMAIL_ADDRESS_VISIBILITY_ADMINS result = self.api_get(user_profile.delivery_email, "/api/v1/users/%s" % (hamlet.id,)) self.assert_json_success(result) - self.assertEqual(result.json()['members'][0]['email'], + self.assertEqual(result.json()['user']['email'], 'user%s@zulip.testserver' % (hamlet.id,)) - self.assertEqual(result.json()['members'][0].get('delivery_email'), + self.assertEqual(result.json()['user'].get('delivery_email'), hamlet.delivery_email) req = dict(email_address_visibility = ujson.dumps(Realm.EMAIL_ADDRESS_VISIBILITY_NOBODY)) @@ -446,9 +446,9 @@ class RealmTest(ZulipTestCase): # EMAIL_ADDRESS_VISIBILITY_NOBODY result = self.api_get(user_profile.delivery_email, "/api/v1/users/%s" % (hamlet.id,)) self.assert_json_success(result) - self.assertEqual(result.json()['members'][0]['email'], + self.assertEqual(result.json()['user']['email'], 'user%s@zulip.testserver' % (hamlet.id,)) - self.assertEqual(result.json()['members'][0].get('delivery_email'), None) + self.assertEqual(result.json()['user'].get('delivery_email'), None) def test_change_stream_creation_policy(self) -> None: # We need an admin user. diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py index caaf775d88..3ac152a9a1 100644 --- a/zerver/tests/test_users.py +++ b/zerver/tests/test_users.py @@ -1341,16 +1341,16 @@ class GetProfileTest(ZulipTestCase): # Tests the GET ../users/{id} api endpoint. user = self.example_user('hamlet') result = ujson.loads(self.client_get('/json/users/{}'.format(user.id)).content) - self.assertEqual(result['members'][0]['email'], user.email) - self.assertEqual(result['members'][0]['full_name'], user.full_name) - self.assertIn("user_id", result['members'][0]) - self.assertNotIn("profile_data", result['members'][0]) - self.assertFalse(result['members'][0]['is_bot']) - self.assertFalse(result['members'][0]['is_admin']) + self.assertEqual(result['user']['email'], user.email) + self.assertEqual(result['user']['full_name'], user.full_name) + self.assertIn("user_id", result['user']) + self.assertNotIn("profile_data", result['user']) + self.assertFalse(result['user']['is_bot']) + self.assertFalse(result['user']['is_admin']) result = ujson.loads(self.client_get('/json/users/{}?include_custom_profile_fields=true'.format(user.id)).content) - self.assertIn('profile_data', result['members'][0]) + self.assertIn('profile_data', result['user']) result = self.client_get('/json/users/{}?'.format(30)) self.assert_json_error(result, "No such user") diff --git a/zerver/views/users.py b/zerver/views/users.py index 04f728e0ac..7e08ce9318 100644 --- a/zerver/views/users.py +++ b/zerver/views/users.py @@ -420,7 +420,13 @@ def get_members_backend(request: HttpRequest, user_profile: UserProfile, user_id members = get_raw_user_data(realm, user_profile, client_gravatar=client_gravatar, target_user=target_user, include_custom_profile_fields=include_custom_profile_fields) - return json_success({'members': members.values()}) + + if target_user is not None: + data = {"user": members[target_user.id]} # type: Dict[str, Any] + else: + data = {"members": [members[k] for k in members]} + + return json_success(data) @require_realm_admin @has_request_variables