2022-05-06 08:25:18 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2020-2022 The Matrix.org Foundation C.I.C.
|
2022-05-06 08:25:18 +00:00
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-05-06 08:25:18 +00:00
|
|
|
*/
|
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
import React, { ComponentProps } from "react";
|
2024-03-22 12:28:13 +00:00
|
|
|
import { SecretStorage, MatrixClient } from "matrix-js-sdk/src/matrix";
|
2023-02-03 08:39:25 +00:00
|
|
|
import { act, fireEvent, render, screen } from "@testing-library/react";
|
|
|
|
import userEvent from "@testing-library/user-event";
|
2022-05-06 08:25:18 +00:00
|
|
|
|
2024-10-14 15:08:42 +00:00
|
|
|
import { mockPlatformPeg, stubClient } from "../../../test-utils";
|
2022-05-06 08:25:18 +00:00
|
|
|
import AccessSecretStorageDialog from "../../../../src/components/views/dialogs/security/AccessSecretStorageDialog";
|
|
|
|
|
2023-02-03 08:39:25 +00:00
|
|
|
const securityKey = "EsTc WKmb ivvk jLS7 Y1NH 5CcQ mP1E JJwj B3Fd pFWm t4Dp dbyu";
|
|
|
|
|
2022-05-06 08:25:18 +00:00
|
|
|
describe("AccessSecretStorageDialog", () => {
|
2024-10-14 15:08:42 +00:00
|
|
|
let mockClient: MatrixClient;
|
2023-02-03 08:39:25 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
const defaultProps: ComponentProps<typeof AccessSecretStorageDialog> = {
|
2023-04-28 08:45:36 +00:00
|
|
|
keyInfo: {} as any,
|
2022-05-06 08:25:18 +00:00
|
|
|
onFinished: jest.fn(),
|
|
|
|
checkPrivateKey: jest.fn(),
|
|
|
|
};
|
|
|
|
|
2023-02-03 08:39:25 +00:00
|
|
|
const renderComponent = (props = {}): void => {
|
|
|
|
render(<AccessSecretStorageDialog {...defaultProps} {...props} />);
|
|
|
|
};
|
2022-05-06 08:25:18 +00:00
|
|
|
|
2023-02-03 08:39:25 +00:00
|
|
|
const enterSecurityKey = (placeholder = "Security Key"): void => {
|
2022-05-06 08:25:18 +00:00
|
|
|
act(() => {
|
2023-02-03 08:39:25 +00:00
|
|
|
fireEvent.change(screen.getByPlaceholderText(placeholder), {
|
|
|
|
target: {
|
|
|
|
value: securityKey,
|
|
|
|
},
|
2022-05-06 08:25:18 +00:00
|
|
|
});
|
2023-02-03 08:39:25 +00:00
|
|
|
// wait for debounce
|
|
|
|
jest.advanceTimersByTime(250);
|
2022-05-06 08:25:18 +00:00
|
|
|
});
|
2023-02-03 08:39:25 +00:00
|
|
|
};
|
2022-05-06 08:25:18 +00:00
|
|
|
|
2023-02-03 08:39:25 +00:00
|
|
|
const submitDialog = async (): Promise<void> => {
|
|
|
|
await userEvent.click(screen.getByText("Continue"), { delay: null });
|
|
|
|
};
|
2022-05-06 08:25:18 +00:00
|
|
|
|
2023-02-03 08:39:25 +00:00
|
|
|
beforeAll(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
mockPlatformPeg();
|
|
|
|
});
|
2022-05-06 08:25:18 +00:00
|
|
|
|
2023-02-03 08:39:25 +00:00
|
|
|
afterAll(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
jest.restoreAllMocks();
|
2022-05-06 08:25:18 +00:00
|
|
|
});
|
|
|
|
|
2023-02-03 08:39:25 +00:00
|
|
|
beforeEach(() => {
|
2024-10-14 15:08:42 +00:00
|
|
|
mockClient = stubClient();
|
2023-02-03 08:39:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Closes the dialog when the form is submitted with a valid key", async () => {
|
2024-10-14 15:08:42 +00:00
|
|
|
jest.spyOn(mockClient.secretStorage, "checkKey").mockResolvedValue(true);
|
2022-05-06 08:25:18 +00:00
|
|
|
|
2023-02-03 08:39:25 +00: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();
|
2022-05-06 08:25:18 +00:00
|
|
|
|
2023-02-03 08:39:25 +00:00
|
|
|
await enterSecurityKey();
|
|
|
|
await submitDialog();
|
|
|
|
|
|
|
|
expect(screen.getByText("Looks good!")).toBeInTheDocument();
|
|
|
|
expect(checkPrivateKey).toHaveBeenCalledWith({ recoveryKey: securityKey });
|
|
|
|
expect(onFinished).toHaveBeenCalledWith({ recoveryKey: securityKey });
|
2022-05-06 08:25:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Notifies the user if they input an invalid Security Key", async () => {
|
2023-02-03 08:39:25 +00:00
|
|
|
const onFinished = jest.fn();
|
|
|
|
const checkPrivateKey = jest.fn().mockResolvedValue(true);
|
|
|
|
renderComponent({ onFinished, checkPrivateKey });
|
|
|
|
|
2024-10-14 15:08:42 +00:00
|
|
|
jest.spyOn(mockClient.secretStorage, "checkKey").mockImplementation(() => {
|
2024-09-19 15:39:20 +00:00
|
|
|
throw new Error("invalid key");
|
2022-05-06 08:25:18 +00:00
|
|
|
});
|
|
|
|
|
2023-02-03 08:39:25 +00:00
|
|
|
await enterSecurityKey();
|
|
|
|
await submitDialog();
|
|
|
|
|
|
|
|
expect(screen.getByText("Continue")).toBeDisabled();
|
|
|
|
expect(screen.getByText("Invalid Security Key")).toBeInTheDocument();
|
2022-05-06 08:25:18 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("Notifies the user if they input an invalid passphrase", async function () {
|
2022-05-06 08:25:18 +00:00
|
|
|
const keyInfo = {
|
2022-12-12 11:24:14 +00:00
|
|
|
name: "test",
|
|
|
|
algorithm: "test",
|
|
|
|
iv: "test",
|
|
|
|
mac: "1:2:3:4",
|
2022-05-06 08:25:18 +00:00
|
|
|
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 08:25:18 +00:00
|
|
|
iterations: 2,
|
2022-12-12 11:24:14 +00:00
|
|
|
salt: "nonempty",
|
2022-05-06 08:25:18 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
const checkPrivateKey = jest.fn().mockResolvedValue(false);
|
2023-02-03 08:39:25 +00:00
|
|
|
renderComponent({ checkPrivateKey, keyInfo });
|
2022-05-06 08:25:18 +00:00
|
|
|
|
2023-02-03 08:39:25 +00:00
|
|
|
await enterSecurityKey("Security Phrase");
|
|
|
|
expect(screen.getByPlaceholderText("Security Phrase")).toHaveValue(securityKey);
|
|
|
|
await submitDialog();
|
2022-05-06 08:25:18 +00:00
|
|
|
|
2024-08-06 17:22:02 +00:00
|
|
|
await expect(
|
|
|
|
screen.findByText(
|
2023-02-03 08:39:25 +00:00
|
|
|
"👎 Unable to access secret storage. Please verify that you entered the correct Security Phrase.",
|
|
|
|
),
|
2024-08-06 17:22:02 +00:00
|
|
|
).resolves.toBeInTheDocument();
|
2023-03-08 11:32:50 +00:00
|
|
|
|
|
|
|
expect(screen.getByPlaceholderText("Security Phrase")).toHaveFocus();
|
2022-05-06 08:25:18 +00:00
|
|
|
});
|
|
|
|
});
|