-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import {newTestUser} from "./clientHelper"; | ||
|
||
import {MatrixClient} from "matrix-bot-sdk"; | ||
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); | ||
assert.equal(Object.keys(fetchedEvent.content).length, 3, "This event should not have been redacted"); | ||
}); | ||
|
||
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, { | ||
content: { | ||
msgtype: 'm.text', | ||
body: 'Hello world', | ||
['m.mentions']: { | ||
user_ids: [ | ||
"@foo:bar", | ||
"@beep:boop", | ||
"@test:here" | ||
] | ||
} | ||
} | ||
}); | ||
|
||
await delay(500); | ||
const fetchedTextEvent = await client.getEvent(room, messageWithTextMentions); | ||
assert.equal(Object.keys(fetchedTextEvent.content).length, 3, "This event should not have been redacted"); | ||
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 | ||
const mentionUsers = Array.from({length: DEFAULT_MAX_MENTIONS}, (_, i) => `@user${i}:example.org`); | ||
const messageWithTextMentions = await client.sendText(room, 'Hello world ' + mentionUsers.join(' ')); | ||
const messageWithMMentions = await client.sendMessage(room, { | ||
content: { | ||
msgtype: 'm.text', | ||
body: 'Hello world', | ||
['m.mentions']: { | ||
user_ids: mentionUsers | ||
} | ||
} | ||
}); | ||
|
||
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"); | ||
}); | ||
}); |