|
| 1 | +/* |
| 2 | +Copyright 2020, 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React from 'react'; |
| 18 | +import { mount, ReactWrapper } from 'enzyme'; |
| 19 | +import { act } from 'react-dom/test-utils'; |
| 20 | +import { IPassphraseInfo } from 'matrix-js-sdk/src/crypto/api'; |
| 21 | + |
| 22 | +import { findByTestId, getMockClientWithEventEmitter, unmockClientPeg } from '../../../test-utils'; |
| 23 | +import { findById, flushPromises } from '../../../test-utils'; |
| 24 | +import AccessSecretStorageDialog from "../../../../src/components/views/dialogs/security/AccessSecretStorageDialog"; |
| 25 | + |
| 26 | +describe("AccessSecretStorageDialog", () => { |
| 27 | + const mockClient = getMockClientWithEventEmitter({ |
| 28 | + keyBackupKeyFromRecoveryKey: jest.fn(), |
| 29 | + checkSecretStorageKey: jest.fn(), |
| 30 | + isValidRecoveryKey: jest.fn(), |
| 31 | + }); |
| 32 | + const defaultProps = { |
| 33 | + onFinished: jest.fn(), |
| 34 | + checkPrivateKey: jest.fn(), |
| 35 | + keyInfo: undefined, |
| 36 | + }; |
| 37 | + const getComponent = (props ={}): ReactWrapper => |
| 38 | + mount(<AccessSecretStorageDialog {...defaultProps} {...props} />); |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + jest.clearAllMocks(); |
| 42 | + mockClient.keyBackupKeyFromRecoveryKey.mockReturnValue('a raw key' as unknown as Uint8Array); |
| 43 | + mockClient.isValidRecoveryKey.mockReturnValue(false); |
| 44 | + }); |
| 45 | + |
| 46 | + afterAll(() => { |
| 47 | + unmockClientPeg(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("Closes the dialog when the form is submitted with a valid key", async () => { |
| 51 | + const onFinished = jest.fn(); |
| 52 | + const checkPrivateKey = jest.fn().mockResolvedValue(true); |
| 53 | + const wrapper = getComponent({ onFinished, checkPrivateKey }); |
| 54 | + |
| 55 | + // force into valid state |
| 56 | + act(() => { |
| 57 | + wrapper.setState({ |
| 58 | + recoveryKeyValid: true, |
| 59 | + recoveryKey: "a", |
| 60 | + }); |
| 61 | + }); |
| 62 | + const e = { preventDefault: () => {} }; |
| 63 | + |
| 64 | + act(() => { |
| 65 | + wrapper.find('form').simulate('submit', e); |
| 66 | + }); |
| 67 | + |
| 68 | + await flushPromises(); |
| 69 | + |
| 70 | + expect(checkPrivateKey).toHaveBeenCalledWith({ recoveryKey: "a" }); |
| 71 | + expect(onFinished).toHaveBeenCalledWith({ recoveryKey: "a" }); |
| 72 | + }); |
| 73 | + |
| 74 | + it("Considers a valid key to be valid", async () => { |
| 75 | + const checkPrivateKey = jest.fn().mockResolvedValue(true); |
| 76 | + const wrapper = getComponent({ checkPrivateKey }); |
| 77 | + mockClient.keyBackupKeyFromRecoveryKey.mockReturnValue('a raw key' as unknown as Uint8Array); |
| 78 | + mockClient.checkSecretStorageKey.mockResolvedValue(true); |
| 79 | + |
| 80 | + const v = "asdf"; |
| 81 | + const e = { target: { value: v } }; |
| 82 | + act(() => { |
| 83 | + findById(wrapper, 'mx_securityKey').find('input').simulate('change', e); |
| 84 | + wrapper.setProps({}); |
| 85 | + }); |
| 86 | + await act(async () => { |
| 87 | + // force a validation now because it debounces |
| 88 | + // @ts-ignore |
| 89 | + await wrapper.instance().validateRecoveryKey(); |
| 90 | + wrapper.setProps({}); |
| 91 | + }); |
| 92 | + |
| 93 | + const submitButton = findByTestId(wrapper, 'dialog-primary-button').at(0); |
| 94 | + // submit button is enabled when key is valid |
| 95 | + expect(submitButton.props().disabled).toBeFalsy(); |
| 96 | + expect(wrapper.find('.mx_AccessSecretStorageDialog_recoveryKeyFeedback').text()).toEqual('Looks good!'); |
| 97 | + }); |
| 98 | + |
| 99 | + it("Notifies the user if they input an invalid Security Key", async () => { |
| 100 | + const checkPrivateKey = jest.fn().mockResolvedValue(false); |
| 101 | + const wrapper = getComponent({ checkPrivateKey }); |
| 102 | + const e = { target: { value: "a" } }; |
| 103 | + mockClient.keyBackupKeyFromRecoveryKey.mockImplementation(() => { |
| 104 | + throw new Error("that's no key"); |
| 105 | + }); |
| 106 | + |
| 107 | + act(() => { |
| 108 | + findById(wrapper, 'mx_securityKey').find('input').simulate('change', e); |
| 109 | + }); |
| 110 | + // force a validation now because it debounces |
| 111 | + // @ts-ignore private |
| 112 | + await wrapper.instance().validateRecoveryKey(); |
| 113 | + |
| 114 | + const submitButton = findByTestId(wrapper, 'dialog-primary-button').at(0); |
| 115 | + // submit button is disabled when recovery key is invalid |
| 116 | + expect(submitButton.props().disabled).toBeTruthy(); |
| 117 | + expect( |
| 118 | + wrapper.find('.mx_AccessSecretStorageDialog_recoveryKeyFeedback').text(), |
| 119 | + ).toEqual('Invalid Security Key'); |
| 120 | + |
| 121 | + wrapper.setProps({}); |
| 122 | + const notification = wrapper.find(".mx_AccessSecretStorageDialog_recoveryKeyFeedback"); |
| 123 | + expect(notification.props().children).toEqual("Invalid Security Key"); |
| 124 | + }); |
| 125 | + |
| 126 | + it("Notifies the user if they input an invalid passphrase", async function() { |
| 127 | + const keyInfo = { |
| 128 | + name: 'test', |
| 129 | + algorithm: 'test', |
| 130 | + iv: 'test', |
| 131 | + mac: '1:2:3:4', |
| 132 | + passphrase: { |
| 133 | + // this type is weird in js-sdk |
| 134 | + // cast 'm.pbkdf2' to itself |
| 135 | + algorithm: 'm.pbkdf2' as IPassphraseInfo['algorithm'], |
| 136 | + iterations: 2, |
| 137 | + salt: 'nonempty', |
| 138 | + }, |
| 139 | + }; |
| 140 | + const checkPrivateKey = jest.fn().mockResolvedValue(false); |
| 141 | + const wrapper = getComponent({ checkPrivateKey, keyInfo }); |
| 142 | + mockClient.isValidRecoveryKey.mockReturnValue(false); |
| 143 | + |
| 144 | + // update passphrase |
| 145 | + act(() => { |
| 146 | + const e = { target: { value: "a" } }; |
| 147 | + findById(wrapper, 'mx_passPhraseInput').at(1).simulate('change', e); |
| 148 | + }); |
| 149 | + wrapper.setProps({}); |
| 150 | + |
| 151 | + // input updated |
| 152 | + expect(findById(wrapper, 'mx_passPhraseInput').at(0).props().value).toEqual('a'); |
| 153 | + |
| 154 | + // submit the form |
| 155 | + act(() => { |
| 156 | + wrapper.find('form').at(0).simulate('submit'); |
| 157 | + }); |
| 158 | + await flushPromises(); |
| 159 | + |
| 160 | + wrapper.setProps({}); |
| 161 | + const notification = wrapper.find(".mx_AccessSecretStorageDialog_keyStatus"); |
| 162 | + expect(notification.props().children).toEqual( |
| 163 | + ["\uD83D\uDC4E ", "Unable to access secret storage. Please verify that you " + |
| 164 | + "entered the correct Security Phrase."]); |
| 165 | + }); |
| 166 | +}); |
0 commit comments