From 1058af6adda41009ef1f7723eb4f427c55833962 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Tue, 17 Sep 2024 18:50:02 +0100 Subject: [PATCH] Playwright test for E2E messages from deleted devices (#47) * Factor out `createSecondBotDevice` utility * Add playwright test for messages from deleted devices Thanks to MSC4147, we now have information on the devices that sent messages, even when the device has since been deleted. Test that out. --- playwright/e2e/crypto/event-shields.spec.ts | 67 ++++++++++++++++----- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/playwright/e2e/crypto/event-shields.spec.ts b/playwright/e2e/crypto/event-shields.spec.ts index 3a85d06333..98142089fb 100644 --- a/playwright/e2e/crypto/event-shields.spec.ts +++ b/playwright/e2e/crypto/event-shields.spec.ts @@ -6,9 +6,12 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only Please see LICENSE files in the repository root for full details. */ +import { Page } from "@playwright/test"; + import { expect, test } from "../../element-web-test"; import { autoJoin, createSharedRoomWithUser, enableKeyBackup, logIntoElement, logOutOfElement, verify } from "./utils"; import { Bot } from "../../pages/bot"; +import { HomeserverInstance } from "../../plugins/homeserver"; test.describe("Cryptography", function () { test.use({ @@ -48,14 +51,7 @@ test.describe("Cryptography", function () { homeserver, }, workerInfo) => { // Bob has a second, not cross-signed, device - const bobSecondDevice = new Bot(page, homeserver, { - bootstrapSecretStorage: false, - bootstrapCrossSigning: false, - }); - bobSecondDevice.setCredentials( - await homeserver.loginUser(bob.credentials.userId, bob.credentials.password), - ); - await bobSecondDevice.prepareClient(); + const bobSecondDevice = await createSecondBotDevice(page, homeserver, bob); await bob.sendEvent(testRoomId, null, "m.room.encrypted", { algorithm: "m.megolm.v1.aes-sha2", @@ -216,14 +212,7 @@ test.describe("Cryptography", function () { test("should show the correct shield on edited e2e events", async ({ page, app, bot: bob, homeserver }) => { // bob has a second, not cross-signed, device - const bobSecondDevice = new Bot(page, homeserver, { - bootstrapSecretStorage: false, - bootstrapCrossSigning: false, - }); - bobSecondDevice.setCredentials( - await homeserver.loginUser(bob.credentials.userId, bob.credentials.password), - ); - await bobSecondDevice.prepareClient(); + const bobSecondDevice = await createSecondBotDevice(page, homeserver, bob); // verify Bob await verify(app, bob); @@ -269,5 +258,51 @@ test.describe("Cryptography", function () { page.locator(".mx_EventTile", { hasText: "Hee!" }).locator(".mx_EventTile_e2eIcon_warning"), ).not.toBeVisible(); }); + + test("should show correct shields on events sent by devices which have since been deleted", async ({ + page, + app, + bot: bob, + homeserver, + }) => { + // Our app is blocked from syncing while Bob sends his messages. + await app.client.network.goOffline(); + + // Bob sends a message from his verified device + await bob.sendMessage(testRoomId, "test encrypted from verified"); + + // And one from a second, not cross-signed, device + const bobSecondDevice = await createSecondBotDevice(page, homeserver, bob); + await bobSecondDevice.waitForNextSync(); // make sure the client knows the room is encrypted + await bobSecondDevice.sendMessage(testRoomId, "test encrypted from unverified"); + + // ... and then logs out both devices. + await bob.evaluate((cli) => cli.logout(true)); + await bobSecondDevice.evaluate((cli) => cli.logout(true)); + + // Let our app start syncing again + await app.client.network.goOnline(); + + // Wait for the messages to arrive + const last = page.locator(".mx_EventTile_last"); + await expect(last).toContainText("test encrypted from unverified"); + const lastE2eIcon = last.locator(".mx_EventTile_e2eIcon"); + await expect(lastE2eIcon).toHaveClass(/mx_EventTile_e2eIcon_warning/); + await lastE2eIcon.focus(); + await expect(page.getByRole("tooltip")).toContainText("Encrypted by a device not verified by its owner."); + + const penultimate = page.locator(".mx_EventTile").filter({ hasText: "test encrypted from verified" }); + await expect(penultimate.locator(".mx_EventTile_e2eIcon")).not.toBeVisible(); + }); }); }); + +async function createSecondBotDevice(page: Page, homeserver: HomeserverInstance, bob: Bot) { + const bobSecondDevice = new Bot(page, homeserver, { + bootstrapSecretStorage: false, + bootstrapCrossSigning: false, + }); + bobSecondDevice.setCredentials(await homeserver.loginUser(bob.credentials.userId, bob.credentials.password)); + await bobSecondDevice.prepareClient(); + return bobSecondDevice; +}