Test HTML messages

This commit is contained in:
Half-Shot 2024-09-20 09:12:55 +01:00
parent 0c375992ea
commit b61f39db2c

View File

@ -34,6 +34,7 @@ describe("Test: Mention spam protection", function () {
const testMessage = await client.sendText(room, 'Hello world'); const testMessage = await client.sendText(room, 'Hello world');
await delay(500); await delay(500);
const fetchedEvent = await client.getEvent(room, testMessage); const fetchedEvent = await client.getEvent(room, testMessage);
assert.equal(Object.keys(fetchedEvent.content).length, 2, "This event should not have been redacted"); assert.equal(Object.keys(fetchedEvent.content).length, 2, "This event should not have been redacted");
}); });
@ -44,22 +45,26 @@ describe("Test: Mention spam protection", function () {
return await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: "!mjolnir enable MentionSpam" }); return await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: "!mjolnir enable MentionSpam" });
}); });
// Also covers HTML mentions // Also covers HTML mentions
const messageWithTextMentions = await client.sendText(room, 'Hello world @foo:bar @beep:boop @test:here'); const mentionUsers = Array.from({length: DEFAULT_MAX_MENTIONS}, (_, i) => `@user${i}:example.org`);
const messageWithTextMentions = await client.sendText(room, mentionUsers.join(' '));
const messageWithHTMLMentions = await client.sendHtmlText(room,
mentionUsers.map(u => `<a href=\"https://matrix.to/#/${encodeURIComponent(u)}\">${u}</a>`).join(' '));
const messageWithMMentions = await client.sendMessage(room, { const messageWithMMentions = await client.sendMessage(room, {
msgtype: 'm.text', msgtype: 'm.text',
body: 'Hello world', body: 'Hello world',
['m.mentions']: { ['m.mentions']: {
user_ids: [ user_ids: mentionUsers
"@foo:bar",
"@beep:boop",
"@test:here"
]
} }
}); });
await delay(500); await delay(500);
const fetchedTextEvent = await client.getEvent(room, messageWithTextMentions); const fetchedTextEvent = await client.getEvent(room, messageWithTextMentions);
assert.equal(Object.keys(fetchedTextEvent.content).length, 2, "This event should not have been redacted"); assert.equal(Object.keys(fetchedTextEvent.content).length, 2, "This event should not have been redacted");
const fetchedHTMLEvent = await client.getEvent(room, messageWithHTMLMentions);
assert.equal(Object.keys(fetchedHTMLEvent.content).length, 4, "This event should not have been redacted");
const fetchedMentionsEvent = await client.getEvent(room, messageWithMMentions); const fetchedMentionsEvent = await client.getEvent(room, messageWithMMentions);
assert.equal(Object.keys(fetchedMentionsEvent.content).length, 3, "This event should not have been redacted"); assert.equal(Object.keys(fetchedMentionsEvent.content).length, 3, "This event should not have been redacted");
}); });
@ -72,6 +77,8 @@ describe("Test: Mention spam protection", function () {
// Also covers HTML mentions // Also covers HTML mentions
const mentionUsers = Array.from({length: DEFAULT_MAX_MENTIONS+1}, (_, i) => `@user${i}:example.org`); const mentionUsers = Array.from({length: DEFAULT_MAX_MENTIONS+1}, (_, i) => `@user${i}:example.org`);
const messageWithTextMentions = await client.sendText(room, mentionUsers.join(' ')); const messageWithTextMentions = await client.sendText(room, mentionUsers.join(' '));
const messageWithHTMLMentions = await client.sendHtmlText(room,
mentionUsers.map(u => `<a href=\"https://matrix.to/#/${encodeURIComponent(u)}\">${u}</a>`).join(' '));
const messageWithMMentions = await client.sendMessage(room, { const messageWithMMentions = await client.sendMessage(room, {
msgtype: 'm.text', msgtype: 'm.text',
body: 'Hello world', body: 'Hello world',
@ -81,8 +88,13 @@ describe("Test: Mention spam protection", function () {
}); });
await delay(500); await delay(500);
const fetchedTextEvent = await client.getEvent(room, messageWithTextMentions); const fetchedTextEvent = await client.getEvent(room, messageWithTextMentions);
assert.equal(Object.keys(fetchedTextEvent.content).length, 0, "This event should have been redacted"); assert.equal(Object.keys(fetchedTextEvent.content).length, 0, "This event should have been redacted");
const fetchedHTMLEvent = await client.getEvent(room, messageWithHTMLMentions);
assert.equal(Object.keys(fetchedHTMLEvent.content).length, 0, "This event should have been redacted");
const fetchedMentionsEvent = await client.getEvent(room, messageWithMMentions); const fetchedMentionsEvent = await client.getEvent(room, messageWithMMentions);
assert.equal(Object.keys(fetchedMentionsEvent.content).length, 0, "This event should have been redacted"); assert.equal(Object.keys(fetchedMentionsEvent.content).length, 0, "This event should have been redacted");
}); });