From 65af82d46f65e2c57b72095e4958341ded84b964 Mon Sep 17 00:00:00 2001 From: jesopo Date: Mon, 28 Feb 2022 19:35:24 +0000 Subject: [PATCH] send whole channel shutdown reason, not just the first word --- src/commands/ShutdownRoomCommand.ts | 3 +- .../commands/shutdownCommandTest.ts | 55 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 test/integration/commands/shutdownCommandTest.ts diff --git a/src/commands/ShutdownRoomCommand.ts b/src/commands/ShutdownRoomCommand.ts index 8bd4085..edfb424 100644 --- a/src/commands/ShutdownRoomCommand.ts +++ b/src/commands/ShutdownRoomCommand.ts @@ -20,6 +20,7 @@ import { RichReply } from "matrix-bot-sdk"; // !mjolnir shutdown room [] export async function execShutdownRoomCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { const victim = parts[3]; + const reason = parts.slice(4).join(" ") || undefined; const isAdmin = await mjolnir.isSynapseAdmin(); if (!isAdmin) { @@ -30,6 +31,6 @@ export async function execShutdownRoomCommand(roomId: string, event: any, mjolni return; } - await mjolnir.shutdownSynapseRoom(await mjolnir.client.resolveRoom(victim), parts[4]); + await mjolnir.shutdownSynapseRoom(await mjolnir.client.resolveRoom(victim), reason); await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅'); } diff --git a/test/integration/commands/shutdownCommandTest.ts b/test/integration/commands/shutdownCommandTest.ts new file mode 100644 index 0000000..df24376 --- /dev/null +++ b/test/integration/commands/shutdownCommandTest.ts @@ -0,0 +1,55 @@ +import { strict as assert } from "assert"; + +import { newTestUser } from "../clientHelper"; + +describe("Test: shutdown command", function() { + let client; + this.beforeEach(async function () { + client = await newTestUser({ name: { contains: "shutdown-command" }}); + await client.start(); + }) + this.afterEach(async function () { + await client.stop(); + }) + it("Mjolnir asks synapse to shut down a channel", async function() { + this.timeout(20000); + const badRoom = await client.createRoom(); + await client.joinRoom(this.mjolnir.managementRoomId); + + let reply1 = new Promise(async (resolve, reject) => { + const msgid = await client.sendMessage(this.mjolnir.managementRoomId, {msgtype: "m.text", body: `!mjolnir shutdown room ${badRoom} closure test`}); + client.on('room.event', (roomId, event) => { + if ( + roomId === this.mjolnir.managementRoomId + && event?.type === "m.reaction" + && event.sender === this.mjolnir.client.userId + && event.content?.["m.relates_to"]?.event_id === msgid + ) { + resolve(event); + } + }); + }); + + const reply2 = new Promise((resolve, reject) => { + this.mjolnir.client.on('room.event', (roomId, event) => { + if ( + roomId !== this.mjolnir.managementRoomId + && roomId !== badRoom + && event?.type === "m.room.message" + && event.sender === this.mjolnir.client.userId + && event.content?.body === "closure test" + ) { + resolve(event); + } + }); + }); + + await reply1 + await reply2 + + await assert.rejects(client.joinRoom(badRoom), e => { + return e.message.endsWith('{"errcode":"M_UNKNOWN","error":"This room has been blocked on this server"}'); + }); + }); +}); +