From 0a0ba2aee0e91e72d0342dc2e739772b13d9f380 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Fri, 8 Nov 2024 16:13:15 +0100 Subject: [PATCH] Add tests to `RestoreKeyBackupDialog` --- .../security/RestoreKeyBackupDialog.tsx | 1 + test/test-utils/test-utils.ts | 5 + .../security/RestoreKeyBackupDialog-test.tsx | 78 +++++- .../RestoreKeyBackupDialog-test.tsx.snap | 260 ++++++++++++++++++ 4 files changed, 343 insertions(+), 1 deletion(-) diff --git a/src/components/views/dialogs/security/RestoreKeyBackupDialog.tsx b/src/components/views/dialogs/security/RestoreKeyBackupDialog.tsx index 9f1677972a1..af84feb8480 100644 --- a/src/components/views/dialogs/security/RestoreKeyBackupDialog.tsx +++ b/src/components/views/dialogs/security/RestoreKeyBackupDialog.tsx @@ -394,6 +394,7 @@ export default class RestoreKeyBackupDialog extends React.PureComponent ", () => { + const keyBackupRestoreResult = { + total: 2, + imported: 1, + }; + + let matrixClient: MatrixClient; beforeEach(() => { - stubClient(); + matrixClient = stubClient(); jest.spyOn(recoveryKeyModule, "decodeRecoveryKey").mockReturnValue(new Uint8Array(32)); + jest.spyOn(matrixClient, "getKeyBackupVersion").mockResolvedValue({ version: "1" } as KeyBackupInfo); }); it("should render", async () => { @@ -48,4 +57,71 @@ describe("", () => { await waitFor(() => expect(screen.getByText("👍 This looks like a valid Security Key!")).toBeInTheDocument()); expect(asFragment()).toMatchSnapshot(); }); + + it("should restore key backup when the key is cached", async () => { + jest.spyOn(matrixClient.getCrypto()!, "restoreKeyBackup").mockResolvedValue(keyBackupRestoreResult); + + const { asFragment } = render(); + await waitFor(() => expect(screen.getByText("Successfully restored 1 keys")).toBeInTheDocument()); + expect(asFragment()).toMatchSnapshot(); + }); + + it("should restore key backup when the key is in secret storage", async () => { + jest.spyOn(matrixClient.getCrypto()!, "restoreKeyBackup") + // Reject when trying to restore from cache + .mockRejectedValueOnce(new Error("key backup not found")) + // Resolve when trying to restore from secret storage + .mockResolvedValue(keyBackupRestoreResult); + jest.spyOn(matrixClient.secretStorage, "hasKey").mockResolvedValue(true); + jest.spyOn(matrixClient, "isKeyBackupKeyStored").mockResolvedValue({}); + + const { asFragment } = render(); + await waitFor(() => expect(screen.getByText("Successfully restored 1 keys")).toBeInTheDocument()); + expect(asFragment()).toMatchSnapshot(); + }); + + it("should restore key backup when security key is filled by user", async () => { + jest.spyOn(matrixClient.getCrypto()!, "restoreKeyBackup") + // Reject when trying to restore from cache + .mockRejectedValueOnce(new Error("key backup not found")) + // Resolve when trying to restore from recovery key + .mockResolvedValue(keyBackupRestoreResult); + + const { asFragment } = render(); + await waitFor(() => expect(screen.getByText("Enter Security Key")).toBeInTheDocument()); + + await userEvent.type(screen.getByRole("textbox"), "my security key"); + await userEvent.click(screen.getByRole("button", { name: "Next" })); + + await waitFor(() => expect(screen.getByText("Successfully restored 1 keys")).toBeInTheDocument()); + expect(asFragment()).toMatchSnapshot(); + }); + + test("should restore key backup when passphrase is filled", async () => { + // Determine that the passphrase is required + jest.spyOn(matrixClient, "getKeyBackupVersion").mockResolvedValue({ + version: "1", + auth_data: { + private_key_salt: "salt", + private_key_iterations: 1, + }, + } as KeyBackupInfo); + + jest.spyOn(matrixClient.getCrypto()!, "restoreKeyBackup") + // Reject when trying to restore from cache + .mockRejectedValue(new Error("key backup not found")); + + jest.spyOn(matrixClient.getCrypto()!, "restoreKeyBackupWithPassphrase").mockResolvedValue( + keyBackupRestoreResult, + ); + + const { asFragment } = render(); + await waitFor(() => expect(screen.getByText("Enter Security Phrase")).toBeInTheDocument()); + // Not role for password https://github.com/w3c/aria/issues/935 + await userEvent.type(screen.getByTestId("passphraseInput"), "my passphrase"); + await userEvent.click(screen.getByRole("button", { name: "Next" })); + + await waitFor(() => expect(screen.getByText("Successfully restored 1 keys")).toBeInTheDocument()); + expect(asFragment()).toMatchSnapshot(); + }); }); diff --git a/test/unit-tests/components/views/dialogs/security/__snapshots__/RestoreKeyBackupDialog-test.tsx.snap b/test/unit-tests/components/views/dialogs/security/__snapshots__/RestoreKeyBackupDialog-test.tsx.snap index 5990d482b8c..b6461595b35 100644 --- a/test/unit-tests/components/views/dialogs/security/__snapshots__/RestoreKeyBackupDialog-test.tsx.snap +++ b/test/unit-tests/components/views/dialogs/security/__snapshots__/RestoreKeyBackupDialog-test.tsx.snap @@ -296,3 +296,263 @@ exports[` should render 1`] = ` /> `; + +exports[` should restore key backup when passphrase is required 1`] = ` + +
+