2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-03-25 22:35:45 +01:00
|
|
|
const {zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-07-28 16:00:58 +02:00
|
|
|
const {page_params, user_settings} = require("../zjsunit/zpage_params");
|
2020-12-01 23:21:38 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const settings_data = zrequire("settings_data");
|
|
|
|
const settings_config = zrequire("settings_config");
|
2020-02-25 12:46:14 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Some methods in settings_data are fairly
|
|
|
|
trivial, so the meaningful tests happen
|
|
|
|
at the higher layers, such as when we
|
|
|
|
test people.js.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const isaac = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "isaac@example.com",
|
|
|
|
delivery_email: "isaac-delivery@example.com",
|
2021-04-28 20:54:03 +02:00
|
|
|
user_id: 30,
|
|
|
|
full_name: "Isaac",
|
2020-02-25 12:46:14 +01:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("email_for_user_settings", () => {
|
2020-02-25 12:46:14 +01:00
|
|
|
const email = settings_data.email_for_user_settings;
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
page_params.realm_email_address_visibility =
|
|
|
|
settings_config.email_address_visibility_values.admins_only.code;
|
2020-02-25 12:46:14 +01:00
|
|
|
assert.equal(email(isaac), undefined);
|
|
|
|
|
|
|
|
page_params.is_admin = true;
|
|
|
|
assert.equal(email(isaac), isaac.delivery_email);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
page_params.realm_email_address_visibility =
|
|
|
|
settings_config.email_address_visibility_values.nobody.code;
|
2020-03-07 01:15:34 +01:00
|
|
|
assert.equal(email(isaac), undefined);
|
2020-02-25 12:46:14 +01:00
|
|
|
|
|
|
|
page_params.is_admin = false;
|
2020-03-07 01:15:34 +01:00
|
|
|
assert.equal(email(isaac), undefined);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
page_params.realm_email_address_visibility =
|
|
|
|
settings_config.email_address_visibility_values.everyone.code;
|
2020-02-25 12:46:14 +01:00
|
|
|
assert.equal(email(isaac), isaac.email);
|
2021-05-21 18:07:30 +02:00
|
|
|
|
|
|
|
page_params.realm_email_address_visibility =
|
|
|
|
settings_config.email_address_visibility_values.moderators.code;
|
|
|
|
assert.equal(email(isaac), undefined);
|
|
|
|
|
|
|
|
page_params.is_moderator = true;
|
|
|
|
assert.equal(email(isaac), isaac.delivery_email);
|
|
|
|
|
|
|
|
page_params.is_moderator = false;
|
|
|
|
page_params.is_admin = true;
|
|
|
|
assert.equal(email(isaac), isaac.delivery_email);
|
2020-02-25 12:46:14 +01:00
|
|
|
});
|
2020-06-17 07:43:53 +02:00
|
|
|
|
2022-12-09 17:34:00 +01:00
|
|
|
run_test("user_can_change_email", () => {
|
|
|
|
const can_change_email = settings_data.user_can_change_email;
|
|
|
|
|
|
|
|
page_params.is_admin = true;
|
|
|
|
assert.equal(can_change_email(), true);
|
|
|
|
|
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.realm_email_changes_disabled = true;
|
|
|
|
assert.equal(can_change_email(), false);
|
|
|
|
|
|
|
|
page_params.realm_email_changes_disabled = false;
|
|
|
|
assert.equal(can_change_email(), true);
|
|
|
|
});
|
|
|
|
|
2021-03-21 16:46:13 +01:00
|
|
|
run_test("user_can_change_name", () => {
|
|
|
|
const can_change_name = settings_data.user_can_change_name;
|
|
|
|
|
|
|
|
page_params.is_admin = true;
|
|
|
|
assert.equal(can_change_name(), true);
|
|
|
|
|
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.realm_name_changes_disabled = true;
|
|
|
|
page_params.server_name_changes_disabled = false;
|
|
|
|
assert.equal(can_change_name(), false);
|
|
|
|
|
|
|
|
page_params.realm_name_changes_disabled = false;
|
|
|
|
page_params.server_name_changes_disabled = false;
|
|
|
|
assert.equal(can_change_name(), true);
|
|
|
|
|
|
|
|
page_params.realm_name_changes_disabled = false;
|
|
|
|
page_params.server_name_changes_disabled = true;
|
|
|
|
assert.equal(can_change_name(), false);
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test("user_can_change_avatar", () => {
|
|
|
|
const can_change_avatar = settings_data.user_can_change_avatar;
|
|
|
|
|
|
|
|
page_params.is_admin = true;
|
|
|
|
assert.equal(can_change_avatar(), true);
|
|
|
|
|
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.realm_avatar_changes_disabled = true;
|
|
|
|
page_params.server_avatar_changes_disabled = false;
|
|
|
|
assert.equal(can_change_avatar(), false);
|
|
|
|
|
|
|
|
page_params.realm_avatar_changes_disabled = false;
|
|
|
|
page_params.server_avatar_changes_disabled = false;
|
|
|
|
assert.equal(can_change_avatar(), true);
|
|
|
|
|
|
|
|
page_params.realm_avatar_changes_disabled = false;
|
|
|
|
page_params.server_avatar_changes_disabled = true;
|
|
|
|
assert.equal(can_change_avatar(), false);
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("user_can_change_logo", () => {
|
2020-06-17 07:43:53 +02:00
|
|
|
const can_change_logo = settings_data.user_can_change_logo;
|
|
|
|
|
|
|
|
page_params.is_admin = true;
|
|
|
|
page_params.zulip_plan_is_not_limited = true;
|
|
|
|
assert.equal(can_change_logo(), true);
|
|
|
|
|
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.zulip_plan_is_not_limited = false;
|
|
|
|
assert.equal(can_change_logo(), false);
|
|
|
|
|
|
|
|
page_params.is_admin = true;
|
|
|
|
page_params.zulip_plan_is_not_limited = false;
|
|
|
|
assert.equal(can_change_logo(), false);
|
|
|
|
|
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.zulip_plan_is_not_limited = true;
|
|
|
|
assert.equal(can_change_logo(), false);
|
|
|
|
});
|
2021-04-28 20:54:03 +02:00
|
|
|
|
2021-06-27 10:00:11 +02:00
|
|
|
run_test("user_can_unsubscribe_other_users", () => {
|
|
|
|
page_params.is_admin = true;
|
|
|
|
assert.equal(settings_data.user_can_unsubscribe_other_users(), true);
|
|
|
|
|
|
|
|
page_params.is_admin = false;
|
|
|
|
assert.equal(settings_data.user_can_unsubscribe_other_users(), false);
|
|
|
|
});
|
|
|
|
|
2021-04-30 09:24:29 +02:00
|
|
|
function test_policy(label, policy, validation_func) {
|
|
|
|
run_test(label, () => {
|
|
|
|
page_params.is_admin = true;
|
|
|
|
page_params[policy] = settings_config.common_policy_values.by_admins_only.code;
|
|
|
|
assert.equal(validation_func(), true);
|
|
|
|
|
|
|
|
page_params.is_admin = false;
|
|
|
|
assert.equal(validation_func(), false);
|
|
|
|
|
|
|
|
page_params.is_moderator = true;
|
|
|
|
page_params[policy] = settings_config.common_policy_values.by_moderators_only.code;
|
|
|
|
assert.equal(validation_func(), true);
|
|
|
|
|
|
|
|
page_params.is_moderator = false;
|
|
|
|
assert.equal(validation_func(), false);
|
|
|
|
|
|
|
|
page_params.is_guest = true;
|
|
|
|
page_params[policy] = settings_config.common_policy_values.by_members.code;
|
|
|
|
assert.equal(validation_func(), false);
|
|
|
|
|
|
|
|
page_params.is_guest = false;
|
|
|
|
assert.equal(validation_func(), true);
|
|
|
|
|
2021-07-22 08:00:06 +02:00
|
|
|
page_params.is_spectator = true;
|
|
|
|
page_params[policy] = settings_config.common_policy_values.by_members.code;
|
|
|
|
assert.equal(validation_func(), false);
|
|
|
|
|
|
|
|
page_params.is_spectator = false;
|
|
|
|
assert.equal(validation_func(), true);
|
|
|
|
|
2021-04-30 09:24:29 +02:00
|
|
|
page_params[policy] = settings_config.common_policy_values.by_full_members.code;
|
|
|
|
page_params.user_id = 30;
|
|
|
|
isaac.date_joined = new Date(Date.now());
|
2021-07-19 19:26:17 +02:00
|
|
|
settings_data.initialize(isaac.date_joined);
|
2021-04-30 09:24:29 +02:00
|
|
|
page_params.realm_waiting_period_threshold = 10;
|
|
|
|
assert.equal(validation_func(), false);
|
|
|
|
|
|
|
|
isaac.date_joined = new Date(Date.now() - 20 * 86400000);
|
2021-07-19 19:26:17 +02:00
|
|
|
settings_data.initialize(isaac.date_joined);
|
2021-04-30 09:24:29 +02:00
|
|
|
assert.equal(validation_func(), true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
test_policy(
|
2021-03-27 05:48:37 +01:00
|
|
|
"user_can_create_private_streams",
|
|
|
|
"realm_create_private_stream_policy",
|
|
|
|
settings_data.user_can_create_private_streams,
|
|
|
|
);
|
|
|
|
test_policy(
|
|
|
|
"user_can_create_public_streams",
|
|
|
|
"realm_create_public_stream_policy",
|
|
|
|
settings_data.user_can_create_public_streams,
|
2021-04-30 09:24:29 +02:00
|
|
|
);
|
|
|
|
test_policy(
|
|
|
|
"user_can_subscribe_other_users",
|
|
|
|
"realm_invite_to_stream_policy",
|
|
|
|
settings_data.user_can_subscribe_other_users,
|
|
|
|
);
|
|
|
|
test_policy(
|
|
|
|
"user_can_invite_others_to_realm",
|
|
|
|
"realm_invite_to_realm_policy",
|
|
|
|
settings_data.user_can_invite_others_to_realm,
|
|
|
|
);
|
2021-04-30 09:35:20 +02:00
|
|
|
test_policy(
|
|
|
|
"user_can_move_messages_between_streams",
|
|
|
|
"realm_move_messages_between_streams_policy",
|
|
|
|
settings_data.user_can_move_messages_between_streams,
|
|
|
|
);
|
2021-06-03 15:47:03 +02:00
|
|
|
test_policy(
|
|
|
|
"user_can_edit_user_groups",
|
|
|
|
"realm_user_group_edit_policy",
|
|
|
|
settings_data.user_can_edit_user_groups,
|
|
|
|
);
|
2021-05-17 19:32:35 +02:00
|
|
|
test_policy(
|
|
|
|
"user_can_add_custom_emoji",
|
|
|
|
"realm_add_custom_emoji_policy",
|
|
|
|
settings_data.user_can_add_custom_emoji,
|
|
|
|
);
|
2021-05-26 21:20:11 +02:00
|
|
|
|
2021-06-23 15:42:54 +02:00
|
|
|
function test_message_policy(label, policy, validation_func) {
|
|
|
|
run_test(label, () => {
|
|
|
|
page_params.is_admin = true;
|
|
|
|
page_params[policy] = settings_config.common_message_policy_values.by_admins_only.code;
|
|
|
|
assert.equal(validation_func(), true);
|
2021-05-26 21:20:11 +02:00
|
|
|
|
2021-06-23 15:42:54 +02:00
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.is_moderator = true;
|
|
|
|
assert.equal(validation_func(), false);
|
2021-05-26 21:20:11 +02:00
|
|
|
|
2021-06-23 15:42:54 +02:00
|
|
|
page_params[policy] = settings_config.common_message_policy_values.by_moderators_only.code;
|
|
|
|
assert.equal(validation_func(), true);
|
2021-05-26 21:20:11 +02:00
|
|
|
|
2021-06-23 15:42:54 +02:00
|
|
|
page_params.is_moderator = false;
|
|
|
|
assert.equal(validation_func(), false);
|
2021-05-26 21:20:11 +02:00
|
|
|
|
2021-06-23 15:42:54 +02:00
|
|
|
page_params.is_guest = true;
|
|
|
|
page_params[policy] = settings_config.common_message_policy_values.by_everyone.code;
|
|
|
|
assert.equal(validation_func(), true);
|
|
|
|
|
|
|
|
page_params[policy] = settings_config.common_message_policy_values.by_members.code;
|
|
|
|
assert.equal(validation_func(), false);
|
|
|
|
|
|
|
|
page_params.is_guest = false;
|
|
|
|
assert.equal(validation_func(), true);
|
|
|
|
|
|
|
|
page_params[policy] = settings_config.common_message_policy_values.by_full_members.code;
|
|
|
|
page_params.user_id = 30;
|
|
|
|
isaac.date_joined = new Date(Date.now());
|
|
|
|
page_params.realm_waiting_period_threshold = 10;
|
2021-07-19 19:26:17 +02:00
|
|
|
settings_data.initialize(isaac.date_joined);
|
2021-06-23 15:42:54 +02:00
|
|
|
assert.equal(validation_func(), false);
|
|
|
|
|
|
|
|
isaac.date_joined = new Date(Date.now() - 20 * 86400000);
|
2021-07-19 19:26:17 +02:00
|
|
|
settings_data.initialize(isaac.date_joined);
|
2021-06-23 15:42:54 +02:00
|
|
|
assert.equal(validation_func(), true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
test_message_policy(
|
2022-10-18 13:48:30 +02:00
|
|
|
"user_can_move_messages_to_another_topic",
|
2021-06-23 15:42:54 +02:00
|
|
|
"realm_edit_topic_policy",
|
2022-10-18 13:48:30 +02:00
|
|
|
settings_data.user_can_move_messages_to_another_topic,
|
2021-06-23 15:42:54 +02:00
|
|
|
);
|
2021-07-08 13:14:17 +02:00
|
|
|
|
2022-10-18 13:48:30 +02:00
|
|
|
run_test("user_can_move_messages_to_another_topic_nobody_case", () => {
|
2022-09-28 16:30:10 +02:00
|
|
|
page_params.is_admin = true;
|
|
|
|
page_params.is_guest = false;
|
2022-09-29 09:03:12 +02:00
|
|
|
page_params.realm_edit_topic_policy = settings_config.edit_topic_policy_values.nobody.code;
|
2022-10-18 13:48:30 +02:00
|
|
|
assert.equal(settings_data.user_can_move_messages_to_another_topic(), false);
|
2022-09-28 16:30:10 +02:00
|
|
|
});
|
|
|
|
|
2022-09-29 09:03:12 +02:00
|
|
|
run_test("user_can_move_messages_between_streams_nobody_case", () => {
|
|
|
|
page_params.is_admin = true;
|
|
|
|
page_params.is_guest = false;
|
|
|
|
page_params.realm_move_messages_between_streams_policy =
|
|
|
|
settings_config.move_messages_between_streams_policy_values.nobody.code;
|
|
|
|
assert.equal(settings_data.user_can_move_messages_between_streams(), false);
|
|
|
|
});
|
|
|
|
|
2021-06-23 12:53:38 +02:00
|
|
|
test_message_policy(
|
|
|
|
"user_can_delete_own_message",
|
|
|
|
"realm_delete_own_message_policy",
|
|
|
|
settings_data.user_can_delete_own_message,
|
|
|
|
);
|
|
|
|
|
2021-07-08 13:14:17 +02:00
|
|
|
run_test("using_dark_theme", () => {
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.color_scheme = settings_config.color_scheme_values.night.code;
|
2021-07-08 13:14:17 +02:00
|
|
|
assert.equal(settings_data.using_dark_theme(), true);
|
|
|
|
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.color_scheme = settings_config.color_scheme_values.automatic.code;
|
2021-07-08 13:14:17 +02:00
|
|
|
|
|
|
|
window.matchMedia = (query) => {
|
|
|
|
assert.equal(query, "(prefers-color-scheme: dark)");
|
|
|
|
return {matches: true};
|
|
|
|
};
|
|
|
|
assert.equal(settings_data.using_dark_theme(), true);
|
|
|
|
|
|
|
|
window.matchMedia = (query) => {
|
|
|
|
assert.equal(query, "(prefers-color-scheme: dark)");
|
|
|
|
return {matches: false};
|
|
|
|
};
|
|
|
|
assert.equal(settings_data.using_dark_theme(), false);
|
|
|
|
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.color_scheme = settings_config.color_scheme_values.day.code;
|
2021-07-08 13:14:17 +02:00
|
|
|
assert.equal(settings_data.using_dark_theme(), false);
|
|
|
|
});
|
2021-07-18 18:18:28 +02:00
|
|
|
|
|
|
|
run_test("user_can_invite_others_to_realm_nobody_case", () => {
|
|
|
|
page_params.is_admin = true;
|
|
|
|
page_params.is_guest = false;
|
|
|
|
page_params.realm_invite_to_realm_policy =
|
|
|
|
settings_config.invite_to_realm_policy_values.nobody.code;
|
|
|
|
assert.equal(settings_data.user_can_invite_others_to_realm(), false);
|
|
|
|
});
|
2021-11-19 17:36:00 +01:00
|
|
|
|
|
|
|
run_test("user_can_create_web_public_streams", () => {
|
|
|
|
page_params.is_owner = true;
|
|
|
|
page_params.server_web_public_streams_enabled = true;
|
|
|
|
page_params.realm_enable_spectator_access = true;
|
|
|
|
page_params.realm_create_web_public_stream_policy =
|
|
|
|
settings_config.create_web_public_stream_policy_values.nobody.code;
|
|
|
|
assert.equal(settings_data.user_can_create_web_public_streams(), false);
|
|
|
|
|
|
|
|
page_params.realm_create_web_public_stream_policy =
|
|
|
|
settings_config.create_web_public_stream_policy_values.by_owners_only.code;
|
|
|
|
assert.equal(settings_data.user_can_create_web_public_streams(), true);
|
|
|
|
|
|
|
|
page_params.realm_enable_spectator_access = false;
|
|
|
|
page_params.server_web_public_streams_enabled = true;
|
|
|
|
assert.equal(settings_data.user_can_create_web_public_streams(), false);
|
|
|
|
|
|
|
|
page_params.realm_enable_spectator_access = true;
|
|
|
|
page_params.server_web_public_streams_enabled = false;
|
|
|
|
assert.equal(settings_data.user_can_create_web_public_streams(), false);
|
|
|
|
|
|
|
|
page_params.realm_enable_spectator_access = false;
|
|
|
|
page_params.server_web_public_streams_enabled = false;
|
|
|
|
assert.equal(settings_data.user_can_create_web_public_streams(), false);
|
|
|
|
|
|
|
|
page_params.realm_enable_spectator_access = true;
|
|
|
|
page_params.server_web_public_streams_enabled = true;
|
|
|
|
page_params.is_owner = false;
|
|
|
|
page_params.is_admin = true;
|
|
|
|
assert.equal(settings_data.user_can_create_web_public_streams(), false);
|
|
|
|
|
|
|
|
page_params.realm_create_web_public_stream_policy =
|
|
|
|
settings_config.create_web_public_stream_policy_values.by_admins_only.code;
|
|
|
|
assert.equal(settings_data.user_can_create_web_public_streams(), true);
|
|
|
|
|
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.is_moderator = true;
|
|
|
|
assert.equal(settings_data.user_can_create_web_public_streams(), false);
|
|
|
|
|
|
|
|
page_params.realm_create_web_public_stream_policy =
|
|
|
|
settings_config.create_web_public_stream_policy_values.by_moderators_only.code;
|
|
|
|
assert.equal(settings_data.user_can_create_web_public_streams(), true);
|
|
|
|
|
|
|
|
page_params.is_moderator = false;
|
|
|
|
assert.equal(settings_data.user_can_create_web_public_streams(), false);
|
|
|
|
});
|