2022-05-06 10:25:18 +02:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
|
Copyright 2020-2022 The Matrix.org Foundation C.I.C.
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-05-06 10:25:18 +02:00
|
|
|
*/
|
|
|
|
|
|
2025-02-05 13:25:06 +00:00
|
|
|
import React, { type ComponentProps } from "react";
|
|
|
|
|
import { type SecretStorage, type MatrixClient } from "matrix-js-sdk/src/matrix";
|
2024-10-14 21:41:58 +05:30
|
|
|
import { act, fireEvent, render, screen } from "jest-matrix-react";
|
2023-02-03 09:39:25 +01:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2024-10-15 14:57:26 +01:00
|
|
|
import { mockPlatformPeg, stubClient } from "../../../../test-utils";
|
|
|
|
|
import AccessSecretStorageDialog from "../../../../../src/components/views/dialogs/security/AccessSecretStorageDialog";
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2025-02-10 17:52:39 +01:00
|
|
|
const recoveryKey = "EsTc WKmb ivvk jLS7 Y1NH 5CcQ mP1E JJwj B3Fd pFWm t4Dp dbyu";
|
2023-02-03 09:39:25 +01:00
|
|
|
|
2022-05-06 10:25:18 +02:00
|
|
|
describe("AccessSecretStorageDialog", () => {
|
2024-10-14 17:08:42 +02:00
|
|
|
let mockClient: MatrixClient;
|
2023-02-03 09:39:25 +01:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
const defaultProps: ComponentProps<typeof AccessSecretStorageDialog> = {
|
2023-04-28 09:45:36 +01:00
|
|
|
keyInfo: {} as any,
|
2022-05-06 10:25:18 +02:00
|
|
|
onFinished: jest.fn(),
|
|
|
|
|
checkPrivateKey: jest.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-03 09:39:25 +01:00
|
|
|
const renderComponent = (props = {}): void => {
|
|
|
|
|
render(<AccessSecretStorageDialog {...defaultProps} {...props} />);
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-09 11:27:14 +01:00
|
|
|
const enterRecoveryKey = async (valueToEnter: string = recoveryKey): Promise<void> => {
|
|
|
|
|
await act(async () => {
|
2025-05-23 17:06:00 -04:00
|
|
|
fireEvent.change(screen.getByRole("textbox"), {
|
2023-02-03 09:39:25 +01:00
|
|
|
target: {
|
2025-06-09 11:27:14 +01:00
|
|
|
value: valueToEnter,
|
2023-02-03 09:39:25 +01:00
|
|
|
},
|
|
|
|
|
});
|
2025-06-09 11:27:14 +01:00
|
|
|
// wait for debounce, and then give `checkPrivateKey` a chance to complete
|
|
|
|
|
await jest.advanceTimersByTimeAsync(250);
|
2023-02-03 09:39:25 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const submitDialog = async (): Promise<void> => {
|
|
|
|
|
await userEvent.click(screen.getByText("Continue"), { delay: null });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
mockPlatformPeg();
|
2022-05-06 10:25:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterAll(() => {
|
2023-02-03 09:39:25 +01:00
|
|
|
jest.useRealTimers();
|
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2024-10-14 17:08:42 +02:00
|
|
|
mockClient = stubClient();
|
2022-05-06 10:25:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("Closes the dialog when the form is submitted with a valid key", async () => {
|
2024-10-14 17:08:42 +02:00
|
|
|
jest.spyOn(mockClient.secretStorage, "checkKey").mockResolvedValue(true);
|
2023-02-03 09:39:25 +01:00
|
|
|
|
2022-05-06 10:25:18 +02:00
|
|
|
const onFinished = jest.fn();
|
|
|
|
|
const checkPrivateKey = jest.fn().mockResolvedValue(true);
|
2023-02-03 09:39:25 +01:00
|
|
|
renderComponent({ onFinished, checkPrivateKey });
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2023-02-03 09:39:25 +01:00
|
|
|
// check that the input field is focused
|
2025-05-23 17:06:00 -04:00
|
|
|
expect(screen.getByRole("textbox")).toHaveFocus();
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2025-02-10 17:52:39 +01:00
|
|
|
await enterRecoveryKey();
|
2023-02-03 09:39:25 +01:00
|
|
|
await submitDialog();
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2025-05-23 17:06:00 -04:00
|
|
|
expect(screen.getByText("Continue")).not.toHaveAttribute("aria-disabled", "true");
|
2025-02-10 17:52:39 +01:00
|
|
|
expect(checkPrivateKey).toHaveBeenCalledWith({ recoveryKey });
|
|
|
|
|
expect(onFinished).toHaveBeenCalledWith({ recoveryKey });
|
2022-05-06 10:25:18 +02:00
|
|
|
});
|
|
|
|
|
|
2025-02-10 17:52:39 +01:00
|
|
|
it("Notifies the user if they input an invalid Recovery Key", async () => {
|
2023-02-03 09:39:25 +01:00
|
|
|
const onFinished = jest.fn();
|
2025-05-23 17:06:00 -04:00
|
|
|
const checkPrivateKey = jest.fn().mockResolvedValue(false);
|
2023-02-03 09:39:25 +01:00
|
|
|
renderComponent({ onFinished, checkPrivateKey });
|
|
|
|
|
|
2024-10-14 17:08:42 +02:00
|
|
|
jest.spyOn(mockClient.secretStorage, "checkKey").mockImplementation(() => {
|
2024-09-19 17:39:20 +02:00
|
|
|
throw new Error("invalid key");
|
2022-05-06 10:25:18 +02:00
|
|
|
});
|
|
|
|
|
|
2025-02-10 17:52:39 +01:00
|
|
|
await enterRecoveryKey();
|
2023-02-03 09:39:25 +01:00
|
|
|
await submitDialog();
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2025-05-23 17:06:00 -04:00
|
|
|
expect(screen.getByText("The recovery key you entered is not correct.")).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText("Continue")).toHaveAttribute("aria-disabled", "true");
|
2022-05-06 10:25:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("Notifies the user if they input an invalid passphrase", async function () {
|
|
|
|
|
const keyInfo = {
|
|
|
|
|
name: "test",
|
|
|
|
|
algorithm: "test",
|
|
|
|
|
iv: "test",
|
|
|
|
|
mac: "1:2:3:4",
|
|
|
|
|
passphrase: {
|
|
|
|
|
// this type is weird in js-sdk
|
|
|
|
|
// cast 'm.pbkdf2' to itself
|
2024-03-22 12:28:13 +00:00
|
|
|
algorithm: "m.pbkdf2" as SecretStorage.PassphraseInfo["algorithm"],
|
2022-05-06 10:25:18 +02:00
|
|
|
iterations: 2,
|
|
|
|
|
salt: "nonempty",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const checkPrivateKey = jest.fn().mockResolvedValue(false);
|
2023-02-03 09:39:25 +01:00
|
|
|
renderComponent({ checkPrivateKey, keyInfo });
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2025-05-23 17:06:00 -04:00
|
|
|
await enterRecoveryKey();
|
|
|
|
|
expect(screen.getByRole("textbox")).toHaveValue(recoveryKey);
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2025-05-23 17:06:00 -04:00
|
|
|
await expect(screen.findByText("The recovery key you entered is not correct.")).resolves.toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText("Continue")).toHaveAttribute("aria-disabled", "true");
|
2024-11-18 22:17:24 -05:00
|
|
|
});
|
2025-06-09 11:27:14 +01:00
|
|
|
|
|
|
|
|
it("Clears the 'invalid recovery key' notice when the input is cleared", async function () {
|
|
|
|
|
renderComponent({ onFinished: () => {}, checkPrivateKey: () => false });
|
|
|
|
|
|
|
|
|
|
jest.spyOn(mockClient.secretStorage, "checkKey").mockRejectedValue(new Error("invalid key"));
|
|
|
|
|
|
|
|
|
|
// First, enter the wrong recovery key
|
|
|
|
|
await enterRecoveryKey();
|
|
|
|
|
expect(screen.getByText("The recovery key you entered is not correct.")).toBeInTheDocument();
|
|
|
|
|
|
|
|
|
|
// Now, clear the input: the notice should be cleared.
|
|
|
|
|
await enterRecoveryKey("");
|
|
|
|
|
expect(screen.queryByText("The recovery key you entered is not correct.")).not.toBeInTheDocument();
|
|
|
|
|
expect(
|
|
|
|
|
screen.getByText("If you have a security key or security phrase, this will work too."),
|
|
|
|
|
).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText("Continue")).toHaveAttribute("aria-disabled", "true");
|
|
|
|
|
});
|
2022-05-06 10:25:18 +02:00
|
|
|
});
|