2022-05-10 19:09:31 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// <reference types="cypress" />
|
|
|
|
|
2022-07-18 14:16:44 +02:00
|
|
|
import type { ISendEventResponse, MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
2023-01-11 00:29:56 +01:00
|
|
|
import { HomeserverInstance } from "../plugins/utils/homeserver";
|
|
|
|
import { Credentials } from "./homeserver";
|
2022-05-10 19:09:31 +02:00
|
|
|
import Chainable = Cypress.Chainable;
|
2023-05-11 12:46:50 +02:00
|
|
|
import { collapseLastLogGroup } from "./log";
|
2022-05-10 19:09:31 +02:00
|
|
|
|
2022-06-30 10:59:25 +02:00
|
|
|
interface CreateBotOpts {
|
2022-12-19 14:12:01 +01:00
|
|
|
/**
|
|
|
|
* A prefix to use for the userid. If unspecified, "bot_" will be used.
|
|
|
|
*/
|
|
|
|
userIdPrefix?: string;
|
2022-06-30 10:59:25 +02:00
|
|
|
/**
|
|
|
|
* Whether the bot should automatically accept all invites.
|
|
|
|
*/
|
|
|
|
autoAcceptInvites?: boolean;
|
|
|
|
/**
|
|
|
|
* The display name to give to that bot user
|
|
|
|
*/
|
|
|
|
displayName?: string;
|
2022-07-18 14:16:44 +02:00
|
|
|
/**
|
|
|
|
* Whether or not to start the syncing client.
|
|
|
|
*/
|
|
|
|
startClient?: boolean;
|
2022-12-15 18:24:33 +01:00
|
|
|
/**
|
|
|
|
* Whether or not to generate cross-signing keys
|
|
|
|
*/
|
|
|
|
bootstrapCrossSigning?: boolean;
|
2023-07-07 18:56:53 +02:00
|
|
|
/**
|
|
|
|
* Whether to use the rust crypto impl. Defaults to false (for now!)
|
|
|
|
*/
|
|
|
|
rustCrypto?: boolean;
|
2022-06-30 10:59:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultCreateBotOptions = {
|
2022-12-19 14:12:01 +01:00
|
|
|
userIdPrefix: "bot_",
|
2022-06-30 10:59:25 +02:00
|
|
|
autoAcceptInvites: true,
|
2022-07-18 14:16:44 +02:00
|
|
|
startClient: true,
|
2022-12-15 18:24:33 +01:00
|
|
|
bootstrapCrossSigning: true,
|
2022-06-30 10:59:25 +02:00
|
|
|
} as CreateBotOpts;
|
|
|
|
|
2022-12-16 15:59:03 +01:00
|
|
|
export interface CypressBot extends MatrixClient {
|
|
|
|
__cypress_password: string;
|
|
|
|
}
|
|
|
|
|
2022-05-10 19:09:31 +02:00
|
|
|
declare global {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
|
|
namespace Cypress {
|
|
|
|
interface Chainable {
|
|
|
|
/**
|
|
|
|
* Returns a new Bot instance
|
2023-01-11 00:29:56 +01:00
|
|
|
* @param homeserver the instance on which to register the bot user
|
2022-06-30 10:59:25 +02:00
|
|
|
* @param opts create bot options
|
|
|
|
*/
|
2023-01-11 00:29:56 +01:00
|
|
|
getBot(homeserver: HomeserverInstance, opts: CreateBotOpts): Chainable<CypressBot>;
|
2023-05-11 12:46:50 +02:00
|
|
|
|
2022-12-15 18:24:33 +01:00
|
|
|
/**
|
|
|
|
* Returns a new Bot instance logged in as an existing user
|
2023-01-11 00:29:56 +01:00
|
|
|
* @param homeserver the instance on which to register the bot user
|
2022-12-15 18:24:33 +01:00
|
|
|
* @param username the username for the bot to log in with
|
|
|
|
* @param password the password for the bot to log in with
|
|
|
|
* @param opts create bot options
|
|
|
|
*/
|
|
|
|
loginBot(
|
2023-01-11 00:29:56 +01:00
|
|
|
homeserver: HomeserverInstance,
|
2022-12-15 18:24:33 +01:00
|
|
|
username: string,
|
|
|
|
password: string,
|
|
|
|
opts: CreateBotOpts,
|
|
|
|
): Chainable<MatrixClient>;
|
2023-05-11 12:46:50 +02:00
|
|
|
|
2022-06-30 10:59:25 +02:00
|
|
|
/**
|
|
|
|
* 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>;
|
2023-05-11 12:46:50 +02:00
|
|
|
|
2022-06-30 10:59:25 +02:00
|
|
|
/**
|
|
|
|
* Let a bot join a room by name
|
|
|
|
* @param cli The bot's MatrixClient
|
|
|
|
* @param roomName Name of the room to join
|
2022-05-10 19:09:31 +02:00
|
|
|
*/
|
2022-06-30 10:59:25 +02:00
|
|
|
botJoinRoomByName(cli: MatrixClient, roomName: string): Chainable<Room>;
|
2023-05-11 12:46:50 +02:00
|
|
|
|
2022-07-18 14:16:44 +02:00
|
|
|
/**
|
|
|
|
* Send a message as a bot into a room
|
|
|
|
* @param cli The bot's MatrixClient
|
|
|
|
* @param roomId ID of the room to join
|
|
|
|
* @param message the message body to send
|
|
|
|
*/
|
|
|
|
botSendMessage(cli: MatrixClient, roomId: string, message: string): Chainable<ISendEventResponse>;
|
2023-05-12 10:04:33 +02:00
|
|
|
/**
|
|
|
|
* Send a message as a bot into a room in a specific thread
|
|
|
|
* @param cli The bot's MatrixClient
|
|
|
|
* @param threadId the thread within which this message should go
|
|
|
|
* @param roomId ID of the room to join
|
|
|
|
* @param message the message body to send
|
|
|
|
*/
|
|
|
|
botSendThreadMessage(
|
|
|
|
cli: MatrixClient,
|
|
|
|
roomId: string,
|
|
|
|
threadId: string,
|
|
|
|
message: string,
|
|
|
|
): Chainable<ISendEventResponse>;
|
2022-05-10 19:09:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 18:24:33 +01:00
|
|
|
function setupBotClient(
|
2023-01-11 00:29:56 +01:00
|
|
|
homeserver: HomeserverInstance,
|
2022-12-15 18:24:33 +01:00
|
|
|
credentials: Credentials,
|
|
|
|
opts: CreateBotOpts,
|
|
|
|
): Chainable<MatrixClient> {
|
2022-06-30 10:59:25 +02:00
|
|
|
opts = Object.assign({}, defaultCreateBotOptions, opts);
|
2023-07-07 18:56:53 +02:00
|
|
|
return cy.window({ log: false }).then(
|
|
|
|
// extra timeout, as this sometimes takes a while
|
|
|
|
{ timeout: 30_000 },
|
|
|
|
async (win): Promise<MatrixClient> => {
|
|
|
|
const keys = {};
|
|
|
|
|
|
|
|
const getCrossSigningKey = (type: string) => {
|
|
|
|
return keys[type];
|
|
|
|
};
|
|
|
|
|
|
|
|
const saveCrossSigningKeys = (k: Record<string, Uint8Array>) => {
|
|
|
|
Object.assign(keys, k);
|
|
|
|
};
|
|
|
|
|
|
|
|
const cli = new win.matrixcs.MatrixClient({
|
|
|
|
baseUrl: homeserver.baseUrl,
|
|
|
|
userId: credentials.userId,
|
|
|
|
deviceId: credentials.deviceId,
|
|
|
|
accessToken: credentials.accessToken,
|
|
|
|
store: new win.matrixcs.MemoryStore(),
|
|
|
|
scheduler: new win.matrixcs.MatrixScheduler(),
|
|
|
|
cryptoStore: new win.matrixcs.MemoryCryptoStore(),
|
|
|
|
cryptoCallbacks: { getCrossSigningKey, saveCrossSigningKeys },
|
2022-05-10 19:09:31 +02:00
|
|
|
});
|
|
|
|
|
2023-07-07 18:56:53 +02:00
|
|
|
if (opts.autoAcceptInvites) {
|
|
|
|
cli.on(win.matrixcs.RoomMemberEvent.Membership, (event, member) => {
|
|
|
|
if (member.membership === "invite" && member.userId === cli.getUserId()) {
|
|
|
|
cli.joinRoom(member.roomId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.startClient) {
|
|
|
|
return cli;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.rustCrypto) {
|
|
|
|
await cli.initRustCrypto({ useIndexedDB: false });
|
|
|
|
} else {
|
|
|
|
await cli.initCrypto();
|
|
|
|
}
|
|
|
|
cli.setGlobalErrorOnUnknownDevices(false);
|
|
|
|
await cli.startClient();
|
|
|
|
|
|
|
|
if (opts.bootstrapCrossSigning) {
|
|
|
|
// XXX: workaround https://github.com/matrix-org/matrix-rust-sdk/issues/2193
|
|
|
|
// wait for out device list to be available, as a proxy for the device keys having been uploaded.
|
|
|
|
await cli.getCrypto()!.getUserDeviceInfo([credentials.userId]);
|
|
|
|
|
|
|
|
await cli.getCrypto()!.bootstrapCrossSigning({
|
|
|
|
authUploadDeviceSigningKeys: async (func) => {
|
|
|
|
await func({
|
|
|
|
type: "m.login.password",
|
|
|
|
identifier: {
|
|
|
|
type: "m.id.user",
|
|
|
|
user: credentials.userId,
|
2022-12-12 12:24:14 +01:00
|
|
|
},
|
2023-07-07 18:56:53 +02:00
|
|
|
password: credentials.password,
|
2022-12-15 18:24:33 +01:00
|
|
|
});
|
2023-07-07 18:56:53 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return cli;
|
|
|
|
},
|
|
|
|
);
|
2022-12-15 18:24:33 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 00:29:56 +01:00
|
|
|
Cypress.Commands.add("getBot", (homeserver: HomeserverInstance, opts: CreateBotOpts): Chainable<CypressBot> => {
|
2022-12-15 18:24:33 +01:00
|
|
|
opts = Object.assign({}, defaultCreateBotOptions, opts);
|
2022-12-19 14:12:01 +01:00
|
|
|
const username = Cypress._.uniqueId(opts.userIdPrefix);
|
2022-12-15 18:24:33 +01:00
|
|
|
const password = Cypress._.uniqueId("password_");
|
2023-05-11 12:46:50 +02:00
|
|
|
Cypress.log({
|
|
|
|
name: "getBot",
|
|
|
|
message: `Create bot user ${username} with opts ${JSON.stringify(opts)}`,
|
|
|
|
groupStart: true,
|
|
|
|
});
|
2022-12-16 15:59:03 +01:00
|
|
|
return cy
|
2023-01-11 00:29:56 +01:00
|
|
|
.registerUser(homeserver, username, password, opts.displayName)
|
2022-12-16 15:59:03 +01:00
|
|
|
.then((credentials) => {
|
2023-01-11 00:29:56 +01:00
|
|
|
return setupBotClient(homeserver, credentials, opts);
|
2022-12-16 15:59:03 +01:00
|
|
|
})
|
|
|
|
.then((client): Chainable<CypressBot> => {
|
|
|
|
Object.assign(client, { __cypress_password: password });
|
2023-05-11 12:46:50 +02:00
|
|
|
Cypress.log({ groupEnd: true, emitOnly: true });
|
|
|
|
collapseLastLogGroup();
|
|
|
|
return cy.wrap(client as CypressBot, { log: false });
|
2022-12-16 15:59:03 +01:00
|
|
|
});
|
2022-05-10 19:09:31 +02:00
|
|
|
});
|
2022-06-30 10:59:25 +02:00
|
|
|
|
2022-12-15 18:24:33 +01:00
|
|
|
Cypress.Commands.add(
|
|
|
|
"loginBot",
|
2023-01-11 00:29:56 +01:00
|
|
|
(
|
|
|
|
homeserver: HomeserverInstance,
|
|
|
|
username: string,
|
|
|
|
password: string,
|
|
|
|
opts: CreateBotOpts,
|
|
|
|
): Chainable<MatrixClient> => {
|
2022-12-15 18:24:33 +01:00
|
|
|
opts = Object.assign({}, defaultCreateBotOptions, { bootstrapCrossSigning: false }, opts);
|
2023-05-11 12:46:50 +02:00
|
|
|
Cypress.log({
|
|
|
|
name: "loginBot",
|
|
|
|
message: `log in as ${username} with opts ${JSON.stringify(opts)}`,
|
|
|
|
groupStart: true,
|
2022-12-15 18:24:33 +01:00
|
|
|
});
|
2023-05-11 12:46:50 +02:00
|
|
|
return cy
|
|
|
|
.loginUser(homeserver, username, password)
|
|
|
|
.then((credentials) => {
|
|
|
|
return setupBotClient(homeserver, credentials, opts);
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
Cypress.log({ groupEnd: true, emitOnly: true });
|
|
|
|
collapseLastLogGroup();
|
|
|
|
cy.wrap(res, { log: false });
|
|
|
|
});
|
2022-12-15 18:24:33 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-06-30 10:59:25 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:34:28 +02:00
|
|
|
return cy.wrap(Promise.reject(`Bot room join failed. Cannot find room '${roomName}'`));
|
2022-06-30 10:59:25 +02:00
|
|
|
});
|
2022-07-18 14:16:44 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
Cypress.Commands.add(
|
|
|
|
"botSendMessage",
|
|
|
|
(cli: MatrixClient, roomId: string, message: string): Chainable<ISendEventResponse> => {
|
|
|
|
return cy.wrap(
|
|
|
|
cli.sendMessage(roomId, {
|
|
|
|
msgtype: "m.text",
|
|
|
|
body: message,
|
|
|
|
}),
|
|
|
|
{ log: false },
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2023-05-12 10:04:33 +02:00
|
|
|
|
|
|
|
Cypress.Commands.add(
|
|
|
|
"botSendThreadMessage",
|
|
|
|
(cli: MatrixClient, roomId: string, threadId: string, message: string): Chainable<ISendEventResponse> => {
|
|
|
|
return cy.wrap(
|
|
|
|
cli.sendMessage(roomId, threadId, {
|
|
|
|
msgtype: "m.text",
|
|
|
|
body: message,
|
|
|
|
}),
|
|
|
|
{ log: false },
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|