2023-05-26 17:29:32 +08:00
|
|
|
/*
|
|
|
|
Copyright 2023 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.
|
|
|
|
*/
|
|
|
|
|
2023-06-07 22:43:44 +08:00
|
|
|
import { act, render, waitFor } from "@testing-library/react";
|
|
|
|
import React, { ComponentProps } from "react";
|
2023-08-08 15:16:04 +08:00
|
|
|
import { User, TypedEventEmitter, Device, MatrixClient } from "matrix-js-sdk/src/matrix";
|
2023-06-23 20:38:06 +08:00
|
|
|
import { mocked, Mocked } from "jest-mock";
|
2023-05-26 17:29:32 +08:00
|
|
|
import {
|
|
|
|
EmojiMapping,
|
|
|
|
ShowSasCallbacks,
|
2023-06-14 22:35:32 +08:00
|
|
|
VerificationPhase as Phase,
|
|
|
|
VerificationRequest,
|
|
|
|
VerificationRequestEvent,
|
2023-06-23 20:38:06 +08:00
|
|
|
Verifier,
|
|
|
|
VerifierEvent,
|
|
|
|
VerifierEventHandlerMap,
|
2024-03-22 20:28:13 +08:00
|
|
|
} from "matrix-js-sdk/src/crypto-api";
|
2023-05-26 17:29:32 +08:00
|
|
|
|
|
|
|
import VerificationPanel from "../../../../src/components/views/right_panel/VerificationPanel";
|
2023-06-23 20:38:06 +08:00
|
|
|
import { flushPromises, stubClient } from "../../../test-utils";
|
2023-05-26 17:29:32 +08:00
|
|
|
|
|
|
|
describe("<VerificationPanel />", () => {
|
2023-06-23 20:38:06 +08:00
|
|
|
let client: MatrixClient;
|
|
|
|
|
2023-05-26 17:29:32 +08:00
|
|
|
beforeEach(() => {
|
2023-06-23 20:38:06 +08:00
|
|
|
client = stubClient();
|
2023-05-26 17:29:32 +08:00
|
|
|
});
|
|
|
|
|
2023-06-07 22:43:44 +08:00
|
|
|
describe("'Ready' phase (dialog mode)", () => {
|
|
|
|
it("should show a 'Start' button", () => {
|
|
|
|
const container = renderComponent({
|
|
|
|
request: makeMockVerificationRequest({
|
|
|
|
phase: Phase.Ready,
|
|
|
|
}),
|
|
|
|
layout: "dialog",
|
|
|
|
});
|
|
|
|
container.getByRole("button", { name: "Start" });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show a QR code if the other side can scan and QR bytes are calculated", async () => {
|
|
|
|
const request = makeMockVerificationRequest({
|
|
|
|
phase: Phase.Ready,
|
|
|
|
});
|
2023-07-13 21:55:55 +08:00
|
|
|
request.generateQRCode.mockResolvedValue(Buffer.from("test", "utf-8"));
|
2023-06-07 22:43:44 +08:00
|
|
|
const container = renderComponent({
|
|
|
|
request: request,
|
|
|
|
layout: "dialog",
|
|
|
|
});
|
|
|
|
container.getByText("Scan this unique code");
|
|
|
|
// it shows a spinner at first; wait for the update which makes it show the QR code
|
|
|
|
await waitFor(() => {
|
|
|
|
container.getByAltText("QR Code");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("'Ready' phase (regular mode)", () => {
|
|
|
|
it("should show a 'Verify by emoji' button", () => {
|
|
|
|
const container = renderComponent({
|
|
|
|
request: makeMockVerificationRequest({ phase: Phase.Ready }),
|
|
|
|
});
|
|
|
|
container.getByRole("button", { name: "Verify by emoji" });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show a QR code if the other side can scan and QR bytes are calculated", async () => {
|
|
|
|
const request = makeMockVerificationRequest({
|
|
|
|
phase: Phase.Ready,
|
|
|
|
});
|
2023-07-13 21:55:55 +08:00
|
|
|
request.generateQRCode.mockResolvedValue(Buffer.from("test", "utf-8"));
|
2023-06-07 22:43:44 +08:00
|
|
|
const container = renderComponent({
|
|
|
|
request: request,
|
|
|
|
member: new User("@other:user"),
|
|
|
|
});
|
|
|
|
container.getByText("Ask @other:user to scan your code:");
|
|
|
|
// it shows a spinner at first; wait for the update which makes it show the QR code
|
|
|
|
await waitFor(() => {
|
|
|
|
container.getByAltText("QR Code");
|
|
|
|
});
|
2023-05-26 17:29:32 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("'Verify by emoji' flow", () => {
|
2023-06-09 23:49:44 +08:00
|
|
|
let mockVerifier: Mocked<Verifier>;
|
2023-05-26 17:29:32 +08:00
|
|
|
let mockRequest: Mocked<VerificationRequest>;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockVerifier = makeMockVerifier();
|
|
|
|
mockRequest = makeMockVerificationRequest({
|
2023-06-09 23:49:44 +08:00
|
|
|
verifier: mockVerifier as unknown as VerificationRequest["verifier"],
|
2023-05-26 17:29:32 +08:00
|
|
|
chosenMethod: "m.sas.v1",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shows a spinner initially", () => {
|
|
|
|
const { container } = renderComponent({
|
|
|
|
request: mockRequest,
|
|
|
|
phase: Phase.Started,
|
|
|
|
});
|
|
|
|
expect(container.getElementsByClassName("mx_Spinner").length).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show some emojis once keys are exchanged", () => {
|
|
|
|
const { container } = renderComponent({
|
|
|
|
request: mockRequest,
|
|
|
|
phase: Phase.Started,
|
|
|
|
});
|
|
|
|
|
|
|
|
// fire the ShowSas event
|
|
|
|
const sasEvent = makeMockSasCallbacks();
|
2023-06-07 21:05:23 +08:00
|
|
|
mockVerifier.getShowSasCallbacks.mockReturnValue(sasEvent);
|
2023-05-26 17:29:32 +08:00
|
|
|
act(() => {
|
|
|
|
mockVerifier.emit(VerifierEvent.ShowSas, sasEvent);
|
|
|
|
});
|
|
|
|
|
|
|
|
const emojis = container.getElementsByClassName("mx_VerificationShowSas_emojiSas_block");
|
|
|
|
expect(emojis.length).toEqual(7);
|
|
|
|
for (const emoji of emojis) {
|
|
|
|
expect(emoji).toHaveTextContent("🦄Unicorn");
|
|
|
|
}
|
|
|
|
});
|
2023-06-23 20:38:06 +08:00
|
|
|
|
|
|
|
describe("'Verify own device' flow", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
Object.defineProperty(mockRequest, "isSelfVerification", { get: () => true });
|
|
|
|
Object.defineProperty(mockRequest, "otherDeviceId", { get: () => "other_device" });
|
|
|
|
|
|
|
|
const otherDeviceDetails = new Device({
|
|
|
|
algorithms: [],
|
|
|
|
deviceId: "other_device",
|
|
|
|
keys: new Map(),
|
|
|
|
userId: "",
|
|
|
|
displayName: "my other device",
|
|
|
|
});
|
|
|
|
|
|
|
|
mocked(client.getCrypto()!).getUserDeviceInfo.mockResolvedValue(
|
|
|
|
new Map([[client.getSafeUserId(), new Map([["other_device", otherDeviceDetails]])]]),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show 'Waiting for you to verify' after confirming", async () => {
|
|
|
|
const rendered = renderComponent({
|
|
|
|
request: mockRequest,
|
|
|
|
phase: Phase.Started,
|
|
|
|
});
|
|
|
|
|
|
|
|
// wait for the device to be looked up
|
|
|
|
await act(() => flushPromises());
|
|
|
|
|
|
|
|
// fire the ShowSas event
|
|
|
|
const sasEvent = makeMockSasCallbacks();
|
|
|
|
mockVerifier.getShowSasCallbacks.mockReturnValue(sasEvent);
|
|
|
|
act(() => {
|
|
|
|
mockVerifier.emit(VerifierEvent.ShowSas, sasEvent);
|
|
|
|
});
|
|
|
|
|
|
|
|
// confirm
|
|
|
|
act(() => {
|
|
|
|
rendered.getByRole("button", { name: "They match" }).click();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(rendered.container).toHaveTextContent(
|
|
|
|
"Waiting for you to verify on your other device, my other device (other_device)…",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2023-05-26 17:29:32 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-06-07 22:43:44 +08:00
|
|
|
function renderComponent(props: Partial<ComponentProps<typeof VerificationPanel>> & { request: VerificationRequest }) {
|
2023-05-26 17:29:32 +08:00
|
|
|
const defaultProps = {
|
|
|
|
layout: "",
|
|
|
|
member: {} as User,
|
2023-06-28 21:05:36 +08:00
|
|
|
onClose: () => {},
|
2023-05-26 17:29:32 +08:00
|
|
|
isRoomEncrypted: false,
|
|
|
|
inDialog: false,
|
2023-06-07 22:43:44 +08:00
|
|
|
phase: props.request.phase,
|
2023-05-26 17:29:32 +08:00
|
|
|
};
|
|
|
|
return render(<VerificationPanel {...defaultProps} {...props} />);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeMockVerificationRequest(props: Partial<VerificationRequest> = {}): Mocked<VerificationRequest> {
|
|
|
|
const request = new TypedEventEmitter<VerificationRequestEvent, any>();
|
|
|
|
Object.assign(request, {
|
|
|
|
cancel: jest.fn(),
|
|
|
|
otherPartySupportsMethod: jest.fn().mockReturnValue(true),
|
2023-07-13 21:55:55 +08:00
|
|
|
generateQRCode: jest.fn().mockResolvedValue(undefined),
|
2023-05-26 17:29:32 +08:00
|
|
|
...props,
|
|
|
|
});
|
|
|
|
return request as unknown as Mocked<VerificationRequest>;
|
|
|
|
}
|
|
|
|
|
2023-06-09 23:49:44 +08:00
|
|
|
function makeMockVerifier(): Mocked<Verifier> {
|
2023-05-26 17:29:32 +08:00
|
|
|
const verifier = new TypedEventEmitter<VerifierEvent, VerifierEventHandlerMap>();
|
|
|
|
Object.assign(verifier, {
|
|
|
|
cancel: jest.fn(),
|
|
|
|
verify: jest.fn(),
|
2023-06-07 21:05:23 +08:00
|
|
|
getShowSasCallbacks: jest.fn(),
|
|
|
|
getReciprocateQrCodeCallbacks: jest.fn(),
|
2023-05-26 17:29:32 +08:00
|
|
|
});
|
2023-06-09 23:49:44 +08:00
|
|
|
return verifier as unknown as Mocked<Verifier>;
|
2023-05-26 17:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeMockSasCallbacks(): ShowSasCallbacks {
|
|
|
|
const unicorn: EmojiMapping = ["🦄", "unicorn"];
|
|
|
|
return {
|
|
|
|
sas: {
|
|
|
|
emoji: new Array<EmojiMapping>(7).fill(unicorn),
|
|
|
|
},
|
|
|
|
cancel: jest.fn(),
|
|
|
|
confirm: jest.fn(),
|
|
|
|
mismatch: jest.fn(),
|
|
|
|
};
|
|
|
|
}
|