2022-05-06 10:25:18 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020, 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
import React, { ComponentProps } from "react";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { IPassphraseInfo } from "matrix-js-sdk/src/crypto/api";
|
2023-02-03 09:39:25 +01:00
|
|
|
import { act, fireEvent, render, screen } from "@testing-library/react";
|
|
|
|
import userEvent from "@testing-library/user-event";
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { Mocked } from "jest-mock";
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2023-02-03 09:39:25 +01:00
|
|
|
import { getMockClientWithEventEmitter, mockPlatformPeg } from "../../../test-utils";
|
2022-05-06 10:25:18 +02:00
|
|
|
import AccessSecretStorageDialog from "../../../../src/components/views/dialogs/security/AccessSecretStorageDialog";
|
|
|
|
|
2023-02-03 09:39:25 +01:00
|
|
|
const securityKey = "EsTc WKmb ivvk jLS7 Y1NH 5CcQ mP1E JJwj B3Fd pFWm t4Dp dbyu";
|
|
|
|
|
2022-05-06 10:25:18 +02:00
|
|
|
describe("AccessSecretStorageDialog", () => {
|
2023-02-03 09:39:25 +01:00
|
|
|
let mockClient: Mocked<MatrixClient>;
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
const defaultProps: ComponentProps<typeof AccessSecretStorageDialog> = {
|
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} />);
|
|
|
|
};
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2023-02-03 09:39:25 +01:00
|
|
|
const enterSecurityKey = (placeholder = "Security Key"): void => {
|
2022-05-06 10:25:18 +02:00
|
|
|
act(() => {
|
2023-02-03 09:39:25 +01:00
|
|
|
fireEvent.change(screen.getByPlaceholderText(placeholder), {
|
|
|
|
target: {
|
|
|
|
value: securityKey,
|
|
|
|
},
|
2022-05-06 10:25:18 +02:00
|
|
|
});
|
2023-02-03 09:39:25 +01:00
|
|
|
// wait for debounce
|
|
|
|
jest.advanceTimersByTime(250);
|
2022-05-06 10:25:18 +02:00
|
|
|
});
|
2023-02-03 09:39:25 +01:00
|
|
|
};
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2023-02-03 09:39:25 +01:00
|
|
|
const submitDialog = async (): Promise<void> => {
|
|
|
|
await userEvent.click(screen.getByText("Continue"), { delay: null });
|
|
|
|
};
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2023-02-03 09:39:25 +01:00
|
|
|
beforeAll(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
mockPlatformPeg();
|
|
|
|
});
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2023-02-03 09:39:25 +01:00
|
|
|
afterAll(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
jest.restoreAllMocks();
|
2022-05-06 10:25:18 +02:00
|
|
|
});
|
|
|
|
|
2023-02-03 09:39:25 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
mockClient = getMockClientWithEventEmitter({
|
|
|
|
keyBackupKeyFromRecoveryKey: jest.fn(),
|
|
|
|
checkSecretStorageKey: jest.fn(),
|
|
|
|
isValidRecoveryKey: jest.fn(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Closes the dialog when the form is submitted with a valid key", async () => {
|
2022-05-06 10:25:18 +02:00
|
|
|
mockClient.checkSecretStorageKey.mockResolvedValue(true);
|
2023-02-03 09:39:25 +01:00
|
|
|
mockClient.isValidRecoveryKey.mockReturnValue(true);
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2023-02-03 09:39:25 +01: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 10:25:18 +02:00
|
|
|
|
2023-02-03 09:39:25 +01:00
|
|
|
await enterSecurityKey();
|
|
|
|
await submitDialog();
|
|
|
|
|
|
|
|
expect(screen.getByText("Looks good!")).toBeInTheDocument();
|
|
|
|
expect(checkPrivateKey).toHaveBeenCalledWith({ recoveryKey: securityKey });
|
|
|
|
expect(onFinished).toHaveBeenCalledWith({ recoveryKey: securityKey });
|
2022-05-06 10:25:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Notifies the user if they input an invalid Security Key", async () => {
|
2023-02-03 09:39:25 +01:00
|
|
|
const onFinished = jest.fn();
|
|
|
|
const checkPrivateKey = jest.fn().mockResolvedValue(true);
|
|
|
|
renderComponent({ onFinished, checkPrivateKey });
|
|
|
|
|
2022-05-06 10:25:18 +02:00
|
|
|
mockClient.keyBackupKeyFromRecoveryKey.mockImplementation(() => {
|
|
|
|
throw new Error("that's no key");
|
|
|
|
});
|
|
|
|
|
2023-02-03 09:39:25 +01:00
|
|
|
await enterSecurityKey();
|
|
|
|
await submitDialog();
|
|
|
|
|
|
|
|
expect(screen.getByText("Continue")).toBeDisabled();
|
|
|
|
expect(screen.getByText("Invalid Security Key")).toBeInTheDocument();
|
2022-05-06 10:25:18 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("Notifies the user if they input an invalid passphrase", async function () {
|
2022-05-06 10:25:18 +02:00
|
|
|
const keyInfo = {
|
2022-12-12 12:24:14 +01:00
|
|
|
name: "test",
|
|
|
|
algorithm: "test",
|
|
|
|
iv: "test",
|
|
|
|
mac: "1:2:3:4",
|
2022-05-06 10:25:18 +02:00
|
|
|
passphrase: {
|
|
|
|
// this type is weird in js-sdk
|
|
|
|
// cast 'm.pbkdf2' to itself
|
2022-12-12 12:24:14 +01:00
|
|
|
algorithm: "m.pbkdf2" as IPassphraseInfo["algorithm"],
|
2022-05-06 10:25:18 +02:00
|
|
|
iterations: 2,
|
2022-12-12 12:24:14 +01:00
|
|
|
salt: "nonempty",
|
2022-05-06 10:25:18 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
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
|
|
|
mockClient.isValidRecoveryKey.mockReturnValue(false);
|
|
|
|
|
2023-02-03 09:39:25 +01:00
|
|
|
await enterSecurityKey("Security Phrase");
|
|
|
|
expect(screen.getByPlaceholderText("Security Phrase")).toHaveValue(securityKey);
|
|
|
|
await submitDialog();
|
2022-05-06 10:25:18 +02:00
|
|
|
|
2023-02-03 09:39:25 +01:00
|
|
|
expect(
|
|
|
|
screen.getByText(
|
|
|
|
"👎 Unable to access secret storage. Please verify that you entered the correct Security Phrase.",
|
|
|
|
),
|
|
|
|
).toBeInTheDocument();
|
2022-05-06 10:25:18 +02:00
|
|
|
});
|
|
|
|
});
|