2023-06-07 17:43:44 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-06-07 17:43:44 +03:00
|
|
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 16:57:16 +03:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2023-06-07 17:43:44 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React, { ComponentProps } from "react";
|
2023-06-28 15:39:34 +03:00
|
|
|
import { mocked, Mocked } from "jest-mock";
|
2024-11-20 16:29:23 +03:00
|
|
|
import { render, RenderResult } from "jest-matrix-react";
|
2023-08-03 15:56:30 +03:00
|
|
|
import { TypedEventEmitter, IMyDevice, MatrixClient, Device } from "matrix-js-sdk/src/matrix";
|
2024-03-25 20:44:45 +03:00
|
|
|
import { VerificationRequest, VerificationRequestEvent } from "matrix-js-sdk/src/crypto-api";
|
2023-06-07 17:43:44 +03:00
|
|
|
|
2024-10-15 16:57:26 +03:00
|
|
|
import VerificationRequestToast from "../../../../../src/components/views/toasts/VerificationRequestToast";
|
2023-06-28 15:39:34 +03:00
|
|
|
import {
|
|
|
|
flushPromises,
|
|
|
|
getMockClientWithEventEmitter,
|
|
|
|
mockClientMethodsCrypto,
|
|
|
|
mockClientMethodsUser,
|
2024-10-15 16:57:26 +03:00
|
|
|
} from "../../../../test-utils";
|
|
|
|
import ToastStore from "../../../../../src/stores/ToastStore";
|
2023-06-07 17:43:44 +03:00
|
|
|
|
|
|
|
function renderComponent(
|
|
|
|
props: Partial<ComponentProps<typeof VerificationRequestToast>> & { request: VerificationRequest },
|
|
|
|
): RenderResult {
|
|
|
|
const propsWithDefaults = {
|
|
|
|
toastKey: "test",
|
|
|
|
...props,
|
|
|
|
};
|
|
|
|
|
|
|
|
return render(<VerificationRequestToast {...propsWithDefaults} />);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("VerificationRequestToast", () => {
|
|
|
|
let client: Mocked<MatrixClient>;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = getMockClientWithEventEmitter({
|
|
|
|
...mockClientMethodsUser(),
|
2023-06-28 15:39:34 +03:00
|
|
|
...mockClientMethodsCrypto(),
|
2023-06-07 17:43:44 +03:00
|
|
|
getDevice: jest.fn(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render a self-verification", async () => {
|
|
|
|
const otherDeviceId = "other_device";
|
|
|
|
const otherIDevice: IMyDevice = { device_id: otherDeviceId, last_seen_ip: "1.1.1.1" };
|
|
|
|
client.getDevice.mockResolvedValue(otherIDevice);
|
|
|
|
|
2023-06-28 15:39:34 +03:00
|
|
|
const otherDeviceInfo = new Device({
|
|
|
|
algorithms: [],
|
|
|
|
keys: new Map(),
|
|
|
|
userId: "",
|
|
|
|
deviceId: otherDeviceId,
|
|
|
|
displayName: "my other device",
|
|
|
|
});
|
|
|
|
const deviceMap = new Map([[client.getSafeUserId(), new Map([[otherDeviceId, otherDeviceInfo]])]]);
|
|
|
|
mocked(client.getCrypto()!.getUserDeviceInfo).mockResolvedValue(deviceMap);
|
2023-06-07 17:43:44 +03:00
|
|
|
|
|
|
|
const request = makeMockVerificationRequest({
|
|
|
|
isSelfVerification: true,
|
|
|
|
otherDeviceId,
|
|
|
|
});
|
|
|
|
const result = renderComponent({ request });
|
2024-11-20 16:29:23 +03:00
|
|
|
await flushPromises();
|
2023-06-07 17:43:44 +03:00
|
|
|
expect(result.container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render a cross-user verification", async () => {
|
|
|
|
const otherUserId = "@other:user";
|
|
|
|
const request = makeMockVerificationRequest({
|
|
|
|
isSelfVerification: false,
|
|
|
|
otherUserId,
|
|
|
|
});
|
|
|
|
const result = renderComponent({ request });
|
2024-11-20 16:29:23 +03:00
|
|
|
await flushPromises();
|
2023-06-07 17:43:44 +03:00
|
|
|
expect(result.container).toMatchSnapshot();
|
|
|
|
});
|
2023-06-14 17:35:32 +03:00
|
|
|
|
|
|
|
it("dismisses itself once the request can no longer be accepted", async () => {
|
|
|
|
const otherUserId = "@other:user";
|
|
|
|
const request = makeMockVerificationRequest({
|
|
|
|
isSelfVerification: false,
|
|
|
|
otherUserId,
|
|
|
|
});
|
|
|
|
renderComponent({ request, toastKey: "testKey" });
|
2024-11-20 16:29:23 +03:00
|
|
|
await flushPromises();
|
2023-06-14 17:35:32 +03:00
|
|
|
|
|
|
|
const dismiss = jest.spyOn(ToastStore.sharedInstance(), "dismissToast");
|
|
|
|
Object.defineProperty(request, "accepting", { value: true });
|
|
|
|
request.emit(VerificationRequestEvent.Change);
|
|
|
|
expect(dismiss).toHaveBeenCalledWith("testKey");
|
|
|
|
});
|
2023-06-07 17:43:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
function makeMockVerificationRequest(props: Partial<VerificationRequest> = {}): Mocked<VerificationRequest> {
|
|
|
|
const request = new TypedEventEmitter<VerificationRequestEvent, any>();
|
|
|
|
Object.assign(request, {
|
|
|
|
...props,
|
|
|
|
});
|
|
|
|
return request as unknown as Mocked<VerificationRequest>;
|
|
|
|
}
|