Migrate crypto test to cypress (#8833)

This commit is contained in:
Michael Weimann 2022-06-30 10:59:25 +02:00 committed by GitHub
parent 171f5adff6
commit 7e47749ce2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 207 additions and 72 deletions

View file

@ -18,10 +18,25 @@ limitations under the License.
import request from "browser-request";
import type { MatrixClient } from "matrix-js-sdk/src/client";
import type { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
import { SynapseInstance } from "../plugins/synapsedocker";
import Chainable = Cypress.Chainable;
interface CreateBotOpts {
/**
* Whether the bot should automatically accept all invites.
*/
autoAcceptInvites?: boolean;
/**
* The display name to give to that bot user
*/
displayName?: string;
}
const defaultCreateBotOptions = {
autoAcceptInvites: true,
} as CreateBotOpts;
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cypress {
@ -29,17 +44,30 @@ declare global {
/**
* Returns a new Bot instance
* @param synapse the instance on which to register the bot user
* @param displayName the display name to give to the bot user
* @param opts create bot options
*/
getBot(synapse: SynapseInstance, displayName?: string): Chainable<MatrixClient>;
getBot(synapse: SynapseInstance, opts: CreateBotOpts): Chainable<MatrixClient>;
/**
* Let a bot join a room
* @param cli The bot's MatrixClient
* @param roomId ID of the room to join
*/
botJoinRoom(cli: MatrixClient, roomId: string): Chainable<Room>;
/**
* Let a bot join a room by name
* @param cli The bot's MatrixClient
* @param roomName Name of the room to join
*/
botJoinRoomByName(cli: MatrixClient, roomName: string): Chainable<Room>;
}
}
}
Cypress.Commands.add("getBot", (synapse: SynapseInstance, displayName?: string): Chainable<MatrixClient> => {
Cypress.Commands.add("getBot", (synapse: SynapseInstance, opts: CreateBotOpts): Chainable<MatrixClient> => {
opts = Object.assign({}, defaultCreateBotOptions, opts);
const username = Cypress._.uniqueId("userId_");
const password = Cypress._.uniqueId("password_");
return cy.registerUser(synapse, username, password, displayName).then(credentials => {
return cy.registerUser(synapse, username, password, opts.displayName).then(credentials => {
return cy.window({ log: false }).then(win => {
const cli = new win.matrixcs.MatrixClient({
baseUrl: synapse.baseUrl,
@ -52,18 +80,37 @@ Cypress.Commands.add("getBot", (synapse: SynapseInstance, displayName?: string):
cryptoStore: new win.matrixcs.MemoryCryptoStore(),
});
cli.on(win.matrixcs.RoomMemberEvent.Membership, (event, member) => {
if (member.membership === "invite" && member.userId === cli.getUserId()) {
cli.joinRoom(member.roomId);
}
});
if (opts.autoAcceptInvites) {
cli.on(win.matrixcs.RoomMemberEvent.Membership, (event, member) => {
if (member.membership === "invite" && member.userId === cli.getUserId()) {
cli.joinRoom(member.roomId);
}
});
}
return cy.wrap(
cli.initCrypto()
.then(() => cli.setGlobalErrorOnUnknownDevices(false))
.then(() => cli.startClient())
.then(() => cli.bootstrapCrossSigning({
authUploadDeviceSigningKeys: async func => { await func({}); },
}))
.then(() => cli),
);
});
});
});
Cypress.Commands.add("botJoinRoom", (cli: MatrixClient, roomId: string): Chainable<Room> => {
return cy.wrap(cli.joinRoom(roomId));
});
Cypress.Commands.add("botJoinRoomByName", (cli: MatrixClient, roomName: string): Chainable<Room> => {
const room = cli.getRooms().find((r) => r.getDefaultRoomName(cli.getUserId()) === roomName);
if (room) {
return cy.botJoinRoom(cli, room.roomId);
}
return cy.wrap(Promise.reject());
});

View file

@ -53,6 +53,10 @@ declare global {
* @param data The data to store.
*/
setAccountData(type: string, data: object): Chainable<{}>;
/**
* Boostraps cross-signing.
*/
bootstrapCrossSigning(): Chainable<void>;
}
}
}
@ -103,3 +107,11 @@ Cypress.Commands.add("setAccountData", (type: string, data: object): Chainable<{
return cli.setAccountData(type, data);
});
});
Cypress.Commands.add("bootstrapCrossSigning", () => {
cy.window({ log: false }).then(win => {
win.mxMatrixClientPeg.matrixClient.bootstrapCrossSigning({
authUploadDeviceSigningKeys: async func => { await func({}); },
});
});
});