Files
ThreadNet-Web/test/unit-tests/components/views/dialogs/AccessSecretStorageDialog-test.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

138 lines
5.2 KiB
TypeScript
Raw Normal View History

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
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";
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
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", () => {
let mockClient: MatrixClient;
2023-02-03 09:39:25 +01:00
2023-02-13 11:39:16 +00:00
const defaultProps: ComponentProps<typeof AccessSecretStorageDialog> = {
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} />);
};
const enterRecoveryKey = async (valueToEnter: string = recoveryKey): Promise<void> => {
await act(async () => {
fireEvent.change(screen.getByRole("textbox"), {
2023-02-03 09:39:25 +01:00
target: {
value: valueToEnter,
2023-02-03 09:39:25 +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(() => {
mockClient = stubClient();
2022-05-06 10:25:18 +02:00
});
it("Closes the dialog when the form is submitted with a valid key", async () => {
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
expect(screen.getByRole("textbox")).toHaveFocus();
2022-05-06 10:25:18 +02:00
await enterRecoveryKey();
2023-02-03 09:39:25 +01:00
await submitDialog();
2022-05-06 10:25:18 +02:00
expect(screen.getByText("Continue")).not.toHaveAttribute("aria-disabled", "true");
expect(checkPrivateKey).toHaveBeenCalledWith({ recoveryKey });
expect(onFinished).toHaveBeenCalledWith({ recoveryKey });
2022-05-06 10:25:18 +02: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();
const checkPrivateKey = jest.fn().mockResolvedValue(false);
2023-02-03 09:39:25 +01:00
renderComponent({ onFinished, checkPrivateKey });
jest.spyOn(mockClient.secretStorage, "checkKey").mockImplementation(() => {
throw new Error("invalid key");
2022-05-06 10:25:18 +02:00
});
await enterRecoveryKey();
2023-02-03 09:39:25 +01:00
await submitDialog();
2022-05-06 10:25:18 +02: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
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
await enterRecoveryKey();
expect(screen.getByRole("textbox")).toHaveValue(recoveryKey);
2022-05-06 10:25:18 +02:00
await expect(screen.findByText("The recovery key you entered is not correct.")).resolves.toBeInTheDocument();
expect(screen.getByText("Continue")).toHaveAttribute("aria-disabled", "true");
});
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
});