2021-05-19 18:55:36 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
|
|
|
const {$t_html} = require("../zjsunit/i18n");
|
2021-10-14 00:23:21 +02:00
|
|
|
const {mock_esm, zrequire} = require("../zjsunit/namespace");
|
2021-05-19 18:55:36 +02:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-07-28 17:47:40 +02:00
|
|
|
const blueslip = require("../zjsunit/zblueslip");
|
2021-05-19 18:55:36 +02:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
|
|
|
|
|
|
|
const channel = mock_esm("../../static/js/channel");
|
|
|
|
const compose_actions = mock_esm("../../static/js/compose_actions");
|
|
|
|
const compose_state = zrequire("compose_state");
|
|
|
|
const ui_util = mock_esm("../../static/js/ui_util");
|
|
|
|
|
|
|
|
const compose_pm_pill = zrequire("compose_pm_pill");
|
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");
|
2022-03-07 22:25:54 +01:00
|
|
|
const resolved_topic = zrequire("../shared/js/resolved_topic");
|
2021-06-25 15:16:29 +02:00
|
|
|
const settings_config = zrequire("settings_config");
|
2021-07-28 17:47:40 +02:00
|
|
|
const settings_data = mock_esm("../../static/js/settings_data");
|
2021-05-19 18:55:36 +02:00
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
|
|
|
|
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",
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-05-19 18:55:36 +02:00
|
|
|
function test_ui(label, f) {
|
|
|
|
// The sloppy_$ flag lets us re-use setup from prior tests.
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test(label, (helpers) => {
|
2021-05-19 19:29:12 +02:00
|
|
|
$("#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
|
|
|
}
|
|
|
|
|
2021-06-28 00:41:05 +02:00
|
|
|
test_ui("validate_stream_message_address_info", ({mock_template}) => {
|
2021-05-19 18:55:36 +02:00
|
|
|
const sub = {
|
|
|
|
stream_id: 101,
|
|
|
|
name: "social",
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(sub);
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(compose_validate.validate_stream_message_address_info("social"));
|
2021-05-19 18:55:36 +02:00
|
|
|
|
|
|
|
sub.subscribed = false;
|
|
|
|
stream_data.add_sub(sub);
|
2021-06-28 00:41:05 +02:00
|
|
|
mock_template("compose_not_subscribed.hbs", false, () => "compose_not_subscribed_stub");
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate_stream_message_address_info("social"));
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal($("#compose-error-msg").html(), "compose_not_subscribed_stub");
|
|
|
|
|
|
|
|
page_params.narrow_stream = false;
|
|
|
|
channel.post = (payload) => {
|
|
|
|
assert.equal(payload.data.stream, "social");
|
|
|
|
payload.data.subscribed = true;
|
|
|
|
payload.success(payload.data);
|
|
|
|
};
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(compose_validate.validate_stream_message_address_info("social"));
|
2021-05-19 18:55:36 +02:00
|
|
|
|
|
|
|
sub.name = "Frontend";
|
|
|
|
sub.stream_id = 102;
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
channel.post = (payload) => {
|
|
|
|
assert.equal(payload.data.stream, "Frontend");
|
|
|
|
payload.data.subscribed = false;
|
|
|
|
payload.success(payload.data);
|
|
|
|
};
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate_stream_message_address_info("Frontend"));
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal($("#compose-error-msg").html(), "compose_not_subscribed_stub");
|
|
|
|
|
|
|
|
channel.post = (payload) => {
|
|
|
|
assert.equal(payload.data.stream, "Frontend");
|
|
|
|
payload.error({status: 404});
|
|
|
|
};
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate_stream_message_address_info("Frontend"));
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
"translated HTML: <p>The stream <b>Frontend</b> does not exist.</p><p>Manage your subscriptions <a href='#streams/all'>on your Streams page</a>.</p>",
|
|
|
|
);
|
|
|
|
|
|
|
|
channel.post = (payload) => {
|
|
|
|
assert.equal(payload.data.stream, "social");
|
|
|
|
payload.error({status: 500});
|
|
|
|
};
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate_stream_message_address_info("social"));
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({defaultMessage: "Error checking subscription"}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-06-28 00:41:05 +02:00
|
|
|
test_ui("validate", ({override, mock_template}) => {
|
2021-05-19 18:55:36 +02:00
|
|
|
override(compose_actions, "update_placeholder_text", () => {});
|
|
|
|
|
|
|
|
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"));
|
2021-05-19 18:55:36 +02:00
|
|
|
$("#private_message_recipient").before = () => {};
|
|
|
|
|
|
|
|
compose_pm_pill.initialize();
|
|
|
|
|
|
|
|
ui_util.place_caret_at_end = () => {};
|
|
|
|
|
|
|
|
$("#zephyr-mirror-error").is = () => {};
|
|
|
|
|
2021-06-28 00:41:05 +02:00
|
|
|
mock_template("input_pill.hbs", false, () => "<div>pill-html</div>");
|
2021-05-19 18:55:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function add_content_to_compose_box() {
|
|
|
|
$("#compose-textarea").val("foobarfoobar");
|
|
|
|
}
|
|
|
|
|
|
|
|
initialize_pm_pill();
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2021-11-30 06:26:05 +01:00
|
|
|
assert.ok(!$("#compose-send-button .loader").visible());
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal($("#compose-send-button").prop("disabled"), false);
|
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({defaultMessage: "You have nothing to send!"}),
|
|
|
|
);
|
|
|
|
|
2021-06-25 15:16:29 +02:00
|
|
|
compose_validate.validate();
|
2021-05-19 18:55:36 +02:00
|
|
|
|
|
|
|
add_content_to_compose_box();
|
|
|
|
let zephyr_checked = false;
|
2022-04-09 23:44:38 +02:00
|
|
|
$("#zephyr-mirror-error").is = (arg) => {
|
|
|
|
assert.equal(arg, ":visible");
|
|
|
|
zephyr_checked = true;
|
|
|
|
return true;
|
2021-05-19 18:55:36 +02:00
|
|
|
};
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(zephyr_checked);
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({
|
|
|
|
defaultMessage: "You need to be running Zephyr mirroring in order to send messages!",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
initialize_pm_pill();
|
|
|
|
add_content_to_compose_box();
|
|
|
|
|
|
|
|
// test validating private messages
|
|
|
|
compose_state.set_message_type("private");
|
|
|
|
|
|
|
|
compose_state.private_message_recipient("");
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({defaultMessage: "Please specify at least one valid recipient"}),
|
|
|
|
);
|
|
|
|
|
|
|
|
initialize_pm_pill();
|
|
|
|
add_content_to_compose_box();
|
|
|
|
compose_state.private_message_recipient("foo@zulip.com");
|
|
|
|
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2021-05-19 18:55:36 +02:00
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({defaultMessage: "Please specify at least one valid recipient"}),
|
|
|
|
);
|
|
|
|
|
|
|
|
compose_state.private_message_recipient("foo@zulip.com,alice@zulip.com");
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2021-05-19 18:55:36 +02:00
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({defaultMessage: "Please specify at least one valid recipient"}),
|
|
|
|
);
|
|
|
|
|
|
|
|
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());
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2020-07-24 02:31:03 +02:00
|
|
|
people.deactivate(bob);
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2020-07-24 02:31:03 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({defaultMessage: "You cannot send messages to deactivated users."}),
|
|
|
|
);
|
|
|
|
|
2021-05-19 18:55:36 +02:00
|
|
|
page_params.realm_is_zephyr_mirror_realm = true;
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(compose_validate.validate());
|
2021-05-19 18:55:36 +02:00
|
|
|
page_params.realm_is_zephyr_mirror_realm = false;
|
|
|
|
|
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());
|
|
|
|
|
2021-05-19 18:55:36 +02:00
|
|
|
compose_state.set_message_type("stream");
|
|
|
|
compose_state.stream_name("");
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({defaultMessage: "Please specify a stream"}),
|
|
|
|
);
|
|
|
|
|
|
|
|
compose_state.stream_name("Denmark");
|
|
|
|
page_params.realm_mandatory_topics = true;
|
|
|
|
compose_state.topic("");
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
2022-02-02 08:48:31 +01:00
|
|
|
$t_html({defaultMessage: "Topics are required in this organization"}),
|
2021-05-19 18:55:36 +02:00
|
|
|
);
|
2022-03-08 11:52:29 +01:00
|
|
|
|
|
|
|
compose_state.topic("(no topic)");
|
|
|
|
assert.ok(!compose_validate.validate());
|
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({defaultMessage: "Topics are required in this organization"}),
|
|
|
|
);
|
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",
|
|
|
|
};
|
|
|
|
|
|
|
|
page_params.user_id = me.user_id;
|
|
|
|
|
|
|
|
const params = {};
|
|
|
|
params.realm_users = [];
|
|
|
|
params.realm_non_active_users = [];
|
|
|
|
params.cross_realm_bots = [welcome_bot];
|
|
|
|
|
|
|
|
people.initialize(page_params.user_id, params);
|
|
|
|
|
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(), []);
|
|
|
|
});
|
|
|
|
|
|
|
|
test_ui("test_wildcard_mention_allowed", () => {
|
|
|
|
page_params.user_id = me.user_id;
|
|
|
|
|
|
|
|
page_params.realm_wildcard_mention_policy =
|
|
|
|
settings_config.wildcard_mention_policy_values.by_everyone.code;
|
|
|
|
page_params.is_guest = true;
|
|
|
|
page_params.is_admin = false;
|
|
|
|
assert.ok(compose_validate.wildcard_mention_allowed());
|
|
|
|
|
|
|
|
page_params.realm_wildcard_mention_policy =
|
|
|
|
settings_config.wildcard_mention_policy_values.nobody.code;
|
|
|
|
page_params.is_admin = true;
|
|
|
|
assert.ok(!compose_validate.wildcard_mention_allowed());
|
|
|
|
|
|
|
|
page_params.realm_wildcard_mention_policy =
|
|
|
|
settings_config.wildcard_mention_policy_values.by_members.code;
|
|
|
|
page_params.is_guest = true;
|
|
|
|
page_params.is_admin = false;
|
|
|
|
assert.ok(!compose_validate.wildcard_mention_allowed());
|
|
|
|
|
|
|
|
page_params.is_guest = false;
|
|
|
|
assert.ok(compose_validate.wildcard_mention_allowed());
|
|
|
|
|
|
|
|
page_params.realm_wildcard_mention_policy =
|
|
|
|
settings_config.wildcard_mention_policy_values.by_moderators_only.code;
|
|
|
|
page_params.is_moderator = false;
|
|
|
|
assert.ok(!compose_validate.wildcard_mention_allowed());
|
|
|
|
|
|
|
|
page_params.is_moderator = true;
|
|
|
|
assert.ok(compose_validate.wildcard_mention_allowed());
|
|
|
|
|
|
|
|
page_params.realm_wildcard_mention_policy =
|
2022-07-05 16:56:12 +02:00
|
|
|
settings_config.wildcard_mention_policy_values.by_admins_only.code;
|
2021-06-25 15:16:29 +02:00
|
|
|
page_params.is_admin = false;
|
|
|
|
assert.ok(!compose_validate.wildcard_mention_allowed());
|
|
|
|
|
|
|
|
// TODO: Add a by_admins_only case when we implement stream-level administrators.
|
|
|
|
|
|
|
|
page_params.is_admin = true;
|
|
|
|
assert.ok(compose_validate.wildcard_mention_allowed());
|
|
|
|
|
|
|
|
page_params.realm_wildcard_mention_policy =
|
|
|
|
settings_config.wildcard_mention_policy_values.by_full_members.code;
|
|
|
|
const person = people.get_by_user_id(page_params.user_id);
|
|
|
|
person.date_joined = new Date(Date.now());
|
|
|
|
page_params.realm_waiting_period_threshold = 10;
|
|
|
|
|
|
|
|
assert.ok(compose_validate.wildcard_mention_allowed());
|
|
|
|
page_params.is_admin = false;
|
|
|
|
assert.ok(!compose_validate.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.
|
|
|
|
page_params.user_id = me.user_id;
|
|
|
|
page_params.realm_mandatory_topics = false;
|
|
|
|
const sub = {
|
|
|
|
stream_id: 101,
|
|
|
|
name: "social",
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
compose_state.stream_name("social");
|
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());
|
|
|
|
assert.ok(!$("#compose-send-status").visible());
|
2021-05-19 18:55:36 +02:00
|
|
|
|
|
|
|
peer_data.get_subscriber_count = (stream_id) => {
|
|
|
|
assert.equal(stream_id, 101);
|
|
|
|
return 16;
|
|
|
|
};
|
2021-06-28 00:41:05 +02:00
|
|
|
mock_template("compose_all_everyone.hbs", false, (data) => {
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal(data.count, 16);
|
|
|
|
return "compose_all_everyone_stub";
|
|
|
|
});
|
|
|
|
let compose_content;
|
|
|
|
$("#compose-all-everyone").append = (data) => {
|
|
|
|
compose_content = data;
|
|
|
|
};
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(compose_validate, "wildcard_mention_allowed", () => 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);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!$("#compose-send-status").visible());
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal(compose_content, "compose_all_everyone_stub");
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok($("#compose-all-everyone").visible());
|
2021-05-19 18:55:36 +02:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(compose_validate, "wildcard_mention_allowed", () => false);
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({
|
|
|
|
defaultMessage: "You do not have permission to use wildcard mentions in this stream.",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-07-13 21:25:31 +02:00
|
|
|
test_ui("test_validate_stream_message_post_policy_admin_only", () => {
|
2021-05-19 18:55:36 +02:00
|
|
|
// 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.
|
|
|
|
page_params.is_admin = false;
|
|
|
|
const sub = {
|
|
|
|
stream_id: 102,
|
|
|
|
name: "stream102",
|
|
|
|
subscribed: true,
|
|
|
|
stream_post_policy: stream_data.stream_post_policy_values.admins.code,
|
|
|
|
};
|
|
|
|
|
|
|
|
compose_state.topic("subject102");
|
|
|
|
compose_state.stream_name("stream102");
|
|
|
|
stream_data.add_sub(sub);
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
2021-12-27 13:03:26 +01:00
|
|
|
$t_html({defaultMessage: "You do not have permission to post in this stream."}),
|
2021-05-19 18:55:36 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Reset error message.
|
|
|
|
compose_state.stream_name("social");
|
|
|
|
|
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.is_guest = true;
|
|
|
|
|
|
|
|
compose_state.topic("subject102");
|
|
|
|
compose_state.stream_name("stream102");
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
2021-12-27 13:03:26 +01:00
|
|
|
$t_html({defaultMessage: "You do not have permission to post in this stream."}),
|
2021-05-19 18:55:36 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-07-13 21:25:31 +02:00
|
|
|
test_ui("test_validate_stream_message_post_policy_moderators_only", () => {
|
2021-05-19 18:55:36 +02:00
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.is_moderator = false;
|
|
|
|
page_params.is_guest = false;
|
|
|
|
|
|
|
|
const sub = {
|
|
|
|
stream_id: 104,
|
|
|
|
name: "stream104",
|
|
|
|
subscribed: true,
|
|
|
|
stream_post_policy: stream_data.stream_post_policy_values.moderators.code,
|
|
|
|
};
|
|
|
|
|
|
|
|
compose_state.topic("subject104");
|
|
|
|
compose_state.stream_name("stream104");
|
|
|
|
stream_data.add_sub(sub);
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({
|
2021-12-27 13:03:26 +01:00
|
|
|
defaultMessage: "You do not have permission to post in this stream.",
|
2021-05-19 18:55:36 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Reset error message.
|
|
|
|
compose_state.stream_name("social");
|
|
|
|
|
|
|
|
page_params.is_guest = true;
|
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({
|
2021-12-27 13:03:26 +01:00
|
|
|
defaultMessage: "You do not have permission to post in this stream.",
|
2021-05-19 18:55:36 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-07-13 21:25:31 +02:00
|
|
|
test_ui("test_validate_stream_message_post_policy_full_members_only", () => {
|
2021-05-19 18:55:36 +02:00
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.is_guest = true;
|
|
|
|
const sub = {
|
|
|
|
stream_id: 103,
|
|
|
|
name: "stream103",
|
|
|
|
subscribed: true,
|
|
|
|
stream_post_policy: stream_data.stream_post_policy_values.non_new_members.code,
|
|
|
|
};
|
|
|
|
|
|
|
|
compose_state.topic("subject103");
|
|
|
|
compose_state.stream_name("stream103");
|
|
|
|
stream_data.add_sub(sub);
|
2021-06-25 15:16:29 +02:00
|
|
|
assert.ok(!compose_validate.validate());
|
2021-05-19 18:55:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
2021-12-27 13:03:26 +01:00
|
|
|
$t_html({defaultMessage: "You do not have permission to post in this stream."}),
|
2021-05-19 18:55:36 +02:00
|
|
|
);
|
|
|
|
});
|
2021-07-07 12:13:19 +02:00
|
|
|
|
2021-07-10 19:33:45 +02:00
|
|
|
test_ui("test_check_overflow_text", () => {
|
2021-07-07 12:13:19 +02:00
|
|
|
page_params.max_message_length = 10000;
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $textarea = $("#compose-textarea");
|
|
|
|
const $indicator = $("#compose_limit_indicator");
|
|
|
|
const $send_button = $("#compose-send-button");
|
2021-07-07 12:13:19 +02:00
|
|
|
|
|
|
|
// Indicator should show red colored text
|
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"));
|
|
|
|
assert.equal($indicator.text(), "10001/10000");
|
|
|
|
assert.ok($textarea.hasClass("over_limit"));
|
2021-07-10 19:33:45 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
2021-07-28 01:03:19 +02:00
|
|
|
"translated HTML: Message length shouldn't be greater than 10000 characters.",
|
2021-07-10 19:33:45 +02:00
|
|
|
);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($send_button.prop("disabled"));
|
2021-07-10 19:33:45 +02:00
|
|
|
|
|
|
|
$("#compose-send-status").stop = () => ({fadeOut: () => {}});
|
2021-07-07 12:13:19 +02:00
|
|
|
|
|
|
|
// Indicator should show orange colored text
|
2022-01-25 11:36:19 +01:00
|
|
|
$textarea.val("a".repeat(9000 + 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(), "9001/10000");
|
|
|
|
assert.ok(!$textarea.hasClass("over_limit"));
|
|
|
|
assert.ok(!$send_button.prop("disabled"));
|
2021-07-07 12:13:19 +02:00
|
|
|
|
|
|
|
// Indicator must be empty
|
2022-01-25 11:36:19 +01:00
|
|
|
$textarea.val("a".repeat(9000));
|
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-13 21:25:31 +02:00
|
|
|
test_ui("test_message_overflow", () => {
|
2021-07-07 12:13:19 +02:00
|
|
|
page_params.max_message_length = 10000;
|
|
|
|
|
|
|
|
const sub = {
|
|
|
|
stream_id: 101,
|
|
|
|
name: "social",
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
page_params.user_id = 30;
|
|
|
|
const message = "a".repeat(10000 + 1);
|
|
|
|
|
|
|
|
compose_state.stream_name("social");
|
|
|
|
compose_state.topic("priyam");
|
|
|
|
$("#compose-textarea").val(message);
|
|
|
|
|
|
|
|
assert.ok(!compose_validate.validate());
|
|
|
|
assert.equal($("#compose-error-msg").html(), "never-been-set");
|
|
|
|
|
|
|
|
$("#compose-textarea").val("a");
|
|
|
|
assert.ok(compose_validate.validate());
|
|
|
|
});
|
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]);
|
|
|
|
|
|
|
|
blueslip.expect("error", "Unknown user_id in get_by_user_id: 999");
|
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
|
|
|
|
test_ui("warn_if_private_stream_is_linked", ({mock_template}) => {
|
|
|
|
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]);
|
|
|
|
|
|
|
|
let denmark = {
|
|
|
|
stream_id: 100,
|
|
|
|
name: "Denmark",
|
|
|
|
};
|
|
|
|
stream_data.add_sub(denmark);
|
|
|
|
|
|
|
|
peer_data.set_subscribers(denmark.stream_id, [1, 2, 3]);
|
|
|
|
|
|
|
|
function test_noop_case(invite_only) {
|
|
|
|
compose_state.set_message_type("stream");
|
|
|
|
denmark.invite_only = invite_only;
|
|
|
|
compose_validate.warn_if_private_stream_is_linked(denmark);
|
|
|
|
assert.equal($("#compose_private_stream_alert").visible(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
const checks = [
|
|
|
|
(function () {
|
|
|
|
let called;
|
|
|
|
mock_template("compose_private_stream_alert.hbs", false, (context) => {
|
|
|
|
called = true;
|
|
|
|
assert.equal(context.stream_name, "Denmark");
|
|
|
|
return "fake-compose_private_stream_alert-template";
|
|
|
|
});
|
|
|
|
return function () {
|
|
|
|
assert.ok(called);
|
|
|
|
};
|
|
|
|
})(),
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
let called;
|
|
|
|
$("#compose_private_stream_alert").append = (html) => {
|
|
|
|
called = true;
|
|
|
|
assert.equal(html, "fake-compose_private_stream_alert-template");
|
|
|
|
};
|
|
|
|
return function () {
|
|
|
|
assert.ok(called);
|
|
|
|
};
|
|
|
|
})(),
|
|
|
|
];
|
|
|
|
|
|
|
|
denmark = {
|
|
|
|
invite_only: true,
|
|
|
|
name: "Denmark",
|
|
|
|
stream_id: 22,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(denmark);
|
|
|
|
|
|
|
|
compose_validate.warn_if_private_stream_is_linked(denmark);
|
|
|
|
assert.equal($("#compose_private_stream_alert").visible(), true);
|
|
|
|
|
|
|
|
for (const f of checks) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
test_ui("warn_if_mentioning_unsubscribed_user", ({override, override_rewire, mock_template}) => {
|
2021-07-28 17:47:40 +02:00
|
|
|
override(settings_data, "user_can_subscribe_other_users", () => true);
|
|
|
|
|
|
|
|
let mentioned = {
|
|
|
|
email: "foo@bar.com",
|
|
|
|
};
|
|
|
|
|
|
|
|
$("#compose_invite_users .compose_invite_user").length = 0;
|
|
|
|
|
|
|
|
function test_noop_case(is_private, is_zephyr_mirror, is_broadcast) {
|
|
|
|
const msg_type = is_private ? "private" : "stream";
|
|
|
|
compose_state.set_message_type(msg_type);
|
|
|
|
page_params.realm_is_zephyr_mirror_realm = is_zephyr_mirror;
|
|
|
|
mentioned.is_broadcast = is_broadcast;
|
|
|
|
compose_validate.warn_if_mentioning_unsubscribed_user(mentioned);
|
|
|
|
assert.equal($("#compose_invite_users").visible(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
test_noop_case(true, false, false);
|
|
|
|
test_noop_case(false, true, false);
|
|
|
|
test_noop_case(false, false, true);
|
|
|
|
|
|
|
|
$("#compose_invite_users").hide();
|
|
|
|
compose_state.set_message_type("stream");
|
|
|
|
page_params.realm_is_zephyr_mirror_realm = false;
|
|
|
|
|
|
|
|
// Test with empty stream name in compose box. It should return noop.
|
|
|
|
assert.equal(compose_state.stream_name(), "");
|
|
|
|
compose_validate.warn_if_mentioning_unsubscribed_user(mentioned);
|
|
|
|
assert.equal($("#compose_invite_users").visible(), false);
|
|
|
|
|
|
|
|
compose_state.stream_name("random");
|
|
|
|
const sub = {
|
|
|
|
stream_id: 111,
|
|
|
|
name: "random",
|
|
|
|
};
|
|
|
|
|
|
|
|
// Test with invalid stream in compose box. It should return noop.
|
|
|
|
compose_validate.warn_if_mentioning_unsubscribed_user(mentioned);
|
|
|
|
assert.equal($("#compose_invite_users").visible(), false);
|
|
|
|
|
|
|
|
// Test mentioning a user that should gets a warning.
|
|
|
|
|
|
|
|
const checks = [
|
|
|
|
(function () {
|
|
|
|
let called;
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(compose_validate, "needs_subscribe_warning", (user_id, stream_id) => {
|
2021-07-28 17:47:40 +02:00
|
|
|
called = true;
|
|
|
|
assert.equal(user_id, 34);
|
|
|
|
assert.equal(stream_id, 111);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
return function () {
|
|
|
|
assert.ok(called);
|
|
|
|
};
|
|
|
|
})(),
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
let called;
|
|
|
|
mock_template("compose_invite_users.hbs", false, (context) => {
|
|
|
|
called = true;
|
|
|
|
assert.equal(context.user_id, 34);
|
|
|
|
assert.equal(context.stream_id, 111);
|
|
|
|
assert.equal(context.name, "Foo Barson");
|
|
|
|
return "fake-compose-invite-user-template";
|
|
|
|
});
|
|
|
|
return function () {
|
|
|
|
assert.ok(called);
|
|
|
|
};
|
|
|
|
})(),
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
let called;
|
|
|
|
$("#compose_invite_users").append = (html) => {
|
|
|
|
called = true;
|
|
|
|
assert.equal(html, "fake-compose-invite-user-template");
|
|
|
|
};
|
|
|
|
return function () {
|
|
|
|
assert.ok(called);
|
|
|
|
};
|
|
|
|
})(),
|
|
|
|
];
|
|
|
|
|
|
|
|
mentioned = {
|
|
|
|
email: "foo@bar.com",
|
|
|
|
user_id: 34,
|
|
|
|
full_name: "Foo Barson",
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
compose_validate.warn_if_mentioning_unsubscribed_user(mentioned);
|
|
|
|
assert.equal($("#compose_invite_users").visible(), true);
|
|
|
|
|
|
|
|
for (const f of checks) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simulate that the row was added to the DOM.
|
2022-03-17 00:31:13 +01:00
|
|
|
const $warning_row = $("<warning-row-stub>");
|
2021-07-28 17:47:40 +02:00
|
|
|
|
|
|
|
let looked_for_existing;
|
2022-01-25 11:36:19 +01:00
|
|
|
$warning_row.data = (field) => {
|
2022-04-09 23:44:38 +02:00
|
|
|
switch (field) {
|
|
|
|
case "user-id":
|
|
|
|
looked_for_existing = true;
|
|
|
|
return "34";
|
|
|
|
/* istanbul ignore next */
|
|
|
|
case "stream-id":
|
|
|
|
return "111";
|
|
|
|
/* istanbul ignore next */
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown field ${field}`);
|
2021-07-28 17:47:40 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $previous_users = $("#compose_invite_users .compose_invite_user");
|
|
|
|
$previous_users.length = 1;
|
|
|
|
$previous_users[0] = $warning_row;
|
2021-07-28 17:47:40 +02:00
|
|
|
$("#compose_invite_users").hide();
|
|
|
|
|
|
|
|
// Now try to mention the same person again. The template should
|
|
|
|
// not render.
|
|
|
|
compose_validate.warn_if_mentioning_unsubscribed_user(mentioned);
|
|
|
|
assert.equal($("#compose_invite_users").visible(), true);
|
|
|
|
assert.ok(looked_for_existing);
|
|
|
|
});
|
2021-07-27 21:48:11 +02:00
|
|
|
|
|
|
|
test_ui("test clear_topic_resolved_warning", () => {
|
|
|
|
$("#compose_resolved_topic").show();
|
|
|
|
$("#compose-send-status").show();
|
|
|
|
|
|
|
|
compose_validate.clear_topic_resolved_warning();
|
|
|
|
|
|
|
|
assert.ok(!$("#compose_resolved_topic").visible());
|
|
|
|
assert.ok(!$("#compose-send-status").visible());
|
|
|
|
});
|
|
|
|
|
|
|
|
test_ui("test warn_if_topic_resolved", ({override, mock_template}) => {
|
|
|
|
override(settings_data, "user_can_move_messages_between_streams", () => true);
|
|
|
|
|
|
|
|
mock_template("compose_resolved_topic.hbs", false, (context) => {
|
|
|
|
assert.ok(context.can_move_topic);
|
2022-03-07 22:59:56 +01:00
|
|
|
assert.ok(resolved_topic.is_resolved(context.topic_name));
|
2021-07-27 21:48:11 +02:00
|
|
|
return "fake-compose_resolved_topic";
|
|
|
|
});
|
|
|
|
|
|
|
|
const sub = {
|
|
|
|
stream_id: 111,
|
|
|
|
name: "random",
|
|
|
|
};
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
|
|
|
|
// The error message area where it is shown
|
2022-01-25 11:36:19 +01:00
|
|
|
const $error_area = $("#compose_resolved_topic");
|
2022-03-16 01:31:01 +01:00
|
|
|
compose_validate.clear_topic_resolved_warning();
|
|
|
|
// Hack to make this empty for zjquery; this is conceptually done
|
|
|
|
// in the previous line.
|
2022-01-25 11:36:19 +01:00
|
|
|
$error_area.html("");
|
|
|
|
assert.ok(!$error_area.visible());
|
2021-07-27 21:48:11 +02:00
|
|
|
|
|
|
|
compose_state.set_message_type("stream");
|
|
|
|
compose_state.stream_name("Do not exist");
|
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
|
|
|
|
|
|
|
// Do not show a warning if stream name does not exist
|
2022-03-16 01:31:01 +01:00
|
|
|
compose_validate.warn_if_topic_resolved(true);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok(!$error_area.visible());
|
2021-07-27 21:48:11 +02:00
|
|
|
|
|
|
|
compose_state.stream_name("random");
|
|
|
|
|
|
|
|
// Show the warning now as stream also exists
|
2022-03-16 01:31:01 +01:00
|
|
|
compose_validate.warn_if_topic_resolved(true);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($error_area.visible());
|
2022-03-16 01:31:01 +01:00
|
|
|
|
|
|
|
// Call it again with false; this should be a noop.
|
|
|
|
compose_validate.warn_if_topic_resolved(false);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($error_area.visible());
|
2021-07-27 21:48:11 +02:00
|
|
|
|
|
|
|
compose_state.topic("hello");
|
|
|
|
|
|
|
|
// The warning will be cleared now
|
2022-03-16 01:31:01 +01:00
|
|
|
compose_validate.warn_if_topic_resolved(true);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok(!$error_area.visible());
|
2022-03-16 01:31:01 +01:00
|
|
|
|
|
|
|
// Calling with false won't do anything.
|
|
|
|
compose_validate.warn_if_topic_resolved(false);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok(!$error_area.visible());
|
2021-07-27 21:48:11 +02:00
|
|
|
});
|