diff --git a/test/integration/fixtures.ts b/test/integration/fixtures.ts index b91cfc8..0ed7fe6 100644 --- a/test/integration/fixtures.ts +++ b/test/integration/fixtures.ts @@ -1,6 +1,5 @@ import { read as configRead } from "../../src/config"; import { makeMjolnir, teardownManagementRoom } from "./mjolnirSetupUtils"; -import { register } from "prom-client"; import dns from 'node:dns'; // Necessary for CI: Node 17+ defaults to using ipv6 first, but Github Actions does not support ipv6 diff --git a/test/integration/nsfwProtectionTest.ts b/test/integration/nsfwProtectionTest.ts new file mode 100644 index 0000000..7e83b94 --- /dev/null +++ b/test/integration/nsfwProtectionTest.ts @@ -0,0 +1,66 @@ +import {newTestUser} from "./clientHelper"; + +import {MatrixClient} from "matrix-bot-sdk"; +import {getFirstReaction} from "./commands/commandUtils"; +import {strict as assert} from "assert"; +import { readFileSync } from 'fs'; + +describe("Test: NSFW protection", function () { + let client: MatrixClient; + let room: string; + this.beforeEach(async function () { + client = await newTestUser(this.config.homeserverUrl, {name: {contains: "nsfw-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("Nsfw protection doesn't redact sfw images", async function() { + this.timeout(20000); + + 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 NsfwProtection` }); + }); + + const data = readFileSync('test_tree.jpg') + const mxc = await client.uploadContent(data, 'image/png') + let content = {"msgtype": "m.image", "body": "test.jpeg", "url": mxc} + let imageMessage = await client.sendMessage(room, content) + + await delay(500) + let processedImage = await client.getEvent(room, imageMessage); + assert.equal(Object.keys(processedImage.content).length, 3, "This event should not have been redacted"); + }); + + it("Nsfw protection redacts nsfw images", async function() { + this.timeout(20000); + // dial the sensitivity on the protection way up so that all images are flagged as NSFW + this.mjolnir.config.nsfwSensitivity = 0.0 + + 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 NsfwProtection` }); + }); + + const data = readFileSync('test_tree.jpg') + const mxc = await client.uploadContent(data, 'image/png') + let content = {"msgtype": "m.image", "body": "test.jpeg", "url": mxc} + let imageMessage = await client.sendMessage(room, content) + + await delay(500) + let processedImage = await client.getEvent(room, imageMessage); + assert.equal(Object.keys(processedImage.content).length, 0, "This event should have been redacted"); + }); +}); \ No newline at end of file diff --git a/test_tree.jpg b/test_tree.jpg new file mode 100644 index 0000000..dcb2eb2 Binary files /dev/null and b/test_tree.jpg differ