From 5d9a8cf64f13decdd293c8f759d36e16c3e19c66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6nig?= Date: Mon, 4 Jun 2018 16:24:21 +0200 Subject: [PATCH] bots: Add token to outgoing webhook zuliprc. We want the Botserver to not only work with the botserverrc, but also with a zuliprc of an outgoing webhook. Because the Botserver uses the outgoing webhook token for authentication, we need to include it in the zuliprc for outgoing webhooks. --- frontend_tests/casper_tests/06-settings.js | 35 +++++++++++++++++++++- static/js/settings_bots.js | 12 ++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/frontend_tests/casper_tests/06-settings.js b/frontend_tests/casper_tests/06-settings.js index 141cb05701..b1517acc3d 100644 --- a/frontend_tests/casper_tests/06-settings.js +++ b/frontend_tests/casper_tests/06-settings.js @@ -1,6 +1,7 @@ var common = require('../casper_lib/common.js').common; var test_credentials = require('../../var/casper/test_credentials.js').test_credentials; var OUTGOING_WEBHOOK_BOT_TYPE = '3'; +var GENERIC_BOT_TYPE = '1'; common.start_and_log_in(); @@ -8,6 +9,7 @@ common.start_and_log_in(); // var form_sel = 'form[action^="/json/settings"]'; var regex_zuliprc = /^data:application\/octet-stream;charset=utf-8,\[api\]\nemail=.+\nkey=.+\nsite=.+\n$/; +var regex_outgoing_webhook_zuliprc = /^data:application\/octet-stream;charset=utf-8,\[api\]\nemail=.+\nkey=.+\nsite=.+\ntoken=.+\n$/; var regex_botserverrc = /^data:application\/octet-stream;charset=utf-8,\[\]\nemail=.+\nkey=.+\nsite=.+\ntoken=.+\n$/; casper.then(function () { @@ -110,7 +112,7 @@ casper.then(function () { }); casper.then(function create_bot() { - casper.test.info('Filling out the create bot form'); + casper.test.info('Filling out the create bot form for an outgoing webhook bot'); casper.fill('#create_bot_form',{ bot_name: 'Bot 1', @@ -136,6 +138,37 @@ casper.then(function () { casper.waitUntilVisible(button_sel + '[href^="data:application"]', function () { casper.test.assertMatch( decodeURIComponent(casper.getElementsAttribute(button_sel, 'href')), + regex_outgoing_webhook_zuliprc, + 'Looks like an outgoing webhook bot ~/.zuliprc file'); + }); +}); + +casper.then(function create_bot() { + casper.test.info('Filling out the create bot form for a normal bot'); + + casper.fill('#create_bot_form',{ + bot_name: 'Bot 2', + bot_short_name: '2', + bot_type: GENERIC_BOT_TYPE, + }); + + casper.test.info('Submitting the create bot form'); + casper.click('#create_bot_button'); +}); + +var second_bot_email = '2-bot@zulip.zulipdev.com'; +var second_button_sel = '.download_bot_zuliprc[data-email="' + second_bot_email + '"]'; + +casper.then(function () { + casper.waitUntilVisible(second_button_sel, function () { + casper.click(second_button_sel); + }); +}); + +casper.then(function () { + casper.waitUntilVisible(second_button_sel + '[href^="data:application"]', function () { + casper.test.assertMatch( + decodeURIComponent(casper.getElementsAttribute(second_button_sel, 'href')), regex_zuliprc, 'Looks like a bot ~/.zuliprc file'); }); diff --git a/static/js/settings_bots.js b/static/js/settings_bots.js index 9ade6cddf2..9d55c4d454 100644 --- a/static/js/settings_bots.js +++ b/static/js/settings_bots.js @@ -97,7 +97,14 @@ function render_bots() { exports.generate_zuliprc_uri = function (bot_id) { var bot = bot_data.get(bot_id); - var data = exports.generate_zuliprc_content(bot.email, bot.api_key); + var data; + var token; + // For outgoing webhooks, include the token in the zuliprc. + // It's needed for authenticating to the Botserver. + if (bot.bot_type === 3) { + token = bot_data.get_services(bot_id)[0].token; + } + data = exports.generate_zuliprc_content(bot.email, bot.api_key, token); return exports.encode_zuliprc_as_uri(data); }; @@ -105,11 +112,12 @@ exports.encode_zuliprc_as_uri = function (zuliprc) { return "data:application/octet-stream;charset=utf-8," + encodeURIComponent(zuliprc); }; -exports.generate_zuliprc_content = function (email, api_key) { +exports.generate_zuliprc_content = function (email, api_key, token) { return "[api]" + "\nemail=" + email + "\nkey=" + api_key + "\nsite=" + page_params.realm_uri + + (token === undefined ? "" : ("\ntoken=" + token)) + // Some tools would not work in files without a trailing new line. "\n"; };