From 8044a78fa68f680f84bdb821a98facef7041f8cb Mon Sep 17 00:00:00 2001 From: gnuxie Date: Thu, 22 Feb 2024 13:11:53 +0000 Subject: [PATCH] Fix reportPollerTest (it works!) --- src/report/ReportManager.ts | 14 ++++--- test/integration/reportPollingTest.ts | 58 ++++++++++++++++++--------- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/report/ReportManager.ts b/src/report/ReportManager.ts index 18c5f808..d4912ec5 100644 --- a/src/report/ReportManager.ts +++ b/src/report/ReportManager.ts @@ -30,9 +30,8 @@ import { LogService, UserID } from "matrix-bot-sdk"; import { htmlToText } from "html-to-text"; import { htmlEscape } from "../utils"; import { JSDOM } from 'jsdom'; -import { EventEmitter } from 'events'; import { Draupnir } from "../Draupnir"; -import { ReactionContent, RoomEvent, StringEventID, StringRoomID, Task, Value, isError } from "matrix-protection-suite"; +import { ReactionContent, RoomEvent, StringEventID, StringRoomID, StringUserID, Task, Value, isError } from "matrix-protection-suite"; /// Regexp, used to extract the action label from an action reaction /// such as `⚽ Kick user @foobar:localhost from room [kick-user]`. @@ -86,10 +85,9 @@ enum Kind { /** * A class designed to respond to abuse reports. */ -export class ReportManager extends EventEmitter { +export class ReportManager { private displayManager: DisplayManager; constructor(public draupnir: Draupnir) { - super(); this.displayManager = new DisplayManager(this); } @@ -117,7 +115,13 @@ export class ReportManager extends EventEmitter { * @param reason A reason provided by the reporter. */ public async handleServerAbuseReport({ roomID, reporterId, event, reason }: { roomID: StringRoomID, reporterId: string, event: RoomEvent, reason?: string }) { - this.emit("report.new", { roomID: roomID, reporterId: reporterId, event: event, reason: reason }); + this.draupnir.handleEventReport({ + event_id: event.event_id, + room_id: roomID, + sender: reporterId as StringUserID, + event: event, + reason: reason + }) if (this.draupnir.config.displayReports) { return this.displayManager.displayReportAndUI({ kind: Kind.SERVER_ABUSE_REPORT, event, reporterId, reason, moderationroomID: this.draupnir.managementRoomID }); } diff --git a/test/integration/reportPollingTest.ts b/test/integration/reportPollingTest.ts index 606ed5e8..061ec94b 100644 --- a/test/integration/reportPollingTest.ts +++ b/test/integration/reportPollingTest.ts @@ -1,33 +1,51 @@ -import { Mjolnir } from "../../src/Mjolnir"; -import { Protection } from "../../src/protections/Protection"; +import { MatrixClient } from "matrix-bot-sdk"; import { newTestUser } from "./clientHelper"; +import { DraupnirTestContext } from "./mjolnirSetupUtils"; +import { ActionResult, DEFAULT_CONSEQUENCE_PROVIDER, MatrixRoomReference, Ok, Protection, ProtectionDescription, StandardProtectionSettings, StringRoomID, findConsequenceProvider } from "matrix-protection-suite"; describe("Test: Report polling", function() { - let client; + let client: MatrixClient; this.beforeEach(async function () { client = await newTestUser(this.config.homeserverUrl, { name: { contains: "protection-settings" }}); }) - it("Mjolnir correctly retrieves a report from synapse", async function() { + it("Mjolnir correctly retrieves a report from synapse", async function(this: DraupnirTestContext) { this.timeout(40000); - - let protectedRoomId = await this.mjolnir.client.createRoom({ invite: [await client.getUserId()] }); + const draupnir = this.draupnir; + if (draupnir === undefined) { + throw new TypeError(`Test didn't setup properly`); + } + let protectedRoomId = await draupnir.client.createRoom({ invite: [await client.getUserId()] }); await client.joinRoom(protectedRoomId); - await this.mjolnir.addProtectedRoom(protectedRoomId); + await draupnir.protectedRoomsSet.protectedRoomsConfig.addRoom(MatrixRoomReference.fromRoomID(protectedRoomId as StringRoomID)); const eventId = await client.sendMessage(protectedRoomId, {msgtype: "m.text", body: "uwNd3q"}); await new Promise(async resolve => { - await this.mjolnir.protectionManager.registerProtection(new class extends Protection { - name = "jYvufI"; - description = "A test protection"; - settings = { }; - handleEvent = async (mjolnir: Mjolnir, roomId: string, event: any) => { }; - handleReport = async (mjolnir: Mjolnir, roomId: string, reporterId: string, event: any, reason?: string) => { - if (reason === "x5h1Je") { - resolve(null); - } - }; - }); - await this.mjolnir.protectionManager.enableProtection("jYvufI"); + const testProtectionDescription: ProtectionDescription = { + name: "jYvufI", + description: "A test protection", + factory: function (description, consequenceProvider, protectedRoomsSet, context, settings): ActionResult { + return Ok({ + handleEventReport(report) { + if (report.reason === "x5h1Je") { + resolve(null); + } + return Promise.resolve(Ok(undefined)); + }, + description: testProtectionDescription, + requiredEventPermissions: [], + requiredPermissions: [] + }) + }, + protectionSettings: new StandardProtectionSettings( + {}, + {} + ) + } + const defaultConsequenceProvider = findConsequenceProvider(DEFAULT_CONSEQUENCE_PROVIDER); + if (defaultConsequenceProvider === undefined) { + throw new TypeError(`Default consequence provider should be defined mate`); + } + await draupnir.protectedRoomsSet.protections.addProtection(testProtectionDescription, defaultConsequenceProvider, draupnir.protectedRoomsSet, draupnir); await client.doRequest( "POST", `/_matrix/client/r0/rooms/${encodeURIComponent(protectedRoomId)}/report/${encodeURIComponent(eventId)}`, "", { @@ -42,5 +60,5 @@ describe("Test: Report polling", function() { // Ok, well apparently that needs a big refactor to change, but if you change the config before running this test, // then you can ensure that report polling works. https://github.com/matrix-org/mjolnir/issues/326. await new Promise(resolve => setTimeout(resolve, 1000)); - }); + } as unknown as Mocha.AsyncFunc); });