Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
bots: Add a new endpoint to get a bot's API key.
Sahil Batracc46c64e191e
diff --git a/api_docs/changelog.md b/api_docs/changelog.mdindex 82b30a62728..6bec71bb2f4 100644--- a/api_docs/changelog.md+++ b/api_docs/changelog.md@@ -20,6 +20,11 @@ format used by the Zulip server that they are interacting with.## Changes in Zulip 12.0+**Feature level 463**++* [`GET /bots/{bot_id}/api_key`](/api/get-bot-api-key): Added+ new endpoint to get a bot's API key.+**Feature level 462*** [`GET /events`](/api/get-events): Added `rendered_description` field to thediff --git a/api_docs/include/rest-endpoints.md b/api_docs/include/rest-endpoints.mdindex daec24fbe4d..2c25d012dbe 100644--- a/api_docs/include/rest-endpoints.md+++ b/api_docs/include/rest-endpoints.md@@ -116,6 +116,7 @@* [Get all alert words](/api/get-alert-words)* [Add alert words](/api/add-alert-words)* [Remove alert words](/api/remove-alert-words)+* [Get a bot's API key](/api/get-bot-api-key)#### Invitationsdiff --git a/version.py b/version.pyindex 0dfb3b64059..63f17c71fc0 100644--- a/version.py+++ b/version.py@@ -33,7 +33,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"# https://zulip.readthedocs.io/en/latest/documentation/api.html#step-by-step-guide# Also available at docs/documentation/api.md.-API_FEATURE_LEVEL = 462+API_FEATURE_LEVEL = 463# 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. Bumpdiff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex 4a715348c86..bcff7fa90ef 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -25063,6 +25063,57 @@ paths:}description: |An example JSON response when the channel folder ID is invalid:+ /bots/{bot_id}/api_key:+ get:+ operationId: get-bot-api-key+ summary: Get a bot's API key+ tags: ["users"]+ description: |+ Fetch the API key for a bot user. Only the bot's owner and+ organization administrators have access to a bot's API key.++ **Changes**: New in Zulip 12.0 (feature level 463).+ parameters:+ - $ref: "#/components/parameters/BotId"+ responses:+ "200":+ description: Success.+ content:+ application/json:+ schema:+ allOf:+ - $ref: "#/components/schemas/JsonSuccessBase"+ - additionalProperties: false+ properties:+ result: {}+ msg: {}+ ignored_parameters_unsupported: {}+ api_key:+ type: string+ description: |+ The API key for the bot.+ example:+ {+ "msg": "",+ "result": "success",+ "api_key": "hkA04ZYcqXKalvYMA8OeXSfzUOLrtbZv",+ }++ "400":+ description: Bad request.+ content:+ application/json:+ schema:+ allOf:+ - $ref: "#/components/schemas/CodedError"+ - example:+ {+ "code": "BAD_REQUEST",+ "msg": "No such bot",+ "result": "error",+ }+ description: |+ An example JSON response when the bot ID is invalid:/real-time:# This entry is a hack; it exists to give us a place to put the text# documenting the parameters for call_on_each_event and friends.@@ -29374,3 +29425,12 @@ components:type: integerexample: 1required: true+ BotId:+ name: bot_id+ in: path+ description: |+ The user ID of the target bot.+ schema:+ type: integer+ example: 16+ required: truediff --git a/zerver/tests/test_bots.py b/zerver/tests/test_bots.pyindex 7420c68b6e8..a6b6809bc87 100644--- a/zerver/tests/test_bots.py+++ b/zerver/tests/test_bots.py@@ -2271,3 +2271,35 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):self.assertEqual(orjson.loads(response.content)["msg"], expected_error_message)with self.assertRaises(UserProfile.DoesNotExist):UserProfile.objects.get(full_name="My Stripe Bot")++ def test_get_bot_api_key(self) -> None:+ self.login("hamlet")+ self.create_bot()+ bot = self.get_bot()+ expected_api_key = bot["api_key"]+ email = "[email protected]"+ bot_id = self.get_bot_user(email).id++ result = self.client_get(f"/json/bots/{bot_id}/api_key")+ api_key = self.assert_json_success(result)["api_key"]+ self.assertEqual(api_key, expected_api_key)++ # Admins can also get the bot's api key owned by others.+ self.login("iago")+ result = self.client_get(f"/json/bots/{bot_id}/api_key")+ api_key = self.assert_json_success(result)["api_key"]+ self.assertEqual(api_key, expected_api_key)++ self.login("cordelia")+ result = self.client_get(f"/json/bots/{bot_id}/api_key")+ self.assert_json_error(result, "Insufficient permission")++ # This endpoint can only be used to get a bot's api key.+ self.login("iago")+ invalid_bot_id = self.example_user("cordelia").id+ result = self.client_get(f"/json/bots/{invalid_bot_id}/api_key")+ self.assert_json_error(result, "No such bot")++ invalid_bot_id = 1000+ result = self.client_get(f"/json/bots/{invalid_bot_id}/api_key")+ self.assert_json_error(result, "No such bot")diff --git a/zerver/views/users.py b/zerver/views/users.pyindex a58ea5b0ab7..23182b31a2d 100644--- a/zerver/views/users.py+++ b/zerver/views/users.py@@ -612,6 +612,19 @@ def regenerate_bot_api_key(return json_success(request, data=json_result)+@require_human_non_guest_user+@typed_endpoint_without_parameters+def get_bot_api_key(+ request: HttpRequest, user_profile: UserProfile, bot_id: PathOnly[int]+) -> HttpResponse:+ bot = access_bot_by_id(user_profile, bot_id)++ json_result = dict(+ api_key=bot.api_key,+ )+ return json_success(request, data=json_result)++@require_human_non_guest_user@typed_endpointdef add_bot_backend(diff --git a/zproject/urls.py b/zproject/urls.pyindex 52f0b940fa3..cf7a7473d41 100644--- a/zproject/urls.py+++ b/zproject/urls.py@@ -253,6 +253,7 @@ from zerver.views.users import (deactivate_bot_backend,deactivate_user_backend,deactivate_user_own_backend,+ get_bot_api_key,get_bots_backend,get_member_backend,get_members_backend,@@ -368,6 +369,7 @@ v1_api_and_json_patterns = [rest_path("users/<int:user_id>/subscriptions/<int:stream_id>", GET=get_subscription_backend),rest_path("users/<email>", GET=get_user_by_email, PATCH=update_user_by_email_api),rest_path("bots", GET=get_bots_backend, POST=add_bot_backend),+ rest_path("bots/<int:bot_id>/api_key", GET=get_bot_api_key),rest_path("bots/<int:bot_id>/api_key/regenerate", POST=regenerate_bot_api_key),rest_path("bots/<int:bot_id>", PATCH=patch_bot_backend, DELETE=deactivate_bot_backend),# invites -> zerver.views.invite