2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2024-09-06 16:41:41 +02:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
2023-02-22 23:04:10 +01:00
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const blueslip = require("./lib/zblueslip");
|
2024-02-13 02:08:24 +01:00
|
|
|
const {current_user, page_params, realm, user_settings} = require("./lib/zpage_params");
|
2016-10-30 15:47:20 +01:00
|
|
|
|
2021-05-20 13:02:00 +02:00
|
|
|
// TODO: Remove after we enable support for
|
|
|
|
// web_public_streams in production.
|
|
|
|
page_params.development_environment = true;
|
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const color_data = zrequire("color_data");
|
2021-06-17 22:26:17 +02:00
|
|
|
const peer_data = zrequire("peer_data");
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2023-10-04 22:48:28 +02:00
|
|
|
const settings_config = zrequire("settings_config");
|
2021-04-15 17:02:54 +02:00
|
|
|
const sub_store = zrequire("sub_store");
|
2021-02-10 04:53:22 +01:00
|
|
|
const stream_data = zrequire("stream_data");
|
2024-08-03 03:05:34 +02:00
|
|
|
const hash_util = zrequire("hash_util");
|
2021-04-04 15:15:18 +02:00
|
|
|
const stream_settings_data = zrequire("stream_settings_data");
|
2022-12-29 17:49:38 +01:00
|
|
|
const user_groups = zrequire("user_groups");
|
2013-08-19 18:46:21 +02:00
|
|
|
|
2024-09-06 16:41:41 +02:00
|
|
|
mock_esm("../src/group_permission_settings", {
|
|
|
|
get_group_permission_setting_config() {
|
|
|
|
return {
|
|
|
|
allow_everyone_group: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-12-28 11:53:50 +01:00
|
|
|
const me = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "me@zulip.com",
|
|
|
|
full_name: "Current User",
|
2020-03-21 20:19:30 +01:00
|
|
|
user_id: 100,
|
2019-12-28 11:53:50 +01:00
|
|
|
};
|
|
|
|
|
2021-06-17 22:26:17 +02:00
|
|
|
const test_user = {
|
|
|
|
email: "test@zulip.com",
|
|
|
|
full_name: "Test User",
|
|
|
|
user_id: 101,
|
|
|
|
};
|
|
|
|
|
2019-12-28 11:53:50 +01:00
|
|
|
// set up user data
|
2022-12-29 17:49:38 +01:00
|
|
|
const admins_group = {
|
|
|
|
name: "Admins",
|
|
|
|
id: 1,
|
|
|
|
members: new Set([1]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([]),
|
|
|
|
};
|
|
|
|
|
2022-12-29 18:36:06 +01:00
|
|
|
const moderators_group = {
|
|
|
|
name: "Members",
|
|
|
|
id: 2,
|
|
|
|
members: new Set([2]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([1]),
|
|
|
|
};
|
|
|
|
|
2024-06-12 12:04:08 +02:00
|
|
|
const everyone_group = {
|
|
|
|
name: "Everyone",
|
|
|
|
id: 3,
|
|
|
|
members: new Set([me.user_id, test_user.user_id]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([]),
|
|
|
|
};
|
|
|
|
|
2021-03-12 20:07:54 +01:00
|
|
|
function test(label, f) {
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test(label, (helpers) => {
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
2021-03-25 22:35:45 +01:00
|
|
|
page_params.realm_users = [];
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_guest = false;
|
2021-03-12 20:07:54 +01:00
|
|
|
people.init();
|
|
|
|
people.add_active_user(me);
|
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
stream_data.clear_subscriptions();
|
2024-06-12 12:04:08 +02:00
|
|
|
user_groups.initialize({
|
|
|
|
realm_user_groups: [admins_group, moderators_group, everyone_group],
|
|
|
|
});
|
2022-07-10 01:06:33 +02:00
|
|
|
f(helpers);
|
2021-03-12 20:07:54 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
test("basics", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const denmark = {
|
2013-08-19 18:46:21 +02:00
|
|
|
subscribed: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
color: "blue",
|
|
|
|
name: "Denmark",
|
2014-02-05 20:35:16 +01:00
|
|
|
stream_id: 1,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: true,
|
2020-07-10 14:25:21 +02:00
|
|
|
invite_only: true,
|
|
|
|
history_public_to_subscribers: true,
|
2013-08-19 18:46:21 +02:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const social = {
|
2013-08-19 18:46:21 +02:00
|
|
|
subscribed: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
color: "red",
|
|
|
|
name: "social",
|
2014-02-05 20:35:16 +01:00
|
|
|
stream_id: 2,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: false,
|
2016-12-03 23:17:57 +01:00
|
|
|
invite_only: true,
|
2020-07-10 14:25:21 +02:00
|
|
|
history_public_to_subscribers: false,
|
2023-10-04 22:48:28 +02:00
|
|
|
stream_post_policy: settings_config.stream_post_policy_values.admins.code,
|
2013-08-19 18:46:21 +02:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const test = {
|
2014-02-20 17:57:30 +01:00
|
|
|
subscribed: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
color: "yellow",
|
|
|
|
name: "test",
|
2014-02-20 17:57:30 +01:00
|
|
|
stream_id: 3,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: true,
|
2016-12-03 23:17:57 +01:00
|
|
|
invite_only: false,
|
2014-02-20 17:57:30 +01:00
|
|
|
};
|
2021-05-20 13:02:00 +02:00
|
|
|
const web_public_stream = {
|
|
|
|
subscribed: false,
|
|
|
|
color: "yellow",
|
|
|
|
name: "web_public_stream",
|
|
|
|
stream_id: 4,
|
|
|
|
is_muted: false,
|
|
|
|
invite_only: false,
|
|
|
|
history_public_to_subscribers: true,
|
|
|
|
is_web_public: true,
|
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(denmark);
|
|
|
|
stream_data.add_sub(social);
|
2021-05-20 13:02:00 +02:00
|
|
|
stream_data.add_sub(web_public_stream);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(stream_data.all_subscribed_streams_are_in_home_view());
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(test);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!stream_data.all_subscribed_streams_are_in_home_view());
|
2013-08-19 18:46:21 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(stream_data.get_sub("denmark"), denmark);
|
|
|
|
assert.equal(stream_data.get_sub("Social"), social);
|
2021-05-20 13:02:00 +02:00
|
|
|
assert.equal(stream_data.get_sub("web_public_stream"), web_public_stream);
|
|
|
|
assert.ok(stream_data.is_web_public(web_public_stream.stream_id));
|
2013-08-19 18:46:21 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.deepEqual(stream_data.subscribed_streams(), ["social", "test"]);
|
|
|
|
assert.deepEqual(stream_data.get_colors(), ["red", "yellow"]);
|
2020-07-25 18:20:54 +02:00
|
|
|
assert.deepEqual(stream_data.subscribed_stream_ids(), [social.stream_id, test.stream_id]);
|
2013-08-19 18:46:21 +02:00
|
|
|
|
2024-08-03 03:05:34 +02:00
|
|
|
assert.ok(stream_data.is_subscribed(social.stream_id));
|
|
|
|
assert.ok(!stream_data.is_subscribed(denmark.stream_id));
|
2013-08-19 18:46:21 +02:00
|
|
|
|
2021-06-03 16:42:03 +02:00
|
|
|
assert.equal(stream_data.get_stream_privacy_policy(test.stream_id), "public");
|
|
|
|
assert.equal(stream_data.get_stream_privacy_policy(social.stream_id), "invite-only");
|
|
|
|
assert.equal(
|
|
|
|
stream_data.get_stream_privacy_policy(denmark.stream_id),
|
|
|
|
"invite-only-public-history",
|
|
|
|
);
|
2021-05-20 13:02:00 +02:00
|
|
|
assert.equal(stream_data.get_stream_privacy_policy(web_public_stream.stream_id), "web-public");
|
2024-08-03 03:05:34 +02:00
|
|
|
assert.ok(stream_data.is_web_public_by_stream_id(web_public_stream.stream_id));
|
|
|
|
assert.ok(!stream_data.is_web_public_by_stream_id(social.stream_id));
|
|
|
|
const unknown_stream_id = 9999;
|
|
|
|
assert.ok(!stream_data.is_web_public_by_stream_id(unknown_stream_id));
|
2020-07-10 14:25:21 +02:00
|
|
|
|
2023-08-06 00:00:41 +02:00
|
|
|
assert.ok(stream_data.is_invite_only_by_stream_id(social.stream_id));
|
|
|
|
// Unknown stream id
|
|
|
|
assert.ok(!stream_data.is_invite_only_by_stream_id(1000));
|
2018-05-14 12:06:56 +02:00
|
|
|
|
2023-06-27 01:40:25 +02:00
|
|
|
assert.equal(stream_data.get_color(social.stream_id), "red");
|
2023-09-26 20:28:39 +02:00
|
|
|
assert.equal(stream_data.get_color(undefined), "#c2c2c2");
|
2024-02-15 07:20:57 +01:00
|
|
|
assert.equal(stream_data.get_color(1234567), "#c2c2c2");
|
2013-08-19 19:25:44 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!stream_data.is_muted(social.stream_id));
|
|
|
|
assert.ok(stream_data.is_muted(denmark.stream_id));
|
2017-07-15 19:12:31 +02:00
|
|
|
|
2023-05-18 15:53:21 +02:00
|
|
|
assert.equal(sub_store.maybe_get_stream_name(), undefined);
|
|
|
|
assert.equal(sub_store.maybe_get_stream_name(social.stream_id), "social");
|
|
|
|
assert.equal(sub_store.maybe_get_stream_name(42), undefined);
|
2017-12-03 20:40:01 +01:00
|
|
|
|
|
|
|
stream_data.set_realm_default_streams([denmark]);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(stream_data.is_default_stream_id(denmark.stream_id));
|
|
|
|
assert.ok(!stream_data.is_default_stream_id(social.stream_id));
|
|
|
|
assert.ok(!stream_data.is_default_stream_id(999999));
|
2020-07-03 16:37:16 +02:00
|
|
|
|
2024-08-18 05:33:38 +02:00
|
|
|
// "new" correct url formats
|
|
|
|
assert.equal(stream_data.slug_to_stream_id("2-social"), 2);
|
2024-08-03 03:05:34 +02:00
|
|
|
assert.equal(hash_util.decode_operand("channel", "2-social"), "2");
|
|
|
|
|
2024-08-18 05:33:38 +02:00
|
|
|
assert.equal(stream_data.slug_to_stream_id("2"), 2);
|
2024-08-03 03:05:34 +02:00
|
|
|
assert.equal(hash_util.decode_operand("channel", "2"), "2");
|
|
|
|
|
2024-08-18 05:33:38 +02:00
|
|
|
// we still get 2 because it's a valid stream id
|
|
|
|
assert.equal(stream_data.slug_to_stream_id("2-whatever"), 2);
|
2024-08-03 03:05:34 +02:00
|
|
|
assert.equal(stream_data.slug_to_stream_id("2-"), 2);
|
|
|
|
|
|
|
|
// legacy, we recognize "social" as a valid channel name
|
2024-08-18 05:33:38 +02:00
|
|
|
assert.equal(stream_data.slug_to_stream_id("social"), 2);
|
2024-08-03 03:05:34 +02:00
|
|
|
assert.equal(hash_util.decode_operand("channel", "social"), "2");
|
|
|
|
|
|
|
|
// These aren't prepended with valid ids nor valid channel names. We
|
|
|
|
// don't get any stream id from the slug, and the decoded operand (the
|
|
|
|
// only caller of `slug_to_stream_id`) returns an empty string (which we
|
|
|
|
// don't display anywhere, since the channel is invalid).
|
|
|
|
assert.equal(stream_data.slug_to_stream_id("999-social"), undefined);
|
|
|
|
assert.equal(hash_util.decode_operand("channel", "999-social"), "");
|
|
|
|
|
|
|
|
assert.equal(stream_data.slug_to_stream_id("99-whatever"), undefined);
|
|
|
|
assert.equal(hash_util.decode_operand("channel", "99-whatever"), "");
|
2021-02-10 14:31:52 +01:00
|
|
|
|
2024-08-18 05:33:38 +02:00
|
|
|
assert.equal(stream_data.slug_to_stream_id("25-or-6-to-4"), undefined);
|
2024-08-03 03:05:34 +02:00
|
|
|
assert.equal(hash_util.decode_operand("channel", "25-or-6-to-4"), "");
|
|
|
|
|
2024-08-18 05:33:38 +02:00
|
|
|
assert.equal(stream_data.slug_to_stream_id("2something"), undefined);
|
2024-08-03 03:05:34 +02:00
|
|
|
assert.equal(hash_util.decode_operand("channel", "2something"), "");
|
|
|
|
|
2024-08-18 05:33:38 +02:00
|
|
|
assert.equal(stream_data.slug_to_stream_id("99whatever"), undefined);
|
2024-08-03 03:05:34 +02:00
|
|
|
assert.equal(hash_util.decode_operand("channel", "99whatever"), "");
|
2023-05-12 23:48:00 +02:00
|
|
|
|
|
|
|
// sub_store
|
|
|
|
assert.equal(sub_store.get(-3), undefined);
|
|
|
|
assert.equal(sub_store.get(undefined), undefined);
|
|
|
|
assert.equal(sub_store.get(1), denmark);
|
2023-06-20 11:30:30 +02:00
|
|
|
|
|
|
|
assert.deepEqual(stream_data.get_options_for_dropdown_widget(), [
|
|
|
|
{
|
|
|
|
name: "social",
|
|
|
|
stream: {
|
|
|
|
color: "red",
|
|
|
|
history_public_to_subscribers: false,
|
|
|
|
invite_only: true,
|
|
|
|
is_muted: false,
|
|
|
|
name: "social",
|
|
|
|
stream_id: 2,
|
|
|
|
stream_post_policy: 2,
|
|
|
|
subscribed: true,
|
|
|
|
},
|
|
|
|
unique_id: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test",
|
|
|
|
stream: {
|
|
|
|
color: "yellow",
|
|
|
|
invite_only: false,
|
|
|
|
is_muted: true,
|
|
|
|
name: "test",
|
|
|
|
stream_id: 3,
|
|
|
|
subscribed: true,
|
|
|
|
},
|
|
|
|
unique_id: 3,
|
|
|
|
},
|
|
|
|
]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-23 05:29:41 +02:00
|
|
|
|
2023-07-29 00:49:51 +02:00
|
|
|
test("get_streams_for_user", () => {
|
2021-06-17 22:26:17 +02:00
|
|
|
const denmark = {
|
|
|
|
subscribed: true,
|
|
|
|
color: "blue",
|
|
|
|
name: "Denmark",
|
|
|
|
stream_id: 1,
|
|
|
|
is_muted: true,
|
|
|
|
invite_only: true,
|
|
|
|
history_public_to_subscribers: true,
|
|
|
|
};
|
|
|
|
const social = {
|
|
|
|
color: "red",
|
|
|
|
name: "social",
|
|
|
|
stream_id: 2,
|
|
|
|
is_muted: false,
|
|
|
|
invite_only: false,
|
|
|
|
history_public_to_subscribers: false,
|
2023-10-04 22:48:28 +02:00
|
|
|
stream_post_policy: settings_config.stream_post_policy_values.admins.code,
|
2021-06-17 22:26:17 +02:00
|
|
|
};
|
|
|
|
const test = {
|
|
|
|
color: "yellow",
|
|
|
|
name: "test",
|
|
|
|
stream_id: 3,
|
|
|
|
is_muted: true,
|
|
|
|
invite_only: true,
|
|
|
|
};
|
2023-07-29 00:49:51 +02:00
|
|
|
const world = {
|
|
|
|
color: "blue",
|
|
|
|
name: "world",
|
|
|
|
stream_id: 4,
|
|
|
|
is_muted: false,
|
|
|
|
invite_only: false,
|
|
|
|
history_public_to_subscribers: false,
|
2023-10-04 22:48:28 +02:00
|
|
|
stream_post_policy: settings_config.stream_post_policy_values.admins.code,
|
2023-07-29 00:49:51 +02:00
|
|
|
};
|
2023-10-18 10:04:05 +02:00
|
|
|
const errors = {
|
|
|
|
color: "green",
|
|
|
|
name: "errors",
|
|
|
|
stream_id: 5,
|
|
|
|
is_muted: false,
|
|
|
|
invite_only: false,
|
|
|
|
history_public_to_subscribers: false,
|
|
|
|
stream_post_policy: settings_config.stream_post_policy_values.admins.code,
|
|
|
|
};
|
|
|
|
const subs = [denmark, social, test, world, errors];
|
2021-06-17 22:26:17 +02:00
|
|
|
for (const sub of subs) {
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
}
|
|
|
|
|
|
|
|
peer_data.set_subscribers(denmark.stream_id, [me.user_id, test_user.user_id]);
|
|
|
|
peer_data.set_subscribers(social.stream_id, [test_user.user_id]);
|
|
|
|
peer_data.set_subscribers(test.stream_id, [test_user.user_id]);
|
2023-07-29 00:49:51 +02:00
|
|
|
peer_data.set_subscribers(world.stream_id, [me.user_id]);
|
2021-06-17 22:26:17 +02:00
|
|
|
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_invite_to_stream_policy = settings_config.common_policy_values.by_admins_only.code;
|
2023-10-18 10:04:05 +02:00
|
|
|
assert.deepEqual(stream_data.get_streams_for_user(me.user_id).can_subscribe, [social, errors]);
|
|
|
|
|
2021-06-17 22:26:17 +02:00
|
|
|
// test_user is subscribed to all three streams, but current user (me)
|
|
|
|
// gets only two because of subscriber visibility policy of stream:
|
|
|
|
// #denmark: current user is subscribed to it so he can see its subscribers.
|
2021-10-18 16:30:46 +02:00
|
|
|
// #social: current user is can get this as neither this is invite only nor current
|
2021-06-17 22:26:17 +02:00
|
|
|
// user is a guest.
|
|
|
|
// #test: current user is no longer subscribed to a private stream, so
|
2023-10-09 21:28:43 +02:00
|
|
|
// he cannot see whether test_user is subscribed to it.
|
2023-07-29 00:49:51 +02:00
|
|
|
assert.deepEqual(stream_data.get_streams_for_user(test_user.user_id).subscribed, [
|
2021-06-17 22:26:17 +02:00
|
|
|
denmark,
|
|
|
|
social,
|
|
|
|
]);
|
2023-07-29 00:49:51 +02:00
|
|
|
assert.deepEqual(stream_data.get_streams_for_user(test_user.user_id).can_subscribe, []);
|
|
|
|
// Verify can subscribe if we're an administrator.
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = true;
|
2023-10-18 10:04:05 +02:00
|
|
|
assert.deepEqual(stream_data.get_streams_for_user(test_user.user_id).can_subscribe, [
|
|
|
|
world,
|
|
|
|
errors,
|
|
|
|
]);
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
2023-10-18 10:04:05 +02:00
|
|
|
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_invite_to_stream_policy = settings_config.common_policy_values.by_members.code;
|
2023-10-18 10:04:05 +02:00
|
|
|
assert.deepEqual(stream_data.get_streams_for_user(test_user.user_id).can_subscribe, [
|
|
|
|
world,
|
|
|
|
errors,
|
|
|
|
]);
|
2021-06-17 22:26:17 +02:00
|
|
|
});
|
|
|
|
|
2021-03-12 20:07:54 +01:00
|
|
|
test("renames", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const id = 42;
|
|
|
|
let sub = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Denmark",
|
2014-02-05 20:35:16 +01:00
|
|
|
subscribed: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
color: "red",
|
2016-12-03 23:17:57 +01:00
|
|
|
stream_id: id,
|
2014-02-05 20:35:16 +01:00
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = stream_data.get_sub("Denmark");
|
|
|
|
assert.equal(sub.color, "red");
|
2021-04-15 17:02:54 +02:00
|
|
|
sub = sub_store.get(id);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sub.color, "red");
|
2016-10-30 17:33:23 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
stream_data.rename_sub(sub, "Sweden");
|
2021-04-15 17:02:54 +02:00
|
|
|
sub = sub_store.get(id);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sub.color, "red");
|
|
|
|
assert.equal(sub.name, "Sweden");
|
2017-05-11 21:49:38 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = stream_data.get_sub("Denmark");
|
2017-05-11 21:49:38 +02:00
|
|
|
assert.equal(sub, undefined);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = stream_data.get_sub_by_name("Denmark");
|
|
|
|
assert.equal(sub.name, "Sweden");
|
2017-05-11 21:49:38 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const actual_id = stream_data.get_stream_id("Denmark");
|
2017-05-11 21:49:38 +02:00
|
|
|
assert.equal(actual_id, 42);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2014-02-05 20:35:16 +01:00
|
|
|
|
2021-03-12 20:07:54 +01:00
|
|
|
test("admin_options", () => {
|
2016-10-17 16:38:15 +02:00
|
|
|
function make_sub() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = {
|
2016-10-17 16:38:15 +02:00
|
|
|
subscribed: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
color: "blue",
|
|
|
|
name: "stream_to_admin",
|
2016-10-17 16:38:15 +02:00
|
|
|
stream_id: 1,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: true,
|
2016-12-03 23:17:57 +01:00
|
|
|
invite_only: false,
|
2023-07-12 12:57:57 +02:00
|
|
|
can_remove_subscribers_group: admins_group.id,
|
2024-03-23 08:06:21 +01:00
|
|
|
date_created: 1691057093,
|
|
|
|
creator_id: null,
|
2016-10-17 16:38:15 +02:00
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2017-04-28 17:55:22 +02:00
|
|
|
return sub;
|
2016-10-17 16:38:15 +02:00
|
|
|
}
|
|
|
|
|
2021-04-04 20:00:22 +02:00
|
|
|
function is_realm_admin(sub) {
|
|
|
|
return stream_settings_data.get_sub_for_settings(sub).is_realm_admin;
|
|
|
|
}
|
|
|
|
|
|
|
|
function can_change_stream_permissions(sub) {
|
|
|
|
return stream_settings_data.get_sub_for_settings(sub).can_change_stream_permissions;
|
|
|
|
}
|
|
|
|
|
2016-10-17 16:38:15 +02:00
|
|
|
// non-admins can't do anything
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
2019-11-02 00:06:25 +01:00
|
|
|
let sub = make_sub();
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!is_realm_admin(sub));
|
|
|
|
assert.ok(!can_change_stream_permissions(sub));
|
2016-10-17 16:38:15 +02:00
|
|
|
|
|
|
|
// just a sanity check that we leave "normal" fields alone
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sub.color, "blue");
|
2016-10-17 16:38:15 +02:00
|
|
|
|
|
|
|
// the remaining cases are for admin users
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = true;
|
2016-10-17 16:38:15 +02:00
|
|
|
|
|
|
|
// admins can make public streams become private
|
|
|
|
sub = make_sub();
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(is_realm_admin(sub));
|
|
|
|
assert.ok(can_change_stream_permissions(sub));
|
2016-10-17 16:38:15 +02:00
|
|
|
|
|
|
|
// admins can only make private streams become public
|
|
|
|
// if they are subscribed
|
|
|
|
sub = make_sub();
|
|
|
|
sub.invite_only = true;
|
|
|
|
sub.subscribed = false;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(is_realm_admin(sub));
|
|
|
|
assert.ok(!can_change_stream_permissions(sub));
|
2016-10-17 16:38:15 +02:00
|
|
|
|
|
|
|
sub = make_sub();
|
|
|
|
sub.invite_only = true;
|
|
|
|
sub.subscribed = true;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(is_realm_admin(sub));
|
|
|
|
assert.ok(can_change_stream_permissions(sub));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-10-17 17:48:56 +02:00
|
|
|
|
2021-03-12 20:07:54 +01:00
|
|
|
test("stream_settings", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const cinnamon = {
|
2016-10-17 17:48:56 +02:00
|
|
|
stream_id: 1,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "c",
|
|
|
|
color: "cinnamon",
|
2016-12-03 23:17:57 +01:00
|
|
|
subscribed: true,
|
2017-08-22 20:51:41 +02:00
|
|
|
invite_only: false,
|
2023-07-12 12:57:57 +02:00
|
|
|
can_remove_subscribers_group: admins_group.id,
|
2024-03-23 08:06:21 +01:00
|
|
|
date_created: 1691057093,
|
|
|
|
creator_id: null,
|
2016-10-17 17:48:56 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const blue = {
|
2016-10-17 17:48:56 +02:00
|
|
|
stream_id: 2,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "b",
|
|
|
|
color: "blue",
|
2016-12-03 23:17:57 +01:00
|
|
|
subscribed: false,
|
2017-08-22 20:51:41 +02:00
|
|
|
invite_only: false,
|
2023-07-12 12:57:57 +02:00
|
|
|
can_remove_subscribers_group: admins_group.id,
|
2024-03-23 08:06:21 +01:00
|
|
|
date_created: 1691057093,
|
|
|
|
creator_id: null,
|
2016-10-17 17:48:56 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const amber = {
|
2016-10-17 17:48:56 +02:00
|
|
|
stream_id: 3,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "a",
|
|
|
|
color: "amber",
|
2016-12-03 23:17:57 +01:00
|
|
|
subscribed: true,
|
2017-08-22 20:51:41 +02:00
|
|
|
invite_only: true,
|
2019-05-02 19:43:27 +02:00
|
|
|
history_public_to_subscribers: true,
|
2023-10-04 22:48:28 +02:00
|
|
|
stream_post_policy: settings_config.stream_post_policy_values.admins.code,
|
2020-06-15 17:00:00 +02:00
|
|
|
message_retention_days: 10,
|
2023-07-12 12:57:57 +02:00
|
|
|
can_remove_subscribers_group: admins_group.id,
|
2024-03-23 08:06:21 +01:00
|
|
|
date_created: 1691057093,
|
|
|
|
creator_id: null,
|
2016-10-17 17:48:56 +02:00
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(cinnamon);
|
|
|
|
stream_data.add_sub(amber);
|
|
|
|
stream_data.add_sub(blue);
|
2016-10-17 17:48:56 +02:00
|
|
|
|
2021-04-04 15:15:18 +02:00
|
|
|
let sub_rows = stream_settings_data.get_streams_for_settings_page();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sub_rows[0].color, "blue");
|
|
|
|
assert.equal(sub_rows[1].color, "amber");
|
|
|
|
assert.equal(sub_rows[2].color, "cinnamon");
|
2016-10-17 17:48:56 +02:00
|
|
|
|
2017-08-22 20:51:41 +02:00
|
|
|
sub_rows = stream_data.get_streams_for_admin();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sub_rows[0].name, "a");
|
|
|
|
assert.equal(sub_rows[1].name, "b");
|
|
|
|
assert.equal(sub_rows[2].name, "c");
|
2017-08-22 20:51:41 +02:00
|
|
|
assert.equal(sub_rows[0].invite_only, true);
|
|
|
|
assert.equal(sub_rows[1].invite_only, false);
|
|
|
|
assert.equal(sub_rows[2].invite_only, false);
|
|
|
|
|
2019-05-02 19:43:27 +02:00
|
|
|
assert.equal(sub_rows[0].history_public_to_subscribers, true);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
2023-10-04 22:48:28 +02:00
|
|
|
sub_rows[0].stream_post_policy === settings_config.stream_post_policy_values.admins.code,
|
2020-07-15 00:34:28 +02:00
|
|
|
true,
|
|
|
|
);
|
2020-06-15 17:00:00 +02:00
|
|
|
assert.equal(sub_rows[0].message_retention_days, 10);
|
2019-05-02 19:43:27 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const sub = stream_data.get_sub("a");
|
2019-05-02 19:43:27 +02:00
|
|
|
stream_data.update_stream_privacy(sub, {
|
|
|
|
invite_only: false,
|
|
|
|
history_public_to_subscribers: false,
|
|
|
|
});
|
2020-02-04 21:50:55 +01:00
|
|
|
stream_data.update_stream_post_policy(sub, 1);
|
2020-06-15 17:00:00 +02:00
|
|
|
stream_data.update_message_retention_setting(sub, -1);
|
2022-12-29 18:36:06 +01:00
|
|
|
stream_data.update_can_remove_subscribers_group_id(sub, moderators_group.id);
|
2019-05-02 19:43:27 +02:00
|
|
|
assert.equal(sub.invite_only, false);
|
|
|
|
assert.equal(sub.history_public_to_subscribers, false);
|
2023-10-04 22:48:28 +02:00
|
|
|
assert.equal(sub.stream_post_policy, settings_config.stream_post_policy_values.everyone.code);
|
2020-06-15 17:00:00 +02:00
|
|
|
assert.equal(sub.message_retention_days, -1);
|
2023-07-12 12:57:57 +02:00
|
|
|
assert.equal(sub.can_remove_subscribers_group, moderators_group.id);
|
2019-05-08 08:57:23 +02:00
|
|
|
|
|
|
|
// For guest user only retrieve subscribed streams
|
2021-04-04 15:15:18 +02:00
|
|
|
sub_rows = stream_settings_data.get_updated_unsorted_subs();
|
2019-05-08 08:57:23 +02:00
|
|
|
assert.equal(sub_rows.length, 3);
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_guest = true;
|
2021-04-04 15:15:18 +02:00
|
|
|
sub_rows = stream_settings_data.get_updated_unsorted_subs();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sub_rows[0].name, "c");
|
|
|
|
assert.equal(sub_rows[1].name, "a");
|
2019-05-08 08:57:23 +02:00
|
|
|
assert.equal(sub_rows.length, 2);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-02-16 03:47:08 +01:00
|
|
|
|
2021-03-12 20:07:54 +01:00
|
|
|
test("default_stream_names", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const announce = {
|
2017-08-22 20:00:09 +02:00
|
|
|
stream_id: 101,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "announce",
|
2017-08-22 20:00:09 +02:00
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const public_stream = {
|
2017-08-22 20:00:09 +02:00
|
|
|
stream_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "public",
|
2017-08-22 20:00:09 +02:00
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const private_stream = {
|
2017-08-22 20:00:09 +02:00
|
|
|
stream_id: 103,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "private",
|
2017-08-22 20:00:09 +02:00
|
|
|
subscribed: true,
|
|
|
|
invite_only: true,
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const general = {
|
2018-07-22 11:30:38 +02:00
|
|
|
stream_id: 104,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "general",
|
2018-07-22 11:30:38 +02:00
|
|
|
subscribed: true,
|
|
|
|
invite_only: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.set_realm_default_streams([announce, general]);
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(announce);
|
|
|
|
stream_data.add_sub(public_stream);
|
|
|
|
stream_data.add_sub(private_stream);
|
|
|
|
stream_data.add_sub(general);
|
2017-08-22 20:00:09 +02:00
|
|
|
|
2020-03-22 13:28:16 +01:00
|
|
|
const names = stream_data.get_non_default_stream_names();
|
2024-05-28 08:17:13 +02:00
|
|
|
assert.deepEqual(names.sort(), [{name: "public", unique_id: 102}]);
|
2018-07-22 11:30:38 +02:00
|
|
|
|
2020-03-22 13:28:16 +01:00
|
|
|
const default_stream_ids = stream_data.get_default_stream_ids();
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(default_stream_ids.sort(), [announce.stream_id, general.stream_id]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-08-22 20:00:09 +02:00
|
|
|
|
2021-03-12 20:07:54 +01:00
|
|
|
test("delete_sub", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const canada = {
|
2017-02-16 03:47:08 +01:00
|
|
|
stream_id: 101,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Canada",
|
2017-02-16 03:47:08 +01:00
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(canada);
|
2017-02-16 03:47:08 +01:00
|
|
|
|
2024-08-03 03:05:34 +02:00
|
|
|
assert.ok(stream_data.is_subscribed(canada.stream_id));
|
2021-06-03 16:42:03 +02:00
|
|
|
assert.equal(stream_data.get_sub("Canada").stream_id, canada.stream_id);
|
|
|
|
assert.equal(sub_store.get(canada.stream_id).name, "Canada");
|
2017-02-16 03:47:08 +01:00
|
|
|
|
|
|
|
stream_data.delete_sub(canada.stream_id);
|
2024-08-03 03:05:34 +02:00
|
|
|
assert.ok(!stream_data.is_subscribed(canada.stream_id));
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!stream_data.get_sub("Canada"));
|
|
|
|
assert.ok(!sub_store.get(canada.stream_id));
|
2017-12-14 16:23:19 +01:00
|
|
|
|
2021-03-31 16:15:24 +02:00
|
|
|
blueslip.expect("warn", "Failed to archive stream 99999");
|
2019-12-30 12:51:16 +01:00
|
|
|
stream_data.delete_sub(99999);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-29 15:35:34 +02:00
|
|
|
|
2021-03-12 20:07:54 +01:00
|
|
|
test("notifications", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const india = {
|
2017-12-03 05:30:58 +01:00
|
|
|
stream_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "India",
|
2023-03-28 02:48:47 +02:00
|
|
|
color: "#000080",
|
2017-12-03 05:30:58 +01:00
|
|
|
subscribed: true,
|
2020-02-27 13:57:11 +01:00
|
|
|
invite_only: false,
|
|
|
|
is_web_public: false,
|
2019-02-13 10:22:16 +01:00
|
|
|
desktop_notifications: null,
|
|
|
|
audible_notifications: null,
|
2019-12-10 03:11:12 +01:00
|
|
|
email_notifications: null,
|
|
|
|
push_notifications: null,
|
|
|
|
wildcard_mentions_notify: null,
|
2017-12-03 05:30:58 +01:00
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(india);
|
2019-02-13 10:22:16 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!stream_data.receives_notifications(india.stream_id, "desktop_notifications"));
|
|
|
|
assert.ok(!stream_data.receives_notifications(india.stream_id, "audible_notifications"));
|
2017-12-03 05:33:29 +01:00
|
|
|
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.enable_stream_desktop_notifications = true;
|
|
|
|
user_settings.enable_stream_audible_notifications = true;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(stream_data.receives_notifications(india.stream_id, "desktop_notifications"));
|
|
|
|
assert.ok(stream_data.receives_notifications(india.stream_id, "audible_notifications"));
|
2019-02-13 10:22:16 +01:00
|
|
|
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.enable_stream_desktop_notifications = false;
|
|
|
|
user_settings.enable_stream_audible_notifications = false;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!stream_data.receives_notifications(india.stream_id, "desktop_notifications"));
|
|
|
|
assert.ok(!stream_data.receives_notifications(india.stream_id, "audible_notifications"));
|
2019-02-13 10:22:16 +01:00
|
|
|
|
|
|
|
india.desktop_notifications = true;
|
|
|
|
india.audible_notifications = true;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(stream_data.receives_notifications(india.stream_id, "desktop_notifications"));
|
|
|
|
assert.ok(stream_data.receives_notifications(india.stream_id, "audible_notifications"));
|
2019-02-13 10:22:16 +01:00
|
|
|
|
|
|
|
india.desktop_notifications = false;
|
|
|
|
india.audible_notifications = false;
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.enable_stream_desktop_notifications = true;
|
|
|
|
user_settings.enable_stream_audible_notifications = true;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!stream_data.receives_notifications(india.stream_id, "desktop_notifications"));
|
|
|
|
assert.ok(!stream_data.receives_notifications(india.stream_id, "audible_notifications"));
|
2019-12-10 03:11:12 +01:00
|
|
|
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.wildcard_mentions_notify = true;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(stream_data.receives_notifications(india.stream_id, "wildcard_mentions_notify"));
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.wildcard_mentions_notify = false;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!stream_data.receives_notifications(india.stream_id, "wildcard_mentions_notify"));
|
2019-12-10 03:11:12 +01:00
|
|
|
india.wildcard_mentions_notify = true;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(stream_data.receives_notifications(india.stream_id, "wildcard_mentions_notify"));
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.wildcard_mentions_notify = true;
|
2019-12-10 03:11:12 +01:00
|
|
|
india.wildcard_mentions_notify = false;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!stream_data.receives_notifications(india.stream_id, "wildcard_mentions_notify"));
|
2019-12-10 03:11:12 +01:00
|
|
|
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.enable_stream_push_notifications = true;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(stream_data.receives_notifications(india.stream_id, "push_notifications"));
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.enable_stream_push_notifications = false;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!stream_data.receives_notifications(india.stream_id, "push_notifications"));
|
2019-12-10 03:11:12 +01:00
|
|
|
india.push_notifications = true;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(stream_data.receives_notifications(india.stream_id, "push_notifications"));
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.enable_stream_push_notifications = true;
|
2019-12-10 03:11:12 +01:00
|
|
|
india.push_notifications = false;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!stream_data.receives_notifications(india.stream_id, "push_notifications"));
|
2019-12-10 03:11:12 +01:00
|
|
|
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.enable_stream_email_notifications = true;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(stream_data.receives_notifications(india.stream_id, "email_notifications"));
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.enable_stream_email_notifications = false;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!stream_data.receives_notifications(india.stream_id, "email_notifications"));
|
2019-12-10 03:11:12 +01:00
|
|
|
india.email_notifications = true;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(stream_data.receives_notifications(india.stream_id, "email_notifications"));
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.enable_stream_email_notifications = true;
|
2019-12-10 03:11:12 +01:00
|
|
|
india.email_notifications = false;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!stream_data.receives_notifications(india.stream_id, "email_notifications"));
|
2020-02-27 13:57:11 +01:00
|
|
|
|
|
|
|
const canada = {
|
|
|
|
stream_id: 103,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Canada",
|
2023-03-28 02:48:47 +02:00
|
|
|
color: "#d80621",
|
2020-02-27 13:57:11 +01:00
|
|
|
subscribed: true,
|
|
|
|
invite_only: true,
|
|
|
|
is_web_public: false,
|
|
|
|
desktop_notifications: null,
|
|
|
|
audible_notifications: null,
|
|
|
|
email_notifications: null,
|
|
|
|
push_notifications: null,
|
|
|
|
wildcard_mentions_notify: null,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(canada);
|
|
|
|
|
|
|
|
const antarctica = {
|
|
|
|
stream_id: 104,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Antarctica",
|
2020-02-27 13:57:11 +01:00
|
|
|
subscribed: true,
|
|
|
|
desktop_notifications: null,
|
|
|
|
audible_notifications: null,
|
|
|
|
email_notifications: null,
|
|
|
|
push_notifications: null,
|
|
|
|
wildcard_mentions_notify: null,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(antarctica);
|
|
|
|
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.enable_stream_desktop_notifications = true;
|
|
|
|
user_settings.enable_stream_audible_notifications = true;
|
|
|
|
user_settings.enable_stream_email_notifications = false;
|
|
|
|
user_settings.enable_stream_push_notifications = false;
|
|
|
|
user_settings.wildcard_mentions_notify = true;
|
2020-02-27 13:57:11 +01:00
|
|
|
|
|
|
|
india.desktop_notifications = null;
|
|
|
|
india.audible_notifications = true;
|
|
|
|
india.email_notifications = true;
|
|
|
|
india.push_notifications = true;
|
|
|
|
india.wildcard_mentions_notify = false;
|
|
|
|
|
|
|
|
canada.desktop_notifications = true;
|
|
|
|
canada.audible_notifications = false;
|
|
|
|
canada.email_notifications = true;
|
|
|
|
canada.push_notifications = null;
|
|
|
|
canada.wildcard_mentions_notify = false;
|
|
|
|
|
|
|
|
antarctica.desktop_notifications = true;
|
|
|
|
antarctica.audible_notifications = null;
|
|
|
|
antarctica.email_notifications = false;
|
|
|
|
antarctica.push_notifications = null;
|
|
|
|
antarctica.wildcard_mentions_notify = null;
|
|
|
|
|
2021-05-09 22:29:53 +02:00
|
|
|
const unmatched_streams =
|
|
|
|
stream_settings_data.get_unmatched_streams_for_notification_settings();
|
2020-02-27 13:57:11 +01:00
|
|
|
const expected_streams = [
|
|
|
|
{
|
|
|
|
desktop_notifications: true,
|
|
|
|
audible_notifications: false,
|
|
|
|
email_notifications: true,
|
|
|
|
push_notifications: false,
|
|
|
|
wildcard_mentions_notify: false,
|
|
|
|
invite_only: true,
|
|
|
|
is_web_public: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream_name: "Canada",
|
2020-02-27 13:57:11 +01:00
|
|
|
stream_id: 103,
|
2023-03-28 02:48:47 +02:00
|
|
|
color: "#d80621",
|
2020-02-27 13:57:11 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desktop_notifications: true,
|
|
|
|
audible_notifications: true,
|
|
|
|
email_notifications: true,
|
|
|
|
push_notifications: true,
|
|
|
|
wildcard_mentions_notify: false,
|
|
|
|
invite_only: false,
|
|
|
|
is_web_public: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream_name: "India",
|
2020-02-27 13:57:11 +01:00
|
|
|
stream_id: 102,
|
2023-03-28 02:48:47 +02:00
|
|
|
color: "#000080",
|
2020-02-27 13:57:11 +01:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
assert.deepEqual(unmatched_streams, expected_streams);
|
2021-04-04 19:35:45 +02:00
|
|
|
|
|
|
|
// Get line coverage on defensive code with bogus stream_id.
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!stream_data.receives_notifications(999999));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-05 18:18:25 +01:00
|
|
|
|
2020-04-14 12:55:18 +02:00
|
|
|
const tony = {
|
|
|
|
stream_id: 999,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "tony",
|
2020-04-14 12:55:18 +02:00
|
|
|
subscribed: true,
|
|
|
|
is_muted: false,
|
|
|
|
};
|
2018-05-07 03:30:13 +02:00
|
|
|
|
2020-04-14 12:55:18 +02:00
|
|
|
const jazy = {
|
|
|
|
stream_id: 500,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "jazy",
|
2020-04-14 12:55:18 +02:00
|
|
|
subscribed: false,
|
|
|
|
is_muted: true,
|
|
|
|
};
|
2018-05-07 03:30:13 +02:00
|
|
|
|
2024-02-07 12:13:02 +01:00
|
|
|
test("is_new_stream_announcements_stream_muted", () => {
|
2020-04-14 12:55:18 +02:00
|
|
|
stream_data.add_sub(tony);
|
|
|
|
stream_data.add_sub(jazy);
|
|
|
|
|
2024-02-07 12:13:02 +01:00
|
|
|
realm.realm_new_stream_announcements_stream_id = tony.stream_id;
|
|
|
|
assert.ok(!stream_data.is_new_stream_announcements_stream_muted());
|
2017-12-05 18:46:01 +01:00
|
|
|
|
2024-02-07 12:13:02 +01:00
|
|
|
realm.realm_new_stream_announcements_stream_id = jazy.stream_id;
|
|
|
|
assert.ok(stream_data.is_new_stream_announcements_stream_muted());
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-07 02:32:47 +01:00
|
|
|
|
2021-11-20 11:51:35 +01:00
|
|
|
test("muted_stream_ids", () => {
|
|
|
|
const denmark = {
|
|
|
|
subscribed: true,
|
|
|
|
color: "blue",
|
|
|
|
name: "Denmark",
|
|
|
|
stream_id: 1,
|
|
|
|
is_muted: true,
|
|
|
|
invite_only: true,
|
|
|
|
history_public_to_subscribers: true,
|
|
|
|
};
|
|
|
|
const social = {
|
|
|
|
subscribed: true,
|
|
|
|
color: "red",
|
|
|
|
name: "social",
|
|
|
|
stream_id: 2,
|
|
|
|
is_muted: false,
|
|
|
|
invite_only: true,
|
|
|
|
history_public_to_subscribers: false,
|
2023-10-04 22:48:28 +02:00
|
|
|
stream_post_policy: settings_config.stream_post_policy_values.admins.code,
|
2021-11-20 11:51:35 +01:00
|
|
|
};
|
|
|
|
const test = {
|
|
|
|
subscribed: true,
|
|
|
|
color: "yellow",
|
|
|
|
name: "test",
|
|
|
|
stream_id: 3,
|
|
|
|
is_muted: true,
|
|
|
|
invite_only: false,
|
|
|
|
};
|
|
|
|
const web_public_stream = {
|
|
|
|
subscribed: true,
|
|
|
|
color: "yellow",
|
|
|
|
name: "web_public_stream",
|
|
|
|
stream_id: 4,
|
|
|
|
is_muted: false,
|
|
|
|
invite_only: false,
|
|
|
|
history_public_to_subscribers: true,
|
|
|
|
is_web_public: true,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(denmark);
|
|
|
|
stream_data.add_sub(social);
|
|
|
|
stream_data.add_sub(test);
|
|
|
|
stream_data.add_sub(web_public_stream);
|
|
|
|
|
|
|
|
assert.deepEqual(stream_data.muted_stream_ids(), [1, 3]);
|
|
|
|
});
|
|
|
|
|
2024-02-07 12:13:02 +01:00
|
|
|
test("realm_has_new_stream_announcements_stream", () => {
|
|
|
|
realm.realm_new_stream_announcements_stream_id = 10;
|
|
|
|
assert.ok(stream_data.realm_has_new_stream_announcements_stream());
|
|
|
|
realm.realm_new_stream_announcements_stream_id = -1;
|
|
|
|
assert.ok(!stream_data.realm_has_new_stream_announcements_stream());
|
2020-04-15 18:29:26 +02:00
|
|
|
});
|
|
|
|
|
2021-03-12 20:07:54 +01:00
|
|
|
test("remove_default_stream", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const remove_me = {
|
2017-12-07 02:32:47 +01:00
|
|
|
stream_id: 674,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "remove_me",
|
2017-12-07 02:32:47 +01:00
|
|
|
subscribed: false,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: true,
|
2017-12-07 02:32:47 +01:00
|
|
|
};
|
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(remove_me);
|
2017-12-07 02:32:47 +01:00
|
|
|
stream_data.set_realm_default_streams([remove_me]);
|
|
|
|
stream_data.remove_default_stream(remove_me.stream_id);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!stream_data.is_default_stream_id(remove_me.stream_id));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-08 17:14:56 +01:00
|
|
|
|
2021-03-12 20:07:54 +01:00
|
|
|
test("canonicalized_name", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepStrictEqual(stream_data.canonicalized_name("Stream_Bar"), "stream_bar");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-05 09:22:13 +01:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
test("create_sub", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const india = {
|
2017-12-05 09:22:13 +01:00
|
|
|
stream_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "India",
|
2017-12-05 09:22:13 +01:00
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const canada = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Canada",
|
2017-12-05 09:22:13 +01:00
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const antarctica = {
|
2017-12-05 09:22:13 +01:00
|
|
|
stream_id: 103,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Antarctica",
|
2017-12-05 09:22:13 +01:00
|
|
|
subscribed: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
color: "#76ce90",
|
2017-12-05 09:22:13 +01:00
|
|
|
};
|
|
|
|
|
2020-06-21 15:23:43 +02:00
|
|
|
const india_sub = stream_data.create_sub_from_server_data(india);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(india_sub);
|
2022-07-10 01:06:33 +02:00
|
|
|
assert.equal(india_sub.color, color_data.colors[0]);
|
2020-06-21 15:23:43 +02:00
|
|
|
const new_sub = stream_data.create_sub_from_server_data(india);
|
|
|
|
// make sure sub doesn't get created twice
|
2017-12-05 09:22:13 +01:00
|
|
|
assert.equal(india_sub, new_sub);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
stream_data.create_sub_from_server_data("Canada", canada);
|
|
|
|
},
|
|
|
|
{message: "We cannot create a sub without a stream_id"},
|
|
|
|
);
|
2017-12-05 09:22:13 +01:00
|
|
|
|
2020-06-21 15:23:43 +02:00
|
|
|
const antarctica_sub = stream_data.create_sub_from_server_data(antarctica);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(antarctica_sub);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(antarctica_sub.color, "#76ce90");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-05 09:22:13 +01:00
|
|
|
|
2024-03-23 08:06:21 +01:00
|
|
|
test("creator_id", () => {
|
|
|
|
people.add_active_user(test_user);
|
2024-06-12 12:04:08 +02:00
|
|
|
realm.realm_can_access_all_users_group = everyone_group.id;
|
|
|
|
current_user.user_id = me.user_id;
|
2024-03-23 08:06:21 +01:00
|
|
|
// When creator id is not a valid user id
|
|
|
|
assert.throws(() => stream_data.maybe_get_creator_details(-1), {
|
|
|
|
name: "Error",
|
|
|
|
message: "Unknown user_id in get_by_user_id: -1",
|
|
|
|
});
|
|
|
|
|
|
|
|
// When there is no creator
|
|
|
|
assert.equal(stream_data.maybe_get_creator_details(null), undefined);
|
|
|
|
|
2024-07-16 12:11:19 +02:00
|
|
|
let creator_details = {...people.get_by_user_id(test_user.user_id), is_active: true};
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
stream_data.maybe_get_creator_details(test_user.user_id),
|
|
|
|
creator_details,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check when creator is deactivated.
|
|
|
|
people.deactivate(test_user);
|
|
|
|
creator_details = {...people.get_by_user_id(test_user.user_id), is_active: false};
|
2024-03-23 08:06:21 +01:00
|
|
|
assert.deepStrictEqual(
|
|
|
|
stream_data.maybe_get_creator_details(test_user.user_id),
|
|
|
|
creator_details,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-03-12 20:07:54 +01:00
|
|
|
test("initialize", () => {
|
2020-02-25 12:16:26 +01:00
|
|
|
function get_params() {
|
|
|
|
const params = {};
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
params.subscriptions = [
|
|
|
|
{
|
|
|
|
name: "subscriptions",
|
|
|
|
stream_id: 2001,
|
|
|
|
},
|
|
|
|
];
|
2017-12-14 18:00:30 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
params.unsubscribed = [
|
|
|
|
{
|
|
|
|
name: "unsubscribed",
|
|
|
|
stream_id: 2002,
|
|
|
|
},
|
|
|
|
];
|
2020-02-25 12:16:26 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
params.never_subscribed = [
|
|
|
|
{
|
|
|
|
name: "never_subscribed",
|
|
|
|
stream_id: 2003,
|
|
|
|
},
|
2020-03-22 17:32:31 +01:00
|
|
|
];
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
params.realm_default_streams = [];
|
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
function initialize() {
|
|
|
|
stream_data.initialize(get_params());
|
2017-12-14 18:00:30 +01:00
|
|
|
}
|
|
|
|
|
2024-02-07 12:13:02 +01:00
|
|
|
realm.realm_new_stream_announcements_stream_id = -1;
|
2020-02-25 12:16:26 +01:00
|
|
|
|
|
|
|
initialize();
|
2017-12-14 18:00:30 +01:00
|
|
|
|
2020-10-07 10:45:28 +02:00
|
|
|
const stream_names = new Set(stream_data.get_streams_for_admin().map((elem) => elem.name));
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(stream_names.has("subscriptions"));
|
|
|
|
assert.ok(stream_names.has("unsubscribed"));
|
|
|
|
assert.ok(stream_names.has("never_subscribed"));
|
2024-02-07 12:13:02 +01:00
|
|
|
assert.equal(stream_data.get_new_stream_announcements_stream(), "");
|
2017-12-14 18:00:30 +01:00
|
|
|
|
2018-05-03 17:28:38 +02:00
|
|
|
// Simulate a private stream the user isn't subscribed to
|
2024-02-07 12:13:02 +01:00
|
|
|
realm.realm_new_stream_announcements_stream_id = 89;
|
2020-02-25 12:16:26 +01:00
|
|
|
initialize();
|
2024-02-07 12:13:02 +01:00
|
|
|
assert.equal(stream_data.get_new_stream_announcements_stream(), "");
|
2018-05-03 17:28:38 +02:00
|
|
|
|
|
|
|
// Now actually subscribe the user to the stream
|
2017-12-14 18:00:30 +01:00
|
|
|
initialize();
|
2019-11-02 00:06:25 +01:00
|
|
|
const foo = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "foo",
|
2017-12-14 18:00:30 +01:00
|
|
|
stream_id: 89,
|
|
|
|
};
|
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(foo);
|
2020-02-25 12:16:26 +01:00
|
|
|
initialize();
|
2024-02-07 12:13:02 +01:00
|
|
|
assert.equal(stream_data.get_new_stream_announcements_stream(), "foo");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-22 14:59:10 +01:00
|
|
|
|
2021-03-12 20:07:54 +01:00
|
|
|
test("edge_cases", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const bad_stream_ids = [555555, 99999];
|
2018-07-29 15:26:45 +02:00
|
|
|
|
|
|
|
// just make sure we don't explode
|
2021-04-04 15:15:18 +02:00
|
|
|
stream_settings_data.sort_for_stream_settings(bad_stream_ids);
|
2018-07-29 15:26:45 +02:00
|
|
|
});
|
2019-01-10 17:57:35 +01:00
|
|
|
|
2021-03-12 20:07:54 +01:00
|
|
|
test("get_invite_stream_data", () => {
|
2019-01-10 17:57:35 +01:00
|
|
|
// add default stream
|
2019-11-02 00:06:25 +01:00
|
|
|
const orie = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Orie",
|
2019-01-10 17:57:35 +01:00
|
|
|
stream_id: 320,
|
|
|
|
invite_only: false,
|
|
|
|
subscribed: true,
|
2022-12-15 06:30:09 +01:00
|
|
|
is_web_public: false,
|
2019-01-10 17:57:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
people.init();
|
2024-01-24 14:03:43 +01:00
|
|
|
people.add_active_user(me);
|
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
current_user.is_admin = true;
|
2019-01-10 17:57:35 +01:00
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(orie);
|
2019-01-10 17:57:35 +01:00
|
|
|
stream_data.set_realm_default_streams([orie]);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const expected_list = [
|
|
|
|
{
|
|
|
|
name: "Orie",
|
|
|
|
stream_id: 320,
|
|
|
|
invite_only: false,
|
2024-01-24 14:03:43 +01:00
|
|
|
subscribed: true,
|
2022-12-15 06:30:09 +01:00
|
|
|
is_web_public: false,
|
2020-07-15 00:34:28 +02:00
|
|
|
},
|
|
|
|
];
|
2019-01-10 17:57:35 +01:00
|
|
|
assert.deepEqual(stream_data.get_invite_stream_data(), expected_list);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const inviter = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Inviter",
|
2019-01-10 17:57:35 +01:00
|
|
|
stream_id: 25,
|
|
|
|
invite_only: true,
|
|
|
|
subscribed: true,
|
2022-12-15 06:30:09 +01:00
|
|
|
is_web_public: false,
|
2019-01-10 17:57:35 +01:00
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(inviter);
|
2019-01-10 17:57:35 +01:00
|
|
|
|
|
|
|
expected_list.push({
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Inviter",
|
2019-01-10 17:57:35 +01:00
|
|
|
stream_id: 25,
|
|
|
|
invite_only: true,
|
2024-01-24 14:03:43 +01:00
|
|
|
subscribed: true,
|
|
|
|
is_web_public: false,
|
|
|
|
});
|
|
|
|
assert.deepEqual(stream_data.get_invite_stream_data(), expected_list);
|
|
|
|
|
|
|
|
// add unsubscribed private stream
|
|
|
|
const tokyo = {
|
|
|
|
name: "Tokyo",
|
|
|
|
stream_id: 12,
|
|
|
|
invite_only: true,
|
|
|
|
subscribed: false,
|
|
|
|
is_web_public: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub(tokyo);
|
|
|
|
assert.deepEqual(stream_data.get_invite_stream_data(), expected_list);
|
|
|
|
|
|
|
|
const random = {
|
|
|
|
name: "Random",
|
|
|
|
stream_id: 34,
|
|
|
|
invite_only: false,
|
|
|
|
subscribed: false,
|
|
|
|
is_web_public: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub(random);
|
|
|
|
|
|
|
|
expected_list.push({
|
|
|
|
name: "Random",
|
|
|
|
stream_id: 34,
|
|
|
|
invite_only: false,
|
|
|
|
subscribed: false,
|
2022-12-15 06:30:09 +01:00
|
|
|
is_web_public: false,
|
2019-01-10 17:57:35 +01:00
|
|
|
});
|
|
|
|
assert.deepEqual(stream_data.get_invite_stream_data(), expected_list);
|
|
|
|
});
|
2021-12-27 10:27:03 +01:00
|
|
|
|
|
|
|
test("can_post_messages_in_stream", () => {
|
|
|
|
const social = {
|
|
|
|
subscribed: true,
|
|
|
|
color: "red",
|
|
|
|
name: "social",
|
|
|
|
stream_id: 2,
|
|
|
|
is_muted: false,
|
|
|
|
invite_only: true,
|
|
|
|
history_public_to_subscribers: false,
|
2023-10-04 22:48:28 +02:00
|
|
|
stream_post_policy: settings_config.stream_post_policy_values.admins.code,
|
2021-12-27 10:27:03 +01:00
|
|
|
};
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
2021-12-27 10:27:03 +01:00
|
|
|
assert.equal(stream_data.can_post_messages_in_stream(social), false);
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = true;
|
2021-12-27 10:27:03 +01:00
|
|
|
assert.equal(stream_data.can_post_messages_in_stream(social), true);
|
|
|
|
|
2023-10-04 22:48:28 +02:00
|
|
|
social.stream_post_policy = settings_config.stream_post_policy_values.moderators.code;
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_moderator = false;
|
|
|
|
current_user.is_admin = false;
|
2021-12-27 10:27:03 +01:00
|
|
|
|
|
|
|
assert.equal(stream_data.can_post_messages_in_stream(social), false);
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_moderator = true;
|
2021-12-27 10:27:03 +01:00
|
|
|
assert.equal(stream_data.can_post_messages_in_stream(social), true);
|
|
|
|
|
2023-10-04 22:48:28 +02:00
|
|
|
social.stream_post_policy = settings_config.stream_post_policy_values.non_new_members.code;
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_moderator = false;
|
2021-12-27 10:27:03 +01:00
|
|
|
me.date_joined = new Date(Date.now());
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_waiting_period_threshold = 10;
|
2021-12-27 10:27:03 +01:00
|
|
|
assert.equal(stream_data.can_post_messages_in_stream(social), false);
|
|
|
|
|
|
|
|
me.date_joined = new Date(Date.now() - 20 * 86400000);
|
|
|
|
assert.equal(stream_data.can_post_messages_in_stream(social), true);
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_guest = true;
|
2021-12-27 10:27:03 +01:00
|
|
|
assert.equal(stream_data.can_post_messages_in_stream(social), false);
|
|
|
|
|
2023-10-04 22:48:28 +02:00
|
|
|
social.stream_post_policy = settings_config.stream_post_policy_values.everyone.code;
|
2021-12-27 10:27:03 +01:00
|
|
|
assert.equal(stream_data.can_post_messages_in_stream(social), true);
|
2022-11-10 01:45:15 +01:00
|
|
|
|
|
|
|
page_params.is_spectator = true;
|
|
|
|
assert.equal(stream_data.can_post_messages_in_stream(social), false);
|
2021-12-27 10:27:03 +01:00
|
|
|
});
|
2022-12-29 17:49:38 +01:00
|
|
|
|
|
|
|
test("can_unsubscribe_others", () => {
|
|
|
|
const admin_user_id = 1;
|
|
|
|
const moderator_user_id = 2;
|
|
|
|
const member_user_id = 3;
|
|
|
|
|
|
|
|
const admins = {
|
|
|
|
name: "Admins",
|
|
|
|
id: 1,
|
|
|
|
members: new Set([admin_user_id]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([]),
|
|
|
|
};
|
|
|
|
const moderators = {
|
|
|
|
name: "Moderators",
|
|
|
|
id: 2,
|
|
|
|
members: new Set([moderator_user_id]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([1]),
|
|
|
|
};
|
|
|
|
const all = {
|
|
|
|
name: "Everyone",
|
|
|
|
id: 3,
|
|
|
|
members: new Set([member_user_id]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([2]),
|
|
|
|
};
|
|
|
|
const nobody = {
|
|
|
|
name: "Nobody",
|
|
|
|
id: 4,
|
|
|
|
members: new Set([]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([]),
|
|
|
|
};
|
|
|
|
|
|
|
|
user_groups.initialize({realm_user_groups: [admins, moderators, all, nobody]});
|
|
|
|
|
|
|
|
const sub = {
|
|
|
|
name: "Denmark",
|
|
|
|
subscribed: true,
|
|
|
|
color: "red",
|
|
|
|
stream_id: 1,
|
2023-07-12 12:57:57 +02:00
|
|
|
can_remove_subscribers_group: admins.id,
|
2022-12-29 17:49:38 +01:00
|
|
|
};
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
|
|
|
|
people.initialize_current_user(admin_user_id);
|
|
|
|
assert.equal(stream_data.can_unsubscribe_others(sub), true);
|
|
|
|
people.initialize_current_user(moderator_user_id);
|
|
|
|
assert.equal(stream_data.can_unsubscribe_others(sub), false);
|
|
|
|
|
2023-07-12 12:57:57 +02:00
|
|
|
sub.can_remove_subscribers_group = moderators.id;
|
2022-12-29 17:49:38 +01:00
|
|
|
people.initialize_current_user(admin_user_id);
|
|
|
|
assert.equal(stream_data.can_unsubscribe_others(sub), true);
|
|
|
|
people.initialize_current_user(moderator_user_id);
|
|
|
|
assert.equal(stream_data.can_unsubscribe_others(sub), true);
|
|
|
|
people.initialize_current_user(member_user_id);
|
|
|
|
assert.equal(stream_data.can_unsubscribe_others(sub), false);
|
|
|
|
|
2023-07-12 12:57:57 +02:00
|
|
|
sub.can_remove_subscribers_group = all.id;
|
2022-12-29 17:49:38 +01:00
|
|
|
people.initialize_current_user(admin_user_id);
|
|
|
|
assert.equal(stream_data.can_unsubscribe_others(sub), true);
|
|
|
|
people.initialize_current_user(moderator_user_id);
|
|
|
|
assert.equal(stream_data.can_unsubscribe_others(sub), true);
|
|
|
|
people.initialize_current_user(member_user_id);
|
|
|
|
assert.equal(stream_data.can_unsubscribe_others(sub), true);
|
|
|
|
|
|
|
|
// Even with the nobody system group, admins can still unsubscribe others.
|
2023-07-12 12:57:57 +02:00
|
|
|
sub.can_remove_subscribers_group = nobody.id;
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = true;
|
2022-12-29 17:49:38 +01:00
|
|
|
assert.equal(stream_data.can_unsubscribe_others(sub), true);
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
2022-12-29 17:49:38 +01:00
|
|
|
assert.equal(stream_data.can_unsubscribe_others(sub), false);
|
|
|
|
|
|
|
|
// This isn't a real state, but we want coverage on !can_view_subscribers.
|
|
|
|
sub.subscribed = false;
|
|
|
|
sub.invite_only = true;
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = true;
|
2022-12-29 17:49:38 +01:00
|
|
|
assert.equal(stream_data.can_unsubscribe_others(sub), true);
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
2022-12-29 17:49:38 +01:00
|
|
|
assert.equal(stream_data.can_unsubscribe_others(sub), false);
|
|
|
|
});
|
2023-06-20 11:30:30 +02:00
|
|
|
|
|
|
|
test("options for dropdown widget", () => {
|
|
|
|
const denmark = {
|
|
|
|
subscribed: true,
|
|
|
|
color: "blue",
|
|
|
|
name: "Denmark",
|
|
|
|
stream_id: 1,
|
|
|
|
is_muted: true,
|
|
|
|
invite_only: true,
|
|
|
|
history_public_to_subscribers: true,
|
|
|
|
};
|
|
|
|
const social = {
|
|
|
|
subscribed: true,
|
|
|
|
color: "red",
|
|
|
|
name: "social",
|
|
|
|
stream_id: 2,
|
|
|
|
is_muted: false,
|
|
|
|
invite_only: true,
|
|
|
|
history_public_to_subscribers: false,
|
2023-10-04 22:48:28 +02:00
|
|
|
stream_post_policy: settings_config.stream_post_policy_values.admins.code,
|
2023-06-20 11:30:30 +02:00
|
|
|
};
|
|
|
|
const test = {
|
|
|
|
subscribed: true,
|
|
|
|
color: "yellow",
|
|
|
|
name: "test",
|
|
|
|
stream_id: 3,
|
|
|
|
is_muted: true,
|
|
|
|
invite_only: false,
|
|
|
|
};
|
|
|
|
const web_public_stream = {
|
|
|
|
subscribed: true,
|
|
|
|
color: "yellow",
|
|
|
|
name: "web_public_stream",
|
|
|
|
stream_id: 4,
|
|
|
|
is_muted: false,
|
|
|
|
invite_only: false,
|
|
|
|
history_public_to_subscribers: true,
|
|
|
|
is_web_public: true,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(denmark);
|
|
|
|
stream_data.add_sub(social);
|
|
|
|
stream_data.add_sub(web_public_stream);
|
|
|
|
stream_data.add_sub(test);
|
|
|
|
|
|
|
|
assert.deepEqual(stream_data.get_options_for_dropdown_widget(), [
|
|
|
|
{
|
|
|
|
name: "Denmark",
|
|
|
|
stream: {
|
|
|
|
subscribed: true,
|
|
|
|
color: "blue",
|
|
|
|
name: "Denmark",
|
|
|
|
stream_id: 1,
|
|
|
|
is_muted: true,
|
|
|
|
invite_only: true,
|
|
|
|
history_public_to_subscribers: true,
|
|
|
|
},
|
|
|
|
unique_id: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "social",
|
|
|
|
stream: {
|
|
|
|
color: "red",
|
|
|
|
history_public_to_subscribers: false,
|
|
|
|
invite_only: true,
|
|
|
|
is_muted: false,
|
|
|
|
name: "social",
|
|
|
|
stream_id: 2,
|
|
|
|
stream_post_policy: 2,
|
|
|
|
subscribed: true,
|
|
|
|
},
|
|
|
|
unique_id: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test",
|
|
|
|
stream: {
|
|
|
|
color: "yellow",
|
|
|
|
invite_only: false,
|
|
|
|
is_muted: true,
|
|
|
|
name: "test",
|
|
|
|
stream_id: 3,
|
|
|
|
subscribed: true,
|
|
|
|
},
|
|
|
|
unique_id: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "web_public_stream",
|
|
|
|
stream: {
|
|
|
|
subscribed: true,
|
|
|
|
color: "yellow",
|
|
|
|
name: "web_public_stream",
|
|
|
|
stream_id: 4,
|
|
|
|
is_muted: false,
|
|
|
|
invite_only: false,
|
|
|
|
history_public_to_subscribers: true,
|
|
|
|
is_web_public: true,
|
|
|
|
},
|
|
|
|
unique_id: 4,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
2023-09-29 19:54:03 +02:00
|
|
|
|
|
|
|
test("can_access_stream_email", () => {
|
|
|
|
const social = {
|
|
|
|
subscribed: true,
|
|
|
|
color: "red",
|
|
|
|
name: "social",
|
|
|
|
stream_id: 2,
|
|
|
|
is_muted: false,
|
|
|
|
invite_only: true,
|
|
|
|
history_public_to_subscribers: false,
|
|
|
|
};
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
2023-09-29 19:54:03 +02:00
|
|
|
assert.equal(stream_data.can_access_stream_email(social), true);
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = true;
|
2023-09-29 19:54:03 +02:00
|
|
|
assert.equal(stream_data.can_access_stream_email(social), true);
|
|
|
|
|
|
|
|
social.subscribed = false;
|
|
|
|
assert.equal(stream_data.can_access_stream_email(social), false);
|
|
|
|
|
|
|
|
social.invite_only = false;
|
|
|
|
assert.equal(stream_data.can_access_stream_email(social), true);
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
2023-09-29 19:54:03 +02:00
|
|
|
assert.equal(stream_data.can_access_stream_email(social), true);
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_guest = true;
|
2023-09-29 19:54:03 +02:00
|
|
|
assert.equal(stream_data.can_access_stream_email(social), false);
|
|
|
|
|
|
|
|
social.subscribed = true;
|
|
|
|
assert.equal(stream_data.can_access_stream_email(social), true);
|
|
|
|
|
|
|
|
social.is_web_public = true;
|
|
|
|
assert.equal(stream_data.can_access_stream_email(social), true);
|
|
|
|
|
|
|
|
social.subscribed = false;
|
|
|
|
assert.equal(stream_data.can_access_stream_email(social), true);
|
|
|
|
|
|
|
|
page_params.is_spectator = true;
|
|
|
|
assert.equal(stream_data.can_access_stream_email(social), false);
|
|
|
|
});
|