element-web/test/unit-tests/components/views/dialogs/AccessSecretStorageDialog-test.tsx

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

126 lines
4.3 KiB
TypeScript
Raw Normal View History

/*
Copyright 2024 New Vector Ltd.
Copyright 2020-2022 The Matrix.org Foundation C.I.C.
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, { ComponentProps } from "react";
import { SecretStorage, MatrixClient } from "matrix-js-sdk/src/matrix";
import { act, fireEvent, render, screen } from "jest-matrix-react";
2023-02-03 11:39:25 +03:00
import userEvent from "@testing-library/user-event";
import { mockPlatformPeg, stubClient } from "../../../../test-utils";
import AccessSecretStorageDialog from "../../../../../src/components/views/dialogs/security/AccessSecretStorageDialog";
2023-02-03 11:39:25 +03:00
const securityKey = "EsTc WKmb ivvk jLS7 Y1NH 5CcQ mP1E JJwj B3Fd pFWm t4Dp dbyu";
describe("AccessSecretStorageDialog", () => {
let mockClient: MatrixClient;
2023-02-03 11:39:25 +03:00
const defaultProps: ComponentProps<typeof AccessSecretStorageDialog> = {
keyInfo: {} as any,
onFinished: jest.fn(),
checkPrivateKey: jest.fn(),
};
2023-02-03 11:39:25 +03:00
const renderComponent = (props = {}): void => {
render(<AccessSecretStorageDialog {...defaultProps} {...props} />);
};
2023-02-03 11:39:25 +03:00
const enterSecurityKey = (placeholder = "Security Key"): void => {
act(() => {
2023-02-03 11:39:25 +03:00
fireEvent.change(screen.getByPlaceholderText(placeholder), {
target: {
value: securityKey,
},
});
2023-02-03 11:39:25 +03:00
// wait for debounce
jest.advanceTimersByTime(250);
});
2023-02-03 11:39:25 +03:00
};
2023-02-03 11:39:25 +03:00
const submitDialog = async (): Promise<void> => {
await userEvent.click(screen.getByText("Continue"), { delay: null });
};
2023-02-03 11:39:25 +03:00
beforeAll(() => {
jest.useFakeTimers();
mockPlatformPeg();
});
2023-02-03 11:39:25 +03:00
afterAll(() => {
jest.useRealTimers();
jest.restoreAllMocks();
});
2023-02-03 11:39:25 +03:00
beforeEach(() => {
mockClient = stubClient();
2023-02-03 11:39:25 +03: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 11:39:25 +03:00
const onFinished = jest.fn();
const checkPrivateKey = jest.fn().mockResolvedValue(true);
renderComponent({ onFinished, checkPrivateKey });
// check that the input field is focused
expect(screen.getByPlaceholderText("Security Key")).toHaveFocus();
2023-02-03 11:39:25 +03:00
await enterSecurityKey();
await submitDialog();
expect(screen.getByText("Looks good!")).toBeInTheDocument();
expect(checkPrivateKey).toHaveBeenCalledWith({ recoveryKey: securityKey });
expect(onFinished).toHaveBeenCalledWith({ recoveryKey: securityKey });
});
it("Notifies the user if they input an invalid Security Key", async () => {
2023-02-03 11:39:25 +03:00
const onFinished = jest.fn();
const checkPrivateKey = jest.fn().mockResolvedValue(true);
renderComponent({ onFinished, checkPrivateKey });
jest.spyOn(mockClient.secretStorage, "checkKey").mockImplementation(() => {
throw new Error("invalid key");
});
2023-02-03 11:39:25 +03:00
await enterSecurityKey();
await submitDialog();
expect(screen.getByText("Continue")).toBeDisabled();
expect(screen.getByText("Invalid Security Key")).toBeInTheDocument();
});
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"],
iterations: 2,
salt: "nonempty",
},
};
const checkPrivateKey = jest.fn().mockResolvedValue(false);
2023-02-03 11:39:25 +03:00
renderComponent({ checkPrivateKey, keyInfo });
2023-02-03 11:39:25 +03:00
await enterSecurityKey("Security Phrase");
expect(screen.getByPlaceholderText("Security Phrase")).toHaveValue(securityKey);
await submitDialog();
await expect(
screen.findByText(
2023-02-03 11:39:25 +03:00
"👎 Unable to access secret storage. Please verify that you entered the correct Security Phrase.",
),
).resolves.toBeInTheDocument();
expect(screen.getByPlaceholderText("Security Phrase")).toHaveFocus();
});
});