2017-11-08 16:53:44 +01:00
|
|
|
var patched_underscore = _.clone(_);
|
|
|
|
patched_underscore.debounce = function (f) { return f; };
|
|
|
|
global.patch_builtin('_', patched_underscore);
|
2016-06-08 05:54:07 +02:00
|
|
|
|
2017-11-08 16:53:44 +01:00
|
|
|
zrequire('people');
|
|
|
|
zrequire('bot_data');
|
2014-02-27 18:27:31 +01:00
|
|
|
|
2014-03-12 19:31:58 +01:00
|
|
|
set_global('$', function (f) {
|
|
|
|
if (f) {
|
|
|
|
return f();
|
|
|
|
}
|
2014-02-27 18:27:31 +01:00
|
|
|
return {trigger: function () {}};
|
|
|
|
});
|
|
|
|
set_global('document', null);
|
|
|
|
|
|
|
|
var page_params = {
|
2018-01-22 18:36:53 +01:00
|
|
|
realm_bots: [{email: 'bot0@zulip.com', user_id: 42, full_name: 'Bot 0'},
|
|
|
|
{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
|
|
|
is_admin: false,
|
|
|
|
};
|
|
|
|
set_global('page_params', page_params);
|
|
|
|
|
2017-01-19 23:04:52 +01:00
|
|
|
global.people.add({
|
|
|
|
email: 'owner@zulip.com',
|
|
|
|
full_name: 'The Human Boss',
|
|
|
|
user_id: 42,
|
|
|
|
});
|
|
|
|
|
2017-01-20 23:16:28 +01:00
|
|
|
global.people.initialize_current_user(42);
|
2017-01-19 23:04:52 +01:00
|
|
|
|
2017-05-25 04:59:29 +02:00
|
|
|
bot_data.initialize();
|
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
|
|
|
|
2014-02-27 00:01:18 +01:00
|
|
|
(function () {
|
|
|
|
var test_bot = {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
(function test_add() {
|
|
|
|
bot_data.add(test_bot);
|
|
|
|
|
2018-01-22 18:36:53 +01:00
|
|
|
var bot = bot_data.get(43);
|
|
|
|
var 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() {
|
|
|
|
var bot;
|
2018-01-16 20:40:03 +01:00
|
|
|
var services;
|
2014-02-27 00:01:18 +01:00
|
|
|
|
|
|
|
bot_data.add(test_bot);
|
|
|
|
|
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);
|
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);
|
|
|
|
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
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_remove() {
|
|
|
|
var bot;
|
|
|
|
|
2017-02-07 19:48:17 +01:00
|
|
|
bot_data.add(_.extend({}, 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
|
|
|
}());
|
|
|
|
|
2014-02-27 18:27:31 +01:00
|
|
|
(function test_owner_can_admin() {
|
|
|
|
var bot;
|
|
|
|
|
|
|
|
bot_data.add(_.extend({owner: 'owner@zulip.com'}, test_bot));
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
bot_data.add(_.extend({owner: 'notowner@zulip.com'}, test_bot));
|
|
|
|
|
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() {
|
|
|
|
var bot;
|
|
|
|
page_params.is_admin = true;
|
|
|
|
|
|
|
|
bot_data.add(test_bot);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
page_params.is_admin = false;
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_get_editable() {
|
|
|
|
var can_admin;
|
|
|
|
|
2018-01-22 18:36:53 +01:00
|
|
|
bot_data.add(_.extend({}, test_bot, {user_id: 44, owner: 'owner@zulip.com', is_active: true}));
|
|
|
|
bot_data.add(_.extend({}, test_bot, {user_id: 45, email: 'bot2@zulip.com', owner: 'owner@zulip.com', is_active: true}));
|
|
|
|
bot_data.add(_.extend({}, 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
|
|
|
|
|
|
|
can_admin = _.pluck(bot_data.get_editable(), 'email');
|
2016-12-13 22:33:22 +01:00
|
|
|
assert.deepEqual(['bot1@zulip.com', 'bot2@zulip.com'], can_admin);
|
|
|
|
|
|
|
|
page_params.is_admin = true;
|
|
|
|
|
|
|
|
can_admin = _.pluck(bot_data.get_editable(), 'email');
|
|
|
|
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() {
|
|
|
|
var 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');
|
|
|
|
}());
|
2014-02-27 00:01:18 +01:00
|
|
|
}());
|