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-21 17:22:14 +01:00
|
|
|
const {zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const bot_data = zrequire("bot_data");
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2020-05-10 21:01:33 +02:00
|
|
|
|
|
|
|
const me = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "me@zulip.com",
|
|
|
|
full_name: "Me Myself",
|
2020-05-10 21:01:33 +02:00
|
|
|
user_id: 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
const fred = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "fred@zulip.com",
|
|
|
|
full_name: "Fred Frederickson",
|
2020-05-10 21:01:33 +02:00
|
|
|
user_id: 3,
|
|
|
|
};
|
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
const bot_data_params = {
|
2020-07-15 00:34:28 +02:00
|
|
|
realm_bots: [
|
|
|
|
{email: "bot0@zulip.com", user_id: 42, full_name: "Bot 0", services: []},
|
|
|
|
{
|
|
|
|
email: "outgoingwebhook@zulip.com",
|
|
|
|
user_id: 314,
|
|
|
|
full_name: "Outgoing webhook",
|
|
|
|
services: [{base_url: "http://foo.com", interface: 1}],
|
|
|
|
},
|
|
|
|
],
|
2014-02-27 18:27:31 +01:00
|
|
|
};
|
2018-08-01 21:17:03 +02:00
|
|
|
|
2021-03-14 10:02:33 +01:00
|
|
|
function test(label, f) {
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test(label, ({override}) => {
|
2021-03-14 10:02:33 +01:00
|
|
|
people.add_active_user(me);
|
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
bot_data.initialize(bot_data_params);
|
|
|
|
// Our startup logic should have added Bot 0 from page_params.
|
|
|
|
assert.equal(bot_data.get(42).full_name, "Bot 0");
|
|
|
|
assert.equal(bot_data.get(314).full_name, "Outgoing webhook");
|
2021-06-16 14:38:37 +02:00
|
|
|
f({override});
|
2021-03-14 10:02:33 +01:00
|
|
|
});
|
|
|
|
}
|
2014-03-12 19:31:58 +01:00
|
|
|
|
2021-03-14 10:02:33 +01:00
|
|
|
test("test_basics", () => {
|
|
|
|
people.add_active_user(fred);
|
2018-04-18 22:08:00 +02:00
|
|
|
const test_bot = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "bot1@zulip.com",
|
2018-01-22 18:36:53 +01:00
|
|
|
user_id: 43,
|
2020-07-15 01:29:15 +02:00
|
|
|
avatar_url: "",
|
|
|
|
full_name: "Bot 1",
|
2018-01-16 20:40:03 +01:00
|
|
|
services: [{base_url: "http://bar.com", interface: 1}],
|
2020-07-15 01:29:15 +02:00
|
|
|
extra: "Not in data",
|
2014-02-27 00:01:18 +01:00
|
|
|
};
|
|
|
|
|
2018-04-18 22:08:00 +02:00
|
|
|
const test_embedded_bot = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "embedded-bot@zulip.com",
|
2018-01-30 19:24:18 +01:00
|
|
|
user_id: 143,
|
2020-07-15 01:29:15 +02:00
|
|
|
avatar_url: "",
|
|
|
|
full_name: "Embedded bot 1",
|
2020-07-15 00:34:28 +02:00
|
|
|
services: [{config_data: {key: "12345678"}, service_name: "giphy"}],
|
2020-07-15 01:29:15 +02:00
|
|
|
owner: "cordelia@zulip.com",
|
2018-01-30 19:24:18 +01:00
|
|
|
};
|
|
|
|
|
2014-02-27 00:01:18 +01:00
|
|
|
(function test_add() {
|
|
|
|
bot_data.add(test_bot);
|
|
|
|
|
2018-04-18 22:08:00 +02:00
|
|
|
const bot = bot_data.get(43);
|
|
|
|
const services = bot_data.get_services(43);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("Bot 1", bot.full_name);
|
|
|
|
assert.equal("http://bar.com", services[0].base_url);
|
2018-01-16 20:40:03 +01:00
|
|
|
assert.equal(1, services[0].interface);
|
2014-02-27 00:01:18 +01:00
|
|
|
assert.equal(undefined, bot.extra);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2014-02-27 00:01:18 +01:00
|
|
|
|
|
|
|
(function test_update() {
|
|
|
|
bot_data.add(test_bot);
|
|
|
|
|
2018-04-18 22:08:00 +02:00
|
|
|
let bot = bot_data.get(43);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("Bot 1", bot.full_name);
|
2020-07-15 00:34:28 +02:00
|
|
|
bot_data.update(43, {
|
|
|
|
full_name: "New Bot 1",
|
|
|
|
services: [{interface: 2, base_url: "http://baz.com"}],
|
|
|
|
});
|
2018-01-22 18:36:53 +01:00
|
|
|
bot = bot_data.get(43);
|
2018-04-18 22:08:00 +02:00
|
|
|
const services = bot_data.get_services(43);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("New Bot 1", bot.full_name);
|
2018-01-16 20:40:03 +01:00
|
|
|
assert.equal(2, services[0].interface);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("http://baz.com", services[0].base_url);
|
2020-05-10 19:21:08 +02:00
|
|
|
|
|
|
|
const change_owner_event = {
|
|
|
|
owner_id: fred.user_id,
|
|
|
|
};
|
|
|
|
bot_data.update(43, change_owner_event);
|
|
|
|
|
|
|
|
bot = bot_data.get(43);
|
|
|
|
assert.equal(bot.owner_id, fred.user_id);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2014-02-27 00:01:18 +01:00
|
|
|
|
2018-01-30 19:24:18 +01:00
|
|
|
(function test_embedded_bot_update() {
|
|
|
|
bot_data.add(test_embedded_bot);
|
2018-04-18 22:08:00 +02:00
|
|
|
const bot_id = 143;
|
|
|
|
const services = bot_data.get_services(bot_id);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("12345678", services[0].config_data.key);
|
|
|
|
bot_data.update(bot_id, {services: [{config_data: {key: "87654321"}}]});
|
|
|
|
assert.equal("87654321", services[0].config_data.key);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-01-30 19:24:18 +01:00
|
|
|
|
2014-02-27 00:01:18 +01:00
|
|
|
(function test_remove() {
|
2018-04-18 22:08:00 +02:00
|
|
|
let bot;
|
2014-02-27 00:01:18 +01:00
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
bot_data.add({...test_bot, is_active: true});
|
2014-02-27 00:01:18 +01:00
|
|
|
|
2018-01-22 18:36:53 +01:00
|
|
|
bot = bot_data.get(43);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("Bot 1", bot.full_name);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(bot.is_active);
|
2018-01-22 18:36:53 +01:00
|
|
|
bot_data.deactivate(43);
|
|
|
|
bot = bot_data.get(43);
|
2017-02-07 19:48:17 +01:00
|
|
|
assert.equal(bot.is_active, false);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2014-02-27 00:01:18 +01:00
|
|
|
|
2020-05-09 20:06:14 +02:00
|
|
|
(function test_all_user_ids() {
|
|
|
|
const all_ids = bot_data.all_user_ids();
|
|
|
|
all_ids.sort();
|
|
|
|
assert.deepEqual(all_ids, [143, 314, 42, 43]);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2020-05-09 20:06:14 +02:00
|
|
|
|
2018-03-08 16:28:37 +01:00
|
|
|
(function test_delete() {
|
2018-04-18 22:08:00 +02:00
|
|
|
let bot;
|
2018-03-08 16:28:37 +01:00
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
bot_data.add({...test_bot, is_active: true});
|
2018-03-08 16:28:37 +01:00
|
|
|
|
|
|
|
bot = bot_data.get(43);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("Bot 1", bot.full_name);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(bot.is_active);
|
2018-08-04 08:04:24 +02:00
|
|
|
bot_data.del(43);
|
2018-03-08 16:28:37 +01:00
|
|
|
bot = bot_data.get(43);
|
|
|
|
assert.equal(bot, undefined);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-03-08 16:28:37 +01:00
|
|
|
|
2014-02-27 18:27:31 +01:00
|
|
|
(function test_get_editable() {
|
2020-05-11 19:14:31 +02:00
|
|
|
bot_data.add({...test_bot, user_id: 44, owner_id: me.user_id, is_active: true});
|
2020-07-15 00:34:28 +02:00
|
|
|
bot_data.add({
|
|
|
|
...test_bot,
|
|
|
|
user_id: 45,
|
|
|
|
email: "bot2@zulip.com",
|
|
|
|
owner_id: me.user_id,
|
|
|
|
is_active: true,
|
|
|
|
});
|
|
|
|
bot_data.add({
|
|
|
|
...test_bot,
|
|
|
|
user_id: 46,
|
|
|
|
email: "bot3@zulip.com",
|
|
|
|
owner_id: fred.user_id,
|
|
|
|
is_active: true,
|
|
|
|
});
|
2014-02-27 18:27:31 +01:00
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
const editable_bots = bot_data.get_editable().map((bot) => bot.email);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.deepEqual(["bot1@zulip.com", "bot2@zulip.com"], editable_bots);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2014-02-27 18:27:31 +01:00
|
|
|
|
2017-05-25 06:00:14 +02:00
|
|
|
(function test_get_all_bots_for_current_user() {
|
2018-04-18 22:08:00 +02:00
|
|
|
const bots = bot_data.get_all_bots_for_current_user();
|
2014-02-27 18:27:31 +01:00
|
|
|
|
2017-05-25 06:00:14 +02:00
|
|
|
assert.equal(bots.length, 2);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(bots[0].email, "bot1@zulip.com");
|
|
|
|
assert.equal(bots[1].email, "bot2@zulip.com");
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-06-02 01:37:58 +02:00
|
|
|
});
|