2021-09-22 12:59:11 -04:00
|
|
|
import axios from "axios";
|
|
|
|
import { HmacSHA1 } from "crypto-js";
|
2021-09-30 10:32:20 -04:00
|
|
|
import { LogService, MatrixClient, MemoryStorageProvider, PantalaimonClient } from "matrix-bot-sdk";
|
2021-09-22 12:59:11 -04:00
|
|
|
import config from "../../src/config";
|
|
|
|
|
2021-09-24 05:12:18 -04:00
|
|
|
/**
|
|
|
|
* Register a user using the synapse admin api that requires the use of a registration secret rather than an admin user.
|
|
|
|
* This should only be used by test code and should not be included from any file in the source directory
|
|
|
|
* either by explicit imports or copy pasting.
|
|
|
|
* @param username The username to give the user.
|
|
|
|
* @param displayname The displayname to give the user.
|
|
|
|
* @param password The password to use.
|
|
|
|
* @param admin True to make the user an admin, false otherwise.
|
|
|
|
* @returns The response from synapse.
|
|
|
|
*/
|
2021-09-22 12:59:11 -04:00
|
|
|
export async function registerUser(username: string, displayname: string, password: string, admin: boolean) {
|
|
|
|
let registerUrl = `${config.homeserverUrl}/_synapse/admin/v1/register`
|
|
|
|
let { data } = await axios.get(registerUrl);
|
|
|
|
let nonce = data.nonce!;
|
|
|
|
let mac = HmacSHA1(`${nonce}\0${username}\0${password}\0${admin ? 'admin' : 'notadmin'}`, 'REGISTRATION_SHARED_SECRET');
|
|
|
|
return await axios.post(registerUrl, {
|
|
|
|
nonce,
|
|
|
|
username,
|
|
|
|
displayname,
|
|
|
|
password,
|
|
|
|
admin,
|
|
|
|
mac: mac.toString()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a new test user with a unique username.
|
|
|
|
* @param isAdmin Whether to make the new user an admin.
|
|
|
|
* @returns A string that is the username and password of a new user.
|
|
|
|
*/
|
|
|
|
export async function registerNewTestUser(isAdmin: boolean) {
|
|
|
|
let isUserValid = false;
|
|
|
|
let username;
|
|
|
|
do {
|
2021-09-24 05:12:18 -04:00
|
|
|
username = `mjolnir-test-user-${Math.floor(Math.random() * 100000)}`
|
2021-09-22 12:59:11 -04:00
|
|
|
await registerUser(username, username, username, isAdmin).then(_ => isUserValid = true).catch(e => {
|
2021-09-24 05:12:18 -04:00
|
|
|
if (e.isAxiosError && e?.response?.data?.errcode === 'M_USER_IN_USE') {
|
2021-09-30 10:32:20 -04:00
|
|
|
LogService.debug("test/clientHelper", `${username} already registered, trying another`);
|
2021-09-22 12:59:11 -04:00
|
|
|
false // continue and try again
|
|
|
|
} else {
|
|
|
|
console.error(`failed to register user ${e}`);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} while (!isUserValid);
|
|
|
|
return username;
|
|
|
|
}
|
|
|
|
|
2021-09-24 05:12:18 -04:00
|
|
|
/**
|
|
|
|
* Registers a unique test user and returns a `MatrixClient` logged in and ready to use.
|
|
|
|
* @param isAdmin Whether to make the user an admin.
|
|
|
|
* @returns A new `MatrixClient` session for a unique test user.
|
|
|
|
*/
|
|
|
|
export async function newTestUser(isAdmin : boolean = false): Promise<MatrixClient> {
|
2021-09-22 12:59:11 -04:00
|
|
|
const username = await registerNewTestUser(isAdmin);
|
|
|
|
const pantalaimon = new PantalaimonClient(config.homeserverUrl, new MemoryStorageProvider());
|
|
|
|
return await pantalaimon.createClientWithCredentials(username, username);
|
|
|
|
}
|
|
|
|
|
2021-09-24 05:12:18 -04:00
|
|
|
/**
|
|
|
|
* Utility to create an event listener for m.notice msgtype m.room.messages.
|
|
|
|
* @param targetRoomdId The roomId to listen into.
|
|
|
|
* @param cb The callback when a m.notice event is found in targetRoomId.
|
|
|
|
* @returns The callback to pass to `MatrixClient.on('room.message', cb)`
|
|
|
|
*/
|
2021-09-22 12:59:11 -04:00
|
|
|
export function noticeListener(targetRoomdId: string, cb) {
|
|
|
|
return (roomId, event) => {
|
|
|
|
if (roomId !== targetRoomdId) return;
|
|
|
|
if (event?.content?.msgtype !== "m.notice") return;
|
|
|
|
cb(event);
|
|
|
|
}
|
2021-09-24 05:12:18 -04:00
|
|
|
}
|