2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const blueslip = require("./lib/zblueslip");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const user_groups = zrequire("user_groups");
|
2024-10-09 22:44:13 +02:00
|
|
|
const {set_realm} = zrequire("state_data");
|
|
|
|
|
|
|
|
const realm = {};
|
|
|
|
set_realm(realm);
|
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",
|
2024-06-04 12:36:52 +02:00
|
|
|
creator_id: null,
|
|
|
|
date_created: 1596710000,
|
2017-12-19 20:39:56 +01:00
|
|
|
id: 0,
|
2021-08-03 18:26:39 +02:00
|
|
|
members: new Set([1, 2]),
|
2021-09-16 20:54:12 +02:00
|
|
|
is_system_group: false,
|
2022-05-16 17:02:44 +02:00
|
|
|
direct_subgroup_ids: new Set([4, 5]),
|
2024-10-08 16:37:34 +02:00
|
|
|
can_add_members_group: 1,
|
2024-10-02 13:13:47 +02:00
|
|
|
can_join_group: 1,
|
2024-10-14 13:29:10 +02:00
|
|
|
can_leave_group: 1,
|
2023-08-04 15:45:37 +02:00
|
|
|
can_manage_group: 1,
|
2023-07-14 06:50:33 +02:00
|
|
|
can_mention_group: 2,
|
2024-09-12 13:51:13 +02:00
|
|
|
deactivated: false,
|
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",
|
2024-06-04 12:36:52 +02:00
|
|
|
creator_id: null,
|
|
|
|
date_created: 1596710000,
|
2017-12-04 02:50:26 +01:00
|
|
|
id: 1,
|
2021-08-03 18:26:39 +02:00
|
|
|
members: new Set([3]),
|
2021-09-16 20:54:12 +02:00
|
|
|
is_system_group: false,
|
2022-05-16 17:02:44 +02:00
|
|
|
direct_subgroup_ids: new Set([]),
|
2024-10-08 16:37:34 +02:00
|
|
|
can_add_members_group: 1,
|
2024-10-02 13:13:47 +02:00
|
|
|
can_join_group: 1,
|
2024-10-14 13:29:10 +02:00
|
|
|
can_leave_group: 1,
|
2023-08-04 15:45:37 +02:00
|
|
|
can_manage_group: 1,
|
2023-07-14 06:50:33 +02:00
|
|
|
can_mention_group: 2,
|
2024-09-12 13:51:13 +02:00
|
|
|
deactivated: false,
|
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]),
|
2021-09-16 20:54:12 +02:00
|
|
|
is_system_group: false,
|
2022-05-16 17:02:44 +02:00
|
|
|
direct_subgroup_ids: new Set([4, 5, 6]),
|
2024-10-02 13:13:47 +02:00
|
|
|
can_join_group: 1,
|
2024-10-14 13:29:10 +02:00
|
|
|
can_leave_group: 1,
|
2023-08-04 15:45:37 +02:00
|
|
|
can_manage_group: 1,
|
2023-07-14 06:50:33 +02:00
|
|
|
can_mention_group: 1,
|
2024-09-12 13:51:13 +02:00
|
|
|
deactivated: false,
|
|
|
|
};
|
|
|
|
const deactivated_group = {
|
|
|
|
name: "Deactivated test group",
|
|
|
|
id: 3,
|
|
|
|
members: new Set([1, 2, 3]),
|
|
|
|
is_system_group: false,
|
|
|
|
direct_subgroup_ids: new Set([4, 5, 6]),
|
2024-10-02 13:13:47 +02:00
|
|
|
can_join_group: 1,
|
2024-10-14 13:29:10 +02:00
|
|
|
can_leave_group: 1,
|
2024-09-12 13:51:13 +02:00
|
|
|
can_manage_group: 1,
|
|
|
|
can_mention_group: 1,
|
|
|
|
deactivated: true,
|
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
|
|
|
|
2024-02-09 14:34:09 +01:00
|
|
|
assert.equal(user_groups.maybe_get_user_group_from_id(99), undefined);
|
|
|
|
assert.deepEqual(user_groups.get_user_group_from_id(admins.id), admins);
|
|
|
|
|
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);
|
2023-12-10 08:43:23 +01:00
|
|
|
assert.equal(user_groups.get_user_group_from_name("new admins").id, 1);
|
2018-03-15 15:50:30 +01:00
|
|
|
|
|
|
|
user_groups.add(all);
|
2024-09-12 13:51:13 +02:00
|
|
|
user_groups.add(deactivated_group);
|
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
|
|
|
|
2024-09-12 13:51:13 +02:00
|
|
|
const all_user_groups_array = user_groups.get_realm_user_groups(true);
|
|
|
|
assert.equal(all_user_groups_array.length, 3);
|
|
|
|
assert.equal(all_user_groups_array[2].name, "Deactivated test group");
|
|
|
|
assert.equal(all_user_groups_array[1].name, "Everyone");
|
|
|
|
assert.equal(all_user_groups_array[0].name, "new admins");
|
|
|
|
|
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);
|
|
|
|
|
2022-05-05 10:04:57 +02:00
|
|
|
assert.ok(!user_groups.is_direct_member_of(4, admins.id));
|
|
|
|
assert.ok(user_groups.is_direct_member_of(3, admins.id));
|
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
|
|
|
|
2022-05-02 16:41:51 +02:00
|
|
|
user_groups.add_subgroups(all.id, [2, 3]);
|
|
|
|
assert.deepEqual(
|
2022-05-16 17:02:44 +02:00
|
|
|
user_groups.get_user_group_from_id(all.id).direct_subgroup_ids,
|
2022-05-02 16:41:51 +02:00
|
|
|
new Set([2, 3, 5, 4, 6]),
|
|
|
|
);
|
|
|
|
|
|
|
|
user_groups.remove_subgroups(all.id, [2, 4]);
|
2022-05-16 17:02:44 +02:00
|
|
|
assert.deepEqual(
|
|
|
|
user_groups.get_user_group_from_id(all.id).direct_subgroup_ids,
|
|
|
|
new Set([3, 5, 6]),
|
|
|
|
);
|
2022-05-02 16:41:51 +02: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
|
|
|
|
2024-09-12 15:24:30 +02:00
|
|
|
const update_deactivated_event = {
|
|
|
|
group_id: admins.id,
|
|
|
|
data: {
|
|
|
|
deactivated: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
user_groups.update(update_deactivated_event);
|
|
|
|
assert.ok(user_groups.get_user_group_from_id(admins.id).deactivated);
|
|
|
|
|
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
|
|
|
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.expect("error", "Could not find user group", 5);
|
2022-05-05 10:04:57 +02:00
|
|
|
assert.equal(user_groups.is_direct_member_of(15, -1), false);
|
2021-08-03 18:26:39 +02:00
|
|
|
user_groups.add_members(-9999);
|
|
|
|
user_groups.remove_members(-9999);
|
2022-05-02 16:41:51 +02:00
|
|
|
user_groups.add_subgroups(-9999);
|
|
|
|
user_groups.remove_subgroups(-9999);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2022-05-04 13:16:50 +02:00
|
|
|
|
|
|
|
run_test("get_recursive_subgroups", () => {
|
|
|
|
const admins = {
|
|
|
|
name: "Admins",
|
|
|
|
description: "foo",
|
|
|
|
id: 1,
|
|
|
|
members: new Set([1]),
|
|
|
|
is_system_group: false,
|
2022-05-16 17:02:44 +02:00
|
|
|
direct_subgroup_ids: new Set([4]),
|
2022-05-04 13:16:50 +02:00
|
|
|
};
|
|
|
|
const all = {
|
|
|
|
name: "Everyone",
|
|
|
|
id: 2,
|
|
|
|
members: new Set([2, 3]),
|
|
|
|
is_system_group: false,
|
2022-05-16 17:02:44 +02:00
|
|
|
direct_subgroup_ids: new Set([1, 3]),
|
2022-05-04 13:16:50 +02:00
|
|
|
};
|
|
|
|
const test = {
|
|
|
|
name: "Test",
|
|
|
|
id: 3,
|
|
|
|
members: new Set([3, 4, 5]),
|
|
|
|
is_system_group: false,
|
2022-05-16 17:02:44 +02:00
|
|
|
direct_subgroup_ids: new Set([2]),
|
2022-05-04 13:16:50 +02:00
|
|
|
};
|
|
|
|
const foo = {
|
|
|
|
name: "Foo",
|
|
|
|
id: 4,
|
|
|
|
members: new Set([6, 7]),
|
|
|
|
is_system_group: false,
|
2022-05-16 17:02:44 +02:00
|
|
|
direct_subgroup_ids: new Set([]),
|
2022-05-04 13:16:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
user_groups.add(admins);
|
|
|
|
user_groups.add(all);
|
|
|
|
user_groups.add(test);
|
|
|
|
user_groups.add(foo);
|
|
|
|
|
|
|
|
// This test setup has a state that won't appear in real data: Groups 2 and 3
|
|
|
|
// each contain the other. We test this corner case because it is a simple way
|
|
|
|
// to verify whether our algorithm correctly avoids visiting groups multiple times
|
|
|
|
// when determining recursive subgroups.
|
|
|
|
// A test case that can occur in practice and would be problematic without this
|
|
|
|
// optimization is a tree where each layer connects to every node in the next layer.
|
2022-05-17 16:17:04 +02:00
|
|
|
assert.deepEqual(user_groups.get_recursive_subgroups(admins), new Set([4]));
|
|
|
|
assert.deepEqual(user_groups.get_recursive_subgroups(all), new Set([4, 1, 2, 3]));
|
|
|
|
assert.deepEqual(user_groups.get_recursive_subgroups(test), new Set([2, 4, 1, 3]));
|
|
|
|
assert.deepEqual(user_groups.get_recursive_subgroups(foo), new Set([]));
|
2022-05-04 13:16:50 +02:00
|
|
|
|
|
|
|
user_groups.add_subgroups(foo.id, [9999]);
|
2022-05-17 16:17:04 +02:00
|
|
|
const foo_group = user_groups.get_user_group_from_id(foo.id);
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.expect("error", "Could not find subgroup", 2);
|
2022-05-17 16:17:04 +02:00
|
|
|
assert.deepEqual(user_groups.get_recursive_subgroups(foo_group), undefined);
|
|
|
|
assert.deepEqual(user_groups.get_recursive_subgroups(test), undefined);
|
2022-05-04 13:16:50 +02:00
|
|
|
});
|
2022-05-02 11:55:30 +02:00
|
|
|
|
2024-07-08 17:34:16 +02:00
|
|
|
run_test("get_recursive_group_members", () => {
|
|
|
|
const admins = {
|
|
|
|
name: "Admins",
|
|
|
|
description: "foo",
|
|
|
|
id: 1,
|
|
|
|
members: new Set([1]),
|
|
|
|
is_system_group: false,
|
|
|
|
direct_subgroup_ids: new Set([4]),
|
|
|
|
};
|
|
|
|
const all = {
|
|
|
|
name: "Everyone",
|
|
|
|
id: 2,
|
|
|
|
members: new Set([2, 3]),
|
|
|
|
is_system_group: false,
|
|
|
|
direct_subgroup_ids: new Set([1, 3]),
|
|
|
|
};
|
|
|
|
const test = {
|
|
|
|
name: "Test",
|
|
|
|
id: 3,
|
|
|
|
members: new Set([3, 4, 5]),
|
|
|
|
is_system_group: false,
|
|
|
|
direct_subgroup_ids: new Set([2]),
|
|
|
|
};
|
|
|
|
const foo = {
|
|
|
|
name: "Foo",
|
|
|
|
id: 4,
|
|
|
|
members: new Set([6, 7]),
|
|
|
|
is_system_group: false,
|
|
|
|
direct_subgroup_ids: new Set([]),
|
|
|
|
};
|
|
|
|
|
|
|
|
user_groups.add(admins);
|
|
|
|
user_groups.add(all);
|
|
|
|
user_groups.add(test);
|
|
|
|
user_groups.add(foo);
|
|
|
|
|
|
|
|
// This test setup has a state that won't appear in real data: Groups 2 and 3
|
|
|
|
// each contain the other. We test this corner case because it is a simple way
|
|
|
|
// to verify whether our algorithm correctly avoids visiting groups multiple times
|
|
|
|
// when determining recursive subgroups.
|
|
|
|
// A test case that can occur in practice and would be problematic without this
|
|
|
|
// optimization is a tree where each layer connects to every node in the next layer.
|
|
|
|
assert.deepEqual([...user_groups.get_recursive_group_members(admins)].sort(), [1, 6, 7]);
|
|
|
|
assert.deepEqual(
|
|
|
|
[...user_groups.get_recursive_group_members(all)].sort(),
|
|
|
|
[1, 2, 3, 4, 5, 6, 7],
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
[...user_groups.get_recursive_group_members(test)].sort(),
|
|
|
|
[1, 2, 3, 4, 5, 6, 7],
|
|
|
|
);
|
|
|
|
assert.deepEqual([...user_groups.get_recursive_group_members(foo)].sort(), [6, 7]);
|
|
|
|
|
|
|
|
user_groups.add_subgroups(foo.id, [9999]);
|
|
|
|
const foo_group = user_groups.get_user_group_from_id(foo.id);
|
|
|
|
blueslip.expect("error", "Could not find subgroup", 2);
|
|
|
|
assert.deepEqual([...user_groups.get_recursive_group_members(foo_group)].sort(), [6, 7]);
|
|
|
|
assert.deepEqual([...user_groups.get_recursive_group_members(test)].sort(), [3, 4, 5]);
|
|
|
|
});
|
|
|
|
|
2022-05-02 11:55:30 +02:00
|
|
|
run_test("is_user_in_group", () => {
|
|
|
|
const admins = {
|
|
|
|
name: "Admins",
|
|
|
|
id: 1,
|
|
|
|
members: new Set([1]),
|
|
|
|
is_system_group: false,
|
2022-05-16 17:02:44 +02:00
|
|
|
direct_subgroup_ids: new Set([4]),
|
2022-05-02 11:55:30 +02:00
|
|
|
};
|
|
|
|
const all = {
|
|
|
|
name: "Everyone",
|
|
|
|
id: 2,
|
|
|
|
members: new Set([2, 3]),
|
|
|
|
is_system_group: false,
|
2022-05-16 17:02:44 +02:00
|
|
|
direct_subgroup_ids: new Set([1, 3]),
|
2022-05-02 11:55:30 +02:00
|
|
|
};
|
|
|
|
const test = {
|
|
|
|
name: "Test",
|
|
|
|
id: 3,
|
|
|
|
members: new Set([4, 5]),
|
|
|
|
is_system_group: false,
|
2022-05-16 17:02:44 +02:00
|
|
|
direct_subgroup_ids: new Set([1]),
|
2022-05-02 11:55:30 +02:00
|
|
|
};
|
|
|
|
const foo = {
|
|
|
|
name: "Foo",
|
|
|
|
id: 4,
|
|
|
|
members: new Set([6, 7]),
|
|
|
|
is_system_group: false,
|
2022-05-16 17:02:44 +02:00
|
|
|
direct_subgroup_ids: new Set([]),
|
2022-05-02 11:55:30 +02:00
|
|
|
};
|
|
|
|
user_groups.add(admins);
|
|
|
|
user_groups.add(all);
|
|
|
|
user_groups.add(test);
|
|
|
|
user_groups.add(foo);
|
|
|
|
|
|
|
|
assert.equal(user_groups.is_user_in_group(admins.id, 1), true);
|
|
|
|
assert.equal(user_groups.is_user_in_group(admins.id, 6), true);
|
|
|
|
assert.equal(user_groups.is_user_in_group(admins.id, 3), false);
|
|
|
|
|
|
|
|
assert.equal(user_groups.is_user_in_group(all.id, 2), true);
|
|
|
|
assert.equal(user_groups.is_user_in_group(all.id, 1), true);
|
|
|
|
assert.equal(user_groups.is_user_in_group(all.id, 6), true);
|
|
|
|
|
|
|
|
assert.equal(user_groups.is_user_in_group(test.id, 4), true);
|
|
|
|
assert.equal(user_groups.is_user_in_group(test.id, 1), true);
|
|
|
|
assert.equal(user_groups.is_user_in_group(test.id, 6), true);
|
|
|
|
assert.equal(user_groups.is_user_in_group(test.id, 3), false);
|
|
|
|
|
|
|
|
assert.equal(user_groups.is_user_in_group(foo.id, 6), true);
|
|
|
|
assert.equal(user_groups.is_user_in_group(foo.id, 3), false);
|
|
|
|
|
2024-08-28 16:22:27 +02:00
|
|
|
assert.equal(user_groups.is_user_in_setting_group(test.id, 4), true);
|
|
|
|
assert.equal(user_groups.is_user_in_setting_group(test.id, 1), true);
|
|
|
|
assert.equal(user_groups.is_user_in_setting_group(test.id, 6), true);
|
|
|
|
assert.equal(user_groups.is_user_in_setting_group(test.id, 3), false);
|
|
|
|
|
|
|
|
const anonymous_setting_group = {
|
|
|
|
direct_members: [8, 9],
|
|
|
|
direct_subgroups: [admins.id, test.id],
|
|
|
|
};
|
|
|
|
assert.equal(user_groups.is_user_in_setting_group(anonymous_setting_group, 8), true);
|
|
|
|
assert.equal(user_groups.is_user_in_setting_group(anonymous_setting_group, 9), true);
|
|
|
|
assert.equal(user_groups.is_user_in_setting_group(anonymous_setting_group, 10), false);
|
|
|
|
|
|
|
|
assert.equal(user_groups.is_user_in_setting_group(anonymous_setting_group, 1), true);
|
|
|
|
assert.equal(user_groups.is_user_in_setting_group(anonymous_setting_group, 4), true);
|
|
|
|
assert.equal(user_groups.is_user_in_setting_group(anonymous_setting_group, 6), true);
|
|
|
|
assert.equal(user_groups.is_user_in_setting_group(anonymous_setting_group, 2), false);
|
|
|
|
assert.equal(user_groups.is_user_in_setting_group(anonymous_setting_group, 3), false);
|
|
|
|
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.expect("error", "Could not find user group");
|
2022-05-02 11:55:30 +02:00
|
|
|
assert.equal(user_groups.is_user_in_group(1111, 3), false);
|
|
|
|
|
|
|
|
user_groups.add_subgroups(foo.id, [9999]);
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.expect("error", "Could not find subgroup");
|
2022-05-02 11:55:30 +02:00
|
|
|
assert.equal(user_groups.is_user_in_group(admins.id, 6), false);
|
|
|
|
});
|
2023-01-09 12:04:10 +01:00
|
|
|
|
2024-10-10 00:50:57 +02:00
|
|
|
run_test("get_realm_user_groups_for_dropdown_list_widget", ({override}) => {
|
2023-04-10 12:59:48 +02:00
|
|
|
const nobody = {
|
2023-07-03 09:18:44 +02:00
|
|
|
name: "role:nobody",
|
2023-04-10 12:59:48 +02:00
|
|
|
description: "foo",
|
|
|
|
id: 1,
|
|
|
|
members: new Set([]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([]),
|
|
|
|
};
|
2023-01-09 12:04:10 +01:00
|
|
|
const owners = {
|
2023-07-03 09:18:44 +02:00
|
|
|
name: "role:owners",
|
2023-01-09 12:04:10 +01:00
|
|
|
description: "foo",
|
2023-04-10 12:59:48 +02:00
|
|
|
id: 2,
|
2023-01-09 12:04:10 +01:00
|
|
|
members: new Set([1]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([]),
|
|
|
|
};
|
|
|
|
const admins = {
|
2023-07-03 09:18:44 +02:00
|
|
|
name: "role:administrators",
|
2023-01-09 12:04:10 +01:00
|
|
|
description: "foo",
|
2023-04-10 12:59:48 +02:00
|
|
|
id: 3,
|
2023-01-09 12:04:10 +01:00
|
|
|
members: new Set([2]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([1]),
|
|
|
|
};
|
|
|
|
const moderators = {
|
2023-07-03 09:18:44 +02:00
|
|
|
name: "role:moderators",
|
2023-01-09 12:04:10 +01:00
|
|
|
description: "foo",
|
2023-04-10 12:59:48 +02:00
|
|
|
id: 4,
|
2023-01-09 12:04:10 +01:00
|
|
|
members: new Set([3]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([2]),
|
|
|
|
};
|
|
|
|
const members = {
|
2023-07-03 09:18:44 +02:00
|
|
|
name: "role:members",
|
2023-01-09 12:04:10 +01:00
|
|
|
description: "foo",
|
2023-04-10 12:59:48 +02:00
|
|
|
id: 5,
|
2023-01-09 12:04:10 +01:00
|
|
|
members: new Set([4]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([6]),
|
|
|
|
};
|
|
|
|
const everyone = {
|
2023-07-03 09:18:44 +02:00
|
|
|
name: "role:everyone",
|
2023-01-09 12:04:10 +01:00
|
|
|
description: "foo",
|
2023-04-10 12:59:48 +02:00
|
|
|
id: 6,
|
2023-01-09 12:04:10 +01:00
|
|
|
members: new Set([]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([4]),
|
|
|
|
};
|
|
|
|
const full_members = {
|
2023-07-03 09:18:44 +02:00
|
|
|
name: "role:fullmembers",
|
2023-01-09 12:04:10 +01:00
|
|
|
description: "foo",
|
2023-04-10 12:59:48 +02:00
|
|
|
id: 7,
|
2023-01-09 12:04:10 +01:00
|
|
|
members: new Set([5]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([3]),
|
|
|
|
};
|
|
|
|
const internet = {
|
2023-07-03 09:18:44 +02:00
|
|
|
name: "role:internet",
|
2023-04-10 12:59:48 +02:00
|
|
|
id: 8,
|
2023-01-09 12:04:10 +01:00
|
|
|
members: new Set([]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([5]),
|
|
|
|
};
|
2023-02-17 08:20:12 +01:00
|
|
|
const students = {
|
|
|
|
description: "Students group",
|
|
|
|
name: "Students",
|
2023-04-10 12:59:48 +02:00
|
|
|
id: 9,
|
2023-02-17 08:20:12 +01:00
|
|
|
members: new Set([1, 2]),
|
|
|
|
is_system_group: false,
|
|
|
|
direct_subgroup_ids: new Set([4, 5]),
|
|
|
|
};
|
2023-01-09 12:04:10 +01:00
|
|
|
|
2024-10-10 00:50:57 +02:00
|
|
|
override(realm, "server_supported_permission_settings", {
|
2023-10-31 04:36:05 +01:00
|
|
|
stream: {
|
|
|
|
can_remove_subscribers_group: {
|
|
|
|
require_system_group: true,
|
|
|
|
allow_internet_group: false,
|
|
|
|
allow_owners_group: false,
|
|
|
|
allow_nobody_group: false,
|
|
|
|
allow_everyone_group: true,
|
|
|
|
default_group_name: "role:administrators",
|
|
|
|
id_field_name: "can_remove_subscribers_group_id",
|
2023-11-23 10:14:35 +01:00
|
|
|
allowed_system_groups: [],
|
2023-10-31 04:36:05 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
realm: {
|
|
|
|
create_multiuse_invite_group: {
|
|
|
|
require_system_group: true,
|
|
|
|
allow_internet_group: false,
|
|
|
|
allow_owners_group: false,
|
|
|
|
allow_nobody_group: true,
|
|
|
|
allow_everyone_group: false,
|
|
|
|
default_group_name: "role:administrators",
|
|
|
|
id_field_name: "create_multiuse_invite_group_id",
|
2023-11-23 10:14:35 +01:00
|
|
|
allowed_system_groups: [],
|
|
|
|
},
|
|
|
|
can_access_all_users_group: {
|
|
|
|
require_system_group: true,
|
|
|
|
allow_internet_group: false,
|
|
|
|
allow_owners_group: false,
|
|
|
|
allow_nobody_group: false,
|
|
|
|
allow_everyone_group: true,
|
|
|
|
default_group_name: "role:everyone",
|
|
|
|
id_field_name: "can_access_all_users_group_id",
|
|
|
|
allowed_system_groups: ["role:everyone", "role:members"],
|
2023-10-31 04:36:05 +01:00
|
|
|
},
|
|
|
|
},
|
2024-10-10 00:50:57 +02:00
|
|
|
});
|
2023-10-31 04:36:05 +01:00
|
|
|
|
2023-11-23 10:14:35 +01:00
|
|
|
let expected_groups_list = [
|
2023-07-19 16:18:20 +02:00
|
|
|
{name: "translated: Admins, moderators, members and guests", unique_id: 6},
|
|
|
|
{name: "translated: Admins, moderators and members", unique_id: 5},
|
|
|
|
{name: "translated: Admins, moderators and full members", unique_id: 7},
|
|
|
|
{name: "translated: Admins and moderators", unique_id: 4},
|
|
|
|
{name: "translated: Admins", unique_id: 3},
|
2023-01-09 12:04:10 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
user_groups.initialize({
|
2023-02-17 08:20:12 +01:00
|
|
|
realm_user_groups: [
|
2023-04-10 12:59:48 +02:00
|
|
|
nobody,
|
2023-02-17 08:20:12 +01:00
|
|
|
owners,
|
|
|
|
admins,
|
|
|
|
moderators,
|
|
|
|
members,
|
|
|
|
everyone,
|
|
|
|
full_members,
|
|
|
|
internet,
|
|
|
|
students,
|
|
|
|
],
|
2023-01-09 12:04:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(
|
2023-10-31 04:36:05 +01:00
|
|
|
user_groups.get_realm_user_groups_for_dropdown_list_widget(
|
|
|
|
"can_remove_subscribers_group",
|
|
|
|
"stream",
|
|
|
|
),
|
2023-02-17 08:20:12 +01:00
|
|
|
expected_groups_list,
|
|
|
|
);
|
|
|
|
|
2023-11-23 10:14:35 +01:00
|
|
|
expected_groups_list = [
|
|
|
|
{name: "translated: Admins, moderators, members and guests", unique_id: 6},
|
|
|
|
{name: "translated: Admins, moderators and members", unique_id: 5},
|
|
|
|
];
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
user_groups.get_realm_user_groups_for_dropdown_list_widget(
|
|
|
|
"can_access_all_users_group",
|
|
|
|
"realm",
|
|
|
|
),
|
|
|
|
expected_groups_list,
|
|
|
|
);
|
|
|
|
|
2023-04-10 12:56:36 +02:00
|
|
|
assert.throws(
|
2023-10-31 04:36:05 +01:00
|
|
|
() =>
|
|
|
|
user_groups.get_realm_user_groups_for_dropdown_list_widget("invalid_setting", "stream"),
|
2023-04-10 12:56:36 +02:00
|
|
|
{
|
|
|
|
name: "Error",
|
|
|
|
message: "Invalid setting: invalid_setting",
|
|
|
|
},
|
2023-01-09 12:04:10 +01:00
|
|
|
);
|
|
|
|
});
|
2024-07-08 18:32:25 +02:00
|
|
|
|
|
|
|
run_test("get_display_group_name", () => {
|
|
|
|
const admins = {
|
user_groups: Do not show "role:" prefix for system groups.
Instead of showing the actual names like "role:everyone",
"role:moderators", etc. for system groups, we show
"Everyone", "Moderators", etc. for system user group in
pills, typeaheads and popovers.
Though system groups are not shown in typeahead as of
this commit, we update the typeahead code as well to
not conside "role:" prefix while matching with the
query as we would soon show system groups in typeahead
when we would add new UI for group-based settings.
Previously, only "role:everyone" group was shown as
"Everyone" in popover and pills but for other system
groups their original names for shown and this commit
changes that behavior to be same for all system groups.
The original display_name field for
settings_config.system_user_groups_list objects, which
was used for dropdown widgets, is also renamed so that
we can use display_name field for the names to be used
in other places.
2024-09-02 16:05:45 +02:00
|
|
|
name: "role:administrators",
|
2024-07-08 18:32:25 +02:00
|
|
|
description: "foo",
|
|
|
|
id: 1,
|
|
|
|
members: new Set([1]),
|
|
|
|
is_system_group: false,
|
|
|
|
direct_subgroup_ids: new Set([4]),
|
|
|
|
};
|
|
|
|
const all = {
|
|
|
|
name: "role:everyone",
|
|
|
|
id: 2,
|
|
|
|
members: new Set([2, 3]),
|
|
|
|
is_system_group: false,
|
|
|
|
direct_subgroup_ids: new Set([1]),
|
|
|
|
};
|
user_groups: Do not show "role:" prefix for system groups.
Instead of showing the actual names like "role:everyone",
"role:moderators", etc. for system groups, we show
"Everyone", "Moderators", etc. for system user group in
pills, typeaheads and popovers.
Though system groups are not shown in typeahead as of
this commit, we update the typeahead code as well to
not conside "role:" prefix while matching with the
query as we would soon show system groups in typeahead
when we would add new UI for group-based settings.
Previously, only "role:everyone" group was shown as
"Everyone" in popover and pills but for other system
groups their original names for shown and this commit
changes that behavior to be same for all system groups.
The original display_name field for
settings_config.system_user_groups_list objects, which
was used for dropdown widgets, is also renamed so that
we can use display_name field for the names to be used
in other places.
2024-09-02 16:05:45 +02:00
|
|
|
const students = {
|
|
|
|
name: "Students",
|
|
|
|
id: 3,
|
|
|
|
members: new Set([1, 3]),
|
|
|
|
is_system_group: false,
|
|
|
|
direct_subgroup_ids: new Set([]),
|
|
|
|
};
|
|
|
|
|
|
|
|
user_groups.initialize({
|
|
|
|
realm_user_groups: [admins, all, students],
|
|
|
|
});
|
2024-07-08 18:32:25 +02:00
|
|
|
|
user_groups: Do not show "role:" prefix for system groups.
Instead of showing the actual names like "role:everyone",
"role:moderators", etc. for system groups, we show
"Everyone", "Moderators", etc. for system user group in
pills, typeaheads and popovers.
Though system groups are not shown in typeahead as of
this commit, we update the typeahead code as well to
not conside "role:" prefix while matching with the
query as we would soon show system groups in typeahead
when we would add new UI for group-based settings.
Previously, only "role:everyone" group was shown as
"Everyone" in popover and pills but for other system
groups their original names for shown and this commit
changes that behavior to be same for all system groups.
The original display_name field for
settings_config.system_user_groups_list objects, which
was used for dropdown widgets, is also renamed so that
we can use display_name field for the names to be used
in other places.
2024-09-02 16:05:45 +02:00
|
|
|
assert.equal(user_groups.get_display_group_name(admins.name), "translated: Administrators");
|
|
|
|
assert.equal(user_groups.get_display_group_name(all.name), "translated: Everyone");
|
|
|
|
assert.equal(user_groups.get_display_group_name(students.name), "Students");
|
2024-07-08 18:32:25 +02:00
|
|
|
});
|