From 61192daff53f41af575dbca4cfddd1a4ee471051 Mon Sep 17 00:00:00 2001 From: gnuxie Date: Wed, 27 Apr 2022 13:45:52 +0100 Subject: [PATCH] Tidy makeAdminCommandTest.ts This was pretty suspect with the number of clients syncing unnecessarily, it was also really hard to read. It also tests the same thing twice, even if it is a slightly different way. https://github.com/matrix-org/mjolnir/pull/219/files#diff-4e8bbc9dde21b7b895e0c081d2e3375c8958c51a10442497dfffb677f0d59a1aR1-R107 --- .../commands/makedminCommandTest.ts | 96 +++---------------- tsconfig.json | 3 +- 2 files changed, 15 insertions(+), 84 deletions(-) diff --git a/test/integration/commands/makedminCommandTest.ts b/test/integration/commands/makedminCommandTest.ts index 115de56..f50190c 100644 --- a/test/integration/commands/makedminCommandTest.ts +++ b/test/integration/commands/makedminCommandTest.ts @@ -2,106 +2,36 @@ import config from "../../../src/config"; import { newTestUser } from "../clientHelper"; -import { PowerLevelAction } from "matrix-bot-sdk/lib/models/PowerLevelAction"; import { LogService } from "matrix-bot-sdk"; import { getFirstReaction } from "./commandUtils"; describe("Test: The make admin command", function () { - afterEach(function () { - this.moderator?.stop(); - this.userA?.stop(); - this.userB?.stop(); - this.userC?.stop(); - }); - - it('Mjölnir make the bot self room administrator', async function () { + it('make Mjölnir the room administrator by "hijacking" a room via the Synapse admin API.', async function () { this.timeout(90000); if (!config.admin?.enableMakeRoomAdminCommand) { - done(); + LogService.warn("makedminCommandTest", `SKIPPING because the make room admin command is disabled`); + this.skip(); } const mjolnir = config.RUNTIME.client!; const mjolnirUserId = await mjolnir.getUserId(); const moderator = await newTestUser({ name: { contains: "moderator" } }); - const userA = await newTestUser({ name: { contains: "a" } }); - const userAId = await userA.getUserId(); - this.moderator = moderator; - this.userA = userA; - let powerLevels: any; + const unrelatedUser = await newTestUser({ name: { contains: "new-admin" } }); + const unrelatedUserId = await unrelatedUser.getUserId(); await moderator.joinRoom(config.managementRoom); - LogService.debug("makeadminTest", `Joining managementRoom: ${config.managementRoom}`); let targetRoom = await moderator.createRoom({ invite: [mjolnirUserId], preset: "public_chat" }); - LogService.debug("makeadminTest", `moderator creating targetRoom: ${targetRoom}; and inviting ${mjolnirUserId}`); await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text.', body: `!mjolnir rooms add ${targetRoom}` }); - LogService.debug("makeadminTest", `Adding targetRoom: ${targetRoom}`); - try { - await moderator.start(); - await userA.start(); - await userA.joinRoom(targetRoom); - powerLevels = await mjolnir.getRoomStateEvent(targetRoom, "m.room.power_levels", ""); - assert.notEqual(powerLevels["users"][mjolnirUserId], 100, `Bot should not yet be an admin of ${targetRoom}`); - await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => { - LogService.debug("makeadminTest", `Sending: !mjolnir make admin ${targetRoom}`); - return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir make admin ${targetRoom}` }); - }); - } finally { - await moderator.stop(); - await userA.stop(); - } + + await unrelatedUser.joinRoom(targetRoom); + let powerLevels = await mjolnir.getRoomStateEvent(targetRoom, "m.room.power_levels", ""); + assert.notEqual(powerLevels["users"][mjolnirUserId], 100, `Bot should not yet be an admin of ${targetRoom}`); + await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => { + return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir make admin ${targetRoom}` }); + }); LogService.debug("makeadminTest", `Making self admin`); powerLevels = await mjolnir.getRoomStateEvent(targetRoom, "m.room.power_levels", ""); assert.equal(powerLevels["users"][mjolnirUserId], 100, "Bot should be a room admin."); - assert.equal(powerLevels["users"][userAId], (0 || undefined), "User A is not supposed to be a room admin."); - }); - - it('Mjölnir make the tester room administrator', async function () { - this.timeout(90000); - if (!config.admin?.enableMakeRoomAdminCommand) { - done(); - } - const mjolnir = config.RUNTIME.client!; - const moderator = await newTestUser({ name: { contains: "moderator" } }); - const userA = await newTestUser({ name: { contains: "a" } }); - const userB = await newTestUser({ name: { contains: "b" } }); - const userC = await newTestUser({ name: { contains: "c" } }); - const userBId = await userB.getUserId(); - const userCId = await userC.getUserId(); - this.moderator = moderator; - this.userA = userA; - this.userB = userB; - this.userC = userC; - let powerLevels: any; - - await moderator.joinRoom(this.mjolnir.managementRoomId); - LogService.debug("makeadminTest", `Joining managementRoom: ${this.mjolnir.managementRoomId}`); - let targetRoom = await userA.createRoom({ invite: [userBId, userCId] }); - LogService.debug("makeadminTest", `User A creating targetRoom: ${targetRoom}; and inviting ${userBId} and ${userCId}`); - try { - await userB.start(); - await userC.start(); - await userB.joinRoom(targetRoom); - await userC.joinRoom(targetRoom); - } finally { - LogService.debug("makeadminTest", `${userBId} and ${userCId} joining targetRoom: ${targetRoom}`); - await userB.stop(); - await userC.stop(); - } - try { - await moderator.start(); - powerLevels = await userA.getRoomStateEvent(targetRoom, "m.room.power_levels", ""); - assert.notEqual(powerLevels["users"][userBId], 100, `User B should not yet be an admin of ${targetRoom}`); - await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => { - LogService.debug("makeadminTest", `Sending: !mjolnir make admin ${targetRoom} ${userBId}`); - return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir make admin ${targetRoom} ${userBId}` }); - }); - } finally { - await moderator.stop(); - } - LogService.debug("makeadminTest", `Making User B admin`); - - powerLevels = await userA.getRoomStateEvent(targetRoom, "m.room.power_levels", ""); - assert.equal(powerLevels["users"][userBId], 100, "User B should be a room admin."); - assert.equal(powerLevels["users"][userCId], (0 || undefined), "User C is not supposed to be a room admin."); + assert.equal(powerLevels["users"][unrelatedUserId], (0 || undefined), "User A is not supposed to be a room admin."); }); }); diff --git a/tsconfig.json b/tsconfig.json index 0327a13..010773c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,6 +21,7 @@ "include": [ "./src/**/*", "./test/integration/manualLaunchScript.ts", - "./test/integration/roomMembersTest.ts" + "./test/integration/roomMembersTest.ts", + "./test/integration/commands/makedminCommandTest.ts" ] }