2021-09-14 04:31:59 -04:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2021-09-08 11:44:06 -04:00
|
|
|
import {
|
|
|
|
MatrixClient,
|
|
|
|
Permalinks,
|
2021-09-22 06:57:45 -04:00
|
|
|
PantalaimonClient,
|
|
|
|
MemoryStorageProvider
|
2021-09-08 11:44:06 -04:00
|
|
|
} from "matrix-bot-sdk";
|
|
|
|
import config from "../../src/config";
|
2021-09-22 06:57:45 -04:00
|
|
|
import * as HmacSHA1 from 'crypto-js/hmac-sha1';
|
|
|
|
import axios from 'axios';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as fs from 'fs/promises';
|
2021-09-22 12:59:11 -04:00
|
|
|
import { setupMjolnir } from '../../src/setup';
|
|
|
|
import { registerUser } from "./clientHelper";
|
2021-09-08 11:44:06 -04:00
|
|
|
|
|
|
|
export async function createManagementRoom(client: MatrixClient) {
|
|
|
|
let roomId = await client.createRoom();
|
|
|
|
return await client.createRoomAlias(config.managementRoom, roomId);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function ensureManagementRoomExists(client: MatrixClient): Promise<string> {
|
|
|
|
return await client.resolveRoom(config.managementRoom).catch(async e => {
|
|
|
|
if (e?.body?.errcode === 'M_NOT_FOUND') {
|
|
|
|
console.info("moderation room hasn't been created yet, so we're making it now.")
|
|
|
|
return await createManagementRoom(client);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function ensureLobbyRoomExists(client: MatrixClient): Promise<string> {
|
|
|
|
const alias = Permalinks.parseUrl(config.protectedRooms[0]).roomIdOrAlias;
|
|
|
|
return await client.resolveRoom(alias).catch(async e => {
|
|
|
|
if (e?.body?.errcode === 'M_NOT_FOUND') {
|
|
|
|
console.info(`${alias} hasn't been created yet, so we're making it now.`)
|
|
|
|
return await client.createRoomAlias(alias, await client.createRoom());
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
});
|
2021-09-22 06:57:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function configureMjolnir() {
|
2021-09-22 12:59:11 -04:00
|
|
|
await fs.copyFile(path.join(__dirname, 'config', 'harness.yaml'), path.join(__dirname, '../../config/harness.yaml'));
|
|
|
|
await registerUser('mjolnir', 'mjolnir', 'mjolnir', true).catch(e => {
|
2021-09-22 06:57:45 -04:00
|
|
|
if (e.isAxiosError && e.response.data.errcode === 'M_USER_IN_USE') {
|
2021-09-22 12:59:11 -04:00
|
|
|
console.log('mjolnir already registered, skipping');
|
2021-09-22 06:57:45 -04:00
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-22 12:59:11 -04:00
|
|
|
// it actually might make sense to give mjolnir a clean plate each time we setup and teardown a test.
|
|
|
|
// the only issues with this might be e.g. if we need to delete a community or something
|
|
|
|
// that mjolnir sets up each time, but tbh we should probably just avoid setting things like that and tearing it down.
|
|
|
|
// One thing that probably should not be persisted between tests is the management room, subscribed lists and protected rooms.
|
|
|
|
export async function makeMjolnir() {
|
|
|
|
await configureMjolnir();
|
|
|
|
console.info('starting mjolnir');
|
|
|
|
const pantalaimon = new PantalaimonClient(config.homeserverUrl, new MemoryStorageProvider());
|
|
|
|
const client = await pantalaimon.createClientWithCredentials(config.pantalaimon.username, config.pantalaimon.password);
|
|
|
|
await ensureManagementRoomExists(client);
|
|
|
|
return await setupMjolnir(client, config);
|
2021-09-23 11:12:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function teardownManagementRoom(client: MatrixClient, roomId: string, alias: string) {
|
|
|
|
await client.deleteRoomAlias(alias);
|
|
|
|
await client.leaveRoom(roomId);
|
2021-09-22 12:59:11 -04:00
|
|
|
}
|