2024-09-19 18:39:20 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2024 New Vector Ltd.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
* Please see LICENSE files in the repository root for full details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2024-10-14 19:11:58 +03:00
|
|
|
import { screen, render, waitFor } from "jest-matrix-react";
|
2024-09-19 18:39:20 +03:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2024-11-13 13:32:06 +03:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { KeyBackupInfo } from "matrix-js-sdk/src/crypto-api";
|
2024-09-19 18:39:20 +03:00
|
|
|
// Needed to be able to mock decodeRecoveryKey
|
|
|
|
// eslint-disable-next-line no-restricted-imports
|
|
|
|
import * as recoveryKeyModule from "matrix-js-sdk/src/crypto-api/recovery-key";
|
|
|
|
|
2024-10-15 16:57:26 +03:00
|
|
|
import RestoreKeyBackupDialog from "../../../../../../src/components/views/dialogs/security/RestoreKeyBackupDialog.tsx";
|
|
|
|
import { stubClient } from "../../../../../test-utils";
|
2024-09-19 18:39:20 +03:00
|
|
|
|
|
|
|
describe("<RestoreKeyBackupDialog />", () => {
|
2024-11-13 13:32:06 +03:00
|
|
|
const keyBackupRestoreResult = {
|
|
|
|
total: 2,
|
|
|
|
imported: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
let matrixClient: MatrixClient;
|
2024-09-19 18:39:20 +03:00
|
|
|
beforeEach(() => {
|
2024-11-13 13:32:06 +03:00
|
|
|
matrixClient = stubClient();
|
2024-09-19 18:39:20 +03:00
|
|
|
jest.spyOn(recoveryKeyModule, "decodeRecoveryKey").mockReturnValue(new Uint8Array(32));
|
2024-11-13 13:32:06 +03:00
|
|
|
jest.spyOn(matrixClient, "getKeyBackupVersion").mockResolvedValue({ version: "1" } as KeyBackupInfo);
|
2024-09-19 18:39:20 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should render", async () => {
|
|
|
|
const { asFragment } = render(<RestoreKeyBackupDialog onFinished={jest.fn()} />);
|
|
|
|
await waitFor(() => expect(screen.getByText("Enter Security Key")).toBeInTheDocument());
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should display an error when recovery key is invalid", async () => {
|
|
|
|
jest.spyOn(recoveryKeyModule, "decodeRecoveryKey").mockImplementation(() => {
|
|
|
|
throw new Error("Invalid recovery key");
|
|
|
|
});
|
|
|
|
const { asFragment } = render(<RestoreKeyBackupDialog onFinished={jest.fn()} />);
|
|
|
|
await waitFor(() => expect(screen.getByText("Enter Security Key")).toBeInTheDocument());
|
|
|
|
|
|
|
|
await userEvent.type(screen.getByRole("textbox"), "invalid key");
|
|
|
|
await waitFor(() => expect(screen.getByText("👎 Not a valid Security Key")).toBeInTheDocument());
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not raise an error when recovery is valid", async () => {
|
|
|
|
const { asFragment } = render(<RestoreKeyBackupDialog onFinished={jest.fn()} />);
|
|
|
|
await waitFor(() => expect(screen.getByText("Enter Security Key")).toBeInTheDocument());
|
|
|
|
|
|
|
|
await userEvent.type(screen.getByRole("textbox"), "valid key");
|
|
|
|
await waitFor(() => expect(screen.getByText("👍 This looks like a valid Security Key!")).toBeInTheDocument());
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
});
|
2024-11-13 13:32:06 +03:00
|
|
|
|
|
|
|
it("should restore key backup when the key is cached", async () => {
|
|
|
|
jest.spyOn(matrixClient.getCrypto()!, "restoreKeyBackup").mockResolvedValue(keyBackupRestoreResult);
|
|
|
|
|
|
|
|
const { asFragment } = render(<RestoreKeyBackupDialog onFinished={jest.fn()} />);
|
|
|
|
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(<RestoreKeyBackupDialog onFinished={jest.fn()} />);
|
|
|
|
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(<RestoreKeyBackupDialog onFinished={jest.fn()} />);
|
|
|
|
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(<RestoreKeyBackupDialog onFinished={jest.fn()} />);
|
|
|
|
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();
|
|
|
|
});
|
2024-09-19 18:39:20 +03:00
|
|
|
});
|