|
| 1 | +import { readTestConfig, setupHarness } from "../utils/harness"; |
| 2 | +import { newTestUser } from "../../integration/clientHelper"; |
| 3 | +import { getFirstReply } from "../../integration/commands/commandUtils"; |
| 4 | +import { MatrixClient } from "matrix-bot-sdk"; |
| 5 | +import { MjolnirAppService } from "../../../src/appservice/AppService"; |
| 6 | +import PolicyList from "../../../src/models/PolicyList"; |
| 7 | +import { CreateEvent } from "matrix-bot-sdk"; |
| 8 | + |
| 9 | +interface Context extends Mocha.Context { |
| 10 | + user?: MatrixClient, |
| 11 | + appservice?: MjolnirAppService |
| 12 | +} |
| 13 | + |
| 14 | +afterEach(function(this: Context) { |
| 15 | + this.user?.stop(); |
| 16 | + // something still runs, and i'm not sure what? -- ignoring with --exit. |
| 17 | + this.appservice?.close(); |
| 18 | +}); |
| 19 | + |
| 20 | +async function isPolicyRoom(user: MatrixClient, roomId: string): Promise<boolean> { |
| 21 | + const createEvent = new CreateEvent(await user.getRoomStateEvent(roomId, "m.room.create", "")); |
| 22 | + return PolicyList.ROOM_TYPE_VARIANTS.includes(createEvent.type); |
| 23 | +} |
| 24 | + |
| 25 | +describe("Test that the app service can provision a mjolnir on invite of the appservice bot", function () { |
| 26 | + it("", async function (this: Context) { |
| 27 | + const config = readTestConfig(); |
| 28 | + this.appservice = await setupHarness(); |
| 29 | + // create a user |
| 30 | + const user = await newTestUser(config.homeserver.url, { name: { contains: "test" } }); |
| 31 | + const roomWeWantProtecting = await user.createRoom(); |
| 32 | + // have the user invite the appservice bot |
| 33 | + this.user = user; |
| 34 | + const roomsInvitedTo: string[] = []; |
| 35 | + await new Promise(async resolve => { |
| 36 | + user.on('room.invite', (roomId: string) => { |
| 37 | + roomsInvitedTo.push(roomId) |
| 38 | + // the appservice should invite it to a policy room and a management room. |
| 39 | + if (roomsInvitedTo.length === 2) { |
| 40 | + resolve(null); |
| 41 | + } |
| 42 | + }); |
| 43 | + await user.start(); |
| 44 | + await user.inviteUser(this.appservice!.bridge.getBot().getUserId(), roomWeWantProtecting); |
| 45 | + }); |
| 46 | + await Promise.all(roomsInvitedTo.map(roomId => user.joinRoom(roomId))); |
| 47 | + const managementRoomId = roomsInvitedTo.filter(async roomId => !(await isPolicyRoom(user, roomId)))[0]; |
| 48 | + await getFirstReply(user, managementRoomId, () => { |
| 49 | + return user.sendMessage(managementRoomId, { body: `!mjolnir status`, msgtype: 'm.text' }); |
| 50 | + }) |
| 51 | + }) |
| 52 | +}) |
0 commit comments