2020-02-11 12:59:49 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
2022-02-21 18:09:01 +01:00
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import { act } from 'react-dom/test-utils';
|
2021-10-23 00:23:32 +02:00
|
|
|
|
2020-02-11 12:59:49 +01:00
|
|
|
import sdk from '../../../skinned-sdk';
|
2021-06-29 14:11:58 +02:00
|
|
|
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
|
2020-02-11 12:59:49 +01:00
|
|
|
import { stubClient } from '../../../test-utils';
|
2022-02-21 18:09:01 +01:00
|
|
|
import { findById, flushPromises } from '../../../utils/test-utils';
|
2020-02-11 12:59:49 +01:00
|
|
|
|
2020-09-10 14:56:07 +02:00
|
|
|
const AccessSecretStorageDialog = sdk.getComponent("dialogs.security.AccessSecretStorageDialog");
|
2020-02-11 12:59:49 +01:00
|
|
|
|
2020-02-11 13:06:40 +01:00
|
|
|
describe("AccessSecretStorageDialog", function() {
|
2022-02-21 18:09:01 +01:00
|
|
|
it("Closes the dialog if _onRecoveryKeyNext is called with a valid key", async () => {
|
|
|
|
const onFinished = jest.fn();
|
|
|
|
const checkPrivateKey = jest.fn().mockResolvedValue(true);
|
|
|
|
const wrapper = mount(
|
2020-02-11 12:59:49 +01:00
|
|
|
<AccessSecretStorageDialog
|
2022-02-21 18:09:01 +01:00
|
|
|
checkPrivateKey={checkPrivateKey}
|
|
|
|
onFinished={onFinished}
|
2020-02-11 13:06:40 +01:00
|
|
|
/>,
|
2020-02-11 12:59:49 +01:00
|
|
|
);
|
2022-02-21 18:09:01 +01:00
|
|
|
wrapper.setState({
|
2020-02-11 12:59:49 +01:00
|
|
|
recoveryKeyValid: true,
|
|
|
|
recoveryKey: "a",
|
|
|
|
});
|
2020-02-11 13:06:40 +01:00
|
|
|
const e = { preventDefault: () => {} };
|
2022-02-21 18:09:01 +01:00
|
|
|
|
|
|
|
wrapper.find('form').simulate('submit', e);
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
expect(checkPrivateKey).toHaveBeenCalledWith({ recoveryKey: "a" });
|
|
|
|
expect(onFinished).toHaveBeenCalledWith({ recoveryKey: "a" });
|
2020-02-11 12:59:49 +01:00
|
|
|
});
|
|
|
|
|
2020-06-26 21:25:38 +02:00
|
|
|
it("Considers a valid key to be valid", async function() {
|
2022-02-21 18:09:01 +01:00
|
|
|
const wrapper = mount(
|
2020-02-11 12:59:49 +01:00
|
|
|
<AccessSecretStorageDialog
|
2021-04-27 17:23:27 +02:00
|
|
|
checkPrivateKey={() => true}
|
2020-02-11 13:06:40 +01:00
|
|
|
/>,
|
2020-02-11 12:59:49 +01:00
|
|
|
);
|
|
|
|
stubClient();
|
2020-06-26 21:25:38 +02:00
|
|
|
MatrixClientPeg.get().keyBackupKeyFromRecoveryKey = () => 'a raw key';
|
|
|
|
MatrixClientPeg.get().checkSecretStorageKey = () => true;
|
2022-02-21 18:09:01 +01:00
|
|
|
|
|
|
|
const v = "asdf";
|
|
|
|
const e = { target: { value: v } };
|
|
|
|
act(() => {
|
|
|
|
findById(wrapper, 'mx_securityKey').find('input').simulate('change', e);
|
|
|
|
});
|
2020-06-26 21:25:38 +02:00
|
|
|
// force a validation now because it debounces
|
2022-02-21 18:09:01 +01:00
|
|
|
await wrapper.instance().validateRecoveryKey();
|
|
|
|
const { recoveryKeyValid } = wrapper.instance().state;
|
2020-02-11 12:59:49 +01:00
|
|
|
expect(recoveryKeyValid).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-02-02 13:02:17 +01:00
|
|
|
it("Notifies the user if they input an invalid Security Key", async function() {
|
2022-02-21 18:09:01 +01:00
|
|
|
const wrapper = mount(
|
2020-02-11 12:59:49 +01:00
|
|
|
<AccessSecretStorageDialog
|
2021-04-27 17:23:27 +02:00
|
|
|
checkPrivateKey={async () => false}
|
2020-02-11 13:06:40 +01:00
|
|
|
/>,
|
2020-02-11 12:59:49 +01:00
|
|
|
);
|
|
|
|
const e = { target: { value: "a" } };
|
|
|
|
stubClient();
|
2020-06-26 21:25:38 +02:00
|
|
|
MatrixClientPeg.get().keyBackupKeyFromRecoveryKey = () => {
|
|
|
|
throw new Error("that's no key");
|
|
|
|
};
|
2022-02-21 18:09:01 +01:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
findById(wrapper, 'mx_securityKey').find('input').simulate('change', e);
|
|
|
|
});
|
2020-06-26 21:25:38 +02:00
|
|
|
// force a validation now because it debounces
|
2022-02-21 18:09:01 +01:00
|
|
|
await wrapper.instance().validateRecoveryKey();
|
2020-06-26 21:25:38 +02:00
|
|
|
|
2022-02-21 18:09:01 +01:00
|
|
|
const { recoveryKeyValid, recoveryKeyCorrect } = wrapper.instance().state;
|
2020-06-26 21:25:38 +02:00
|
|
|
expect(recoveryKeyValid).toBe(false);
|
|
|
|
expect(recoveryKeyCorrect).toBe(false);
|
2022-02-21 18:09:01 +01:00
|
|
|
|
|
|
|
wrapper.setProps({});
|
|
|
|
const notification = wrapper.find(".mx_AccessSecretStorageDialog_recoveryKeyFeedback");
|
|
|
|
expect(notification.props().children).toEqual("Invalid Security Key");
|
2020-02-11 12:59:49 +01:00
|
|
|
});
|
|
|
|
|
2022-02-02 13:02:17 +01:00
|
|
|
it("Notifies the user if they input an invalid passphrase", async function() {
|
2022-02-21 18:09:01 +01:00
|
|
|
const wrapper = mount(
|
2020-02-11 12:59:49 +01:00
|
|
|
<AccessSecretStorageDialog
|
2021-04-27 17:23:27 +02:00
|
|
|
checkPrivateKey={() => false}
|
|
|
|
onFinished={() => {}}
|
|
|
|
keyInfo={{
|
|
|
|
passphrase: {
|
|
|
|
salt: 'nonempty',
|
|
|
|
iterations: 2,
|
|
|
|
},
|
|
|
|
}}
|
2020-02-11 13:06:40 +01:00
|
|
|
/>,
|
2020-02-11 12:59:49 +01:00
|
|
|
);
|
|
|
|
const e = { target: { value: "a" } };
|
|
|
|
stubClient();
|
|
|
|
MatrixClientPeg.get().isValidRecoveryKey = () => false;
|
2022-02-21 18:09:01 +01:00
|
|
|
wrapper.instance().onPassPhraseChange(e);
|
|
|
|
await wrapper.instance().onPassPhraseNext({ preventDefault: () => { } });
|
|
|
|
|
|
|
|
wrapper.setProps({});
|
|
|
|
const notification = wrapper.find(".mx_AccessSecretStorageDialog_keyStatus");
|
|
|
|
expect(notification.props().children).toEqual(
|
2020-02-11 12:59:49 +01:00
|
|
|
["\uD83D\uDC4E ", "Unable to access secret storage. Please verify that you " +
|
2022-02-02 13:02:17 +01:00
|
|
|
"entered the correct Security Phrase."]);
|
2020-02-11 12:59:49 +01:00
|
|
|
});
|
|
|
|
});
|