2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-03-25 22:35:45 +01:00
|
|
|
const {mock_cjs, mock_esm, 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");
|
2021-03-25 22:35:45 +01:00
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
2020-12-01 00:02:16 +01: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-03-11 05:43:45 +01:00
|
|
|
mock_cjs("jquery", $);
|
2021-03-10 06:10:32 +01:00
|
|
|
const avatar = mock_esm("../../static/js/avatar");
|
2021-03-10 06:32:52 +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
|
|
|
}
|
2021-03-10 06:32:52 +01:00
|
|
|
mock_cjs("clipboard", ClipboardJS);
|
2020-07-28 00:14:57 +02:00
|
|
|
|
2021-02-24 22:31:50 +01:00
|
|
|
const bot_data = zrequire("bot_data");
|
|
|
|
const settings_bots = zrequire("settings_bots");
|
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
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
function test(label, f) {
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test(label, ({override}) => {
|
2021-04-03 19:07:13 +02:00
|
|
|
page_params.realm_uri = "https://chat.example.com";
|
|
|
|
page_params.realm_embedded_bots = [
|
|
|
|
{name: "converter", config: {}},
|
|
|
|
{name: "giphy", config: {key: "12345678"}},
|
|
|
|
{name: "foobot", config: {bar: "baz", qux: "quux"}},
|
|
|
|
];
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
f({override});
|
2021-04-03 19:07:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
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
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
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();
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!create_payload_url.hasClass("required"));
|
|
|
|
assert.ok(!payload_url_inputbox.visible());
|
|
|
|
assert.ok($("#select_service_name").hasClass("required"));
|
|
|
|
assert.ok($("#service_name_list").visible());
|
|
|
|
assert.ok(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();
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(create_payload_url.hasClass("required"));
|
|
|
|
assert.ok(payload_url_inputbox.visible());
|
|
|
|
assert.ok(!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();
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!create_payload_url.hasClass("required"));
|
|
|
|
assert.ok(!payload_url_inputbox.visible());
|
|
|
|
assert.ok(!config_inputbox.visible());
|
2017-06-24 22:38:27 +02:00
|
|
|
}
|
2017-06-20 00:32:20 +02:00
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test("test tab clicks", ({override}) => {
|
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);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(tabs.add.hasClass("active"));
|
|
|
|
assert.ok(!tabs.active.hasClass("active"));
|
|
|
|
assert.ok(!tabs.inactive.hasClass("active"));
|
2021-02-22 13:10:25 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(forms.add.visible());
|
|
|
|
assert.ok(!forms.active.visible());
|
|
|
|
assert.ok(!forms.inactive.visible());
|
2021-02-22 13:10:25 +01:00
|
|
|
|
|
|
|
click_on_tab(tabs.active);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!tabs.add.hasClass("active"));
|
|
|
|
assert.ok(tabs.active.hasClass("active"));
|
|
|
|
assert.ok(!tabs.inactive.hasClass("active"));
|
2021-02-22 13:10:25 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!forms.add.visible());
|
|
|
|
assert.ok(forms.active.visible());
|
|
|
|
assert.ok(!forms.inactive.visible());
|
2021-02-22 13:10:25 +01:00
|
|
|
|
|
|
|
click_on_tab(tabs.inactive);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!tabs.add.hasClass("active"));
|
|
|
|
assert.ok(!tabs.active.hasClass("active"));
|
|
|
|
assert.ok(tabs.inactive.hasClass("active"));
|
2021-02-22 13:10:25 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!forms.add.visible());
|
|
|
|
assert.ok(!forms.active.visible());
|
|
|
|
assert.ok(forms.inactive.visible());
|
2018-06-12 21:42:43 +02:00
|
|
|
});
|
2018-06-13 16:59:15 +02:00
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
test("can_create_new_bots", () => {
|
2018-06-13 16:59:15 +02:00
|
|
|
page_params.is_admin = true;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(settings_bots.can_create_new_bots());
|
2018-06-13 16:59:15 +02:00
|
|
|
|
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.realm_bot_creation_policy = 1;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(settings_bots.can_create_new_bots());
|
2018-06-13 16:59:15 +02:00
|
|
|
|
|
|
|
page_params.realm_bot_creation_policy = 3;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!settings_bots.can_create_new_bots());
|
2018-06-13 16:59:15 +02:00
|
|
|
});
|