Skip to content

Commit 44c6d1d

Browse files
committed
test(e2e): checkDeviceIsConnectedKeyBackup is checking the key backup with the matrix client and the crypto api instead of relying of the Security & Privacy tab.
1 parent 84c6146 commit 44c6d1d

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

playwright/e2e/crypto/device-verification.spec.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ test.describe("Device verification", { tag: "@no-webkit" }, () => {
6868

6969
// Check that the current device is connected to key backup
7070
// For now we don't check that the backup key is in cache because it's a bit flaky,
71-
// as we need to wait for the secret gossiping to happen and the settings dialog doesn't refresh automatically.
72-
await checkDeviceIsConnectedKeyBackup(page, expectedBackupVersion, false);
71+
// as we need to wait for the secret gossiping to happen.
72+
await checkDeviceIsConnectedKeyBackup(app, expectedBackupVersion, false);
7373
});
7474

7575
test("Verify device with QR code during login", async ({ page, app, credentials, homeserver }) => {
@@ -112,9 +112,7 @@ test.describe("Device verification", { tag: "@no-webkit" }, () => {
112112
await checkDeviceIsCrossSigned(app);
113113

114114
// Check that the current device is connected to key backup
115-
// For now we don't check that the backup key is in cache because it's a bit flaky,
116-
// as we need to wait for the secret gossiping to happen and the settings dialog doesn't refresh automatically.
117-
await checkDeviceIsConnectedKeyBackup(page, expectedBackupVersion, false);
115+
await checkDeviceIsConnectedKeyBackup(app, expectedBackupVersion, true);
118116
});
119117

120118
test("Verify device with Security Phrase during login", async ({ page, app, credentials, homeserver }) => {
@@ -135,7 +133,7 @@ test.describe("Device verification", { tag: "@no-webkit" }, () => {
135133

136134
// Check that the current device is connected to key backup
137135
// The backup decryption key should be in cache also, as we got it directly from the 4S
138-
await checkDeviceIsConnectedKeyBackup(page, expectedBackupVersion, true);
136+
await checkDeviceIsConnectedKeyBackup(app, expectedBackupVersion, true);
139137
});
140138

141139
test("Verify device with Security Key during login", async ({ page, app, credentials, homeserver }) => {
@@ -158,7 +156,7 @@ test.describe("Device verification", { tag: "@no-webkit" }, () => {
158156

159157
// Check that the current device is connected to key backup
160158
// The backup decryption key should be in cache also, as we got it directly from the 4S
161-
await checkDeviceIsConnectedKeyBackup(page, expectedBackupVersion, true);
159+
await checkDeviceIsConnectedKeyBackup(app, expectedBackupVersion, true);
162160
});
163161

164162
test("Handle incoming verification request with SAS", async ({ page, credentials, homeserver, toasts }) => {

playwright/e2e/crypto/utils.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ export async function checkDeviceIsCrossSigned(app: ElementAppPage): Promise<voi
139139
* Check that the current device is connected to the expected key backup.
140140
* Also checks that the decryption key is known and cached locally.
141141
*
142-
* @param page - the page to check
142+
* @param app - app page
143143
* @param expectedBackupVersion - the version of the backup we expect to be connected to.
144144
* @param checkBackupKeyInCache - whether to check that the backup key is cached locally.
145145
*/
146146
export async function checkDeviceIsConnectedKeyBackup(
147-
page: Page,
147+
app: ElementAppPage,
148148
expectedBackupVersion: string,
149149
checkBackupKeyInCache: boolean,
150150
): Promise<void> {
@@ -155,23 +155,41 @@ export async function checkDeviceIsConnectedKeyBackup(
155155
);
156156
}
157157

158-
await page.getByRole("button", { name: "User menu" }).click();
159-
await page.locator(".mx_UserMenu_contextMenu").getByRole("menuitem", { name: "Security & Privacy" }).click();
160-
await expect(page.locator(".mx_Dialog").getByRole("button", { name: "Restore from Backup" })).toBeVisible();
158+
const backupData = await app.client.evaluate(async (client: MatrixClient) => {
159+
const crypto = client.getCrypto();
160+
if (!crypto) return;
161161

162-
// expand the advanced section to see the active version in the reports
163-
await page.locator(".mx_SecureBackupPanel_advanced").locator("..").click();
162+
const backupInfo = await crypto.getKeyBackupInfo();
163+
const backupKeyStored = Boolean(await client.isKeyBackupKeyStored());
164+
const backupKeyFromCache = await crypto.getSessionBackupPrivateKey();
165+
const backupKeyCached = Boolean(backupKeyFromCache);
166+
const backupKeyWellFormed = backupKeyFromCache instanceof Uint8Array;
167+
const activeBackupVersion = await crypto.getActiveSessionBackupVersion();
164168

165-
if (checkBackupKeyInCache) {
166-
const cacheDecryptionKeyStatusElement = page.locator(".mx_SecureBackupPanel_statusList tr:nth-child(2) td");
167-
await expect(cacheDecryptionKeyStatusElement).toHaveText("cached locally, well formed");
169+
return { backupInfo, backupKeyStored, backupKeyCached, backupKeyWellFormed, activeBackupVersion };
170+
});
171+
172+
if (!backupData) {
173+
throw new Error("Crypo module is not available");
168174
}
169175

170-
await expect(page.locator(".mx_SecureBackupPanel_statusList tr:nth-child(5) td")).toHaveText(
171-
expectedBackupVersion + " (Algorithm: m.megolm_backup.v1.curve25519-aes-sha2)",
172-
);
176+
const { backupInfo, backupKeyStored, backupKeyCached, backupKeyWellFormed, activeBackupVersion } = backupData;
177+
178+
// We have a key backup
179+
expect(backupInfo).toBeDefined();
180+
// The key backup version is as expected
181+
expect(backupInfo.version).toBe(expectedBackupVersion);
182+
// The active backup version is as expected
183+
expect(activeBackupVersion).toBe(expectedBackupVersion);
184+
// The backup key is stored in 4S
185+
expect(backupKeyStored).toBe(true);
173186

174-
await expect(page.locator(".mx_SecureBackupPanel_statusList tr:nth-child(6) td")).toHaveText(expectedBackupVersion);
187+
if (checkBackupKeyInCache) {
188+
// The backup key is available locally
189+
expect(backupKeyCached).toBe(true);
190+
// The backup key is well-formed
191+
expect(backupKeyWellFormed).toBe(true);
192+
}
175193
}
176194

177195
/**

playwright/e2e/settings/encryption-user-tab/recovery.spec.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ test.describe("Recovery section in Encryption tab", () => {
4747

4848
// Check that the current device is connected to key backup
4949
// The backup decryption key should be in cache also, as we got it directly from the 4S
50-
await app.closeDialog();
51-
await checkDeviceIsConnectedKeyBackup(page, expectedBackupVersion, true);
50+
await checkDeviceIsConnectedKeyBackup(app, expectedBackupVersion, true);
5251
});
5352

5453
test(
@@ -115,9 +114,8 @@ test.describe("Recovery section in Encryption tab", () => {
115114
// The recovery key is now set up and the user can change it
116115
await expect(dialog.getByRole("button", { name: "Change recovery key" })).toBeVisible();
117116

118-
await app.closeDialog();
119117
// Check that the current device is connected to key backup and the backup version is the expected one
120-
await checkDeviceIsConnectedKeyBackup(page, "1", true);
118+
await checkDeviceIsConnectedKeyBackup(app, "1", true);
121119
});
122120

123121
// Test what happens if the cross-signing secrets are in secret storage but are not cached in the local DB.
@@ -149,8 +147,7 @@ test.describe("Recovery section in Encryption tab", () => {
149147

150148
// Check that the current device is connected to key backup
151149
// The backup decryption key should be in cache also, as we got it directly from the 4S
152-
await app.closeDialog();
153-
await checkDeviceIsConnectedKeyBackup(page, expectedBackupVersion, true);
150+
await checkDeviceIsConnectedKeyBackup(app, expectedBackupVersion, true);
154151
},
155152
);
156153
});

0 commit comments

Comments
 (0)