Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Cypress: add a way to specify the prefix for userIDs (#9787)
Browse files Browse the repository at this point in the history
This helps a lot with debugging tests where there are multiple users.
  • Loading branch information
richvdh authored Dec 19, 2022
1 parent 3ec75fd commit ddfa627
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cypress/e2e/crypto/crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ describe("Cryptography", function () {
cy.startSynapse("default")
.as("synapse")
.then((synapse: SynapseInstance) => {
cy.initTestUser(synapse, "Alice");
cy.getBot(synapse, { displayName: "Bob", autoAcceptInvites: false }).as("bob");
cy.initTestUser(synapse, "Alice", undefined, "alice_");
cy.getBot(synapse, { displayName: "Bob", autoAcceptInvites: false, userIdPrefix: "bob_" }).as("bob");
});
});

Expand Down
7 changes: 6 additions & 1 deletion cypress/support/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import { Credentials } from "./synapse";
import Chainable = Cypress.Chainable;

interface CreateBotOpts {
/**
* A prefix to use for the userid. If unspecified, "bot_" will be used.
*/
userIdPrefix?: string;
/**
* Whether the bot should automatically accept all invites.
*/
Expand All @@ -41,6 +45,7 @@ interface CreateBotOpts {
}

const defaultCreateBotOptions = {
userIdPrefix: "bot_",
autoAcceptInvites: true,
startClient: true,
bootstrapCrossSigning: true,
Expand Down Expand Up @@ -153,7 +158,7 @@ function setupBotClient(

Cypress.Commands.add("getBot", (synapse: SynapseInstance, opts: CreateBotOpts): Chainable<MatrixClient> => {
opts = Object.assign({}, defaultCreateBotOptions, opts);
const username = Cypress._.uniqueId("userId_");
const username = Cypress._.uniqueId(opts.userIdPrefix);
const password = Cypress._.uniqueId("password_");
return cy.registerUser(synapse, username, password, opts.displayName).then((credentials) => {
cy.log(`Registered bot user ${username} with displayname ${opts.displayName}`);
Expand Down
12 changes: 10 additions & 2 deletions cypress/support/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ declare global {
* @param synapse the synapse returned by startSynapse
* @param displayName the displayName to give the test user
* @param prelaunchFn optional function to run before the app is visited
* @param userIdPrefix optional prefix to use for the generated user id. If unspecified, `user_` will be
* useed.
*/
initTestUser(
synapse: SynapseInstance,
displayName: string,
prelaunchFn?: () => void,
userIdPrefix?: string,
): Chainable<UserCredentials>;
/**
* Logs into synapse with the given username/password
Expand Down Expand Up @@ -91,7 +94,12 @@ Cypress.Commands.add(
// eslint-disable-next-line max-len
Cypress.Commands.add(
"initTestUser",
(synapse: SynapseInstance, displayName: string, prelaunchFn?: () => void): Chainable<UserCredentials> => {
(
synapse: SynapseInstance,
displayName: string,
prelaunchFn?: () => void,
userIdPrefix = "user_",
): Chainable<UserCredentials> => {
// XXX: work around Cypress not clearing IDB between tests
cy.window({ log: false }).then((win) => {
win.indexedDB.databases()?.then((databases) => {
Expand All @@ -101,7 +109,7 @@ Cypress.Commands.add(
});
});

const username = Cypress._.uniqueId("userId_");
const username = Cypress._.uniqueId(userIdPrefix);
const password = Cypress._.uniqueId("password_");
return cy
.registerUser(synapse, username, password, displayName)
Expand Down

0 comments on commit ddfa627

Please sign in to comment.