2022-07-20 07:26:25 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-07-20 07:26:25 +00:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-07-20 07:26:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2024-10-14 16:11:58 +00:00
|
|
|
import { render, screen } from "jest-matrix-react";
|
2024-11-19 10:09:25 +00:00
|
|
|
import { waitFor } from "@testing-library/dom";
|
2022-07-20 07:26:25 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import EncryptionEvent from "../../../../../src/components/views/messages/EncryptionEvent";
|
|
|
|
import { createTestClient, mkMessage } from "../../../../test-utils";
|
|
|
|
import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
|
|
|
|
import { LocalRoom } from "../../../../../src/models/LocalRoom";
|
|
|
|
import DMRoomMap from "../../../../../src/utils/DMRoomMap";
|
|
|
|
import MatrixClientContext from "../../../../../src/contexts/MatrixClientContext";
|
2022-07-20 07:26:25 +00:00
|
|
|
|
|
|
|
const renderEncryptionEvent = (client: MatrixClient, event: MatrixEvent) => {
|
|
|
|
render(
|
|
|
|
<MatrixClientContext.Provider value={client}>
|
|
|
|
<EncryptionEvent mxEvent={event} />
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-11-20 13:29:23 +00:00
|
|
|
const checkTexts = async (title: string, subTitle: string) => {
|
|
|
|
await screen.findByText(title);
|
|
|
|
await screen.findByText(subTitle);
|
2022-07-20 07:26:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
describe("EncryptionEvent", () => {
|
|
|
|
const roomId = "!room:example.com";
|
|
|
|
const algorithm = "m.megolm.v1.aes-sha2";
|
|
|
|
let client: MatrixClient;
|
|
|
|
let event: MatrixEvent;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
client = createTestClient();
|
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(client);
|
2023-06-15 07:46:19 +00:00
|
|
|
jest.spyOn(MatrixClientPeg, "safeGet").mockReturnValue(client);
|
2022-07-20 07:26:25 +00:00
|
|
|
event = mkMessage({
|
|
|
|
event: true,
|
|
|
|
room: roomId,
|
2023-02-15 13:36:22 +00:00
|
|
|
user: client.getUserId()!,
|
2022-07-20 07:26:25 +00:00
|
|
|
});
|
|
|
|
jest.spyOn(DMRoomMap, "shared").mockReturnValue({
|
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
} as unknown as DMRoomMap);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("for an encrypted room", () => {
|
|
|
|
beforeEach(() => {
|
2023-02-15 13:36:22 +00:00
|
|
|
event.event.content!.algorithm = algorithm;
|
2024-11-19 10:09:25 +00:00
|
|
|
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
|
2023-02-15 13:36:22 +00:00
|
|
|
const room = new Room(roomId, client, client.getUserId()!);
|
2022-07-20 07:26:25 +00:00
|
|
|
mocked(client.getRoom).mockReturnValue(room);
|
|
|
|
});
|
|
|
|
|
2024-11-19 10:09:25 +00:00
|
|
|
it("should show the expected texts", async () => {
|
2022-07-20 07:26:25 +00:00
|
|
|
renderEncryptionEvent(client, event);
|
2024-11-19 10:09:25 +00:00
|
|
|
await waitFor(() =>
|
|
|
|
checkTexts(
|
|
|
|
"Encryption enabled",
|
|
|
|
"Messages in this room are end-to-end encrypted. " +
|
|
|
|
"When people join, you can verify them in their profile, just tap on their profile picture.",
|
|
|
|
),
|
2022-07-20 07:26:25 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("with same previous algorithm", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(event, "getPrevContent").mockReturnValue({
|
|
|
|
algorithm: algorithm,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-11-19 10:09:25 +00:00
|
|
|
it("should show the expected texts", async () => {
|
2022-07-20 07:26:25 +00:00
|
|
|
renderEncryptionEvent(client, event);
|
2024-11-19 10:09:25 +00:00
|
|
|
await waitFor(() => checkTexts("Encryption enabled", "Some encryption parameters have been changed."));
|
2022-07-20 07:26:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("with unknown algorithm", () => {
|
|
|
|
beforeEach(() => {
|
2023-02-15 13:36:22 +00:00
|
|
|
event.event.content!.algorithm = "unknown";
|
2022-07-20 07:26:25 +00:00
|
|
|
});
|
|
|
|
|
2024-11-19 10:09:25 +00:00
|
|
|
it("should show the expected texts", async () => {
|
2022-07-20 07:26:25 +00:00
|
|
|
renderEncryptionEvent(client, event);
|
2024-11-19 10:09:25 +00:00
|
|
|
await waitFor(() => checkTexts("Encryption enabled", "Ignored attempt to disable encryption"));
|
2022-07-20 07:26:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("for an unencrypted room", () => {
|
|
|
|
beforeEach(() => {
|
2024-11-19 10:09:25 +00:00
|
|
|
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(false);
|
2022-07-20 07:26:25 +00:00
|
|
|
renderEncryptionEvent(client, event);
|
|
|
|
});
|
|
|
|
|
2024-11-19 10:09:25 +00:00
|
|
|
it("should show the expected texts", async () => {
|
|
|
|
expect(client.getCrypto()!.isEncryptionEnabledInRoom).toHaveBeenCalledWith(roomId);
|
|
|
|
await waitFor(() =>
|
|
|
|
checkTexts("Encryption not enabled", "The encryption used by this room isn't supported."),
|
|
|
|
);
|
2022-07-20 07:26:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("for an encrypted local room", () => {
|
|
|
|
beforeEach(() => {
|
2023-02-15 13:36:22 +00:00
|
|
|
event.event.content!.algorithm = algorithm;
|
2024-11-19 10:09:25 +00:00
|
|
|
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
|
2023-02-15 13:36:22 +00:00
|
|
|
const localRoom = new LocalRoom(roomId, client, client.getUserId()!);
|
2022-07-20 07:26:25 +00:00
|
|
|
mocked(client.getRoom).mockReturnValue(localRoom);
|
|
|
|
renderEncryptionEvent(client, event);
|
|
|
|
});
|
|
|
|
|
2024-11-20 13:29:23 +00:00
|
|
|
it("should show the expected texts", async () => {
|
2024-11-19 10:09:25 +00:00
|
|
|
expect(client.getCrypto()!.isEncryptionEnabledInRoom).toHaveBeenCalledWith(roomId);
|
2024-11-20 13:29:23 +00:00
|
|
|
await checkTexts("Encryption enabled", "Messages in this chat will be end-to-end encrypted.");
|
2022-07-20 07:26:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|