2018-07-10 21:21:58 +02:00
|
|
|
set_global('blueslip', global.make_zblueslip());
|
2017-12-19 20:39:56 +01:00
|
|
|
set_global('page_params', {});
|
2017-12-04 02:50:26 +01:00
|
|
|
|
2019-12-29 13:49:29 +01:00
|
|
|
const Dict = zrequire('dict').Dict;
|
2017-12-04 02:50:26 +01:00
|
|
|
zrequire('user_groups');
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('user_groups', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const students = {
|
2017-12-19 20:39:56 +01:00
|
|
|
name: 'Students',
|
|
|
|
id: 0,
|
2018-03-04 17:44:12 +01:00
|
|
|
members: [1, 2],
|
2017-12-19 20:39:56 +01:00
|
|
|
};
|
|
|
|
global.page_params.realm_user_groups = [students];
|
|
|
|
|
|
|
|
user_groups.initialize();
|
|
|
|
assert.equal(user_groups.get_user_group_from_id(students.id), students);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const admins = {
|
2017-12-04 02:50:26 +01:00
|
|
|
name: 'Admins',
|
2018-03-15 15:50:30 +01:00
|
|
|
description: 'foo',
|
2017-12-04 02:50:26 +01:00
|
|
|
id: 1,
|
2018-03-04 17:44:12 +01:00
|
|
|
members: [3],
|
2017-12-04 02:50:26 +01:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const all = {
|
2017-12-04 02:50:26 +01:00
|
|
|
name: 'Everyone',
|
|
|
|
id: 2,
|
2018-03-04 17:44:12 +01:00
|
|
|
members: [1, 2, 3],
|
2017-12-04 02:50:26 +01:00
|
|
|
};
|
2017-12-19 20:39:56 +01:00
|
|
|
|
2017-12-04 02:50:26 +01:00
|
|
|
user_groups.add(admins);
|
|
|
|
assert.equal(user_groups.get_user_group_from_id(admins.id), admins);
|
2017-12-19 20:39:56 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const update_name_event = {
|
2018-03-14 17:52:38 +01:00
|
|
|
group_id: admins.id,
|
|
|
|
data: {
|
|
|
|
name: "new admins",
|
|
|
|
},
|
|
|
|
};
|
2018-03-15 15:50:30 +01:00
|
|
|
user_groups.update(update_name_event);
|
2018-03-14 17:52:38 +01:00
|
|
|
assert.equal(user_groups.get_user_group_from_id(admins.id).name, "new admins");
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const update_des_event = {
|
2018-03-15 15:50:30 +01:00
|
|
|
group_id: admins.id,
|
|
|
|
data: {
|
|
|
|
description: "administer",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
user_groups.update(update_des_event);
|
|
|
|
assert.equal(user_groups.get_user_group_from_id(admins.id).description, "administer");
|
|
|
|
|
2018-07-10 21:21:58 +02:00
|
|
|
blueslip.set_test_data('error', 'Unknown group_id in get_user_group_from_id: ' + all.id);
|
2017-12-04 02:50:26 +01:00
|
|
|
assert.equal(user_groups.get_user_group_from_id(all.id), undefined);
|
2019-02-15 23:38:59 +01:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 1);
|
2018-07-10 21:21:58 +02:00
|
|
|
blueslip.clear_test_data();
|
2017-12-04 02:50:26 +01:00
|
|
|
|
2017-12-19 20:39:56 +01:00
|
|
|
user_groups.remove(students);
|
2018-07-10 21:21:58 +02:00
|
|
|
|
|
|
|
blueslip.set_test_data('error', 'Unknown group_id in get_user_group_from_id: ' + students.id);
|
2017-12-19 20:39:56 +01:00
|
|
|
assert.equal(user_groups.get_user_group_from_id(students.id), undefined);
|
2019-02-15 23:38:59 +01:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 1);
|
2018-07-10 21:21:58 +02:00
|
|
|
blueslip.clear_test_data();
|
2018-03-15 15:50:30 +01:00
|
|
|
|
|
|
|
assert.equal(user_groups.get_user_group_from_name(all.name), undefined);
|
|
|
|
assert.equal(user_groups.get_user_group_from_name(admins.name).id, 1);
|
|
|
|
|
|
|
|
user_groups.add(all);
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_groups_array = user_groups.get_realm_user_groups();
|
2018-03-15 15:50:30 +01:00
|
|
|
assert.equal(user_groups_array.length, 2);
|
|
|
|
assert.equal(user_groups_array[1].name, 'Everyone');
|
|
|
|
assert.equal(user_groups_array[0].name, 'new admins');
|
|
|
|
|
|
|
|
assert(!user_groups.is_member_of(admins.id, 4));
|
|
|
|
assert(user_groups.is_member_of(admins.id, 3));
|
|
|
|
|
|
|
|
user_groups.add_members(all.id, [5, 4]);
|
|
|
|
assert.deepEqual(user_groups.get_user_group_from_id(all.id).members,
|
2018-05-07 03:30:13 +02:00
|
|
|
Dict.from_array([1, 2, 3, 5, 4]));
|
2018-03-15 15:50:30 +01:00
|
|
|
|
|
|
|
user_groups.remove_members(all.id, [1, 4]);
|
|
|
|
assert.deepEqual(user_groups.get_user_group_from_id(all.id).members,
|
2018-05-07 03:30:13 +02:00
|
|
|
Dict.from_array([2, 3, 5]));
|
2018-03-15 15:50:30 +01:00
|
|
|
|
|
|
|
assert(user_groups.is_user_group(admins));
|
2019-11-02 00:06:25 +01:00
|
|
|
const object = {
|
2018-03-15 15:50:30 +01:00
|
|
|
name: 'core',
|
|
|
|
id: 3,
|
|
|
|
};
|
|
|
|
assert(!user_groups.is_user_group(object));
|
|
|
|
|
|
|
|
user_groups.init();
|
|
|
|
assert.equal(user_groups.get_realm_user_groups().length, 0);
|
2019-01-15 00:39:03 +01:00
|
|
|
|
|
|
|
blueslip.set_test_data('error', 'Could not find user group with ID -1');
|
|
|
|
assert.equal(user_groups.is_member_of(-1, 15), false);
|
2019-02-15 23:38:59 +01:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 1);
|
2019-01-15 00:39:03 +01:00
|
|
|
blueslip.clear_test_data();
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|