2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
|
|
|
const {rewiremock, 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
|
|
|
|
2021-02-23 03:54:07 +01:00
|
|
|
const page_params = set_global("page_params", {
|
2016-12-07 18:38:59 +01:00
|
|
|
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-03-06 17:37:51 +01:00
|
|
|
const avatar = rewiremock("../../static/js/avatar").with({});
|
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-24 22:31:50 +01:00
|
|
|
const bot_data = zrequire("bot_data");
|
|
|
|
const settings_bots = zrequire("settings_bots");
|
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
|
|
|
settings_bots.__Rewire__("ClipboardJS", 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) => {
|
|
|
|
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());
|
|
|
|
});
|