2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
const {zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-03-16 23:38:59 +01:00
|
|
|
const blueslip = require("../zjsunit/zblueslip");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const user_groups = zrequire("user_groups");
|
2017-12-04 02:50:26 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("user_groups", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const students = {
|
2021-08-03 18:26:39 +02:00
|
|
|
description: "Students group",
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Students",
|
2017-12-19 20:39:56 +01:00
|
|
|
id: 0,
|
2021-08-03 18:26:39 +02:00
|
|
|
members: new Set([1, 2]),
|
2017-12-19 20:39:56 +01:00
|
|
|
};
|
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
const params = {};
|
|
|
|
params.realm_user_groups = [students];
|
2021-06-01 14:49:13 +02:00
|
|
|
const user_id_not_in_any_group = 0;
|
|
|
|
const user_id_part_of_a_group = 2;
|
2020-02-25 12:16:26 +01:00
|
|
|
|
|
|
|
user_groups.initialize(params);
|
2021-08-03 18:26:39 +02:00
|
|
|
assert.deepEqual(user_groups.get_user_group_from_id(students.id), students);
|
2017-12-19 20:39:56 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const admins = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Admins",
|
|
|
|
description: "foo",
|
2017-12-04 02:50:26 +01:00
|
|
|
id: 1,
|
2021-08-03 18:26:39 +02:00
|
|
|
members: new Set([3]),
|
2017-12-04 02:50:26 +01:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const all = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Everyone",
|
2017-12-04 02:50:26 +01:00
|
|
|
id: 2,
|
2021-08-03 18:26:39 +02:00
|
|
|
members: new Set([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);
|
2021-08-03 18:26:39 +02:00
|
|
|
assert.deepEqual(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");
|
|
|
|
|
2021-08-03 18:21:15 +02:00
|
|
|
assert.throws(() => user_groups.get_user_group_from_id(all.id), {
|
|
|
|
name: "Error",
|
|
|
|
message: "Unknown group_id in get_user_group_from_id: 2",
|
|
|
|
});
|
2017-12-19 20:39:56 +01:00
|
|
|
user_groups.remove(students);
|
2018-07-10 21:21:58 +02:00
|
|
|
|
2021-08-03 18:21:15 +02:00
|
|
|
assert.throws(() => user_groups.get_user_group_from_id(students.id), {
|
|
|
|
name: "Error",
|
|
|
|
message: "Unknown group_id in get_user_group_from_id: 0",
|
|
|
|
});
|
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);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(user_groups_array[1].name, "Everyone");
|
|
|
|
assert.equal(user_groups_array[0].name, "new admins");
|
2018-03-15 15:50:30 +01:00
|
|
|
|
2021-06-01 14:49:13 +02:00
|
|
|
const groups_of_users = user_groups.get_user_groups_of_user(user_id_part_of_a_group);
|
|
|
|
assert.equal(groups_of_users.length, 1);
|
|
|
|
assert.equal(groups_of_users[0].name, "Everyone");
|
|
|
|
|
|
|
|
const groups_of_users_nomatch = user_groups.get_user_groups_of_user(user_id_not_in_any_group);
|
|
|
|
assert.equal(groups_of_users_nomatch.length, 0);
|
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!user_groups.is_member_of(admins.id, 4));
|
|
|
|
assert.ok(user_groups.is_member_of(admins.id, 3));
|
2018-03-15 15:50:30 +01:00
|
|
|
|
|
|
|
user_groups.add_members(all.id, [5, 4]);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(user_groups.get_user_group_from_id(all.id).members, new Set([1, 2, 3, 5, 4]));
|
2018-03-15 15:50:30 +01:00
|
|
|
|
|
|
|
user_groups.remove_members(all.id, [1, 4]);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(user_groups.get_user_group_from_id(all.id).members, new Set([2, 3, 5]));
|
2018-03-15 15:50:30 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(user_groups.is_user_group(admins));
|
2019-11-02 00:06:25 +01:00
|
|
|
const object = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "core",
|
2018-03-15 15:50:30 +01:00
|
|
|
id: 3,
|
|
|
|
};
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!user_groups.is_user_group(object));
|
2018-03-15 15:50:30 +01:00
|
|
|
|
|
|
|
user_groups.init();
|
|
|
|
assert.equal(user_groups.get_realm_user_groups().length, 0);
|
2019-01-15 00:39:03 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Could not find user group with ID -1");
|
2019-01-15 00:39:03 +01:00
|
|
|
assert.equal(user_groups.is_member_of(-1, 15), false);
|
2021-08-03 18:26:39 +02:00
|
|
|
|
|
|
|
blueslip.expect("error", "Could not find user group with ID -9999", 2);
|
|
|
|
user_groups.add_members(-9999);
|
|
|
|
user_groups.remove_members(-9999);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|