2019-11-02 00:06:25 +01:00
|
|
|
const _settings_bots = {
|
2018-07-02 20:06:09 +02:00
|
|
|
render_bots: () => {},
|
2018-08-01 21:17:03 +02:00
|
|
|
};
|
2014-02-27 18:27:31 +01:00
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
const bot_data_params = {
|
2020-02-08 01:27:04 +01:00
|
|
|
realm_bots: [{email: 'bot0@zulip.com', user_id: 42, full_name: 'Bot 0',
|
|
|
|
services: []},
|
2018-01-22 18:36:53 +01:00
|
|
|
{email: 'outgoingwebhook@zulip.com', user_id: 314, full_name: "Outgoing webhook",
|
2018-01-16 20:40:03 +01:00
|
|
|
services: [{base_url: "http://foo.com", interface: 1}]}],
|
2014-02-27 18:27:31 +01:00
|
|
|
};
|
2018-08-01 21:17:03 +02:00
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
set_global('page_params', {is_admin: false});
|
2018-08-01 21:17:03 +02:00
|
|
|
set_global('settings_bots', _settings_bots);
|
|
|
|
|
|
|
|
zrequire('people');
|
|
|
|
zrequire('bot_data');
|
2014-02-27 18:27:31 +01:00
|
|
|
|
2020-03-21 21:54:16 +01:00
|
|
|
people.add({
|
2017-01-19 23:04:52 +01:00
|
|
|
email: 'owner@zulip.com',
|
|
|
|
full_name: 'The Human Boss',
|
|
|
|
user_id: 42,
|
|
|
|
});
|
|
|
|
|
2020-03-21 21:54:16 +01:00
|
|
|
people.initialize_current_user(42);
|
2017-01-19 23:04:52 +01:00
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
bot_data.initialize(bot_data_params);
|
2014-03-12 19:31:58 +01:00
|
|
|
// Our startup logic should have added Bot 0 from page_params.
|
2018-01-22 18:36:53 +01:00
|
|
|
assert.equal(bot_data.get(42).full_name, 'Bot 0');
|
|
|
|
assert.equal(bot_data.get(314).full_name, 'Outgoing webhook');
|
2014-03-12 19:31:58 +01:00
|
|
|
|
2018-06-02 01:37:58 +02:00
|
|
|
run_test('test_basics', () => {
|
2018-04-18 22:08:00 +02:00
|
|
|
const test_bot = {
|
2014-02-27 00:01:18 +01:00
|
|
|
email: 'bot1@zulip.com',
|
2018-01-22 18:36:53 +01:00
|
|
|
user_id: 43,
|
2014-02-27 00:01:18 +01:00
|
|
|
avatar_url: '',
|
|
|
|
full_name: 'Bot 1',
|
2018-01-16 20:40:03 +01:00
|
|
|
services: [{base_url: "http://bar.com", interface: 1}],
|
2016-12-03 23:17:57 +01: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 = {
|
2018-01-30 19:24:18 +01:00
|
|
|
email: 'embedded-bot@zulip.com',
|
|
|
|
user_id: 143,
|
|
|
|
avatar_url: '',
|
|
|
|
full_name: 'Embedded bot 1',
|
|
|
|
services: [{config_data: {key: '12345678'},
|
|
|
|
service_name: "giphy"}],
|
2018-11-16 10:14:34 +01: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);
|
2014-02-27 00:01:18 +01:00
|
|
|
assert.equal('Bot 1', bot.full_name);
|
2018-01-16 20:40:03 +01:00
|
|
|
assert.equal('http://bar.com', services[0].base_url);
|
|
|
|
assert.equal(1, services[0].interface);
|
2014-02-27 00:01:18 +01:00
|
|
|
assert.equal(undefined, bot.extra);
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_update() {
|
|
|
|
bot_data.add(test_bot);
|
|
|
|
|
2018-04-18 22:08:00 +02:00
|
|
|
let bot = bot_data.get(43);
|
2014-02-27 00:01:18 +01:00
|
|
|
assert.equal('Bot 1', bot.full_name);
|
2018-01-22 18:36:53 +01:00
|
|
|
bot_data.update(43, {full_name: 'New Bot 1',
|
2018-01-30 18:27:04 +01:00
|
|
|
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);
|
2014-02-27 00:01:18 +01:00
|
|
|
assert.equal('New Bot 1', bot.full_name);
|
2018-01-16 20:40:03 +01:00
|
|
|
assert.equal(2, services[0].interface);
|
|
|
|
assert.equal('http://baz.com', services[0].base_url);
|
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);
|
2018-01-30 19:24:18 +01: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);
|
|
|
|
}());
|
|
|
|
|
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
|
|
|
|
2018-04-22 02:30:07 +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);
|
2014-02-27 00:01:18 +01:00
|
|
|
assert.equal('Bot 1', bot.full_name);
|
2017-02-07 19:48:17 +01:00
|
|
|
assert(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);
|
2014-02-27 00:01:18 +01: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
|
|
|
|
2018-04-22 02:30:07 +02:00
|
|
|
bot_data.add({ ...test_bot, is_active: true });
|
2018-03-08 16:28:37 +01:00
|
|
|
|
|
|
|
bot = bot_data.get(43);
|
|
|
|
assert.equal('Bot 1', bot.full_name);
|
|
|
|
assert(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);
|
|
|
|
}());
|
|
|
|
|
2014-02-27 18:27:31 +01:00
|
|
|
(function test_owner_can_admin() {
|
2018-04-18 22:08:00 +02:00
|
|
|
let bot;
|
2014-02-27 18:27:31 +01:00
|
|
|
|
2018-04-22 02:30:07 +02:00
|
|
|
bot_data.add({owner: 'owner@zulip.com', ...test_bot});
|
2014-02-27 18:27:31 +01:00
|
|
|
|
2018-01-22 18:36:53 +01:00
|
|
|
bot = bot_data.get(43);
|
2014-02-27 18:27:31 +01:00
|
|
|
assert(bot.can_admin);
|
|
|
|
|
2018-04-22 02:30:07 +02:00
|
|
|
bot_data.add({owner: 'notowner@zulip.com', ...test_bot});
|
2014-02-27 18:27:31 +01:00
|
|
|
|
2018-01-22 18:36:53 +01:00
|
|
|
bot = bot_data.get(43);
|
2014-02-27 18:27:31 +01:00
|
|
|
assert.equal(false, bot.can_admin);
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_admin_can_admin() {
|
|
|
|
page_params.is_admin = true;
|
|
|
|
|
|
|
|
bot_data.add(test_bot);
|
|
|
|
|
2018-04-18 22:08:00 +02:00
|
|
|
const bot = bot_data.get(43);
|
2014-02-27 18:27:31 +01:00
|
|
|
assert(bot.can_admin);
|
|
|
|
|
|
|
|
page_params.is_admin = false;
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_get_editable() {
|
2018-04-18 22:08:00 +02:00
|
|
|
let can_admin;
|
2014-02-27 18:27:31 +01:00
|
|
|
|
2018-12-07 22:14:28 +01:00
|
|
|
bot_data.add({...test_bot, user_id: 44, owner: 'owner@zulip.com', is_active: true});
|
2018-04-22 02:30:07 +02:00
|
|
|
bot_data.add({...test_bot, user_id: 45, email: 'bot2@zulip.com', owner: 'owner@zulip.com', is_active: true});
|
|
|
|
bot_data.add({...test_bot, user_id: 46, email: 'bot3@zulip.com', owner: 'not_owner@zulip.com', is_active: true});
|
2014-02-27 18:27:31 +01:00
|
|
|
|
2020-02-09 04:39:08 +01:00
|
|
|
can_admin = bot_data.get_editable().map(bot => bot.email);
|
2016-12-13 22:33:22 +01:00
|
|
|
assert.deepEqual(['bot1@zulip.com', 'bot2@zulip.com'], can_admin);
|
|
|
|
|
|
|
|
page_params.is_admin = true;
|
|
|
|
|
2020-02-09 04:39:08 +01:00
|
|
|
can_admin = bot_data.get_editable().map(bot => bot.email);
|
2016-12-13 22:33:22 +01:00
|
|
|
assert.deepEqual(['bot1@zulip.com', 'bot2@zulip.com'], can_admin);
|
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);
|
|
|
|
assert.equal(bots[0].email, 'bot1@zulip.com');
|
|
|
|
assert.equal(bots[1].email, 'bot2@zulip.com');
|
|
|
|
}());
|
2018-11-16 10:14:34 +01:00
|
|
|
|
|
|
|
(function test_get_bot_owner_email() {
|
|
|
|
let bot_owner_email = bot_data.get_bot_owner_email(test_embedded_bot.user_id);
|
|
|
|
assert.equal('cordelia@zulip.com', bot_owner_email);
|
|
|
|
bot_owner_email = bot_data.get_bot_owner_email(test_bot.user_id);
|
|
|
|
assert.equal(undefined, bot_owner_email);
|
|
|
|
}());
|
2018-06-02 01:37:58 +02:00
|
|
|
});
|