|
| 1 | +/* |
| 2 | +Copyright 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 { render } from '@testing-library/react'; |
| 19 | +import { act } from 'react-dom/test-utils'; |
| 20 | +import { DeviceInfo } from 'matrix-js-sdk/src/crypto/deviceinfo'; |
| 21 | +import { logger } from 'matrix-js-sdk/src/logger'; |
| 22 | +import { DeviceTrustLevel } from 'matrix-js-sdk/src/crypto/CrossSigning'; |
| 23 | + |
| 24 | +import SessionManagerTab from '../../../../../../src/components/views/settings/tabs/user/SessionManagerTab'; |
| 25 | +import MatrixClientContext from '../../../../../../src/contexts/MatrixClientContext'; |
| 26 | +import { |
| 27 | + flushPromisesWithFakeTimers, |
| 28 | + getMockClientWithEventEmitter, |
| 29 | + mockClientMethodsUser, |
| 30 | +} from '../../../../../test-utils'; |
| 31 | + |
| 32 | +jest.useFakeTimers(); |
| 33 | + |
| 34 | +describe('<SessionManagerTab />', () => { |
| 35 | + const aliceId = '@alice:server.org'; |
| 36 | + const deviceId = 'alices_device'; |
| 37 | + |
| 38 | + const alicesDevice = { |
| 39 | + device_id: deviceId, |
| 40 | + }; |
| 41 | + const alicesMobileDevice = { |
| 42 | + device_id: 'alices_mobile_device', |
| 43 | + }; |
| 44 | + |
| 45 | + const mockCrossSigningInfo = { |
| 46 | + checkDeviceTrust: jest.fn(), |
| 47 | + }; |
| 48 | + const mockClient = getMockClientWithEventEmitter({ |
| 49 | + ...mockClientMethodsUser(aliceId), |
| 50 | + getStoredCrossSigningForUser: jest.fn().mockReturnValue(mockCrossSigningInfo), |
| 51 | + getDevices: jest.fn(), |
| 52 | + getStoredDevice: jest.fn(), |
| 53 | + getDeviceId: jest.fn().mockReturnValue(deviceId), |
| 54 | + }); |
| 55 | + |
| 56 | + const defaultProps = {}; |
| 57 | + const getComponent = (props = {}): React.ReactElement => |
| 58 | + ( |
| 59 | + <MatrixClientContext.Provider value={mockClient}> |
| 60 | + <SessionManagerTab {...defaultProps} {...props} /> |
| 61 | + </MatrixClientContext.Provider> |
| 62 | + ); |
| 63 | + |
| 64 | + beforeEach(() => { |
| 65 | + jest.clearAllMocks(); |
| 66 | + jest.spyOn(logger, 'error').mockRestore(); |
| 67 | + mockClient.getDevices.mockResolvedValue({ devices: [] }); |
| 68 | + mockClient.getStoredDevice.mockImplementation((_userId, id) => { |
| 69 | + const device = [alicesDevice, alicesMobileDevice].find(device => device.device_id === id); |
| 70 | + return device ? new DeviceInfo(device.device_id) : null; |
| 71 | + }); |
| 72 | + mockCrossSigningInfo.checkDeviceTrust |
| 73 | + .mockReset() |
| 74 | + .mockReturnValue(new DeviceTrustLevel(false, false, false, false)); |
| 75 | + }); |
| 76 | + |
| 77 | + it('renders spinner while devices load', () => { |
| 78 | + const { container } = render(getComponent()); |
| 79 | + expect(container.getElementsByClassName('mx_Spinner').length).toBeTruthy(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('removes spinner when device fetch fails', async () => { |
| 83 | + mockClient.getDevices.mockRejectedValue({ httpStatus: 404 }); |
| 84 | + const { container } = render(getComponent()); |
| 85 | + expect(mockClient.getDevices).toHaveBeenCalled(); |
| 86 | + |
| 87 | + await act(async () => { |
| 88 | + await flushPromisesWithFakeTimers(); |
| 89 | + }); |
| 90 | + expect(container.getElementsByClassName('mx_Spinner').length).toBeFalsy(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('removes spinner when device fetch fails', async () => { |
| 94 | + // eat the expected error log |
| 95 | + jest.spyOn(logger, 'error').mockImplementation(() => {}); |
| 96 | + mockClient.getDevices.mockRejectedValue({ httpStatus: 404 }); |
| 97 | + const { container } = render(getComponent()); |
| 98 | + |
| 99 | + await act(async () => { |
| 100 | + await flushPromisesWithFakeTimers(); |
| 101 | + }); |
| 102 | + expect(container.getElementsByClassName('mx_Spinner').length).toBeFalsy(); |
| 103 | + |
| 104 | + // TODO when UI is done, check for error UI |
| 105 | + }); |
| 106 | + |
| 107 | + it('does not fail when checking device verification fails', async () => { |
| 108 | + const logSpy = jest.spyOn(logger, 'error').mockImplementation(() => {}); |
| 109 | + mockClient.getDevices.mockResolvedValue({ devices: [alicesDevice, alicesMobileDevice] }); |
| 110 | + const noCryptoError = new Error("End-to-end encryption disabled"); |
| 111 | + mockClient.getStoredDevice.mockImplementation(() => { throw noCryptoError; }); |
| 112 | + render(getComponent()); |
| 113 | + |
| 114 | + await act(async () => { |
| 115 | + await flushPromisesWithFakeTimers(); |
| 116 | + }); |
| 117 | + |
| 118 | + // called for each device despite error |
| 119 | + expect(mockClient.getStoredDevice).toHaveBeenCalledWith(aliceId, alicesDevice.device_id); |
| 120 | + expect(mockClient.getStoredDevice).toHaveBeenCalledWith(aliceId, alicesMobileDevice.device_id); |
| 121 | + expect(logSpy).toHaveBeenCalledWith('Error getting device cross-signing info', noCryptoError); |
| 122 | + // TODO when UI is done, check for error UI |
| 123 | + }); |
| 124 | + |
| 125 | + it('sets device verification status correctly', async () => { |
| 126 | + mockClient.getDevices.mockResolvedValue({ devices: [alicesDevice, alicesMobileDevice] }); |
| 127 | + mockCrossSigningInfo.checkDeviceTrust |
| 128 | + // alices device is trusted |
| 129 | + .mockReturnValueOnce(new DeviceTrustLevel(true, true, false, false)) |
| 130 | + // alices mobile device is not |
| 131 | + .mockReturnValueOnce(new DeviceTrustLevel(false, false, false, false)); |
| 132 | + |
| 133 | + const { getByTestId } = render(getComponent()); |
| 134 | + |
| 135 | + await act(async () => { |
| 136 | + await flushPromisesWithFakeTimers(); |
| 137 | + }); |
| 138 | + |
| 139 | + expect(mockCrossSigningInfo.checkDeviceTrust).toHaveBeenCalledTimes(2); |
| 140 | + expect(getByTestId(`device-tile-${alicesDevice.device_id}`)).toMatchSnapshot(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('renders current session section', async () => { |
| 144 | + mockClient.getDevices.mockResolvedValue({ devices: [alicesDevice, alicesMobileDevice] }); |
| 145 | + const noCryptoError = new Error("End-to-end encryption disabled"); |
| 146 | + mockClient.getStoredDevice.mockImplementation(() => { throw noCryptoError; }); |
| 147 | + const { getByTestId } = render(getComponent()); |
| 148 | + |
| 149 | + await act(async () => { |
| 150 | + await flushPromisesWithFakeTimers(); |
| 151 | + }); |
| 152 | + |
| 153 | + expect(getByTestId('current-session-section')).toMatchSnapshot(); |
| 154 | + }); |
| 155 | +}); |
0 commit comments