2021-05-19 18:55:36 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2022-08-19 21:36:33 +02:00
|
|
|
const {mock_banners} = require("./lib/compose_banner");
|
2023-02-22 23:04:10 +01:00
|
|
|
const {$t} = require("./lib/i18n");
|
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
2023-12-14 23:51:33 +01:00
|
|
|
const {run_test, noop} = require("./lib/test");
|
2023-02-22 23:04:10 +01:00
|
|
|
const blueslip = require("./lib/zblueslip");
|
|
|
|
const $ = require("./lib/zjquery");
|
2024-03-28 01:04:36 +01:00
|
|
|
const {current_user, realm} = require("./lib/zpage_params");
|
2022-08-19 21:36:33 +02:00
|
|
|
|
2022-08-30 22:24:54 +02:00
|
|
|
const compose_banner = zrequire("compose_banner");
|
2021-05-19 18:55:36 +02:00
|
|
|
const compose_pm_pill = zrequire("compose_pm_pill");
|
2022-08-19 21:36:33 +02:00
|
|
|
const compose_state = zrequire("compose_state");
|
2021-06-25 15:16:29 +02:00
|
|
|
const compose_validate = zrequire("compose_validate");
|
2021-05-19 18:55:36 +02:00
|
|
|
const peer_data = zrequire("peer_data");
|
|
|
|
const people = zrequire("people");
|
2023-02-22 23:03:47 +01:00
|
|
|
const resolved_topic = zrequire("../shared/src/resolved_topic");
|
2021-06-25 15:16:29 +02:00
|
|
|
const settings_config = zrequire("settings_config");
|
2023-02-22 23:04:10 +01:00
|
|
|
const settings_data = mock_esm("../src/settings_data");
|
2021-05-19 18:55:36 +02:00
|
|
|
const stream_data = zrequire("stream_data");
|
2023-05-07 14:45:04 +02:00
|
|
|
const compose_recipient = zrequire("/compose_recipient");
|
2024-07-08 19:00:08 +02:00
|
|
|
const user_groups = zrequire("user_groups");
|
2021-05-19 18:55:36 +02:00
|
|
|
|
|
|
|
const me = {
|
|
|
|
email: "me@example.com",
|
|
|
|
user_id: 30,
|
|
|
|
full_name: "Me Myself",
|
|
|
|
date_joined: new Date(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const alice = {
|
|
|
|
email: "alice@example.com",
|
|
|
|
user_id: 31,
|
|
|
|
full_name: "Alice",
|
|
|
|
};
|
|
|
|
|
|
|
|
const bob = {
|
|
|
|
email: "bob@example.com",
|
|
|
|
user_id: 32,
|
|
|
|
full_name: "Bob",
|
2024-07-08 19:00:08 +02:00
|
|
|
is_admin: true,
|
2021-05-19 18:55:36 +02:00
|
|
|
};
|
|
|
|
|
2023-06-27 01:40:25 +02:00
|
|
|
const social_sub = {
|
|
|
|
stream_id: 101,
|
|
|
|
name: "social",
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(social_sub);
|
|
|
|
|
2021-05-19 18:55:36 +02:00
|
|
|
people.add_active_user(me);
|
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
|
|
|
|
people.add_active_user(alice);
|
|
|
|
people.add_active_user(bob);
|
|
|
|
|
2021-07-23 00:22:20 +02:00
|
|
|
const welcome_bot = {
|
|
|
|
email: "welcome-bot@example.com",
|
|
|
|
user_id: 4,
|
|
|
|
full_name: "Welcome Bot",
|
|
|
|
is_bot: true,
|
|
|
|
// cross realm bots have no owner
|
|
|
|
};
|
|
|
|
|
|
|
|
people.add_cross_realm_user(welcome_bot);
|
|
|
|
|
2024-07-08 19:00:08 +02:00
|
|
|
const nobody = {
|
|
|
|
name: "role:nobody",
|
|
|
|
id: 1,
|
|
|
|
members: new Set([]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([]),
|
|
|
|
};
|
|
|
|
const everyone = {
|
|
|
|
name: "role:everyone",
|
|
|
|
id: 2,
|
|
|
|
members: new Set([30]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([]),
|
|
|
|
};
|
|
|
|
const admin = {
|
|
|
|
name: "role:administrators",
|
|
|
|
id: 3,
|
|
|
|
members: new Set([32]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([]),
|
|
|
|
};
|
|
|
|
|
|
|
|
user_groups.initialize({realm_user_groups: [nobody, everyone, admin]});
|
2021-05-19 18:55:36 +02:00
|
|
|
function test_ui(label, f) {
|
2023-10-09 20:41:12 +02:00
|
|
|
// The sloppy_$ flag lets us reuse setup from prior tests.
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test(label, (helpers) => {
|
2023-11-02 19:29:54 +01:00
|
|
|
$("textarea#compose-textarea").val("some message");
|
2022-07-10 01:06:33 +02:00
|
|
|
f(helpers);
|
2021-05-19 19:29:12 +02:00
|
|
|
});
|
2021-05-19 18:55:36 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:27:44 +02:00
|
|
|
function stub_message_row($textarea) {
|
|
|
|
const $stub = $.create("message_row_stub");
|
|
|
|
$textarea.closest = (selector) => {
|
|
|
|
assert.equal(selector, ".message_row");
|
|
|
|
$stub.length = 0;
|
|
|
|
return $stub;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-28 00:41:05 +02:00
|
|
|
test_ui("validate_stream_message_address_info", ({mock_template}) => {
|
2022-08-19 22:24:06 +02:00
|
|
|
mock_banners();
|
2023-06-27 01:40:25 +02:00
|
|
|
|
|
|
|
const party_sub = {
|
2021-05-19 18:55:36 +02:00
|
|
|
stream_id: 101,
|
2023-06-27 01:40:25 +02:00
|
|
|
name: "party",
|
2021-05-19 18:55:36 +02:00
|
|
|
subscribed: true,
|
|
|
|
};
|
2023-06-27 01:40:25 +02:00
|
|
|
stream_data.add_sub(party_sub);
|
2024-03-28 00:59:31 +01:00
|
|
|
assert.ok(compose_validate.validate_stream_message_address_info(party_sub));
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2023-06-27 01:40:25 +02:00
|
|
|
party_sub.subscribed = false;
|
|
|
|
stream_data.add_sub(party_sub);
|
2022-08-23 00:12:29 +02:00
|
|
|
$("#compose_banners .user_not_subscribed").length = 0;
|
|
|
|
let user_not_subscribed_rendered = false;
|
|
|
|
mock_template("compose_banner/compose_banner.hbs", true, (data, html) => {
|
2022-08-30 22:24:54 +02:00
|
|
|
assert.equal(data.classname, compose_banner.CLASSNAMES.user_not_subscribed);
|
2022-08-23 00:12:29 +02:00
|
|
|
user_not_subscribed_rendered = true;
|
|
|
|
return html;
|
|
|
|
});
|
2024-03-28 00:59:31 +01:00
|
|
|
assert.ok(!compose_validate.validate_stream_message_address_info(party_sub));
|
2022-08-23 00:12:29 +02:00
|
|
|
assert.ok(user_not_subscribed_rendered);
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2023-06-27 01:40:25 +02:00
|
|
|
party_sub.name = "Frontend";
|
|
|
|
party_sub.stream_id = 102;
|
|
|
|
stream_data.add_sub(party_sub);
|
2022-08-23 00:12:29 +02:00
|
|
|
user_not_subscribed_rendered = false;
|
2024-03-28 00:59:31 +01:00
|
|
|
assert.ok(!compose_validate.validate_stream_message_address_info(party_sub));
|
2022-08-23 00:12:29 +02:00
|
|
|
assert.ok(user_not_subscribed_rendered);
|
2021-05-19 18:55:36 +02:00
|
|
|
});
|
|
|
|
|
2023-10-05 08:04:23 +02:00
|
|
|
test_ui("validate", ({mock_template}) => {
|
2021-05-19 18:55:36 +02:00
|
|
|
function initialize_pm_pill() {
|
|
|
|
$.clear_all_elements();
|
|
|
|
|
|
|
|
$("#compose-send-button").prop("disabled", false);
|
|
|
|
$("#compose-send-button").trigger("focus");
|
2021-11-30 06:26:05 +01:00
|
|
|
$("#compose-send-button .loader").hide();
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $pm_pill_container = $.create("fake-pm-pill-container");
|
2021-05-19 18:55:36 +02:00
|
|
|
$("#private_message_recipient")[0] = {};
|
2022-01-25 11:36:19 +01:00
|
|
|
$("#private_message_recipient").set_parent($pm_pill_container);
|
|
|
|
$pm_pill_container.set_find_results(".input", $("#private_message_recipient"));
|
2023-12-14 23:51:33 +01:00
|
|
|
$("#private_message_recipient").before = noop;
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2023-06-18 08:58:48 +02:00
|
|
|
compose_pm_pill.initialize({
|
|
|
|
on_pill_create_or_remove: compose_recipient.update_placeholder_text,
|
|
|
|
});
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2023-12-14 23:51:33 +01:00
|
|
|
$("#zephyr-mirror-error").is = noop;
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2021-06-28 00:41:05 +02:00
|
|
|
mock_template("input_pill.hbs", false, () => "<div>pill-html</div>");
|
2022-08-19 22:24:06 +02:00
|
|
|
|
|
|
|
mock_banners();
|
2021-05-19 18:55:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function add_content_to_compose_box() {
|
2023-11-02 19:29:54 +01:00
|
|
|
$("textarea#compose-textarea").val("foobarfoobar");
|
2021-05-19 18:55:36 +02:00
|
|
|
}
|
|
|
|
|
2023-06-16 17:37:19 +02:00
|
|
|
// test validating direct messages
|
2021-05-19 18:55:36 +02:00
|
|
|
compose_state.set_message_type("private");
|
|
|
|
|
|
|
|
initialize_pm_pill();
|
|
|
|
add_content_to_compose_box();
|
2022-08-19 22:24:06 +02:00
|
|
|
compose_state.private_message_recipient("");
|
|
|
|
let pm_recipient_error_rendered = false;
|
2024-07-08 19:00:08 +02:00
|
|
|
realm.realm_direct_message_permission_group = everyone.id;
|
|
|
|
realm.realm_direct_message_initiator_group = everyone.id;
|
2022-08-19 22:24:06 +02:00
|
|
|
mock_template("compose_banner/compose_banner.hbs", false, (data) => {
|
2022-08-30 22:24:54 +02:00
|
|
|
assert.equal(data.classname, compose_banner.CLASSNAMES.missing_private_message_recipient);
|
2022-08-19 22:24:06 +02:00
|
|
|
assert.equal(
|
|
|
|
data.banner_text,
|
|
|
|
$t({defaultMessage: "Please specify at least one valid recipient."}),
|
|
|
|
);
|
|
|
|
pm_recipient_error_rendered = true;
|
2023-12-28 07:50:10 +01:00
|
|
|
return "<banner-stub>";
|
2022-08-19 22:24:06 +02:00
|
|
|
});
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2022-08-19 22:24:06 +02:00
|
|
|
assert.ok(pm_recipient_error_rendered);
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2022-08-19 22:24:06 +02:00
|
|
|
pm_recipient_error_rendered = false;
|
2021-05-19 18:55:36 +02:00
|
|
|
|
|
|
|
people.add_active_user(bob);
|
|
|
|
compose_state.private_message_recipient("bob@example.com");
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(compose_validate.validate());
|
2022-08-19 22:24:06 +02:00
|
|
|
assert.ok(!pm_recipient_error_rendered);
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2024-07-08 19:00:08 +02:00
|
|
|
realm.realm_direct_message_initiator_group = admin.id;
|
|
|
|
assert.ok(compose_validate.validate());
|
|
|
|
assert.ok(!pm_recipient_error_rendered);
|
|
|
|
|
|
|
|
realm.realm_direct_message_permission_group = admin.id;
|
|
|
|
assert.ok(compose_validate.validate());
|
|
|
|
assert.ok(!pm_recipient_error_rendered);
|
|
|
|
|
|
|
|
realm.realm_direct_message_initiator_group = everyone.id;
|
|
|
|
realm.realm_direct_message_permission_group = everyone.id;
|
2020-07-24 02:31:03 +02:00
|
|
|
people.deactivate(bob);
|
2022-08-19 22:24:06 +02:00
|
|
|
let deactivated_user_error_rendered = false;
|
|
|
|
mock_template("compose_banner/compose_banner.hbs", false, (data) => {
|
2022-08-30 22:24:54 +02:00
|
|
|
assert.equal(data.classname, compose_banner.CLASSNAMES.deactivated_user);
|
2022-08-19 22:24:06 +02:00
|
|
|
assert.equal(
|
|
|
|
data.banner_text,
|
|
|
|
$t({defaultMessage: "You cannot send messages to deactivated users."}),
|
|
|
|
);
|
|
|
|
deactivated_user_error_rendered = true;
|
2023-12-28 07:50:10 +01:00
|
|
|
return "<banner-stub>";
|
2022-08-19 22:24:06 +02:00
|
|
|
});
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2022-08-19 22:24:06 +02:00
|
|
|
assert.ok(deactivated_user_error_rendered);
|
2020-07-24 02:31:03 +02:00
|
|
|
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_is_zephyr_mirror_realm = true;
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(compose_validate.validate());
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_is_zephyr_mirror_realm = false;
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2021-07-23 00:22:20 +02:00
|
|
|
initialize_pm_pill();
|
|
|
|
add_content_to_compose_box();
|
|
|
|
compose_state.private_message_recipient("welcome-bot@example.com");
|
|
|
|
assert.ok(compose_validate.validate());
|
|
|
|
|
2022-08-19 22:24:06 +02:00
|
|
|
let zephyr_error_rendered = false;
|
|
|
|
mock_template("compose_banner/compose_banner.hbs", false, (data) => {
|
2022-08-30 22:24:54 +02:00
|
|
|
if (data.classname === compose_banner.CLASSNAMES.zephyr_not_running) {
|
2022-08-19 22:24:06 +02:00
|
|
|
assert.equal(
|
|
|
|
data.banner_text,
|
|
|
|
$t({
|
|
|
|
defaultMessage:
|
|
|
|
"You need to be running Zephyr mirroring in order to send messages!",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
zephyr_error_rendered = true;
|
|
|
|
}
|
2023-12-28 07:50:10 +01:00
|
|
|
return "<banner-stub>";
|
2022-08-19 22:24:06 +02:00
|
|
|
});
|
|
|
|
initialize_pm_pill();
|
|
|
|
compose_state.private_message_recipient("welcome-bot@example.com");
|
2023-11-02 19:29:54 +01:00
|
|
|
$("textarea#compose-textarea").toggleClass = (classname, value) => {
|
2023-01-03 09:35:18 +01:00
|
|
|
assert.equal(classname, "invalid");
|
|
|
|
assert.equal(value, true);
|
|
|
|
};
|
2022-08-19 22:24:06 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
|
|
|
assert.ok(!$("#compose-send-button .loader").visible());
|
|
|
|
assert.equal($("#compose-send-button").prop("disabled"), false);
|
|
|
|
compose_validate.validate();
|
|
|
|
|
|
|
|
add_content_to_compose_box();
|
|
|
|
let zephyr_checked = false;
|
|
|
|
$("#zephyr-mirror-error").is = (arg) => {
|
|
|
|
assert.equal(arg, ":visible");
|
|
|
|
zephyr_checked = true;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
assert.ok(!compose_validate.validate());
|
|
|
|
assert.ok(zephyr_checked);
|
|
|
|
assert.ok(zephyr_error_rendered);
|
|
|
|
|
|
|
|
initialize_pm_pill();
|
|
|
|
add_content_to_compose_box();
|
|
|
|
|
|
|
|
// test validating stream messages
|
2021-05-19 18:55:36 +02:00
|
|
|
compose_state.set_message_type("stream");
|
2023-06-27 01:40:25 +02:00
|
|
|
compose_state.set_stream_id("");
|
2022-08-19 22:24:06 +02:00
|
|
|
let empty_stream_error_rendered = false;
|
|
|
|
mock_template("compose_banner/compose_banner.hbs", false, (data) => {
|
2022-08-30 22:24:54 +02:00
|
|
|
assert.equal(data.classname, compose_banner.CLASSNAMES.missing_stream);
|
2024-04-18 18:36:57 +02:00
|
|
|
assert.equal(data.banner_text, $t({defaultMessage: "Please specify a channel."}));
|
2022-08-19 22:24:06 +02:00
|
|
|
empty_stream_error_rendered = true;
|
2023-12-28 07:50:10 +01:00
|
|
|
return "<banner-stub>";
|
2022-08-19 22:24:06 +02:00
|
|
|
});
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2022-08-19 22:24:06 +02:00
|
|
|
assert.ok(empty_stream_error_rendered);
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2023-05-07 14:45:04 +02:00
|
|
|
const denmark = {
|
|
|
|
stream_id: 100,
|
|
|
|
name: "Denmark",
|
|
|
|
};
|
|
|
|
stream_data.add_sub(denmark);
|
2023-06-27 01:40:25 +02:00
|
|
|
compose_state.set_stream_id(denmark.stream_id);
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_mandatory_topics = true;
|
2021-05-19 18:55:36 +02:00
|
|
|
compose_state.topic("");
|
2022-08-19 22:24:06 +02:00
|
|
|
let missing_topic_error_rendered = false;
|
|
|
|
mock_template("compose_banner/compose_banner.hbs", false, (data) => {
|
2022-08-30 22:24:54 +02:00
|
|
|
assert.equal(data.classname, compose_banner.CLASSNAMES.topic_missing);
|
2022-08-19 22:24:06 +02:00
|
|
|
assert.equal(
|
|
|
|
data.banner_text,
|
|
|
|
$t({defaultMessage: "Topics are required in this organization."}),
|
|
|
|
);
|
|
|
|
missing_topic_error_rendered = true;
|
2023-12-28 07:50:10 +01:00
|
|
|
return "<banner-stub>";
|
2022-08-19 22:24:06 +02:00
|
|
|
});
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2022-08-19 22:24:06 +02:00
|
|
|
assert.ok(missing_topic_error_rendered);
|
2022-03-08 11:52:29 +01:00
|
|
|
|
2022-08-19 22:24:06 +02:00
|
|
|
missing_topic_error_rendered = false;
|
2022-03-08 11:52:29 +01:00
|
|
|
compose_state.topic("(no topic)");
|
|
|
|
assert.ok(!compose_validate.validate());
|
2022-08-19 22:24:06 +02:00
|
|
|
assert.ok(missing_topic_error_rendered);
|
2021-05-19 18:55:36 +02:00
|
|
|
});
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
test_ui("get_invalid_recipient_emails", ({override_rewire}) => {
|
2021-05-19 18:55:36 +02:00
|
|
|
const welcome_bot = {
|
|
|
|
email: "welcome-bot@example.com",
|
|
|
|
user_id: 124,
|
|
|
|
full_name: "Welcome Bot",
|
|
|
|
};
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.user_id = me.user_id;
|
2021-05-19 18:55:36 +02:00
|
|
|
|
|
|
|
const params = {};
|
|
|
|
params.realm_users = [];
|
|
|
|
params.realm_non_active_users = [];
|
|
|
|
params.cross_realm_bots = [welcome_bot];
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
people.initialize(current_user.user_id, params);
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(compose_state, "private_message_recipient", () => "welcome-bot@example.com");
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.deepEqual(compose_validate.get_invalid_recipient_emails(), []);
|
|
|
|
});
|
|
|
|
|
2023-11-24 15:06:42 +01:00
|
|
|
test_ui("test_stream_wildcard_mention_allowed", ({override_rewire}) => {
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.user_id = me.user_id;
|
2021-06-25 15:16:29 +02:00
|
|
|
|
2023-11-15 03:55:43 +01:00
|
|
|
// First, check for large streams (>15 subscribers) where the wildcard mention
|
|
|
|
// policy matters.
|
|
|
|
override_rewire(peer_data, "get_subscriber_count", () => 16);
|
|
|
|
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_wildcard_mention_policy =
|
2021-06-25 15:16:29 +02:00
|
|
|
settings_config.wildcard_mention_policy_values.by_everyone.code;
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_guest = true;
|
|
|
|
current_user.is_admin = false;
|
2023-11-24 15:06:42 +01:00
|
|
|
assert.ok(compose_validate.stream_wildcard_mention_allowed());
|
2021-06-25 15:16:29 +02:00
|
|
|
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_wildcard_mention_policy =
|
2021-06-25 15:16:29 +02:00
|
|
|
settings_config.wildcard_mention_policy_values.nobody.code;
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = true;
|
2023-11-24 15:06:42 +01:00
|
|
|
assert.ok(!compose_validate.stream_wildcard_mention_allowed());
|
2021-06-25 15:16:29 +02:00
|
|
|
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_wildcard_mention_policy =
|
2021-06-25 15:16:29 +02:00
|
|
|
settings_config.wildcard_mention_policy_values.by_members.code;
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_guest = true;
|
|
|
|
current_user.is_admin = false;
|
2023-11-24 15:06:42 +01:00
|
|
|
assert.ok(!compose_validate.stream_wildcard_mention_allowed());
|
2021-06-25 15:16:29 +02:00
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_guest = false;
|
2023-11-24 15:06:42 +01:00
|
|
|
assert.ok(compose_validate.stream_wildcard_mention_allowed());
|
2021-06-25 15:16:29 +02:00
|
|
|
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_wildcard_mention_policy =
|
2021-06-25 15:16:29 +02:00
|
|
|
settings_config.wildcard_mention_policy_values.by_moderators_only.code;
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_moderator = false;
|
2023-11-24 15:06:42 +01:00
|
|
|
assert.ok(!compose_validate.stream_wildcard_mention_allowed());
|
2021-06-25 15:16:29 +02:00
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_moderator = true;
|
2023-11-24 15:06:42 +01:00
|
|
|
assert.ok(compose_validate.stream_wildcard_mention_allowed());
|
2021-06-25 15:16:29 +02:00
|
|
|
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_wildcard_mention_policy =
|
2022-07-05 16:56:12 +02:00
|
|
|
settings_config.wildcard_mention_policy_values.by_admins_only.code;
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
2023-11-24 15:06:42 +01:00
|
|
|
assert.ok(!compose_validate.stream_wildcard_mention_allowed());
|
2021-06-25 15:16:29 +02:00
|
|
|
|
|
|
|
// TODO: Add a by_admins_only case when we implement stream-level administrators.
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = true;
|
2023-11-24 15:06:42 +01:00
|
|
|
assert.ok(compose_validate.stream_wildcard_mention_allowed());
|
2021-06-25 15:16:29 +02:00
|
|
|
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_wildcard_mention_policy =
|
2021-06-25 15:16:29 +02:00
|
|
|
settings_config.wildcard_mention_policy_values.by_full_members.code;
|
2024-02-13 02:08:16 +01:00
|
|
|
const person = people.get_by_user_id(current_user.user_id);
|
2021-06-25 15:16:29 +02:00
|
|
|
person.date_joined = new Date(Date.now());
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_waiting_period_threshold = 10;
|
2021-06-25 15:16:29 +02:00
|
|
|
|
2023-11-24 15:06:42 +01:00
|
|
|
assert.ok(compose_validate.stream_wildcard_mention_allowed());
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
2023-11-24 15:06:42 +01:00
|
|
|
assert.ok(!compose_validate.stream_wildcard_mention_allowed());
|
2023-11-15 03:55:43 +01:00
|
|
|
|
|
|
|
// Now, check for small streams (<=15 subscribers) where the wildcard mention
|
|
|
|
// policy doesn't matter; everyone is allowed to use wildcard mentions.
|
|
|
|
override_rewire(peer_data, "get_subscriber_count", () => 14);
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_wildcard_mention_policy =
|
2023-11-15 03:55:43 +01:00
|
|
|
settings_config.wildcard_mention_policy_values.by_admins_only.code;
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
|
|
|
current_user.is_guest = true;
|
2023-11-24 15:06:42 +01:00
|
|
|
assert.ok(compose_validate.stream_wildcard_mention_allowed());
|
2021-05-19 18:55:36 +02:00
|
|
|
});
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
test_ui("validate_stream_message", ({override_rewire, mock_template}) => {
|
2021-05-19 18:55:36 +02:00
|
|
|
// This test is in kind of continuation to test_validate but since it is
|
|
|
|
// primarily used to get coverage over functions called from validate()
|
|
|
|
// we are separating it up in different test. Though their relative position
|
|
|
|
// of execution should not be changed.
|
2022-08-19 22:24:06 +02:00
|
|
|
mock_banners();
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.user_id = me.user_id;
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_mandatory_topics = false;
|
2023-06-27 01:40:25 +02:00
|
|
|
|
|
|
|
const special_sub = {
|
2021-05-19 18:55:36 +02:00
|
|
|
stream_id: 101,
|
2023-06-27 01:40:25 +02:00
|
|
|
name: "special",
|
2021-05-19 18:55:36 +02:00
|
|
|
subscribed: true,
|
|
|
|
};
|
2023-06-27 01:40:25 +02:00
|
|
|
stream_data.add_sub(special_sub);
|
|
|
|
|
|
|
|
compose_state.set_stream_id(special_sub.stream_id);
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(compose_validate.validate());
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!$("#compose-all-everyone").visible());
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2023-08-04 23:40:48 +02:00
|
|
|
override_rewire(peer_data, "get_subscriber_count", (stream_id) => {
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal(stream_id, 101);
|
|
|
|
return 16;
|
2023-08-04 23:40:48 +02:00
|
|
|
});
|
2023-11-21 07:04:19 +01:00
|
|
|
let stream_wildcard_warning_rendered = false;
|
2022-08-20 21:07:32 +02:00
|
|
|
$("#compose_banner_area .wildcard_warning").length = 0;
|
2023-11-21 07:04:19 +01:00
|
|
|
mock_template("compose_banner/stream_wildcard_warning.hbs", false, (data) => {
|
|
|
|
stream_wildcard_warning_rendered = true;
|
2022-08-20 21:07:32 +02:00
|
|
|
assert.equal(data.subscriber_count, 16);
|
2023-12-28 07:50:10 +01:00
|
|
|
return "<banner-stub>";
|
2021-05-19 18:55:36 +02:00
|
|
|
});
|
|
|
|
|
2023-11-24 16:10:03 +01:00
|
|
|
override_rewire(compose_validate, "wildcard_mention_policy_authorizes_user", () => true);
|
2021-05-19 18:55:36 +02:00
|
|
|
compose_state.message_content("Hey @**all**");
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal($("#compose-send-button").prop("disabled"), false);
|
2023-11-21 07:04:19 +01:00
|
|
|
assert.ok(stream_wildcard_warning_rendered);
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2022-08-19 22:24:06 +02:00
|
|
|
let wildcards_not_allowed_rendered = false;
|
2023-11-21 20:12:13 +01:00
|
|
|
mock_template("compose_banner/wildcard_mention_not_allowed_error.hbs", false, (data) => {
|
2022-08-30 22:24:54 +02:00
|
|
|
assert.equal(data.classname, compose_banner.CLASSNAMES.wildcards_not_allowed);
|
2024-04-19 13:20:33 +02:00
|
|
|
assert.equal(data.wildcard_mention_string, "all");
|
2022-08-19 22:24:06 +02:00
|
|
|
wildcards_not_allowed_rendered = true;
|
2023-12-28 07:50:10 +01:00
|
|
|
return "<banner-stub>";
|
2022-08-19 22:24:06 +02:00
|
|
|
});
|
2023-11-24 16:10:03 +01:00
|
|
|
override_rewire(compose_validate, "wildcard_mention_policy_authorizes_user", () => false);
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2022-08-19 22:24:06 +02:00
|
|
|
assert.ok(wildcards_not_allowed_rendered);
|
2021-05-19 18:55:36 +02:00
|
|
|
});
|
|
|
|
|
2023-10-05 08:04:23 +02:00
|
|
|
test_ui("test_validate_stream_message_post_policy_admin_only", ({mock_template}) => {
|
|
|
|
// This test is in continuation with test_validate but it has been separated out
|
|
|
|
// for better readability. Their relative position of execution should not be changed.
|
|
|
|
// Although the position with respect to test_validate_stream_message does not matter
|
|
|
|
// as different stream is used for this test.
|
|
|
|
mock_banners();
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
2023-10-05 08:04:23 +02:00
|
|
|
const sub_stream_102 = {
|
|
|
|
stream_id: 102,
|
|
|
|
name: "stream102",
|
|
|
|
subscribed: true,
|
|
|
|
stream_post_policy: settings_config.stream_post_policy_values.admins.code,
|
|
|
|
};
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2023-10-05 08:04:23 +02:00
|
|
|
stream_data.add_sub(sub_stream_102);
|
|
|
|
compose_state.topic("topic102");
|
|
|
|
compose_state.set_stream_id(sub_stream_102.stream_id);
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2023-10-05 08:04:23 +02:00
|
|
|
let banner_rendered = false;
|
|
|
|
mock_template("compose_banner/compose_banner.hbs", false, (data) => {
|
|
|
|
assert.equal(data.classname, compose_banner.CLASSNAMES.no_post_permissions);
|
|
|
|
assert.equal(
|
|
|
|
data.banner_text,
|
|
|
|
$t({
|
2024-04-18 18:36:57 +02:00
|
|
|
defaultMessage: "You do not have permission to post in this channel.",
|
2023-10-05 08:04:23 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
banner_rendered = true;
|
2023-12-28 07:50:10 +01:00
|
|
|
return "<banner-stub>";
|
2023-10-05 08:04:23 +02:00
|
|
|
});
|
|
|
|
assert.ok(!compose_validate.validate());
|
|
|
|
assert.ok(banner_rendered);
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2023-10-05 08:04:23 +02:00
|
|
|
// Reset error message.
|
|
|
|
compose_state.set_stream_id(social_sub.stream_id);
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
|
|
|
current_user.is_guest = true;
|
2023-10-05 08:04:23 +02:00
|
|
|
|
|
|
|
compose_state.topic("topic102");
|
|
|
|
compose_state.set_stream_id(sub_stream_102.stream_id);
|
|
|
|
banner_rendered = false;
|
|
|
|
assert.ok(!compose_validate.validate());
|
|
|
|
assert.ok(banner_rendered);
|
|
|
|
});
|
|
|
|
|
|
|
|
test_ui("test_validate_stream_message_post_policy_moderators_only", ({mock_template}) => {
|
|
|
|
mock_banners();
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
|
|
|
current_user.is_moderator = false;
|
|
|
|
current_user.is_guest = false;
|
2023-10-05 08:04:23 +02:00
|
|
|
|
|
|
|
const sub = {
|
|
|
|
stream_id: 104,
|
|
|
|
name: "stream104",
|
|
|
|
subscribed: true,
|
|
|
|
stream_post_policy: settings_config.stream_post_policy_values.moderators.code,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
compose_state.topic("topic104");
|
|
|
|
compose_state.set_stream_id(sub.stream_id);
|
|
|
|
let banner_rendered = false;
|
|
|
|
mock_template("compose_banner/compose_banner.hbs", false, (data) => {
|
|
|
|
assert.equal(data.classname, compose_banner.CLASSNAMES.no_post_permissions);
|
|
|
|
assert.equal(
|
|
|
|
data.banner_text,
|
|
|
|
$t({
|
2024-04-18 18:36:57 +02:00
|
|
|
defaultMessage: "You do not have permission to post in this channel.",
|
2023-10-05 08:04:23 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
banner_rendered = true;
|
2023-12-28 07:50:10 +01:00
|
|
|
return "<banner-stub>";
|
2023-10-05 08:04:23 +02:00
|
|
|
});
|
|
|
|
assert.ok(!compose_validate.validate());
|
|
|
|
assert.ok(banner_rendered);
|
|
|
|
// Reset error message.
|
|
|
|
compose_state.set_stream_id(social_sub.stream_id);
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_guest = true;
|
2023-10-05 08:04:23 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
|
|
|
assert.ok(banner_rendered);
|
|
|
|
});
|
|
|
|
|
|
|
|
test_ui("test_validate_stream_message_post_policy_full_members_only", ({mock_template}) => {
|
|
|
|
mock_banners();
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.is_admin = false;
|
|
|
|
current_user.is_guest = true;
|
2023-10-05 08:04:23 +02:00
|
|
|
const sub = {
|
|
|
|
stream_id: 103,
|
|
|
|
name: "stream103",
|
|
|
|
subscribed: true,
|
|
|
|
stream_post_policy: settings_config.stream_post_policy_values.non_new_members.code,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
compose_state.topic("topic103");
|
|
|
|
compose_state.set_stream_id(sub.stream_id);
|
|
|
|
let banner_rendered = false;
|
|
|
|
mock_template("compose_banner/compose_banner.hbs", false, (data) => {
|
|
|
|
assert.equal(data.classname, compose_banner.CLASSNAMES.no_post_permissions);
|
|
|
|
assert.equal(
|
|
|
|
data.banner_text,
|
|
|
|
$t({
|
2024-04-18 18:36:57 +02:00
|
|
|
defaultMessage: "You do not have permission to post in this channel.",
|
2023-10-05 08:04:23 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
banner_rendered = true;
|
2023-12-28 07:50:10 +01:00
|
|
|
return "<banner-stub>";
|
2023-10-05 08:04:23 +02:00
|
|
|
});
|
|
|
|
assert.ok(!compose_validate.validate());
|
|
|
|
assert.ok(banner_rendered);
|
|
|
|
});
|
2021-07-07 12:13:19 +02:00
|
|
|
|
2022-08-19 22:24:06 +02:00
|
|
|
test_ui("test_check_overflow_text", ({mock_template}) => {
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.max_message_length = 10000;
|
2021-07-07 12:13:19 +02:00
|
|
|
|
2023-11-02 19:29:54 +01:00
|
|
|
const $textarea = $("textarea#compose-textarea");
|
2023-11-01 18:43:48 +01:00
|
|
|
const $indicator = $("#compose-limit-indicator");
|
2021-07-07 12:13:19 +02:00
|
|
|
|
|
|
|
// Indicator should show red colored text
|
2023-11-07 18:51:28 +01:00
|
|
|
let limit_indicator_html;
|
|
|
|
mock_template("compose_limit_indicator.hbs", true, (_data, html) => {
|
|
|
|
limit_indicator_html = html;
|
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
$textarea.val("a".repeat(10000 + 1));
|
2021-07-10 19:33:45 +02:00
|
|
|
compose_validate.check_overflow_text();
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($indicator.hasClass("over_limit"));
|
2024-04-24 08:46:47 +02:00
|
|
|
assert.equal(limit_indicator_html, "-1\n");
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($textarea.hasClass("over_limit"));
|
2023-11-10 21:00:25 +01:00
|
|
|
assert.ok($(".message-send-controls").hasClass("disabled-message-send-controls"));
|
2021-07-10 19:33:45 +02:00
|
|
|
|
2021-07-07 12:13:19 +02:00
|
|
|
// Indicator should show orange colored text
|
2024-04-24 08:46:47 +02:00
|
|
|
$textarea.val("a".repeat(9100));
|
2021-07-10 19:33:45 +02:00
|
|
|
compose_validate.check_overflow_text();
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok(!$indicator.hasClass("over_limit"));
|
2024-04-24 08:46:47 +02:00
|
|
|
assert.equal(limit_indicator_html, "900\n");
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok(!$textarea.hasClass("over_limit"));
|
2023-11-10 21:00:25 +01:00
|
|
|
assert.ok(!$(".message-send-controls").hasClass("disabled-message-send-controls"));
|
2021-07-07 12:13:19 +02:00
|
|
|
|
|
|
|
// Indicator must be empty
|
2024-04-24 08:46:47 +02:00
|
|
|
$textarea.val("a".repeat(9100 - 1));
|
2021-07-10 19:33:45 +02:00
|
|
|
compose_validate.check_overflow_text();
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok(!$indicator.hasClass("over_limit"));
|
|
|
|
assert.equal($indicator.text(), "");
|
|
|
|
assert.ok(!$textarea.hasClass("over_limit"));
|
2021-07-07 12:13:19 +02:00
|
|
|
});
|
2021-07-28 17:47:40 +02:00
|
|
|
|
|
|
|
test_ui("needs_subscribe_warning", () => {
|
|
|
|
const invalid_user_id = 999;
|
|
|
|
|
|
|
|
const test_bot = {
|
|
|
|
full_name: "Test Bot",
|
|
|
|
email: "test-bot@example.com",
|
|
|
|
user_id: 135,
|
|
|
|
is_bot: true,
|
|
|
|
};
|
|
|
|
people.add_active_user(test_bot);
|
|
|
|
|
|
|
|
const sub = {
|
|
|
|
stream_id: 110,
|
|
|
|
name: "stream",
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
peer_data.set_subscribers(sub.stream_id, [bob.user_id, me.user_id]);
|
|
|
|
|
2023-06-16 15:23:45 +02:00
|
|
|
blueslip.expect("error", "Unknown user_id in maybe_get_user_by_id");
|
2021-07-28 17:47:40 +02:00
|
|
|
// Test with an invalid user id.
|
|
|
|
assert.equal(compose_validate.needs_subscribe_warning(invalid_user_id, sub.stream_id), false);
|
|
|
|
|
|
|
|
// Test with bot user.
|
|
|
|
assert.equal(compose_validate.needs_subscribe_warning(test_bot.user_id, sub.stream_id), false);
|
|
|
|
|
|
|
|
// Test when user is subscribed to the stream.
|
|
|
|
assert.equal(compose_validate.needs_subscribe_warning(bob.user_id, sub.stream_id), false);
|
|
|
|
|
|
|
|
peer_data.remove_subscriber(sub.stream_id, bob.user_id);
|
|
|
|
// Test when the user is not subscribed.
|
|
|
|
assert.equal(compose_validate.needs_subscribe_warning(bob.user_id, sub.stream_id), true);
|
|
|
|
});
|
|
|
|
|
2023-10-05 08:04:23 +02:00
|
|
|
test_ui("warn_if_private_stream_is_linked", ({mock_template}) => {
|
2023-05-02 11:05:37 +02:00
|
|
|
const $textarea = $("<textarea>").attr("id", "compose-textarea");
|
2023-05-24 12:27:44 +02:00
|
|
|
stub_message_row($textarea);
|
2021-07-28 17:47:40 +02:00
|
|
|
const test_sub = {
|
|
|
|
name: compose_state.stream_name(),
|
|
|
|
stream_id: 99,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub(test_sub);
|
|
|
|
peer_data.set_subscribers(test_sub.stream_id, [1, 2]);
|
|
|
|
|
2023-05-26 02:48:11 +02:00
|
|
|
const denmark = {
|
2021-07-28 17:47:40 +02:00
|
|
|
stream_id: 100,
|
|
|
|
name: "Denmark",
|
|
|
|
};
|
|
|
|
stream_data.add_sub(denmark);
|
|
|
|
|
|
|
|
peer_data.set_subscribers(denmark.stream_id, [1, 2, 3]);
|
|
|
|
|
2022-08-23 01:03:37 +02:00
|
|
|
let banner_rendered = false;
|
|
|
|
mock_template("compose_banner/private_stream_warning.hbs", false, (data) => {
|
2022-08-30 22:24:54 +02:00
|
|
|
assert.equal(data.classname, compose_banner.CLASSNAMES.private_stream_warning);
|
2024-04-19 13:20:33 +02:00
|
|
|
assert.equal(data.channel_name, "Denmark");
|
2022-08-23 01:03:37 +02:00
|
|
|
banner_rendered = true;
|
2023-12-28 07:50:10 +01:00
|
|
|
return "<banner-stub>";
|
2022-08-23 01:03:37 +02:00
|
|
|
});
|
|
|
|
|
2021-07-28 17:47:40 +02:00
|
|
|
function test_noop_case(invite_only) {
|
2022-08-23 01:03:37 +02:00
|
|
|
banner_rendered = false;
|
2021-07-28 17:47:40 +02:00
|
|
|
compose_state.set_message_type("stream");
|
|
|
|
denmark.invite_only = invite_only;
|
2023-05-02 11:05:37 +02:00
|
|
|
compose_validate.warn_if_private_stream_is_linked(denmark, $textarea);
|
2022-08-23 01:03:37 +02:00
|
|
|
assert.ok(!banner_rendered);
|
2021-07-28 17:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
test_noop_case(false);
|
|
|
|
// invite_only=true and current compose stream subscribers are a subset
|
|
|
|
// of mentioned_stream subscribers.
|
|
|
|
test_noop_case(true);
|
|
|
|
|
|
|
|
$("#compose_private").hide();
|
|
|
|
compose_state.set_message_type("stream");
|
|
|
|
|
2023-05-26 02:48:11 +02:00
|
|
|
// Not everyone is subscribed to secret_stream in denmark, so the
|
|
|
|
// warning is rendered.
|
2023-10-05 08:04:23 +02:00
|
|
|
compose_state.set_selected_recipient_id(denmark.stream_id);
|
2023-05-26 02:48:11 +02:00
|
|
|
const secret_stream = {
|
2021-07-28 17:47:40 +02:00
|
|
|
invite_only: true,
|
|
|
|
name: "Denmark",
|
|
|
|
stream_id: 22,
|
|
|
|
};
|
2023-05-26 02:48:11 +02:00
|
|
|
stream_data.add_sub(secret_stream);
|
2022-08-23 01:03:37 +02:00
|
|
|
banner_rendered = false;
|
2024-01-09 18:32:31 +01:00
|
|
|
const $banner_container = $("#compose_banners");
|
|
|
|
$banner_container.set_find_results(".private_stream_warning", []);
|
2023-05-26 02:48:11 +02:00
|
|
|
compose_validate.warn_if_private_stream_is_linked(secret_stream, $textarea);
|
2022-08-23 01:03:37 +02:00
|
|
|
assert.ok(banner_rendered);
|
2024-01-09 18:32:31 +01:00
|
|
|
|
|
|
|
// Simulate that the row was added to the DOM.
|
|
|
|
const $warning_row = $("#compose_banners .private_stream_warning");
|
2024-05-03 02:45:17 +02:00
|
|
|
$warning_row.attr("data-stream-id", "22");
|
2024-01-09 18:32:31 +01:00
|
|
|
$("#compose_banners .private_stream_warning").length = 1;
|
|
|
|
$("#compose_banners .private_stream_warning")[0] = $warning_row;
|
|
|
|
|
|
|
|
// Now try to mention the same stream again. The template should
|
|
|
|
// not render.
|
|
|
|
banner_rendered = false;
|
|
|
|
$banner_container.set_find_results(".private_stream_warning", $warning_row);
|
|
|
|
compose_validate.warn_if_private_stream_is_linked(secret_stream, $textarea);
|
|
|
|
assert.ok(!banner_rendered);
|
2021-07-28 17:47:40 +02:00
|
|
|
});
|
|
|
|
|
2023-10-05 08:04:23 +02:00
|
|
|
test_ui("warn_if_mentioning_unsubscribed_user", ({override, mock_template}) => {
|
2023-05-02 11:05:37 +02:00
|
|
|
const $textarea = $("<textarea>").attr("id", "compose-textarea");
|
2023-05-24 12:27:44 +02:00
|
|
|
stub_message_row($textarea);
|
2023-06-27 01:40:25 +02:00
|
|
|
compose_state.set_stream_id("");
|
2021-07-28 17:47:40 +02:00
|
|
|
override(settings_data, "user_can_subscribe_other_users", () => true);
|
|
|
|
|
2022-11-23 21:01:27 +01:00
|
|
|
let mentioned_details = {
|
2024-06-06 22:55:50 +02:00
|
|
|
user: {
|
|
|
|
email: "foo@bar.com",
|
|
|
|
},
|
2021-07-28 17:47:40 +02:00
|
|
|
};
|
|
|
|
|
2022-08-20 02:51:13 +02:00
|
|
|
let new_banner_rendered = false;
|
|
|
|
mock_template("compose_banner/not_subscribed_warning.hbs", false, (data) => {
|
2022-08-30 22:24:54 +02:00
|
|
|
assert.equal(data.classname, compose_banner.CLASSNAMES.recipient_not_subscribed);
|
2022-08-20 02:51:13 +02:00
|
|
|
assert.equal(data.user_id, 34);
|
|
|
|
assert.equal(data.stream_id, 111);
|
|
|
|
assert.equal(data.name, "Foo Barson");
|
|
|
|
new_banner_rendered = true;
|
2023-12-28 07:50:10 +01:00
|
|
|
return "<banner-stub>";
|
2022-08-20 02:51:13 +02:00
|
|
|
});
|
2021-07-28 17:47:40 +02:00
|
|
|
|
2024-06-06 22:55:50 +02:00
|
|
|
function test_noop_case(is_private, is_zephyr_mirror, type) {
|
2022-08-20 02:51:13 +02:00
|
|
|
new_banner_rendered = false;
|
2021-07-28 17:47:40 +02:00
|
|
|
const msg_type = is_private ? "private" : "stream";
|
|
|
|
compose_state.set_message_type(msg_type);
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_is_zephyr_mirror_realm = is_zephyr_mirror;
|
2024-06-06 22:55:50 +02:00
|
|
|
mentioned_details.type = type;
|
2023-05-02 11:05:37 +02:00
|
|
|
compose_validate.warn_if_mentioning_unsubscribed_user(mentioned_details, $textarea);
|
2022-08-20 02:51:13 +02:00
|
|
|
assert.ok(!new_banner_rendered);
|
2021-07-28 17:47:40 +02:00
|
|
|
}
|
|
|
|
|
2024-06-06 22:55:50 +02:00
|
|
|
test_noop_case(true, false, "user");
|
|
|
|
test_noop_case(false, true, "user");
|
|
|
|
test_noop_case(false, false, "broadcast");
|
2021-07-28 17:47:40 +02:00
|
|
|
|
|
|
|
$("#compose_invite_users").hide();
|
|
|
|
compose_state.set_message_type("stream");
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_is_zephyr_mirror_realm = false;
|
2021-07-28 17:47:40 +02:00
|
|
|
|
|
|
|
// Test with empty stream name in compose box. It should return noop.
|
2022-08-20 02:51:13 +02:00
|
|
|
new_banner_rendered = false;
|
2021-07-28 17:47:40 +02:00
|
|
|
assert.equal(compose_state.stream_name(), "");
|
2023-05-02 11:05:37 +02:00
|
|
|
compose_validate.warn_if_mentioning_unsubscribed_user(mentioned_details, $textarea);
|
2022-08-20 02:51:13 +02:00
|
|
|
assert.ok(!new_banner_rendered);
|
2021-07-28 17:47:40 +02:00
|
|
|
|
|
|
|
const sub = {
|
|
|
|
stream_id: 111,
|
|
|
|
name: "random",
|
|
|
|
};
|
2023-05-07 14:45:04 +02:00
|
|
|
stream_data.add_sub(sub);
|
2023-06-27 01:40:25 +02:00
|
|
|
compose_state.set_stream_id(sub.stream_id);
|
2021-07-28 17:47:40 +02:00
|
|
|
|
|
|
|
// Test with invalid stream in compose box. It should return noop.
|
2022-08-20 02:51:13 +02:00
|
|
|
new_banner_rendered = false;
|
2023-05-02 11:05:37 +02:00
|
|
|
compose_validate.warn_if_mentioning_unsubscribed_user(mentioned_details, $textarea);
|
2022-08-20 02:51:13 +02:00
|
|
|
assert.ok(!new_banner_rendered);
|
2021-07-28 17:47:40 +02:00
|
|
|
|
|
|
|
// Test mentioning a user that should gets a warning.
|
2022-11-23 21:01:27 +01:00
|
|
|
mentioned_details = {
|
2024-06-06 22:55:50 +02:00
|
|
|
type: "user",
|
|
|
|
user: {
|
|
|
|
email: "foo@bar.com",
|
|
|
|
user_id: 34,
|
|
|
|
full_name: "Foo Barson",
|
|
|
|
},
|
2021-07-28 17:47:40 +02:00
|
|
|
};
|
2024-06-06 22:55:50 +02:00
|
|
|
people.add_active_user(mentioned_details.user);
|
2021-07-28 17:47:40 +02:00
|
|
|
|
2022-08-20 02:51:13 +02:00
|
|
|
new_banner_rendered = false;
|
2023-05-02 11:05:37 +02:00
|
|
|
const $banner_container = $("#compose_banners");
|
|
|
|
$banner_container.set_find_results(".recipient_not_subscribed", []);
|
|
|
|
|
|
|
|
compose_validate.warn_if_mentioning_unsubscribed_user(mentioned_details, $textarea);
|
2022-08-20 02:51:13 +02:00
|
|
|
assert.ok(new_banner_rendered);
|
2021-07-28 17:47:40 +02:00
|
|
|
|
|
|
|
// Simulate that the row was added to the DOM.
|
2022-08-20 02:51:13 +02:00
|
|
|
const $warning_row = $("#compose_banners .recipient_not_subscribed");
|
2024-05-03 02:52:50 +02:00
|
|
|
$warning_row.attr("data-user-id", "34");
|
2024-05-03 02:45:17 +02:00
|
|
|
$warning_row.attr("data-stream-id", "111");
|
2022-08-20 02:51:13 +02:00
|
|
|
$("#compose_banners .recipient_not_subscribed").length = 1;
|
|
|
|
$("#compose_banners .recipient_not_subscribed")[0] = $warning_row;
|
2021-07-28 17:47:40 +02:00
|
|
|
|
|
|
|
// Now try to mention the same person again. The template should
|
|
|
|
// not render.
|
2022-08-20 02:51:13 +02:00
|
|
|
new_banner_rendered = false;
|
2023-05-02 11:05:37 +02:00
|
|
|
$banner_container.set_find_results(".recipient_not_subscribed", $warning_row);
|
|
|
|
compose_validate.warn_if_mentioning_unsubscribed_user(mentioned_details, $textarea);
|
2022-08-20 02:51:13 +02:00
|
|
|
assert.ok(!new_banner_rendered);
|
2021-07-28 17:47:40 +02:00
|
|
|
});
|
2021-07-27 21:48:11 +02:00
|
|
|
|
2023-10-05 08:04:23 +02:00
|
|
|
test_ui("test warn_if_topic_resolved", ({override, mock_template}) => {
|
2022-08-19 21:36:33 +02:00
|
|
|
mock_banners();
|
|
|
|
$("#compose_banners .topic_resolved").length = 0;
|
2022-10-18 13:48:30 +02:00
|
|
|
override(settings_data, "user_can_move_messages_to_another_topic", () => true);
|
2021-07-27 21:48:11 +02:00
|
|
|
|
2022-08-19 21:36:33 +02:00
|
|
|
let error_shown = false;
|
|
|
|
mock_template("compose_banner/compose_banner.hbs", false, (data) => {
|
2022-08-30 22:24:54 +02:00
|
|
|
assert.equal(data.classname, compose_banner.CLASSNAMES.topic_resolved);
|
2022-08-19 21:36:33 +02:00
|
|
|
assert.equal(
|
|
|
|
data.banner_text,
|
|
|
|
$t({
|
|
|
|
defaultMessage:
|
|
|
|
"You are sending a message to a resolved topic. You can send as-is or unresolve the topic first.",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
error_shown = true;
|
2023-12-28 07:50:10 +01:00
|
|
|
return "<banner-stub>";
|
2021-07-27 21:48:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const sub = {
|
|
|
|
stream_id: 111,
|
|
|
|
name: "random",
|
|
|
|
};
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
|
|
|
|
compose_state.set_message_type("stream");
|
2023-06-27 01:40:25 +02:00
|
|
|
compose_state.set_stream_id("");
|
2022-03-07 23:25:06 +01:00
|
|
|
compose_state.topic(resolved_topic.resolve_name("hello"));
|
2022-03-16 01:31:01 +01:00
|
|
|
compose_state.message_content("content");
|
2021-07-27 21:48:11 +02:00
|
|
|
|
2022-08-19 21:36:33 +02:00
|
|
|
error_shown = false;
|
2022-03-16 01:31:01 +01:00
|
|
|
compose_validate.warn_if_topic_resolved(true);
|
2022-08-19 21:36:33 +02:00
|
|
|
assert.ok(!error_shown);
|
2021-07-27 21:48:11 +02:00
|
|
|
|
2023-06-27 01:40:25 +02:00
|
|
|
compose_state.set_stream_id(sub.stream_id);
|
2021-07-27 21:48:11 +02:00
|
|
|
|
|
|
|
// Show the warning now as stream also exists
|
2022-08-19 21:36:33 +02:00
|
|
|
error_shown = false;
|
2022-03-16 01:31:01 +01:00
|
|
|
compose_validate.warn_if_topic_resolved(true);
|
2022-08-19 21:36:33 +02:00
|
|
|
assert.ok(error_shown);
|
2022-03-16 01:31:01 +01:00
|
|
|
|
2023-02-02 05:43:24 +01:00
|
|
|
// We reset the state to be able to show the banner again
|
|
|
|
compose_state.set_recipient_viewed_topic_resolved_banner(false);
|
|
|
|
|
2022-08-19 21:36:33 +02:00
|
|
|
// Call it again with false; this should do the same thing.
|
|
|
|
error_shown = false;
|
2022-03-16 01:31:01 +01:00
|
|
|
compose_validate.warn_if_topic_resolved(false);
|
2022-08-19 21:36:33 +02:00
|
|
|
assert.ok(error_shown);
|
2021-07-27 21:48:11 +02:00
|
|
|
|
2023-02-02 05:43:24 +01:00
|
|
|
// Call the func again. This should not show the error because
|
|
|
|
// we have already shown the error once for this topic.
|
|
|
|
error_shown = false;
|
|
|
|
compose_validate.warn_if_topic_resolved(false);
|
|
|
|
assert.ok(!error_shown);
|
|
|
|
|
2021-07-27 21:48:11 +02:00
|
|
|
compose_state.topic("hello");
|
|
|
|
|
|
|
|
// The warning will be cleared now
|
2022-08-19 21:36:33 +02:00
|
|
|
error_shown = false;
|
2022-03-16 01:31:01 +01:00
|
|
|
compose_validate.warn_if_topic_resolved(true);
|
2022-08-19 21:36:33 +02:00
|
|
|
assert.ok(!error_shown);
|
2022-03-16 01:31:01 +01:00
|
|
|
|
|
|
|
// Calling with false won't do anything.
|
2022-08-19 21:36:33 +02:00
|
|
|
error_shown = false;
|
2022-03-16 01:31:01 +01:00
|
|
|
compose_validate.warn_if_topic_resolved(false);
|
2022-08-19 21:36:33 +02:00
|
|
|
assert.ok(!error_shown);
|
2021-07-27 21:48:11 +02:00
|
|
|
});
|