settings: Fix copy-from-clipboard behavior for bot tokens.

We do this by cleaning up the API for generate_zuliprc_content,
allowing us to deduplicate the previously incorrect code.
This commit is contained in:
Tim Abbott
2020-01-31 15:04:23 -08:00
parent eac07698dd
commit df6b90db3c
3 changed files with 24 additions and 23 deletions

View File

@@ -41,13 +41,10 @@ run_test('generate_zuliprc_uri', () => {
});
run_test('generate_zuliprc_content', () => {
const user = {
email: "admin12@chatting.net",
api_key: "nSlA0mUm7G42LP85lMv7syqFTzDE2q34",
};
const content = settings_bots.generate_zuliprc_content(user.email, user.api_key);
const expected = "[api]\nemail=admin12@chatting.net\n" +
"key=nSlA0mUm7G42LP85lMv7syqFTzDE2q34\n" +
const bot_user = bot_data.get(1);
const content = settings_bots.generate_zuliprc_content(bot_user);
const expected = "[api]\nemail=error-bot@zulip.org\n" +
"key=QadL788EkiottHmukyhHgePUFHREiu8b\n" +
"site=https://chat.example.com\n";
assert.equal(content, expected);

View File

@@ -314,8 +314,12 @@ exports.set_up = function () {
});
$("#download_zuliprc").on("click", function () {
const data = settings_bots.generate_zuliprc_content(people.my_current_email(),
$("#api_key_value").text());
const bot_object = {
user_id: people.my_current_user_id(),
email: people.my_current_email(),
api_key: $("#api_key_value").text(),
};
const data = settings_bots.generate_zuliprc_content(bot_object);
$(this).attr("href", settings_bots.encode_zuliprc_as_uri(data));
});
});

View File

@@ -115,13 +115,7 @@ exports.render_bots = function () {
exports.generate_zuliprc_uri = function (bot_id) {
const bot = bot_data.get(bot_id);
let 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;
}
const data = exports.generate_zuliprc_content(bot.email, bot.api_key, token);
const data = exports.generate_zuliprc_content(bot);
return exports.encode_zuliprc_as_uri(data);
};
@@ -129,10 +123,16 @@ 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, token) {
exports.generate_zuliprc_content = function (bot) {
let 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.user_id)[0].token;
}
return "[api]" +
"\nemail=" + email +
"\nkey=" + api_key +
"\nemail=" + bot.email +
"\nkey=" + bot.api_key +
"\nsite=" + page_params.realm_uri +
(token === undefined ? "" : "\ntoken=" + token) +
// Some tools would not work in files without a trailing new line.
@@ -491,10 +491,10 @@ exports.set_up = function () {
new ClipboardJS('#copy_zuliprc', {
text: function (trigger) {
const bot_info = trigger.closest(".bot-information-box");
const email = $(bot_info).find(".email .value").text();
const api_key = $(bot_info).find(".api_key .api-key-value-and-button .value").text();
const data = exports.generate_zuliprc_content(email.trim(), api_key.trim());
const bot_info = $(trigger).closest(".bot-information-box").find(".bot_info");
const bot_id = parseInt(bot_info.attr("data-user-id"), 10);
const bot = bot_data.get(bot_id);
const data = exports.generate_zuliprc_content(bot);
return data;
},
});