/json/bots: Replace email with user_id in API to generate bot_api_key.

Fixes #3643.
This commit is contained in:
Yashashvi Dave
2018-05-15 21:43:07 +05:30
committed by Tim Abbott
parent d6e2f9fc88
commit b949d10592
5 changed files with 11 additions and 9 deletions

View File

@@ -335,9 +335,9 @@ exports.set_up = function () {
});
$("#active_bots_list").on("click", "button.regenerate_bot_api_key", function (e) {
var email = $(e.currentTarget).data('email');
var bot_id = $(e.currentTarget).attr('data-user-id');
channel.post({
url: '/json/bots/' + encodeURIComponent(email) + '/api_key/regenerate',
url: '/json/bots/' + encodeURIComponent(bot_id) + '/api_key/regenerate',
idempotent: true,
success: function (data) {
var row = $(e.currentTarget).closest("li");

View File

@@ -33,7 +33,7 @@
<span class="api-key-value-and-button no-select">
<!-- have the `.text-select` in `.no-select` so that the value doesn't have trailing whitespace. -->
<span class="value text-select">{{api_key}}</span>
<button type="submit" class="button no-style btn-secondary regenerate_bot_api_key" title="{{t 'Generate new API key' }}" data-email="{{email}}">
<button type="submit" class="button no-style btn-secondary regenerate_bot_api_key" title="{{t 'Generate new API key' }}" data-user-id="{{user_id}}">
<i class="icon-vector-refresh"></i>
</button>
</span>

View File

@@ -527,7 +527,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
self.login(self.example_email('othello'))
email = 'hambot-bot@zulip.testserver'
result = self.client_post("/json/bots/hambot-bot@zulip.testserver/api_key/regenerate")
result = self.client_post("/json/bots/{}/api_key/regenerate".format(self.get_bot_user(email).id))
self.assert_json_error(result, 'Insufficient permission')
bot_info = {
@@ -546,7 +546,8 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
self.create_bot()
bot = self.get_bot()
old_api_key = bot['api_key']
result = self.client_post('/json/bots/hambot-bot@zulip.testserver/api_key/regenerate')
email = 'hambot-bot@zulip.testserver'
result = self.client_post('/json/bots/{}/api_key/regenerate'.format(self.get_bot_user(email).id))
self.assert_json_success(result)
new_api_key = result.json()['api_key']
self.assertNotEqual(old_api_key, new_api_key)
@@ -555,7 +556,8 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
def test_update_api_key_for_invalid_user(self) -> None:
self.login(self.example_email('hamlet'))
result = self.client_post('/json/bots/nonexistentuser@zulip.com/api_key/regenerate')
invalid_user_id = 1000
result = self.client_post('/json/bots/{}/api_key/regenerate'.format(invalid_user_id))
self.assert_json_error(result, 'No such user')
def test_add_bot_with_bot_type_default(self) -> None:

View File

@@ -246,9 +246,9 @@ def patch_bot_backend(
@require_non_guest_human_user
@has_request_variables
def regenerate_bot_api_key(request: HttpRequest, user_profile: UserProfile, email: str) -> HttpResponse:
def regenerate_bot_api_key(request: HttpRequest, user_profile: UserProfile, bot_id: int) -> HttpResponse:
try:
bot = get_user(email, user_profile.realm)
bot = get_user_profile_by_id(bot_id)
except UserProfile.DoesNotExist:
return json_error(_('No such user'))

View File

@@ -136,7 +136,7 @@ v1_api_and_json_patterns = [
url(r'^bots$', rest_dispatch,
{'GET': 'zerver.views.users.get_bots_backend',
'POST': 'zerver.views.users.add_bot_backend'}),
url(r'^bots/(?!me/)(?P<email>[^/]*)/api_key/regenerate$', rest_dispatch,
url(r'^bots/(?P<bot_id>[0-9]+)/api_key/regenerate$', rest_dispatch,
{'POST': 'zerver.views.users.regenerate_bot_api_key'}),
url(r'^bots/(?P<bot_id>[0-9]+)$', rest_dispatch,
{'PATCH': 'zerver.views.users.patch_bot_backend',