2022-05-06 08:25:18 +00: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.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import React from "react";
|
2022-08-02 13:10:43 +00:00
|
|
|
// eslint-disable-next-line deprecate/import
|
2022-12-12 11:24:14 +00:00
|
|
|
import { mount, ReactWrapper } from "enzyme";
|
|
|
|
import { act } from "react-dom/test-utils";
|
|
|
|
import { IPassphraseInfo } from "matrix-js-sdk/src/crypto/api";
|
2022-05-06 08:25:18 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import { findByAttr, getMockClientWithEventEmitter, unmockClientPeg } from "../../../test-utils";
|
|
|
|
import { findById, flushPromises } from "../../../test-utils";
|
2022-05-06 08:25:18 +00:00
|
|
|
import AccessSecretStorageDialog from "../../../../src/components/views/dialogs/security/AccessSecretStorageDialog";
|
|
|
|
|
|
|
|
describe("AccessSecretStorageDialog", () => {
|
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
keyBackupKeyFromRecoveryKey: jest.fn(),
|
|
|
|
checkSecretStorageKey: jest.fn(),
|
|
|
|
isValidRecoveryKey: jest.fn(),
|
|
|
|
});
|
|
|
|
const defaultProps = {
|
|
|
|
onFinished: jest.fn(),
|
|
|
|
checkPrivateKey: jest.fn(),
|
|
|
|
keyInfo: undefined,
|
|
|
|
};
|
2022-12-12 11:24:14 +00:00
|
|
|
const getComponent = (props = {}): ReactWrapper =>
|
2022-05-06 08:25:18 +00:00
|
|
|
mount(<AccessSecretStorageDialog {...defaultProps} {...props} />);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
2022-12-12 11:24:14 +00:00
|
|
|
mockClient.keyBackupKeyFromRecoveryKey.mockReturnValue("a raw key" as unknown as Uint8Array);
|
2022-05-06 08:25:18 +00:00
|
|
|
mockClient.isValidRecoveryKey.mockReturnValue(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
unmockClientPeg();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Closes the dialog when the form is submitted with a valid key", async () => {
|
|
|
|
const onFinished = jest.fn();
|
|
|
|
const checkPrivateKey = jest.fn().mockResolvedValue(true);
|
|
|
|
const wrapper = getComponent({ onFinished, checkPrivateKey });
|
|
|
|
|
|
|
|
// force into valid state
|
|
|
|
act(() => {
|
|
|
|
wrapper.setState({
|
|
|
|
recoveryKeyValid: true,
|
|
|
|
recoveryKey: "a",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const e = { preventDefault: () => {} };
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
wrapper.find("form").simulate("submit", e);
|
2022-05-06 08:25:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
expect(checkPrivateKey).toHaveBeenCalledWith({ recoveryKey: "a" });
|
|
|
|
expect(onFinished).toHaveBeenCalledWith({ recoveryKey: "a" });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Considers a valid key to be valid", async () => {
|
|
|
|
const checkPrivateKey = jest.fn().mockResolvedValue(true);
|
|
|
|
const wrapper = getComponent({ checkPrivateKey });
|
2022-12-12 11:24:14 +00:00
|
|
|
mockClient.keyBackupKeyFromRecoveryKey.mockReturnValue("a raw key" as unknown as Uint8Array);
|
2022-05-06 08:25:18 +00:00
|
|
|
mockClient.checkSecretStorageKey.mockResolvedValue(true);
|
|
|
|
|
|
|
|
const v = "asdf";
|
|
|
|
const e = { target: { value: v } };
|
|
|
|
act(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
findById(wrapper, "mx_securityKey").find("input").simulate("change", e);
|
2022-05-06 08:25:18 +00:00
|
|
|
wrapper.setProps({});
|
|
|
|
});
|
|
|
|
await act(async () => {
|
|
|
|
// force a validation now because it debounces
|
|
|
|
// @ts-ignore
|
|
|
|
await wrapper.instance().validateRecoveryKey();
|
|
|
|
wrapper.setProps({});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const submitButton = findByAttr("data-testid")(wrapper, "dialog-primary-button").at(0);
|
2022-05-06 08:25:18 +00:00
|
|
|
// submit button is enabled when key is valid
|
|
|
|
expect(submitButton.props().disabled).toBeFalsy();
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(wrapper.find(".mx_AccessSecretStorageDialog_recoveryKeyFeedback").text()).toEqual("Looks good!");
|
2022-05-06 08:25:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Notifies the user if they input an invalid Security Key", async () => {
|
|
|
|
const checkPrivateKey = jest.fn().mockResolvedValue(false);
|
|
|
|
const wrapper = getComponent({ checkPrivateKey });
|
|
|
|
const e = { target: { value: "a" } };
|
|
|
|
mockClient.keyBackupKeyFromRecoveryKey.mockImplementation(() => {
|
|
|
|
throw new Error("that's no key");
|
|
|
|
});
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
findById(wrapper, "mx_securityKey").find("input").simulate("change", e);
|
2022-05-06 08:25:18 +00:00
|
|
|
});
|
|
|
|
// force a validation now because it debounces
|
|
|
|
// @ts-ignore private
|
|
|
|
await wrapper.instance().validateRecoveryKey();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const submitButton = findByAttr("data-testid")(wrapper, "dialog-primary-button").at(0);
|
2022-05-06 08:25:18 +00:00
|
|
|
// submit button is disabled when recovery key is invalid
|
|
|
|
expect(submitButton.props().disabled).toBeTruthy();
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(wrapper.find(".mx_AccessSecretStorageDialog_recoveryKeyFeedback").text()).toEqual(
|
|
|
|
"Invalid Security Key",
|
|
|
|
);
|
2022-05-06 08:25:18 +00:00
|
|
|
|
|
|
|
wrapper.setProps({});
|
|
|
|
const notification = wrapper.find(".mx_AccessSecretStorageDialog_recoveryKeyFeedback");
|
|
|
|
expect(notification.props().children).toEqual("Invalid Security Key");
|
|
|
|
});
|
|
|
|
|
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
|
2022-12-12 11:24:14 +00:00
|
|
|
algorithm: "m.pbkdf2" as IPassphraseInfo["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);
|
|
|
|
const wrapper = getComponent({ checkPrivateKey, keyInfo });
|
|
|
|
mockClient.isValidRecoveryKey.mockReturnValue(false);
|
|
|
|
|
|
|
|
// update passphrase
|
|
|
|
act(() => {
|
|
|
|
const e = { target: { value: "a" } };
|
2022-12-12 11:24:14 +00:00
|
|
|
findById(wrapper, "mx_passPhraseInput").at(1).simulate("change", e);
|
2022-05-06 08:25:18 +00:00
|
|
|
});
|
|
|
|
wrapper.setProps({});
|
|
|
|
|
|
|
|
// input updated
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(findById(wrapper, "mx_passPhraseInput").at(0).props().value).toEqual("a");
|
2022-05-06 08:25:18 +00:00
|
|
|
|
|
|
|
// submit the form
|
|
|
|
act(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
wrapper.find("form").at(0).simulate("submit");
|
2022-05-06 08:25:18 +00:00
|
|
|
});
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
wrapper.setProps({});
|
|
|
|
const notification = wrapper.find(".mx_AccessSecretStorageDialog_keyStatus");
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(notification.props().children).toEqual([
|
|
|
|
"\uD83D\uDC4E ",
|
|
|
|
"Unable to access secret storage. Please verify that you " + "entered the correct Security Phrase.",
|
|
|
|
]);
|
2022-05-06 08:25:18 +00:00
|
|
|
});
|
|
|
|
});
|