2016-12-07 18:38:59 +01:00
|
|
|
set_global("page_params", {
|
|
|
|
realm_uri: "https://chat.example.com",
|
2018-05-07 03:30:13 +02:00
|
|
|
realm_embedded_bots: [
|
|
|
|
{name: "converter", config: {}},
|
|
|
|
{name:"giphy", config: {key: "12345678"}},
|
|
|
|
{name:"foobot", config: {bar: "baz", qux: "quux"}},
|
|
|
|
],
|
2018-06-01 11:27:02 +02:00
|
|
|
realm_bots: [{api_key: 'QadL788EkiottHmukyhHgePUFHREiu8b',
|
|
|
|
email: 'error-bot@zulip.org',
|
|
|
|
full_name: 'Error bot',
|
|
|
|
user_id: 1},
|
|
|
|
],
|
2016-12-07 18:38:59 +01:00
|
|
|
});
|
|
|
|
|
2017-06-20 00:32:20 +02:00
|
|
|
set_global("avatar", {});
|
|
|
|
|
|
|
|
set_global('$', global.make_zjquery());
|
2018-03-29 22:11:32 +02:00
|
|
|
set_global('i18n', global.stub_i18n);
|
2017-06-20 00:32:20 +02:00
|
|
|
|
2017-11-08 18:50:24 +01:00
|
|
|
zrequire('bot_data');
|
|
|
|
zrequire('settings_bots');
|
2018-01-07 19:24:14 +01:00
|
|
|
zrequire('Handlebars', 'handlebars');
|
2018-06-01 11:27:02 +02:00
|
|
|
zrequire('people');
|
2018-01-07 19:24:14 +01:00
|
|
|
zrequire('templates');
|
2016-12-07 18:38:59 +01:00
|
|
|
|
2018-06-27 09:35:30 +02:00
|
|
|
set_global('ClipboardJS', function (sel) {
|
|
|
|
assert.equal(sel, '#copy_zuliprc');
|
|
|
|
});
|
|
|
|
|
2018-06-01 11:27:02 +02:00
|
|
|
bot_data.initialize();
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('generate_zuliprc_uri', () => {
|
2018-06-01 11:27:02 +02:00
|
|
|
var uri = settings_bots.generate_zuliprc_uri(1);
|
2016-12-07 18:38:59 +01:00
|
|
|
var 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"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(uri, expected);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-12-07 18:38:59 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('generate_zuliprc_content', () => {
|
2016-12-07 18:38:59 +01:00
|
|
|
var user = {
|
|
|
|
email: "admin12@chatting.net",
|
2017-01-12 00:17:43 +01:00
|
|
|
api_key: "nSlA0mUm7G42LP85lMv7syqFTzDE2q34",
|
2016-12-07 18:38:59 +01:00
|
|
|
};
|
2017-04-06 22:47:27 +02:00
|
|
|
var content = settings_bots.generate_zuliprc_content(user.email, user.api_key);
|
2016-12-07 18:38:59 +01:00
|
|
|
var expected = "[api]\nemail=admin12@chatting.net\n" +
|
|
|
|
"key=nSlA0mUm7G42LP85lMv7syqFTzDE2q34\n" +
|
|
|
|
"site=https://chat.example.com\n";
|
|
|
|
|
|
|
|
assert.equal(content, expected);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-05-30 16:12:02 +02:00
|
|
|
|
2018-05-29 10:18:06 +02:00
|
|
|
run_test('generate_botserverrc_content', () => {
|
2017-05-30 16:12:02 +02:00
|
|
|
var user = {
|
|
|
|
email: "vabstest-bot@zulip.com",
|
|
|
|
api_key: "nSlA0mUm7G42LP85lMv7syqFTzDE2q34",
|
|
|
|
};
|
2018-05-30 11:09:35 +02:00
|
|
|
var service = {
|
|
|
|
token: "abcd1234",
|
|
|
|
};
|
|
|
|
var content = settings_bots.generate_botserverrc_content(user.email,
|
|
|
|
user.api_key,
|
|
|
|
service.token);
|
2018-05-23 16:28:41 +02:00
|
|
|
var expected = "[]\nemail=vabstest-bot@zulip.com\n" +
|
2017-05-30 16:12:02 +02:00
|
|
|
"key=nSlA0mUm7G42LP85lMv7syqFTzDE2q34\n" +
|
2018-05-30 11:09:35 +02:00
|
|
|
"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) {
|
|
|
|
var create_payload_url = $('#create_payload_url');
|
|
|
|
var payload_url_inputbox = $('#payload_url_inputbox');
|
2018-01-07 19:24:14 +01:00
|
|
|
var config_inputbox = $('#config_inputbox');
|
2017-07-15 18:23:44 +02:00
|
|
|
var EMBEDDED_BOT_TYPE = '4';
|
2017-06-24 22:38:27 +02:00
|
|
|
var OUTGOING_WEBHOOK_BOT_TYPE = '3';
|
|
|
|
var GENERIC_BOT_TYPE = '1';
|
|
|
|
|
2017-07-15 18:23:44 +02:00
|
|
|
$('#create_bot_type :selected').val(EMBEDDED_BOT_TYPE);
|
|
|
|
f.apply();
|
|
|
|
assert(!create_payload_url.hasClass('required'));
|
|
|
|
assert(!payload_url_inputbox.visible());
|
|
|
|
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
|
|
|
|
2017-06-24 22:38:27 +02:00
|
|
|
$('#create_bot_type :selected').val(OUTGOING_WEBHOOK_BOT_TYPE);
|
|
|
|
f.apply();
|
|
|
|
assert(create_payload_url.hasClass('required'));
|
|
|
|
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
|
|
|
|
|
|
|
$('#create_bot_type :selected').val(GENERIC_BOT_TYPE);
|
|
|
|
f.apply();
|
2018-06-06 18:19:09 +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
|
|
|
|
2018-06-12 20:58:57 +02:00
|
|
|
function set_up() {
|
|
|
|
set_global('$', global.make_zjquery());
|
|
|
|
|
2017-06-20 00:32:20 +02:00
|
|
|
// bunch of stubs
|
|
|
|
|
2018-06-12 20:51:31 +02:00
|
|
|
$.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
|
|
|
|
2018-06-12 20:51:31 +02:00
|
|
|
$('#create_bot_type').on = (action, f) => {
|
2017-06-24 22:38:27 +02:00
|
|
|
if (action === 'change') {
|
|
|
|
test_create_bot_type_input_box_toggle(f);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-12 20:51:31 +02:00
|
|
|
$('#config_inputbox').children = () => {
|
2018-01-07 19:24:14 +01:00
|
|
|
var mock_children = {
|
2018-06-12 20:51:31 +02:00
|
|
|
hide: () => {
|
2018-01-07 19:24:14 +01:00
|
|
|
return;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return mock_children;
|
|
|
|
};
|
|
|
|
global.compile_template('embedded_bot_config_item');
|
2018-06-12 20:51:31 +02:00
|
|
|
avatar.build_bot_create_widget = () => {};
|
|
|
|
avatar.build_bot_edit_widget = () => {};
|
2017-06-20 00:32:20 +02:00
|
|
|
|
|
|
|
settings_bots.set_up();
|
2018-06-12 20:58:57 +02:00
|
|
|
}
|
2017-06-20 00:32:20 +02:00
|
|
|
|
2018-06-12 20:58:57 +02:00
|
|
|
run_test('set_up', () => {
|
|
|
|
set_up();
|
|
|
|
});
|
2018-06-12 21:42:43 +02:00
|
|
|
|
|
|
|
run_test('test tab clicks', () => {
|
|
|
|
set_up();
|
|
|
|
|
|
|
|
function click_on_tab(tab_elem) {
|
|
|
|
var e = {
|
|
|
|
preventDefault: () => {},
|
|
|
|
stopPropagation: () => {},
|
|
|
|
};
|
|
|
|
tab_elem.click(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
const tabs = {
|
|
|
|
add: $('#bots_lists_navbar .add-a-new-bot-tab'),
|
|
|
|
active: $('#bots_lists_navbar .active-bots-tab'),
|
|
|
|
inactive: $('#bots_lists_navbar .inactive-bots-tab'),
|
|
|
|
};
|
|
|
|
|
|
|
|
$('#bots_lists_navbar .active').removeClass = (cls) => {
|
|
|
|
assert.equal(cls, 'active');
|
|
|
|
_.each(tabs, (tab) => {
|
|
|
|
tab.removeClass('active');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const forms = {
|
|
|
|
add: $('#add-a-new-bot-form'),
|
|
|
|
active: $('#active_bots_list'),
|
|
|
|
inactive: $('#inactive_bots_list'),
|
|
|
|
};
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
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());
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
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());
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
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-13 16:59:15 +02:00
|
|
|
|
|
|
|
run_test('can_create_new_bots', () => {
|
|
|
|
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());
|
|
|
|
});
|