2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2024-11-13 07:05:32 +01:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace.cjs");
|
|
|
|
const {run_test} = require("./lib/test.cjs");
|
|
|
|
const blueslip = require("./lib/zblueslip.cjs");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const channel = mock_esm("../src/channel");
|
|
|
|
const message_store = mock_esm("../src/message_store");
|
|
|
|
const widgetize = mock_esm("../src/widgetize");
|
2021-02-28 01:10:03 +01:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const submessage = zrequire("submessage");
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("get_message_events", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let msg = {};
|
2018-02-12 15:12:40 +01:00
|
|
|
|
|
|
|
assert.equal(submessage.get_message_events(msg), undefined);
|
|
|
|
|
|
|
|
msg = {
|
|
|
|
submessages: [],
|
|
|
|
};
|
|
|
|
assert.equal(submessage.get_message_events(msg), undefined);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const submessages = [
|
2024-03-28 08:24:44 +01:00
|
|
|
{id: 222, sender_id: 99, content: '{"type":"new_option","idx":1,"option":"bar"}'},
|
|
|
|
{
|
|
|
|
id: 9,
|
|
|
|
sender_id: 33,
|
|
|
|
content: '{"widget_type": "poll", "extra_data": {"question": "foo", "options": []}}',
|
|
|
|
},
|
2018-02-12 15:12:40 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
msg = {
|
|
|
|
locally_echoed: true,
|
2020-07-20 22:18:43 +02:00
|
|
|
submessages,
|
2018-02-12 15:12:40 +01:00
|
|
|
};
|
|
|
|
assert.equal(submessage.get_message_events(msg), undefined);
|
|
|
|
|
|
|
|
msg = {
|
2020-07-20 22:18:43 +02:00
|
|
|
submessages,
|
2018-02-12 15:12:40 +01:00
|
|
|
};
|
|
|
|
assert.deepEqual(submessage.get_message_events(msg), [
|
2024-03-28 08:24:44 +01:00
|
|
|
{
|
|
|
|
sender_id: 33,
|
|
|
|
data: {
|
|
|
|
widget_type: "poll",
|
|
|
|
extra_data: {
|
|
|
|
question: "foo",
|
|
|
|
options: [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sender_id: 99,
|
|
|
|
data: {
|
|
|
|
type: "new_option",
|
|
|
|
idx: 1,
|
|
|
|
option: "bar",
|
|
|
|
},
|
|
|
|
},
|
2018-02-12 15:12:40 +01:00
|
|
|
]);
|
2018-05-30 20:29:49 +02:00
|
|
|
});
|
2018-02-12 15:12:40 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("make_server_callback", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = 444;
|
|
|
|
const callback = submessage.make_server_callback(message_id);
|
|
|
|
let was_posted;
|
2018-02-12 15:12:40 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.post = (opts) => {
|
2018-02-12 15:12:40 +01:00
|
|
|
was_posted = true;
|
|
|
|
assert.deepEqual(opts, {
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/submessage",
|
2018-02-12 15:12:40 +01:00
|
|
|
data: {
|
2020-07-20 22:18:43 +02:00
|
|
|
message_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
msg_type: "whatever",
|
2018-02-12 15:12:40 +01:00
|
|
|
content: '{"foo":32}',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
callback({
|
2020-07-15 01:29:15 +02:00
|
|
|
msg_type: "whatever",
|
2018-02-12 15:12:40 +01:00
|
|
|
data: {foo: 32},
|
|
|
|
});
|
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(was_posted);
|
2018-05-30 20:29:49 +02:00
|
|
|
});
|
2018-05-30 20:36:09 +02:00
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("check sender", ({override}) => {
|
2021-06-15 00:46:15 +02:00
|
|
|
const message_id = 101;
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
id: message_id,
|
|
|
|
sender_id: 1,
|
2024-03-28 08:24:44 +01:00
|
|
|
submessages: [
|
|
|
|
{
|
|
|
|
sender_id: 2,
|
|
|
|
content:
|
|
|
|
'{"widget_type": "poll", "extra_data": {"question": "foo", "options": []}}',
|
|
|
|
},
|
|
|
|
],
|
2021-06-15 00:46:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
override(message_store, "get", (arg) => {
|
|
|
|
assert.equal(arg, message_id);
|
|
|
|
return message;
|
|
|
|
});
|
|
|
|
|
|
|
|
blueslip.expect("warn", "User 2 tried to hijack message 101");
|
|
|
|
|
|
|
|
submessage.process_submessages({
|
|
|
|
message_id,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("handle_event", () => {
|
2018-05-30 20:36:09 +02:00
|
|
|
const message = {
|
|
|
|
id: 42,
|
|
|
|
};
|
|
|
|
|
|
|
|
const event = {
|
2018-05-30 22:52:11 +02:00
|
|
|
id: 11,
|
2020-07-15 01:29:15 +02:00
|
|
|
msg_type: "widget",
|
2018-05-30 20:36:09 +02:00
|
|
|
sender_id: 99,
|
|
|
|
message_id: message.id,
|
2018-05-30 21:12:16 +02:00
|
|
|
content: '"some_data"',
|
2018-05-30 20:36:09 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let args;
|
2018-05-30 20:36:09 +02:00
|
|
|
widgetize.handle_event = (opts) => {
|
|
|
|
args = opts;
|
|
|
|
};
|
|
|
|
|
2018-05-30 22:52:11 +02:00
|
|
|
message_store.get = (msg_id) => {
|
|
|
|
assert.equal(msg_id, message.id);
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
2018-05-30 20:36:09 +02:00
|
|
|
submessage.handle_event(event);
|
|
|
|
|
|
|
|
assert.deepEqual(args, {
|
|
|
|
sender_id: 99,
|
|
|
|
message_id: 42,
|
2020-07-15 01:29:15 +02:00
|
|
|
data: "some_data",
|
2018-05-30 20:36:09 +02:00
|
|
|
});
|
2018-05-30 22:52:11 +02:00
|
|
|
|
|
|
|
assert.deepEqual(message.submessages[0], event);
|
2018-05-30 20:36:09 +02:00
|
|
|
});
|