|
| 1 | +import { MjolnirAppService } from "../../../src/appservice/AppService"; |
| 2 | +import { newTestUser } from "../../integration/clientHelper"; |
| 3 | +import { isPolicyRoom, readTestConfig, setupHarness } from "../utils/harness"; |
| 4 | +import { CreateMjolnirResponse, MjolnirWebAPIClient } from "../utils/webAPIClient"; |
| 5 | +import { MatrixClient } from "matrix-bot-sdk"; |
| 6 | +import { getFirstReply } from "../../integration/commands/commandUtils"; |
| 7 | +import expect from "expect"; |
| 8 | + |
| 9 | + |
| 10 | +interface Context extends Mocha.Context { |
| 11 | + appservice?: MjolnirAppService |
| 12 | + user?: MatrixClient |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +describe("Test that the app service can provision a mjolnir when requested from the web API", function () { |
| 17 | + afterEach(function(this: Context) { |
| 18 | + this.user?.stop(); |
| 19 | + if (this.appservice) { |
| 20 | + return this.appservice.close(); |
| 21 | + } else { |
| 22 | + console.warn("Missing Appservice in this context, so cannot stop it.") |
| 23 | + } |
| 24 | + }); |
| 25 | + it("", async function (this: Context) { |
| 26 | + const config = readTestConfig(); |
| 27 | + this.appservice = await setupHarness(); |
| 28 | + // create a user |
| 29 | + const user = await newTestUser(config.homeserver.url, { name: { contains: "test" } }); |
| 30 | + const apiClient = await MjolnirWebAPIClient.makeClient(user, "http://localhost:9001"); |
| 31 | + const roomToProtectId = await user.createRoom({ preset: "public_chat" }); |
| 32 | + |
| 33 | + this.user = user; |
| 34 | + const roomsInvitedTo: string[] = []; |
| 35 | + const mjolnirDetails: CreateMjolnirResponse = await new Promise(async resolve => { |
| 36 | + const mjolnirDetailsPromise = apiClient.createMjolnir(roomToProtectId); |
| 37 | + user.on('room.invite', (roomId: string) => { |
| 38 | + roomsInvitedTo.push(roomId) |
| 39 | + // the appservice should invite it to a policy room and a management room. |
| 40 | + if (roomsInvitedTo.length === 2) { |
| 41 | + mjolnirDetailsPromise.then(resolve); |
| 42 | + } |
| 43 | + }); |
| 44 | + await user.start(); |
| 45 | + }); |
| 46 | + await Promise.all(roomsInvitedTo.map(roomId => user.joinRoom(roomId))); |
| 47 | + const managementRoomId = roomsInvitedTo.filter(async roomId => !(await isPolicyRoom(user, roomId)))[0]; |
| 48 | + expect(managementRoomId).toBe(mjolnirDetails.managementRoomId); |
| 49 | + const event = await getFirstReply(user, managementRoomId, () => { |
| 50 | + return user.sendMessage(managementRoomId, { body: `!mjolnir status`, msgtype: 'm.text' }); |
| 51 | + }) |
| 52 | + expect(event.sender).toBe(mjolnirDetails.mjolnirUserId); |
| 53 | + }) |
| 54 | +}) |
0 commit comments