Skip to content

Commit

Permalink
e2e test: by default the bot should not use a passphrase to create th…
Browse files Browse the repository at this point in the history
…e recovery key (#29214)

* test(crypto): by default do not use a passphrase to create the recovery key

* test(crypto): update tests
  • Loading branch information
florianduros authored Feb 7, 2025
1 parent 4a231c6 commit 7b3ce5d
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion playwright/e2e/crypto/device-verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test.describe("Device verification", { tag: "@no-webkit" }, () => {
let expectedBackupVersion: string;

test.beforeEach(async ({ page, homeserver, credentials }) => {
const res = await createBot(page, homeserver, credentials);
const res = await createBot(page, homeserver, credentials, true);
aliceBotClient = res.botClient;
expectedBackupVersion = res.expectedBackupVersion;
});
Expand Down
1 change: 0 additions & 1 deletion playwright/e2e/crypto/toasts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ test.describe("Key storage out of sync toast", () => {
await expect(page.getByRole("alert").first()).toMatchScreenshot("key-storage-out-of-sync-toast.png");

await page.getByRole("button", { name: "Enter recovery key" }).click();
await page.locator(".mx_Dialog").getByRole("button", { name: "use your Security Key" }).click();

await page.getByRole("textbox", { name: "Security key" }).fill(recoveryKey.encodedPrivateKey);
await page.getByRole("button", { name: "Continue" }).click();
Expand Down
3 changes: 3 additions & 0 deletions playwright/e2e/crypto/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import { Bot } from "../../pages/bot";
* @param page - the playwright `page` fixture
* @param homeserver - the homeserver to use
* @param credentials - the credentials to use for the bot client
* @param usePassphrase - whether to use a passphrase when creating the recovery key
*/
export async function createBot(
page: Page,
homeserver: HomeserverInstance,
credentials: Credentials,
usePassphrase = false,
): Promise<{ botClient: Bot; recoveryKey: GeneratedSecretStorageKey; expectedBackupVersion: string }> {
// Visit the login page of the app, to load the matrix sdk
await page.goto("/#/login");
Expand All @@ -44,6 +46,7 @@ export async function createBot(
const botClient = new Bot(page, homeserver, {
bootstrapCrossSigning: true,
bootstrapSecretStorage: true,
usePassphrase,
});
botClient.setCredentials(credentials);
// Backup is prepared in the background. Poll until it is ready.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ test.describe("Encryption tab", () => {
"should prompt to enter the recovery key when the secrets are not cached locally",
{ tag: "@screenshot" },
async ({ page, app, util }) => {
await verifySession(app, "new passphrase");
await verifySession(app, recoveryKey.encodedPrivateKey);
// We need to delete the cached secrets
await deleteCachedSecrets(page);

Expand Down Expand Up @@ -99,7 +99,7 @@ test.describe("Encryption tab", () => {
app,
util,
}) => {
await verifySession(app, "new passphrase");
await verifySession(app, recoveryKey.encodedPrivateKey);
// We need to delete the cached secrets
await deleteCachedSecrets(page);

Expand Down
5 changes: 1 addition & 4 deletions playwright/e2e/settings/encryption-user-tab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Helpers {
*/
async verifyDevice(recoveryKey: GeneratedSecretStorageKey) {
// Select the security phrase
await this.page.getByRole("button", { name: "Verify with Security Key or Phrase" }).click();
await this.page.getByRole("button", { name: "Verify with Security Key" }).click();
await this.enterRecoveryKey(recoveryKey);
await this.page.getByRole("button", { name: "Done" }).click();
}
Expand All @@ -53,9 +53,6 @@ class Helpers {
* @param recoveryKey
*/
async enterRecoveryKey(recoveryKey: GeneratedSecretStorageKey) {
// Select to use recovery key
await this.page.getByRole("button", { name: "use your Security Key" }).click();

// Fill the recovery key
const dialog = this.page.locator(".mx_Dialog");
await dialog.getByRole("textbox").fill(recoveryKey.encodedPrivateKey);
Expand Down
9 changes: 6 additions & 3 deletions playwright/e2e/settings/encryption-user-tab/recovery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@

import { test, expect } from ".";
import { checkDeviceIsConnectedKeyBackup, createBot, verifySession } from "../../crypto/utils";
import type { GeneratedSecretStorageKey } from "matrix-js-sdk/src/crypto-api";

test.describe("Recovery section in Encryption tab", () => {
test.use({
displayName: "Alice",
});

let recoveryKey: GeneratedSecretStorageKey;
test.beforeEach(async ({ page, homeserver, credentials }) => {
// The bot bootstraps cross-signing, creates a key backup and sets up a recovery key
await createBot(page, homeserver, credentials);
const res = await createBot(page, homeserver, credentials);
recoveryKey = res.recoveryKey;
});

test(
"should change the recovery key",
{ tag: ["@screenshot", "@no-webkit"] },
async ({ page, app, homeserver, credentials, util, context }) => {
await verifySession(app, "new passphrase");
await verifySession(app, recoveryKey.encodedPrivateKey);
const dialog = await util.openEncryptionTab();

// The user can only change the recovery key
Expand All @@ -49,7 +52,7 @@ test.describe("Recovery section in Encryption tab", () => {
);

test("should setup the recovery key", { tag: ["@screenshot", "@no-webkit"] }, async ({ page, app, util }) => {
await verifySession(app, "new passphrase");
await verifySession(app, recoveryKey.encodedPrivateKey);
await util.removeSecretStorageDefaultKeyId();

// The key backup is deleted and the user needs to set it up
Expand Down
11 changes: 8 additions & 3 deletions playwright/pages/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@ export interface CreateBotOpts {
* Whether to bootstrap the secret storage
*/
bootstrapSecretStorage?: boolean;
/**
* Whether to use a passphrase when creating the recovery key
*/
usePassphrase?: boolean;
}

const defaultCreateBotOptions = {
userIdPrefix: "bot_",
autoAcceptInvites: true,
startClient: true,
bootstrapCrossSigning: true,
usePassphrase: false,
} satisfies CreateBotOpts;

type ExtendedMatrixClient = MatrixClient & { __playwright_recovery_key: GeneratedSecretStorageKey };
Expand Down Expand Up @@ -206,8 +211,8 @@ export class Bot extends Client {
}

if (this.opts.bootstrapSecretStorage) {
await clientHandle.evaluate(async (cli) => {
const passphrase = "new passphrase";
await clientHandle.evaluate(async (cli, usePassphrase) => {
const passphrase = usePassphrase ? "new passphrase" : undefined;
const recoveryKey = await cli.getCrypto().createRecoveryKeyFromPassphrase(passphrase);
Object.assign(cli, { __playwright_recovery_key: recoveryKey });

Expand All @@ -216,7 +221,7 @@ export class Bot extends Client {
setupNewKeyBackup: true,
createSecretStorageKey: () => Promise.resolve(recoveryKey),
});
});
}, this.opts.usePassphrase);
}

return clientHandle;
Expand Down

0 comments on commit 7b3ce5d

Please sign in to comment.