2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-07-28 00:14:57 +02:00
|
|
|
const rewiremock = require("rewiremock/node");
|
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
const {set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-02-21 15:38:51 +01:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2016-12-07 18:38:59 +01:00
|
|
|
set_global("page_params", {
|
|
|
|
realm_uri: "https://chat.example.com",
|
2018-05-07 03:30:13 +02:00
|
|
|
realm_embedded_bots: [
|
|
|
|
{name: "converter", config: {}},
|
2018-12-18 19:34:45 +01:00
|
|
|
{name: "giphy", config: {key: "12345678"}},
|
|
|
|
{name: "foobot", config: {bar: "baz", qux: "quux"}},
|
2018-05-07 03:30:13 +02:00
|
|
|
],
|
2020-02-25 12:16:26 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const bot_data_params = {
|
2020-07-15 00:34:28 +02:00
|
|
|
realm_bots: [
|
|
|
|
{
|
|
|
|
api_key: "QadL788EkiottHmukyhHgePUFHREiu8b",
|
|
|
|
email: "error-bot@zulip.org",
|
|
|
|
full_name: "Error bot",
|
|
|
|
user_id: 1,
|
|
|
|
services: [],
|
|
|
|
},
|
2018-06-01 11:27:02 +02:00
|
|
|
],
|
2020-02-25 12:16:26 +01:00
|
|
|
};
|
2016-12-07 18:38:59 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const avatar = set_global("avatar", {});
|
2017-06-20 00:32:20 +02:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const bot_data = zrequire("bot_data");
|
2016-12-07 18:38:59 +01:00
|
|
|
|
2020-07-02 01:55:18 +02:00
|
|
|
function ClipboardJS(sel) {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sel, "#copy_zuliprc");
|
2020-07-02 01:55:18 +02:00
|
|
|
}
|
2020-07-28 00:14:57 +02:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const settings_bots = rewiremock.proxy(() => zrequire("settings_bots"), {
|
2020-07-28 00:14:57 +02:00
|
|
|
clipboard: ClipboardJS,
|
|
|
|
});
|
2018-06-27 09:35:30 +02:00
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
bot_data.initialize(bot_data_params);
|
2018-06-01 11:27:02 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("generate_zuliprc_uri", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const uri = settings_bots.generate_zuliprc_uri(1);
|
2020-07-15 00:34:28 +02:00
|
|
|
const expected =
|
|
|
|
"data:application/octet-stream;charset=utf-8," +
|
|
|
|
encodeURIComponent(
|
|
|
|
"[api]\nemail=error-bot@zulip.org\n" +
|
|
|
|
"key=QadL788EkiottHmukyhHgePUFHREiu8b\n" +
|
|
|
|
"site=https://chat.example.com\n",
|
|
|
|
);
|
2016-12-07 18:38:59 +01:00
|
|
|
|
|
|
|
assert.equal(uri, expected);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-12-07 18:38:59 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("generate_zuliprc_content", () => {
|
2020-02-01 00:04:23 +01:00
|
|
|
const bot_user = bot_data.get(1);
|
|
|
|
const content = settings_bots.generate_zuliprc_content(bot_user);
|
2020-07-15 00:34:28 +02:00
|
|
|
const expected =
|
|
|
|
"[api]\nemail=error-bot@zulip.org\n" +
|
|
|
|
"key=QadL788EkiottHmukyhHgePUFHREiu8b\n" +
|
|
|
|
"site=https://chat.example.com\n";
|
2016-12-07 18:38:59 +01:00
|
|
|
|
|
|
|
assert.equal(content, expected);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-05-30 16:12:02 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("generate_botserverrc_content", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user = {
|
2017-05-30 16:12:02 +02:00
|
|
|
email: "vabstest-bot@zulip.com",
|
|
|
|
api_key: "nSlA0mUm7G42LP85lMv7syqFTzDE2q34",
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const service = {
|
2018-05-30 11:09:35 +02:00
|
|
|
token: "abcd1234",
|
|
|
|
};
|
2020-07-15 00:34:28 +02:00
|
|
|
const content = settings_bots.generate_botserverrc_content(
|
|
|
|
user.email,
|
|
|
|
user.api_key,
|
|
|
|
service.token,
|
|
|
|
);
|
|
|
|
const expected =
|
|
|
|
"[]\nemail=vabstest-bot@zulip.com\n" +
|
|
|
|
"key=nSlA0mUm7G42LP85lMv7syqFTzDE2q34\n" +
|
|
|
|
"site=https://chat.example.com\n" +
|
|
|
|
"token=abcd1234\n";
|
2017-05-30 16:12:02 +02:00
|
|
|
|
|
|
|
assert.equal(content, expected);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-20 00:32:20 +02:00
|
|
|
|
2017-06-24 22:38:27 +02:00
|
|
|
function test_create_bot_type_input_box_toggle(f) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const create_payload_url = $("#create_payload_url");
|
|
|
|
const payload_url_inputbox = $("#payload_url_inputbox");
|
|
|
|
const config_inputbox = $("#config_inputbox");
|
|
|
|
const EMBEDDED_BOT_TYPE = "4";
|
|
|
|
const OUTGOING_WEBHOOK_BOT_TYPE = "3";
|
|
|
|
const GENERIC_BOT_TYPE = "1";
|
|
|
|
|
|
|
|
$("#create_bot_type :selected").val(EMBEDDED_BOT_TYPE);
|
2020-02-12 01:35:16 +01:00
|
|
|
f();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(!create_payload_url.hasClass("required"));
|
2017-07-15 18:23:44 +02:00
|
|
|
assert(!payload_url_inputbox.visible());
|
2020-07-15 01:29:15 +02:00
|
|
|
assert($("#select_service_name").hasClass("required"));
|
|
|
|
assert($("#service_name_list").visible());
|
2018-01-07 19:24:14 +01:00
|
|
|
assert(config_inputbox.visible());
|
2017-07-15 18:23:44 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#create_bot_type :selected").val(OUTGOING_WEBHOOK_BOT_TYPE);
|
2020-02-12 01:35:16 +01:00
|
|
|
f();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(create_payload_url.hasClass("required"));
|
2017-06-24 22:38:27 +02:00
|
|
|
assert(payload_url_inputbox.visible());
|
2018-01-07 19:24:14 +01:00
|
|
|
assert(!config_inputbox.visible());
|
2017-06-24 22:38:27 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#create_bot_type :selected").val(GENERIC_BOT_TYPE);
|
2020-02-12 01:35:16 +01:00
|
|
|
f();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(!create_payload_url.hasClass("required"));
|
2017-06-24 22:38:27 +02:00
|
|
|
assert(!payload_url_inputbox.visible());
|
2018-01-07 19:24:14 +01:00
|
|
|
assert(!config_inputbox.visible());
|
2017-06-24 22:38:27 +02:00
|
|
|
}
|
2017-06-20 00:32:20 +02:00
|
|
|
|
2021-02-22 13:10:25 +01:00
|
|
|
run_test("test tab clicks", (override) => {
|
2021-02-21 15:01:54 +01:00
|
|
|
$.clear_all_elements();
|
2018-06-12 20:58:57 +02:00
|
|
|
|
2021-02-22 13:10:25 +01:00
|
|
|
override($.validator, "addMethod", () => {});
|
2017-06-20 00:32:20 +02:00
|
|
|
|
2018-06-12 20:51:31 +02:00
|
|
|
$("#create_bot_form").validate = () => {};
|
2017-06-20 00:32:20 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#config_inputbox").children = () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const mock_children = {
|
2020-07-02 01:41:40 +02:00
|
|
|
hide: () => {},
|
2018-01-07 19:24:14 +01:00
|
|
|
};
|
|
|
|
return mock_children;
|
|
|
|
};
|
2021-02-22 13:10:25 +01:00
|
|
|
|
|
|
|
override(avatar, "build_bot_create_widget", () => {});
|
2017-06-20 00:32:20 +02:00
|
|
|
|
|
|
|
settings_bots.set_up();
|
2020-07-21 00:23:06 +02:00
|
|
|
|
|
|
|
test_create_bot_type_input_box_toggle(() => $("#create_bot_type").trigger("change"));
|
2018-06-12 21:42:43 +02:00
|
|
|
|
|
|
|
function click_on_tab(tab_elem) {
|
2020-07-20 21:24:26 +02:00
|
|
|
tab_elem.trigger("click");
|
2018-06-12 21:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const tabs = {
|
2020-07-15 01:29:15 +02:00
|
|
|
add: $("#bots_lists_navbar .add-a-new-bot-tab"),
|
|
|
|
active: $("#bots_lists_navbar .active-bots-tab"),
|
|
|
|
inactive: $("#bots_lists_navbar .inactive-bots-tab"),
|
2018-06-12 21:42:43 +02:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#bots_lists_navbar .active").removeClass = (cls) => {
|
|
|
|
assert.equal(cls, "active");
|
2020-02-06 02:23:02 +01:00
|
|
|
for (const tab of Object.values(tabs)) {
|
2020-07-15 01:29:15 +02:00
|
|
|
tab.removeClass("active");
|
2020-02-06 02:23:02 +01:00
|
|
|
}
|
2018-06-12 21:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const forms = {
|
2020-07-15 01:29:15 +02:00
|
|
|
add: $("#add-a-new-bot-form"),
|
|
|
|
active: $("#active_bots_list"),
|
|
|
|
inactive: $("#inactive_bots_list"),
|
2018-06-12 21:42:43 +02:00
|
|
|
};
|
|
|
|
|
2021-02-22 13:10:25 +01:00
|
|
|
click_on_tab(tabs.add);
|
|
|
|
assert(tabs.add.hasClass("active"));
|
|
|
|
assert(!tabs.active.hasClass("active"));
|
|
|
|
assert(!tabs.inactive.hasClass("active"));
|
|
|
|
|
|
|
|
assert(forms.add.visible());
|
|
|
|
assert(!forms.active.visible());
|
|
|
|
assert(!forms.inactive.visible());
|
|
|
|
|
|
|
|
click_on_tab(tabs.active);
|
|
|
|
assert(!tabs.add.hasClass("active"));
|
|
|
|
assert(tabs.active.hasClass("active"));
|
|
|
|
assert(!tabs.inactive.hasClass("active"));
|
|
|
|
|
|
|
|
assert(!forms.add.visible());
|
|
|
|
assert(forms.active.visible());
|
|
|
|
assert(!forms.inactive.visible());
|
|
|
|
|
|
|
|
click_on_tab(tabs.inactive);
|
|
|
|
assert(!tabs.add.hasClass("active"));
|
|
|
|
assert(!tabs.active.hasClass("active"));
|
|
|
|
assert(tabs.inactive.hasClass("active"));
|
|
|
|
|
|
|
|
assert(!forms.add.visible());
|
|
|
|
assert(!forms.active.visible());
|
|
|
|
assert(forms.inactive.visible());
|
2018-06-12 21:42:43 +02:00
|
|
|
});
|
2018-06-13 16:59:15 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("can_create_new_bots", () => {
|
2018-06-13 16:59:15 +02:00
|
|
|
page_params.is_admin = true;
|
|
|
|
assert(settings_bots.can_create_new_bots());
|
|
|
|
|
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.realm_bot_creation_policy = 1;
|
|
|
|
assert(settings_bots.can_create_new_bots());
|
|
|
|
|
|
|
|
page_params.realm_bot_creation_policy = 3;
|
|
|
|
assert(!settings_bots.can_create_new_bots());
|
|
|
|
});
|