2022-04-28 11:44:04 +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 { render } from "@testing-library/react";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { EventEmitter } from "events";
|
|
|
|
import { MatrixEvent, EventType } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { CryptoEvent } from "matrix-js-sdk/src/crypto";
|
|
|
|
import { UserTrustLevel } from "matrix-js-sdk/src/crypto/CrossSigning";
|
|
|
|
import { VerificationRequest } from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest";
|
2022-04-28 11:44:04 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import MKeyVerificationConclusion from "../../../../src/components/views/messages/MKeyVerificationConclusion";
|
|
|
|
import { getMockClientWithEventEmitter } from "../../../test-utils";
|
2022-04-28 11:44:04 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const trustworthy = { isCrossSigningVerified: () => true } as unknown as UserTrustLevel;
|
|
|
|
const untrustworthy = { isCrossSigningVerified: () => false } as unknown as UserTrustLevel;
|
2022-04-28 11:44:04 +00:00
|
|
|
|
|
|
|
describe("MKeyVerificationConclusion", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
const userId = "@user:server";
|
2022-04-28 11:44:04 +00:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
getRoom: jest.fn(),
|
|
|
|
getUserId: jest.fn().mockReturnValue(userId),
|
|
|
|
checkUserTrust: jest.fn(),
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const getMockVerificationRequest = ({
|
|
|
|
pending,
|
|
|
|
cancelled,
|
|
|
|
done,
|
|
|
|
otherUserId,
|
|
|
|
}: {
|
|
|
|
pending?: boolean;
|
|
|
|
cancelled?: boolean;
|
|
|
|
done?: boolean;
|
|
|
|
otherUserId?: string;
|
|
|
|
}) => {
|
2022-04-28 11:44:04 +00:00
|
|
|
class MockVerificationRequest extends EventEmitter {
|
|
|
|
constructor(
|
2023-02-16 09:38:44 +00:00
|
|
|
public readonly pending?: boolean,
|
|
|
|
public readonly cancelled?: boolean,
|
|
|
|
public readonly done?: boolean,
|
|
|
|
public readonly otherUserId?: string,
|
2022-04-28 11:44:04 +00:00
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new MockVerificationRequest(pending, cancelled, done, otherUserId) as unknown as VerificationRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
mockClient.checkUserTrust.mockReturnValue(trustworthy);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockRestore();
|
2022-04-28 11:44:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't render if there's no verificationRequest", () => {
|
|
|
|
const event = new MatrixEvent({});
|
2023-02-22 10:52:55 +00:00
|
|
|
const { container } = render(<MKeyVerificationConclusion mxEvent={event} />);
|
|
|
|
expect(container).toBeEmpty();
|
2022-04-28 11:44:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't render if the verificationRequest is pending", () => {
|
|
|
|
const event = new MatrixEvent({});
|
|
|
|
event.verificationRequest = getMockVerificationRequest({ pending: true });
|
2023-02-22 10:52:55 +00:00
|
|
|
const { container } = render(<MKeyVerificationConclusion mxEvent={event} />);
|
|
|
|
expect(container).toBeEmpty();
|
2022-04-28 11:44:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't render if the event type is cancel but the request type isn't", () => {
|
|
|
|
const event = new MatrixEvent({ type: EventType.KeyVerificationCancel });
|
|
|
|
event.verificationRequest = getMockVerificationRequest({ cancelled: false });
|
2023-02-22 10:52:55 +00:00
|
|
|
const { container } = render(<MKeyVerificationConclusion mxEvent={event} />);
|
|
|
|
expect(container).toBeEmpty();
|
2022-04-28 11:44:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't render if the event type is done but the request type isn't", () => {
|
|
|
|
const event = new MatrixEvent({ type: "m.key.verification.done" });
|
|
|
|
event.verificationRequest = getMockVerificationRequest({ done: false });
|
2023-02-22 10:52:55 +00:00
|
|
|
const { container } = render(<MKeyVerificationConclusion mxEvent={event} />);
|
|
|
|
expect(container).toBeEmpty();
|
2022-04-28 11:44:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't render if the user isn't actually trusted", () => {
|
|
|
|
mockClient.checkUserTrust.mockReturnValue(untrustworthy);
|
|
|
|
|
|
|
|
const event = new MatrixEvent({ type: "m.key.verification.done" });
|
|
|
|
event.verificationRequest = getMockVerificationRequest({ done: true });
|
2023-02-22 10:52:55 +00:00
|
|
|
const { container } = render(<MKeyVerificationConclusion mxEvent={event} />);
|
|
|
|
expect(container).toBeEmpty();
|
2022-04-28 11:44:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should rerender appropriately if user trust status changes", () => {
|
|
|
|
mockClient.checkUserTrust.mockReturnValue(untrustworthy);
|
|
|
|
|
|
|
|
const event = new MatrixEvent({ type: "m.key.verification.done" });
|
|
|
|
event.verificationRequest = getMockVerificationRequest({ done: true, otherUserId: "@someuser:domain" });
|
2023-02-22 10:52:55 +00:00
|
|
|
const { container } = render(<MKeyVerificationConclusion mxEvent={event} />);
|
|
|
|
expect(container).toBeEmpty();
|
2022-04-28 11:44:04 +00:00
|
|
|
|
|
|
|
mockClient.checkUserTrust.mockReturnValue(trustworthy);
|
|
|
|
|
|
|
|
/* Ensure we don't rerender for every trust status change of any user */
|
2022-11-04 10:48:08 +00:00
|
|
|
mockClient.emit(
|
|
|
|
CryptoEvent.UserTrustStatusChanged,
|
|
|
|
"@anotheruser:domain",
|
|
|
|
new UserTrustLevel(true, true, true),
|
|
|
|
);
|
2023-02-22 10:52:55 +00:00
|
|
|
expect(container).toBeEmpty();
|
2022-04-28 11:44:04 +00:00
|
|
|
|
|
|
|
/* But when our user changes, we do rerender */
|
2022-11-04 10:48:08 +00:00
|
|
|
mockClient.emit(
|
|
|
|
CryptoEvent.UserTrustStatusChanged,
|
|
|
|
event.verificationRequest.otherUserId,
|
|
|
|
new UserTrustLevel(true, true, true),
|
|
|
|
);
|
2023-02-22 10:52:55 +00:00
|
|
|
expect(container).not.toBeEmpty();
|
2022-04-28 11:44:04 +00:00
|
|
|
});
|
|
|
|
});
|