2022-08-10 16:26:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 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";
|
2023-02-22 10:52:55 +00:00
|
|
|
import { act, fireEvent, render } from "@testing-library/react";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { CrossSigningInfo } from "matrix-js-sdk/src/crypto/CrossSigning";
|
|
|
|
import { DeviceInfo } from "matrix-js-sdk/src/crypto/deviceinfo";
|
|
|
|
import { sleep } from "matrix-js-sdk/src/utils";
|
|
|
|
import { PUSHER_DEVICE_ID, PUSHER_ENABLED } from "matrix-js-sdk/src/@types/event";
|
2022-08-10 16:26:48 +00:00
|
|
|
|
|
|
|
import DevicesPanel from "../../../../src/components/views/settings/DevicesPanel";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { flushPromises, getMockClientWithEventEmitter, mkPusher, mockClientMethodsUser } from "../../../test-utils";
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
|
|
|
|
describe("<DevicesPanel />", () => {
|
|
|
|
const userId = "@alice:server.org";
|
|
|
|
const device1 = { device_id: "device_1" };
|
|
|
|
const device2 = { device_id: "device_2" };
|
|
|
|
const device3 = { device_id: "device_3" };
|
2022-08-10 16:26:48 +00:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
...mockClientMethodsUser(userId),
|
|
|
|
getDevices: jest.fn(),
|
|
|
|
getDeviceId: jest.fn().mockReturnValue(device1.device_id),
|
|
|
|
deleteMultipleDevices: jest.fn(),
|
|
|
|
getStoredCrossSigningForUser: jest.fn().mockReturnValue(new CrossSigningInfo(userId, {}, {})),
|
2022-12-12 11:24:14 +00:00
|
|
|
getStoredDevice: jest.fn().mockReturnValue(new DeviceInfo("id")),
|
2022-08-10 16:26:48 +00:00
|
|
|
generateClientSecret: jest.fn(),
|
2022-09-27 12:35:54 +00:00
|
|
|
getPushers: jest.fn(),
|
|
|
|
setPusher: jest.fn(),
|
2022-08-10 16:26:48 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const getComponent = () => (
|
2022-10-19 12:31:20 +00:00
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<DevicesPanel />
|
2022-12-12 11:24:14 +00:00
|
|
|
</MatrixClientContext.Provider>
|
|
|
|
);
|
2022-08-10 16:26:48 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
mockClient.getDevices.mockReset().mockResolvedValue({ devices: [device1, device2, device3] });
|
2022-09-27 12:35:54 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
mockClient.getPushers.mockReset().mockResolvedValue({
|
|
|
|
pushers: [
|
|
|
|
mkPusher({
|
2022-09-27 12:35:54 +00:00
|
|
|
[PUSHER_DEVICE_ID.name]: device1.device_id,
|
|
|
|
[PUSHER_ENABLED.name]: true,
|
2022-12-12 11:24:14 +00:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2022-08-10 16:26:48 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders device panel with devices", async () => {
|
2022-08-10 16:26:48 +00:00
|
|
|
const { container } = render(getComponent());
|
|
|
|
await flushPromises();
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("device deletion", () => {
|
2022-08-10 16:26:48 +00:00
|
|
|
const interactiveAuthError = { httpStatus: 401, data: { flows: [{ stages: ["m.login.password"] }] } };
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const toggleDeviceSelection = (container: HTMLElement, deviceId: string) =>
|
|
|
|
act(() => {
|
2023-02-16 17:21:44 +00:00
|
|
|
const checkbox = container.querySelector(`#device-tile-checkbox-${deviceId}`)!;
|
2022-12-12 11:24:14 +00:00
|
|
|
fireEvent.click(checkbox);
|
|
|
|
});
|
2022-08-10 16:26:48 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockClient.deleteMultipleDevices.mockReset();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("deletes selected devices when interactive auth is not required", async () => {
|
2022-08-10 16:26:48 +00:00
|
|
|
mockClient.deleteMultipleDevices.mockResolvedValue({});
|
|
|
|
mockClient.getDevices
|
|
|
|
.mockResolvedValueOnce({ devices: [device1, device2, device3] })
|
|
|
|
// pretend it was really deleted on refresh
|
|
|
|
.mockResolvedValueOnce({ devices: [device1, device3] });
|
|
|
|
|
|
|
|
const { container, getByTestId } = render(getComponent());
|
|
|
|
await flushPromises();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(container.getElementsByClassName("mx_DevicesPanel_device").length).toEqual(3);
|
2022-08-10 16:26:48 +00:00
|
|
|
|
|
|
|
toggleDeviceSelection(container, device2.device_id);
|
|
|
|
|
|
|
|
mockClient.getDevices.mockClear();
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
fireEvent.click(getByTestId("sign-out-devices-btn"));
|
2022-08-10 16:26:48 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(container.getElementsByClassName("mx_Spinner").length).toBeTruthy();
|
2022-08-10 16:26:48 +00:00
|
|
|
expect(mockClient.deleteMultipleDevices).toHaveBeenCalledWith([device2.device_id], undefined);
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
// devices refreshed
|
|
|
|
expect(mockClient.getDevices).toHaveBeenCalled();
|
|
|
|
// and rerendered
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(container.getElementsByClassName("mx_DevicesPanel_device").length).toEqual(2);
|
2022-08-10 16:26:48 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("deletes selected devices when interactive auth is required", async () => {
|
2022-08-10 16:26:48 +00:00
|
|
|
mockClient.deleteMultipleDevices
|
|
|
|
// require auth
|
|
|
|
.mockRejectedValueOnce(interactiveAuthError)
|
|
|
|
// then succeed
|
|
|
|
.mockResolvedValueOnce({});
|
|
|
|
|
|
|
|
mockClient.getDevices
|
|
|
|
.mockResolvedValueOnce({ devices: [device1, device2, device3] })
|
|
|
|
// pretend it was really deleted on refresh
|
|
|
|
.mockResolvedValueOnce({ devices: [device1, device3] });
|
|
|
|
|
|
|
|
const { container, getByTestId, getByLabelText } = render(getComponent());
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
// reset mock count after initial load
|
|
|
|
mockClient.getDevices.mockClear();
|
|
|
|
|
|
|
|
toggleDeviceSelection(container, device2.device_id);
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
fireEvent.click(getByTestId("sign-out-devices-btn"));
|
2022-08-10 16:26:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
// modal rendering has some weird sleeps
|
2022-08-16 14:05:10 +00:00
|
|
|
await sleep(100);
|
2022-08-10 16:26:48 +00:00
|
|
|
|
|
|
|
expect(mockClient.deleteMultipleDevices).toHaveBeenCalledWith([device2.device_id], undefined);
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const modal = document.getElementsByClassName("mx_Dialog");
|
2022-08-10 16:26:48 +00:00
|
|
|
expect(modal).toMatchSnapshot();
|
|
|
|
|
|
|
|
// fill password and submit for interactive auth
|
|
|
|
act(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
fireEvent.change(getByLabelText("Password"), { target: { value: "topsecret" } });
|
|
|
|
fireEvent.submit(getByLabelText("Password"));
|
2022-08-10 16:26:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
// called again with auth
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(mockClient.deleteMultipleDevices).toHaveBeenCalledWith([device2.device_id], {
|
|
|
|
identifier: {
|
|
|
|
type: "m.id.user",
|
|
|
|
user: userId,
|
|
|
|
},
|
|
|
|
password: "",
|
|
|
|
type: "m.login.password",
|
|
|
|
user: userId,
|
|
|
|
});
|
2022-08-10 16:26:48 +00:00
|
|
|
// devices refreshed
|
|
|
|
expect(mockClient.getDevices).toHaveBeenCalled();
|
|
|
|
// and rerendered
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(container.getElementsByClassName("mx_DevicesPanel_device").length).toEqual(2);
|
2022-08-10 16:26:48 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("clears loading state when interactive auth fail is cancelled", async () => {
|
2022-08-10 16:26:48 +00:00
|
|
|
mockClient.deleteMultipleDevices
|
|
|
|
// require auth
|
|
|
|
.mockRejectedValueOnce(interactiveAuthError)
|
|
|
|
// then succeed
|
|
|
|
.mockResolvedValueOnce({});
|
|
|
|
|
|
|
|
mockClient.getDevices
|
|
|
|
.mockResolvedValueOnce({ devices: [device1, device2, device3] })
|
|
|
|
// pretend it was really deleted on refresh
|
|
|
|
.mockResolvedValueOnce({ devices: [device1, device3] });
|
|
|
|
|
|
|
|
const { container, getByTestId } = render(getComponent());
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
// reset mock count after initial load
|
|
|
|
mockClient.getDevices.mockClear();
|
|
|
|
|
|
|
|
toggleDeviceSelection(container, device2.device_id);
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
fireEvent.click(getByTestId("sign-out-devices-btn"));
|
2022-08-10 16:26:48 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(container.getElementsByClassName("mx_Spinner").length).toBeTruthy();
|
2022-08-10 16:26:48 +00:00
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
// modal rendering has some weird sleeps
|
2022-10-17 09:16:04 +00:00
|
|
|
await sleep(20);
|
2022-08-10 16:26:48 +00:00
|
|
|
|
|
|
|
// close the modal without submission
|
|
|
|
act(() => {
|
2023-02-16 17:21:44 +00:00
|
|
|
const modalCloseButton = document.querySelector('[aria-label="Close dialog"]')!;
|
2022-08-10 16:26:48 +00:00
|
|
|
fireEvent.click(modalCloseButton);
|
|
|
|
});
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
// not refreshed
|
|
|
|
expect(mockClient.getDevices).not.toHaveBeenCalled();
|
|
|
|
// spinner removed
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(container.getElementsByClassName("mx_Spinner").length).toBeFalsy();
|
2022-08-10 16:26:48 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|