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-05-10 21:01:33 +02:00
|
|
|
set_global('settings_bots', _settings_bots);
|
|
|
|
|
|
|
|
zrequire('bot_data');
|
|
|
|
zrequire('people');
|
|
|
|
|
|
|
|
const me = {
|
|
|
|
email: 'me@zulip.com',
|
|
|
|
full_name: 'Me Myself',
|
|
|
|
user_id: 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
const fred = {
|
|
|
|
email: 'fred@zulip.com',
|
|
|
|
full_name: 'Fred Frederickson',
|
|
|
|
user_id: 3,
|
|
|
|
};
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(me);
|
|
|
|
people.add_active_user(fred);
|
2020-05-10 21:01:33 +02:00
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
|
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
|
|
|
|
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);
|
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);
|
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
|
|
|
}());
|
|
|
|
|
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]);
|
|
|
|
}());
|
|
|
|
|
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_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});
|
|
|
|
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-05-10 21:07:07 +02:00
|
|
|
const editable_bots = bot_data.get_editable().map(bot => bot.email);
|
2020-03-25 01:21:47 +01:00
|
|
|
assert.deepEqual(['bot1@zulip.com', 'bot2@zulip.com'], editable_bots);
|
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-06-02 01:37:58 +02:00
|
|
|
});
|