add nsfw integration test

This commit is contained in:
H. Shay 2024-09-10 16:46:37 -07:00
parent 0ee021b8eb
commit 0b3734d7ec
3 changed files with 66 additions and 1 deletions

View File

@ -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

View File

@ -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");
});
});

BIN
test_tree.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 953 KiB