From 63173d5554663cb263af0b6a19500e6f8353a1fd Mon Sep 17 00:00:00 2001 From: Kartik Srivastava Date: Wed, 12 Aug 2020 02:31:01 +0530 Subject: [PATCH] api: Return 'user_id' in 'POST /users' response. This adds 'user_id' to the simple success response for 'POST /users' api endpoint, to make it convenient for API clients to get details about users they just created. Appropriate changes have been made in the docs and test_users.py. Fixes #16072. --- templates/zerver/api/changelog.md | 2 ++ templates/zerver/api/create-user.md | 4 ++++ version.py | 2 +- zerver/openapi/zulip.yaml | 11 ++++++++++- zerver/tests/test_bots.py | 1 + zerver/tests/test_users.py | 2 ++ zerver/views/users.py | 5 +++-- 7 files changed, 23 insertions(+), 4 deletions(-) diff --git a/templates/zerver/api/changelog.md b/templates/zerver/api/changelog.md index 989055056a..727af3f291 100644 --- a/templates/zerver/api/changelog.md +++ b/templates/zerver/api/changelog.md @@ -15,6 +15,8 @@ below features are supported. * [`GET users/me/subscriptions`](/api/get-subscriptions), [`GET /streams`](/api/get-streams): Added `date_created` to Stream objects. +* [`POST /users`](/api/create-user), `POST /bots`: The ID of the newly + created user is now returned in the response. Feature levels 28 and 29 are reserved for future use in 3.x bug fix releases. diff --git a/templates/zerver/api/create-user.md b/templates/zerver/api/create-user.md index 467c3a7f2a..f59022835d 100644 --- a/templates/zerver/api/create-user.md +++ b/templates/zerver/api/create-user.md @@ -27,6 +27,10 @@ More examples and documentation can be found [here](https://github.com/zulip/zul ## Response +#### Return values + +{generate_return_values_table|zulip.yaml|/users:post} + #### Example response A typical successful JSON response may look like: diff --git a/version.py b/version.py index 70fbc08032..b3e74ed866 100644 --- a/version.py +++ b/version.py @@ -29,7 +29,7 @@ DESKTOP_WARNING_VERSION = "5.2.0" # # Changes should be accompanied by documentation explaining what the # new level means in templates/zerver/api/changelog.md. -API_FEATURE_LEVEL = 27 +API_FEATURE_LEVEL = 30 # 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 34b6d1bb8c..5482b0c269 100644 --- a/zerver/openapi/zulip.yaml +++ b/zerver/openapi/zulip.yaml @@ -4081,7 +4081,16 @@ paths: content: application/json: schema: - $ref: "#/components/schemas/JsonSuccess" + allOf: + - $ref: "#/components/schemas/JsonSuccess" + - properties: + user_id: + type: integer + description: | + The ID assigned to the newly created user. + + **Changes**: New in Zulip 4.0 (feature level 30). + - example: {"msg": "", "result": "success", "user_id": 25} "400": description: Bad request. content: diff --git a/zerver/tests/test_bots.py b/zerver/tests/test_bots.py index 3ba5b801b2..a4db0146f5 100644 --- a/zerver/tests/test_bots.py +++ b/zerver/tests/test_bots.py @@ -175,6 +175,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin): if e['event']['type'] == 'realm_bot' ] + self.assertEqual(result["user_id"], bot.id) self.assertEqual( dict( type='realm_bot', diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py index cb397a9612..19d03f1292 100644 --- a/zerver/tests/test_users.py +++ b/zerver/tests/test_users.py @@ -823,7 +823,9 @@ class AdminCreateUserTest(ZulipTestCase): # Romeo is a newly registered user new_user = get_user_by_delivery_email('romeo@zulip.net', get_realm('zulip')) + result = orjson.loads(result.content) self.assertEqual(new_user.full_name, 'Romeo Montague') + self.assertEqual(new_user.id, result['user_id']) # Make sure the recipient field is set correctly. self.assertEqual(new_user.recipient, Recipient.objects.get(type=Recipient.PERSONAL, diff --git a/zerver/views/users.py b/zerver/views/users.py index 0866510659..2bf3c14cf1 100644 --- a/zerver/views/users.py +++ b/zerver/views/users.py @@ -413,6 +413,7 @@ def add_bot_backend( api_key = get_api_key(bot_profile) json_result = dict( + user_id=bot_profile.id, api_key=api_key, avatar_url=avatar_url(bot_profile), default_sending_stream=get_stream_name(bot_profile.default_sending_stream), @@ -521,8 +522,8 @@ def create_user_backend( if not check_password_strength(password): return json_error(PASSWORD_TOO_WEAK_ERROR) - do_create_user(email, password, realm, full_name, acting_user=user_profile) - return json_success() + target_user = do_create_user(email, password, realm, full_name, acting_user=user_profile) + return json_success({'user_id': target_user.id}) def get_profile_backend(request: HttpRequest, user_profile: UserProfile) -> HttpResponse: raw_user_data = get_raw_user_data(user_profile.realm, user_profile,