mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 13:03:29 +00:00
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:
@@ -41,13 +41,10 @@ run_test('generate_zuliprc_uri', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
run_test('generate_zuliprc_content', () => {
|
run_test('generate_zuliprc_content', () => {
|
||||||
const user = {
|
const bot_user = bot_data.get(1);
|
||||||
email: "admin12@chatting.net",
|
const content = settings_bots.generate_zuliprc_content(bot_user);
|
||||||
api_key: "nSlA0mUm7G42LP85lMv7syqFTzDE2q34",
|
const expected = "[api]\nemail=error-bot@zulip.org\n" +
|
||||||
};
|
"key=QadL788EkiottHmukyhHgePUFHREiu8b\n" +
|
||||||
const content = settings_bots.generate_zuliprc_content(user.email, user.api_key);
|
|
||||||
const expected = "[api]\nemail=admin12@chatting.net\n" +
|
|
||||||
"key=nSlA0mUm7G42LP85lMv7syqFTzDE2q34\n" +
|
|
||||||
"site=https://chat.example.com\n";
|
"site=https://chat.example.com\n";
|
||||||
|
|
||||||
assert.equal(content, expected);
|
assert.equal(content, expected);
|
||||||
|
|||||||
@@ -314,8 +314,12 @@ exports.set_up = function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
$("#download_zuliprc").on("click", function () {
|
$("#download_zuliprc").on("click", function () {
|
||||||
const data = settings_bots.generate_zuliprc_content(people.my_current_email(),
|
const bot_object = {
|
||||||
$("#api_key_value").text());
|
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));
|
$(this).attr("href", settings_bots.encode_zuliprc_as_uri(data));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -115,13 +115,7 @@ exports.render_bots = function () {
|
|||||||
|
|
||||||
exports.generate_zuliprc_uri = function (bot_id) {
|
exports.generate_zuliprc_uri = function (bot_id) {
|
||||||
const bot = bot_data.get(bot_id);
|
const bot = bot_data.get(bot_id);
|
||||||
let token;
|
const data = exports.generate_zuliprc_content(bot);
|
||||||
// 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);
|
|
||||||
return exports.encode_zuliprc_as_uri(data);
|
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);
|
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]" +
|
return "[api]" +
|
||||||
"\nemail=" + email +
|
"\nemail=" + bot.email +
|
||||||
"\nkey=" + api_key +
|
"\nkey=" + bot.api_key +
|
||||||
"\nsite=" + page_params.realm_uri +
|
"\nsite=" + page_params.realm_uri +
|
||||||
(token === undefined ? "" : "\ntoken=" + token) +
|
(token === undefined ? "" : "\ntoken=" + token) +
|
||||||
// Some tools would not work in files without a trailing new line.
|
// Some tools would not work in files without a trailing new line.
|
||||||
@@ -491,10 +491,10 @@ exports.set_up = function () {
|
|||||||
|
|
||||||
new ClipboardJS('#copy_zuliprc', {
|
new ClipboardJS('#copy_zuliprc', {
|
||||||
text: function (trigger) {
|
text: function (trigger) {
|
||||||
const bot_info = trigger.closest(".bot-information-box");
|
const bot_info = $(trigger).closest(".bot-information-box").find(".bot_info");
|
||||||
const email = $(bot_info).find(".email .value").text();
|
const bot_id = parseInt(bot_info.attr("data-user-id"), 10);
|
||||||
const api_key = $(bot_info).find(".api_key .api-key-value-and-button .value").text();
|
const bot = bot_data.get(bot_id);
|
||||||
const data = exports.generate_zuliprc_content(email.trim(), api_key.trim());
|
const data = exports.generate_zuliprc_content(bot);
|
||||||
return data;
|
return data;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user