mjolnir/test/integration/mentionSpamProtectionTest.ts

89 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-09-19 10:50:50 -04:00
import {newTestUser} from "./clientHelper";
2024-09-19 11:26:27 -04:00
import {MatrixClient} from "@vector-im/matrix-bot-sdk";
2024-09-19 10:50:50 -04:00
import {getFirstReaction} from "./commands/commandUtils";
import {strict as assert} from "assert";
import { DEFAULT_MAX_MENTIONS } from "../../src/protections/MentionSpam";
describe("Test: Mention spam protection", function () {
let client: MatrixClient;
let room: string;
this.beforeEach(async function () {
client = await newTestUser(this.config.homeserverUrl, {name: {contains: "mention-spam-protection"}});
await client.start();
const mjolnirId = await this.mjolnir.client.getUserId();
room = await client.createRoom({ invite: [mjolnirId] });
await client.joinRoom(room);
await client.joinRoom(this.config.managementRoom);
await client.setUserPowerLevel(mjolnirId, room, 100);
})
this.afterEach(async function () {
await client.stop();
})
function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
it("does not redact a normal message", async function() {
await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir rooms add ${room}` });
await getFirstReaction(client, this.mjolnir.managementRoomId, '✅', async () => {
return await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: "!mjolnir enable MentionSpam" });
});
const testMessage = await client.sendText(room, 'Hello world');
await delay(500);
const fetchedEvent = await client.getEvent(room, testMessage);
2024-09-19 11:17:37 -04:00
assert.equal(Object.keys(fetchedEvent.content).length, 2, "This event should not have been redacted");
2024-09-19 10:50:50 -04:00
});
it("does not redact a message with some mentions", async function() {
await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir rooms add ${room}` });
await getFirstReaction(client, this.mjolnir.managementRoomId, '✅', async () => {
return await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: "!mjolnir enable MentionSpam" });
});
// Also covers HTML mentions
const messageWithTextMentions = await client.sendText(room, 'Hello world @foo:bar @beep:boop @test:here');
const messageWithMMentions = await client.sendMessage(room, {
2024-09-19 11:17:37 -04:00
msgtype: 'm.text',
body: 'Hello world',
['m.mentions']: {
user_ids: [
"@foo:bar",
"@beep:boop",
"@test:here"
]
2024-09-19 10:50:50 -04:00
}
});
await delay(500);
const fetchedTextEvent = await client.getEvent(room, messageWithTextMentions);
2024-09-19 11:17:37 -04:00
assert.equal(Object.keys(fetchedTextEvent.content).length, 2, "This event should not have been redacted");
2024-09-19 10:50:50 -04:00
const fetchedMentionsEvent = await client.getEvent(room, messageWithMMentions);
assert.equal(Object.keys(fetchedMentionsEvent.content).length, 3, "This event should not have been redacted");
});
it("does redact a message with too many mentions", async function() {
await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir rooms add ${room}` });
await getFirstReaction(client, this.mjolnir.managementRoomId, '✅', async () => {
return await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: "!mjolnir enable MentionSpam" });
});
// Also covers HTML mentions
2024-09-19 11:43:44 -04:00
const mentionUsers = Array.from({length: DEFAULT_MAX_MENTIONS+1}, (_, i) => `@user${i}:example.org`);
2024-09-19 10:50:50 -04:00
const messageWithTextMentions = await client.sendText(room, 'Hello world ' + mentionUsers.join(' '));
const messageWithMMentions = await client.sendMessage(room, {
2024-09-19 11:17:37 -04:00
msgtype: 'm.text',
body: 'Hello world',
['m.mentions']: {
user_ids: mentionUsers
2024-09-19 10:50:50 -04:00
}
});
await delay(500);
const fetchedTextEvent = await client.getEvent(room, messageWithTextMentions);
assert.equal(Object.keys(fetchedTextEvent.content).length, 0, "This event should have been redacted");
const fetchedMentionsEvent = await client.getEvent(room, messageWithMMentions);
assert.equal(Object.keys(fetchedMentionsEvent.content).length, 0, "This event should have been redacted");
});
});