2022-10-24 09:50:21 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2022, 2023 The Matrix.org Foundation C.I.C.
|
2022-10-24 09:50:21 +03:00
|
|
|
|
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.
|
2022-10-24 09:50:21 +03:00
|
|
|
*/
|
|
|
|
|
2022-11-25 16:54:06 +03:00
|
|
|
import * as React from "react";
|
2024-10-14 19:11:58 +03:00
|
|
|
import { act, fireEvent, render, screen, waitFor } from "jest-matrix-react";
|
2023-04-05 05:46:17 +03:00
|
|
|
import { mocked } from "jest-mock";
|
2023-08-09 10:18:41 +03:00
|
|
|
import {
|
2023-09-18 20:12:18 +03:00
|
|
|
EventType,
|
|
|
|
IEventDecryptionResult,
|
2023-08-09 10:18:41 +03:00
|
|
|
MatrixClient,
|
2023-09-18 20:12:18 +03:00
|
|
|
MatrixEvent,
|
|
|
|
NotificationCountType,
|
2023-08-09 10:18:41 +03:00
|
|
|
PendingEventOrdering,
|
2023-09-18 20:12:18 +03:00
|
|
|
Room,
|
|
|
|
TweakName,
|
2023-08-09 10:18:41 +03:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2024-11-04 15:46:38 +03:00
|
|
|
import {
|
|
|
|
CryptoApi,
|
|
|
|
DecryptionFailureCode,
|
|
|
|
EventEncryptionInfo,
|
|
|
|
EventShieldColour,
|
|
|
|
EventShieldReason,
|
|
|
|
} from "matrix-js-sdk/src/crypto-api";
|
2024-03-22 18:48:29 +03:00
|
|
|
import { mkEncryptedMatrixEvent } from "matrix-js-sdk/src/testing";
|
2022-10-24 09:50:21 +03:00
|
|
|
|
2024-10-15 16:57:26 +03:00
|
|
|
import EventTile, { EventTileProps } from "../../../../../src/components/views/rooms/EventTile";
|
|
|
|
import MatrixClientContext from "../../../../../src/contexts/MatrixClientContext";
|
2024-12-02 12:49:52 +03:00
|
|
|
import { TimelineRenderingType } from "../../../../../src/contexts/RoomContext";
|
2024-10-15 16:57:26 +03:00
|
|
|
import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
|
|
|
|
import { filterConsole, flushPromises, getRoomContext, mkEvent, mkMessage, stubClient } from "../../../../test-utils";
|
|
|
|
import { mkThread } from "../../../../test-utils/threads";
|
|
|
|
import DMRoomMap from "../../../../../src/utils/DMRoomMap";
|
|
|
|
import dis from "../../../../../src/dispatcher/dispatcher";
|
|
|
|
import { Action } from "../../../../../src/dispatcher/actions";
|
|
|
|
import { IRoomState } from "../../../../../src/components/structures/RoomView";
|
|
|
|
import PinningUtils from "../../../../../src/utils/PinningUtils";
|
|
|
|
import { Layout } from "../../../../../src/settings/enums/Layout";
|
2024-12-02 12:49:52 +03:00
|
|
|
import { ScopedRoomContextProvider } from "../../../../../src/contexts/ScopedRoomContext.tsx";
|
2022-10-24 09:50:21 +03:00
|
|
|
|
|
|
|
describe("EventTile", () => {
|
|
|
|
const ROOM_ID = "!roomId:example.org";
|
|
|
|
let mxEvent: MatrixEvent;
|
|
|
|
let room: Room;
|
|
|
|
let client: MatrixClient;
|
2022-11-25 16:54:06 +03:00
|
|
|
|
2022-10-24 09:50:21 +03:00
|
|
|
// let changeEvent: (event: MatrixEvent) => void;
|
|
|
|
|
2023-09-18 20:12:18 +03:00
|
|
|
/** wrap the EventTile up in context providers, and with basic properties, as it would be by MessagePanel normally. */
|
|
|
|
function WrappedEventTile(props: {
|
|
|
|
roomContext: IRoomState;
|
|
|
|
eventTilePropertyOverrides?: Partial<EventTileProps>;
|
|
|
|
}) {
|
|
|
|
return (
|
|
|
|
<MatrixClientContext.Provider value={client}>
|
2024-12-02 12:49:52 +03:00
|
|
|
<ScopedRoomContextProvider {...props.roomContext}>
|
2024-04-15 16:54:37 +03:00
|
|
|
<EventTile
|
|
|
|
mxEvent={mxEvent}
|
|
|
|
replacingEventId={mxEvent.replacingEventId()}
|
|
|
|
{...(props.eventTilePropertyOverrides ?? {})}
|
|
|
|
/>
|
2024-12-02 12:49:52 +03:00
|
|
|
</ScopedRoomContextProvider>
|
2023-09-18 20:12:18 +03:00
|
|
|
</MatrixClientContext.Provider>
|
|
|
|
);
|
2022-10-24 09:50:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getComponent(
|
|
|
|
overrides: Partial<EventTileProps> = {},
|
|
|
|
renderingType: TimelineRenderingType = TimelineRenderingType.Room,
|
2024-12-02 13:20:13 +03:00
|
|
|
roomContext: Partial<IRoomState> = {},
|
2022-10-24 09:50:21 +03:00
|
|
|
) {
|
|
|
|
const context = getRoomContext(room, {
|
|
|
|
timelineRenderingType: renderingType,
|
2024-12-02 13:20:13 +03:00
|
|
|
...roomContext,
|
2022-10-24 09:50:21 +03:00
|
|
|
});
|
2023-09-18 20:12:18 +03:00
|
|
|
return render(<WrappedEventTile roomContext={context} eventTilePropertyOverrides={overrides} />);
|
2022-10-24 09:50:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
|
|
|
stubClient();
|
2023-06-05 20:12:23 +03:00
|
|
|
client = MatrixClientPeg.safeGet();
|
2022-10-24 09:50:21 +03:00
|
|
|
|
2023-04-05 05:46:17 +03:00
|
|
|
room = new Room(ROOM_ID, client, client.getSafeUserId(), {
|
2022-10-24 09:50:21 +03:00
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
2023-11-29 16:36:52 +03:00
|
|
|
timelineSupport: true,
|
2022-10-24 09:50:21 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
jest.spyOn(client, "getRoom").mockReturnValue(room);
|
2022-10-28 14:48:15 +03:00
|
|
|
jest.spyOn(client, "decryptEventIfNeeded").mockResolvedValue();
|
2022-10-24 09:50:21 +03:00
|
|
|
|
|
|
|
mxEvent = mkMessage({
|
|
|
|
room: room.roomId,
|
|
|
|
user: "@alice:example.org",
|
|
|
|
msg: "Hello world!",
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-10-04 10:11:37 +03:00
|
|
|
afterEach(() => {
|
|
|
|
jest.spyOn(PinningUtils, "isPinned").mockReturnValue(false);
|
|
|
|
});
|
|
|
|
|
2022-10-28 14:48:15 +03:00
|
|
|
describe("EventTile thread summary", () => {
|
|
|
|
beforeEach(() => {
|
2023-01-30 15:20:11 +03:00
|
|
|
jest.spyOn(client, "supportsThreads").mockReturnValue(true);
|
2022-10-28 14:48:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("removes the thread summary when thread is deleted", async () => {
|
|
|
|
const {
|
|
|
|
rootEvent,
|
|
|
|
events: [, reply],
|
|
|
|
} = mkThread({
|
|
|
|
room,
|
|
|
|
client,
|
|
|
|
authorId: "@alice:example.org",
|
|
|
|
participantUserIds: ["@alice:example.org"],
|
|
|
|
length: 2, // root + 1 answer
|
|
|
|
});
|
|
|
|
getComponent(
|
|
|
|
{
|
|
|
|
mxEvent: rootEvent,
|
|
|
|
},
|
|
|
|
TimelineRenderingType.Room,
|
|
|
|
);
|
|
|
|
|
|
|
|
await waitFor(() => expect(screen.queryByTestId("thread-summary")).not.toBeNull());
|
|
|
|
|
|
|
|
const redaction = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomRedaction,
|
|
|
|
user: "@alice:example.org",
|
|
|
|
room: room.roomId,
|
|
|
|
redacts: reply.getId(),
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
act(() => room.processThreadedEvents([redaction], false));
|
|
|
|
|
|
|
|
await waitFor(() => expect(screen.queryByTestId("thread-summary")).toBeNull());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-24 09:50:21 +03:00
|
|
|
describe("EventTile renderingType: ThreadsList", () => {
|
2023-01-11 14:49:03 +03:00
|
|
|
it("shows an unread notification badge", () => {
|
2022-10-24 09:50:21 +03:00
|
|
|
const { container } = getComponent({}, TimelineRenderingType.ThreadsList);
|
|
|
|
|
2023-01-11 14:49:03 +03:00
|
|
|
// By default, the thread will assume it is read.
|
2022-10-24 09:50:21 +03:00
|
|
|
expect(container.getElementsByClassName("mx_NotificationBadge")).toHaveLength(0);
|
|
|
|
|
|
|
|
act(() => {
|
2022-11-25 16:54:06 +03:00
|
|
|
room.setThreadUnreadNotificationCount(mxEvent.getId()!, NotificationCountType.Total, 3);
|
2022-10-24 09:50:21 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(container.getElementsByClassName("mx_NotificationBadge")).toHaveLength(1);
|
2024-01-25 19:53:41 +03:00
|
|
|
expect(container.getElementsByClassName("mx_NotificationBadge_level_highlight")).toHaveLength(0);
|
2022-10-24 09:50:21 +03:00
|
|
|
|
|
|
|
act(() => {
|
2022-11-25 16:54:06 +03:00
|
|
|
room.setThreadUnreadNotificationCount(mxEvent.getId()!, NotificationCountType.Highlight, 1);
|
2022-10-24 09:50:21 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(container.getElementsByClassName("mx_NotificationBadge")).toHaveLength(1);
|
2024-01-25 19:53:41 +03:00
|
|
|
expect(container.getElementsByClassName("mx_NotificationBadge_level_highlight")).toHaveLength(1);
|
2022-10-24 09:50:21 +03:00
|
|
|
});
|
|
|
|
});
|
2022-11-25 16:54:06 +03:00
|
|
|
|
2024-10-04 10:11:37 +03:00
|
|
|
describe("EventTile renderingType: Threads", () => {
|
|
|
|
it("should display the pinned message badge", async () => {
|
|
|
|
jest.spyOn(PinningUtils, "isPinned").mockReturnValue(true);
|
|
|
|
getComponent({}, TimelineRenderingType.Thread);
|
|
|
|
|
|
|
|
expect(screen.getByText("Pinned message")).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-10-14 21:08:05 +03:00
|
|
|
describe("EventTile renderingType: File", () => {
|
|
|
|
it("should not display the pinned message badge", async () => {
|
|
|
|
jest.spyOn(PinningUtils, "isPinned").mockReturnValue(true);
|
|
|
|
getComponent({}, TimelineRenderingType.File);
|
|
|
|
|
|
|
|
expect(screen.queryByText("Pinned message")).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-10-04 10:11:37 +03:00
|
|
|
describe("EventTile renderingType: default", () => {
|
|
|
|
it.each([[Layout.Group], [Layout.Bubble], [Layout.IRC]])(
|
|
|
|
"should display the pinned message badge",
|
|
|
|
async (layout) => {
|
|
|
|
jest.spyOn(PinningUtils, "isPinned").mockReturnValue(true);
|
|
|
|
getComponent({ layout });
|
|
|
|
|
|
|
|
expect(screen.getByText("Pinned message")).toBeInTheDocument();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-12-21 13:25:50 +03:00
|
|
|
describe("EventTile in the right panel", () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
const dmRoomMap: DMRoomMap = {
|
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
} as unknown as DMRoomMap;
|
|
|
|
DMRoomMap.setShared(dmRoomMap);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders the room name for notifications", () => {
|
|
|
|
const { container } = getComponent({}, TimelineRenderingType.Notification);
|
|
|
|
expect(container.getElementsByClassName("mx_EventTile_details")[0]).toHaveTextContent(
|
|
|
|
"@alice:example.org in !roomId:example.org",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders the sender for the thread list", () => {
|
|
|
|
const { container } = getComponent({}, TimelineRenderingType.ThreadsList);
|
|
|
|
expect(container.getElementsByClassName("mx_EventTile_details")[0]).toHaveTextContent("@alice:example.org");
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[TimelineRenderingType.Notification, Action.ViewRoom],
|
|
|
|
[TimelineRenderingType.ThreadsList, Action.ShowThread],
|
|
|
|
])("type %s dispatches %s", (renderingType, action) => {
|
|
|
|
jest.spyOn(dis, "dispatch");
|
|
|
|
|
|
|
|
const { container } = getComponent({}, renderingType);
|
|
|
|
|
|
|
|
fireEvent.click(container.querySelector("li")!);
|
|
|
|
|
|
|
|
expect(dis.dispatch).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
action,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2022-11-25 16:54:06 +03:00
|
|
|
describe("Event verification", () => {
|
2023-09-18 20:12:18 +03:00
|
|
|
// data for our stubbed getEncryptionInfoForEvent: a map from event id to result
|
|
|
|
const eventToEncryptionInfoMap = new Map<string, EventEncryptionInfo>();
|
2022-11-25 16:54:06 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
eventToEncryptionInfoMap.clear();
|
|
|
|
|
2023-04-24 16:19:46 +03:00
|
|
|
const mockCrypto = {
|
2023-09-18 20:12:18 +03:00
|
|
|
// a mocked version of getEncryptionInfoForEvent which will pick its result from `eventToEncryptionInfoMap`
|
|
|
|
getEncryptionInfoForEvent: async (event: MatrixEvent) => eventToEncryptionInfoMap.get(event.getId()!)!,
|
2023-04-24 16:19:46 +03:00
|
|
|
} as unknown as CryptoApi;
|
|
|
|
client.getCrypto = () => mockCrypto;
|
2022-11-25 16:54:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("shows a warning for an event from an unverified device", async () => {
|
2024-03-22 18:48:29 +03:00
|
|
|
mxEvent = await mkEncryptedMatrixEvent({
|
2022-11-25 16:54:06 +03:00
|
|
|
plainContent: { msgtype: "m.text", body: "msg1" },
|
|
|
|
plainType: "m.room.message",
|
2024-03-22 18:48:29 +03:00
|
|
|
sender: "@alice:example.org",
|
|
|
|
roomId: room.roomId,
|
2022-11-25 16:54:06 +03:00
|
|
|
});
|
|
|
|
eventToEncryptionInfoMap.set(mxEvent.getId()!, {
|
2023-09-18 20:12:18 +03:00
|
|
|
shieldColour: EventShieldColour.RED,
|
|
|
|
shieldReason: EventShieldReason.UNSIGNED_DEVICE,
|
|
|
|
} as EventEncryptionInfo);
|
2022-11-25 16:54:06 +03:00
|
|
|
|
|
|
|
const { container } = getComponent();
|
2024-11-20 16:29:23 +03:00
|
|
|
await flushPromises();
|
2022-11-25 16:54:06 +03:00
|
|
|
|
|
|
|
const eventTiles = container.getElementsByClassName("mx_EventTile");
|
|
|
|
expect(eventTiles).toHaveLength(1);
|
|
|
|
|
|
|
|
// there should be a warning shield
|
|
|
|
expect(container.getElementsByClassName("mx_EventTile_e2eIcon")).toHaveLength(1);
|
|
|
|
expect(container.getElementsByClassName("mx_EventTile_e2eIcon")[0].classList).toContain(
|
|
|
|
"mx_EventTile_e2eIcon_warning",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shows no shield for a verified event", async () => {
|
2024-03-22 18:48:29 +03:00
|
|
|
mxEvent = await mkEncryptedMatrixEvent({
|
2022-11-25 16:54:06 +03:00
|
|
|
plainContent: { msgtype: "m.text", body: "msg1" },
|
|
|
|
plainType: "m.room.message",
|
2024-03-22 18:48:29 +03:00
|
|
|
sender: "@alice:example.org",
|
|
|
|
roomId: room.roomId,
|
2022-11-25 16:54:06 +03:00
|
|
|
});
|
|
|
|
eventToEncryptionInfoMap.set(mxEvent.getId()!, {
|
2023-09-18 20:12:18 +03:00
|
|
|
shieldColour: EventShieldColour.NONE,
|
|
|
|
shieldReason: null,
|
|
|
|
} as EventEncryptionInfo);
|
2022-11-25 16:54:06 +03:00
|
|
|
|
|
|
|
const { container } = getComponent();
|
2024-11-20 16:29:23 +03:00
|
|
|
await flushPromises();
|
2022-11-25 16:54:06 +03:00
|
|
|
|
|
|
|
const eventTiles = container.getElementsByClassName("mx_EventTile");
|
|
|
|
expect(eventTiles).toHaveLength(1);
|
|
|
|
|
|
|
|
// there should be no warning
|
|
|
|
expect(container.getElementsByClassName("mx_EventTile_e2eIcon")).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
2023-09-18 20:12:18 +03:00
|
|
|
it.each([
|
|
|
|
[EventShieldReason.UNKNOWN, "Unknown error"],
|
|
|
|
[EventShieldReason.UNVERIFIED_IDENTITY, "unverified user"],
|
|
|
|
[EventShieldReason.UNSIGNED_DEVICE, "device not verified by its owner"],
|
|
|
|
[EventShieldReason.UNKNOWN_DEVICE, "unknown or deleted device"],
|
|
|
|
[EventShieldReason.AUTHENTICITY_NOT_GUARANTEED, "can't be guaranteed"],
|
|
|
|
[EventShieldReason.MISMATCHED_SENDER_KEY, "Encrypted by an unverified session"],
|
2024-11-26 19:00:52 +03:00
|
|
|
[EventShieldReason.SENT_IN_CLEAR, "Not encrypted"],
|
|
|
|
[EventShieldReason.VERIFICATION_VIOLATION, "Sender's verified identity has changed"],
|
2023-09-18 20:12:18 +03:00
|
|
|
])("shows the correct reason code for %i (%s)", async (reasonCode: EventShieldReason, expectedText: string) => {
|
2024-03-22 18:48:29 +03:00
|
|
|
mxEvent = await mkEncryptedMatrixEvent({
|
2023-09-18 20:12:18 +03:00
|
|
|
plainContent: { msgtype: "m.text", body: "msg1" },
|
|
|
|
plainType: "m.room.message",
|
2024-03-22 18:48:29 +03:00
|
|
|
sender: "@alice:example.org",
|
|
|
|
roomId: room.roomId,
|
2023-09-18 20:12:18 +03:00
|
|
|
});
|
|
|
|
eventToEncryptionInfoMap.set(mxEvent.getId()!, {
|
|
|
|
shieldColour: EventShieldColour.GREY,
|
|
|
|
shieldReason: reasonCode,
|
|
|
|
} as EventEncryptionInfo);
|
|
|
|
|
|
|
|
const { container } = getComponent();
|
2024-11-20 16:29:23 +03:00
|
|
|
await flushPromises();
|
2023-09-18 20:12:18 +03:00
|
|
|
|
|
|
|
const e2eIcons = container.getElementsByClassName("mx_EventTile_e2eIcon");
|
|
|
|
expect(e2eIcons).toHaveLength(1);
|
|
|
|
expect(e2eIcons[0].classList).toContain("mx_EventTile_e2eIcon_normal");
|
2024-01-11 14:49:24 +03:00
|
|
|
fireEvent.focus(e2eIcons[0]);
|
2024-10-14 19:11:58 +03:00
|
|
|
expect(e2eIcons[0].getAttribute("aria-labelledby")).toBeTruthy();
|
|
|
|
expect(document.getElementById(e2eIcons[0].getAttribute("aria-labelledby")!)).toHaveTextContent(
|
2024-01-11 14:49:24 +03:00
|
|
|
expectedText,
|
|
|
|
);
|
2023-09-18 20:12:18 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("undecryptable event", () => {
|
|
|
|
filterConsole("Error decrypting event");
|
|
|
|
|
|
|
|
it("shows an undecryptable warning", async () => {
|
|
|
|
mxEvent = mkEvent({
|
|
|
|
type: "m.room.encrypted",
|
|
|
|
room: room.roomId,
|
|
|
|
user: "@alice:example.org",
|
|
|
|
event: true,
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockCrypto = {
|
|
|
|
decryptEvent: async (_ev): Promise<IEventDecryptionResult> => {
|
|
|
|
throw new Error("can't decrypt");
|
|
|
|
},
|
2024-03-07 01:59:41 +03:00
|
|
|
} as Parameters<MatrixEvent["attemptDecryption"]>[0];
|
2023-09-18 20:12:18 +03:00
|
|
|
await mxEvent.attemptDecryption(mockCrypto);
|
|
|
|
|
|
|
|
const { container } = getComponent();
|
2024-11-20 16:29:23 +03:00
|
|
|
await flushPromises();
|
2023-09-18 20:12:18 +03:00
|
|
|
|
|
|
|
const eventTiles = container.getElementsByClassName("mx_EventTile");
|
|
|
|
expect(eventTiles).toHaveLength(1);
|
|
|
|
|
|
|
|
expect(container.getElementsByClassName("mx_EventTile_e2eIcon")).toHaveLength(1);
|
|
|
|
expect(container.getElementsByClassName("mx_EventTile_e2eIcon")[0].classList).toContain(
|
|
|
|
"mx_EventTile_e2eIcon_decryption_failure",
|
|
|
|
);
|
|
|
|
});
|
2024-11-04 15:46:38 +03:00
|
|
|
|
|
|
|
it("should not show a shield for previously-verified users", async () => {
|
|
|
|
mxEvent = mkEvent({
|
|
|
|
type: "m.room.encrypted",
|
|
|
|
room: room.roomId,
|
|
|
|
user: "@alice:example.org",
|
|
|
|
event: true,
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockCrypto = {
|
|
|
|
decryptEvent: async (_ev): Promise<IEventDecryptionResult> => {
|
|
|
|
throw new Error("can't decrypt");
|
|
|
|
},
|
|
|
|
} as Parameters<MatrixEvent["attemptDecryption"]>[0];
|
|
|
|
await mxEvent.attemptDecryption(mockCrypto);
|
|
|
|
mxEvent["_decryptionFailureReason"] = DecryptionFailureCode.SENDER_IDENTITY_PREVIOUSLY_VERIFIED;
|
|
|
|
|
|
|
|
const { container } = getComponent();
|
|
|
|
await act(flushPromises);
|
|
|
|
|
|
|
|
const eventTiles = container.getElementsByClassName("mx_EventTile");
|
|
|
|
expect(eventTiles).toHaveLength(1);
|
|
|
|
|
|
|
|
expect(container.getElementsByClassName("mx_EventTile_e2eIcon")).toHaveLength(0);
|
|
|
|
});
|
2023-09-18 20:12:18 +03:00
|
|
|
});
|
|
|
|
|
2022-11-25 16:54:06 +03:00
|
|
|
it("should update the warning when the event is edited", async () => {
|
|
|
|
// we start out with an event from the trusted device
|
2024-03-22 18:48:29 +03:00
|
|
|
mxEvent = await mkEncryptedMatrixEvent({
|
2022-11-25 16:54:06 +03:00
|
|
|
plainContent: { msgtype: "m.text", body: "msg1" },
|
|
|
|
plainType: "m.room.message",
|
2024-03-22 18:48:29 +03:00
|
|
|
sender: "@alice:example.org",
|
|
|
|
roomId: room.roomId,
|
2022-11-25 16:54:06 +03:00
|
|
|
});
|
|
|
|
eventToEncryptionInfoMap.set(mxEvent.getId()!, {
|
2023-09-18 20:12:18 +03:00
|
|
|
shieldColour: EventShieldColour.NONE,
|
|
|
|
shieldReason: null,
|
|
|
|
} as EventEncryptionInfo);
|
|
|
|
|
|
|
|
const roomContext = getRoomContext(room, {});
|
|
|
|
const { container, rerender } = render(<WrappedEventTile roomContext={roomContext} />);
|
2022-11-25 16:54:06 +03:00
|
|
|
|
2024-11-20 16:29:23 +03:00
|
|
|
await flushPromises();
|
2022-11-25 16:54:06 +03:00
|
|
|
|
|
|
|
const eventTiles = container.getElementsByClassName("mx_EventTile");
|
|
|
|
expect(eventTiles).toHaveLength(1);
|
|
|
|
|
|
|
|
// there should be no warning
|
|
|
|
expect(container.getElementsByClassName("mx_EventTile_e2eIcon")).toHaveLength(0);
|
|
|
|
|
|
|
|
// then we replace the event with one from the unverified device
|
2024-03-22 18:48:29 +03:00
|
|
|
const replacementEvent = await mkEncryptedMatrixEvent({
|
2022-11-25 16:54:06 +03:00
|
|
|
plainContent: { msgtype: "m.text", body: "msg1" },
|
|
|
|
plainType: "m.room.message",
|
2024-03-22 18:48:29 +03:00
|
|
|
sender: "@alice:example.org",
|
|
|
|
roomId: room.roomId,
|
2022-11-25 16:54:06 +03:00
|
|
|
});
|
|
|
|
eventToEncryptionInfoMap.set(replacementEvent.getId()!, {
|
2023-09-18 20:12:18 +03:00
|
|
|
shieldColour: EventShieldColour.RED,
|
|
|
|
shieldReason: EventShieldReason.UNSIGNED_DEVICE,
|
|
|
|
} as EventEncryptionInfo);
|
2022-11-25 16:54:06 +03:00
|
|
|
|
2023-04-24 16:19:46 +03:00
|
|
|
await act(async () => {
|
2022-11-25 16:54:06 +03:00
|
|
|
mxEvent.makeReplaced(replacementEvent);
|
2023-09-18 20:12:18 +03:00
|
|
|
rerender(<WrappedEventTile roomContext={roomContext} />);
|
|
|
|
await flushPromises;
|
2022-11-25 16:54:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// check it was updated
|
|
|
|
expect(container.getElementsByClassName("mx_EventTile_e2eIcon")).toHaveLength(1);
|
|
|
|
expect(container.getElementsByClassName("mx_EventTile_e2eIcon")[0].classList).toContain(
|
|
|
|
"mx_EventTile_e2eIcon_warning",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should update the warning when the event is replaced with an unencrypted one", async () => {
|
|
|
|
// we start out with an event from the trusted device
|
2024-03-22 18:48:29 +03:00
|
|
|
mxEvent = await mkEncryptedMatrixEvent({
|
2022-11-25 16:54:06 +03:00
|
|
|
plainContent: { msgtype: "m.text", body: "msg1" },
|
|
|
|
plainType: "m.room.message",
|
2024-03-22 18:48:29 +03:00
|
|
|
sender: "@alice:example.org",
|
|
|
|
roomId: room.roomId,
|
2022-11-25 16:54:06 +03:00
|
|
|
});
|
2023-09-18 20:12:18 +03:00
|
|
|
|
2022-11-25 16:54:06 +03:00
|
|
|
eventToEncryptionInfoMap.set(mxEvent.getId()!, {
|
2023-09-18 20:12:18 +03:00
|
|
|
shieldColour: EventShieldColour.NONE,
|
|
|
|
shieldReason: null,
|
|
|
|
} as EventEncryptionInfo);
|
2022-11-25 16:54:06 +03:00
|
|
|
|
2024-12-02 13:20:13 +03:00
|
|
|
const roomContext = getRoomContext(room, { isRoomEncrypted: true });
|
2023-09-18 20:12:18 +03:00
|
|
|
const { container, rerender } = render(<WrappedEventTile roomContext={roomContext} />);
|
2024-11-20 16:29:23 +03:00
|
|
|
await flushPromises();
|
2022-11-25 16:54:06 +03:00
|
|
|
|
|
|
|
const eventTiles = container.getElementsByClassName("mx_EventTile");
|
|
|
|
expect(eventTiles).toHaveLength(1);
|
|
|
|
|
|
|
|
// there should be no warning
|
|
|
|
expect(container.getElementsByClassName("mx_EventTile_e2eIcon")).toHaveLength(0);
|
|
|
|
|
|
|
|
// then we replace the event with an unencrypted one
|
|
|
|
const replacementEvent = await mkMessage({
|
|
|
|
msg: "msg2",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
room: room.roomId,
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2023-04-24 16:19:46 +03:00
|
|
|
await act(async () => {
|
2022-11-25 16:54:06 +03:00
|
|
|
mxEvent.makeReplaced(replacementEvent);
|
2023-09-18 20:12:18 +03:00
|
|
|
rerender(<WrappedEventTile roomContext={roomContext} />);
|
|
|
|
await flushPromises;
|
2022-11-25 16:54:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// check it was updated
|
|
|
|
expect(container.getElementsByClassName("mx_EventTile_e2eIcon")).toHaveLength(1);
|
|
|
|
expect(container.getElementsByClassName("mx_EventTile_e2eIcon")[0].classList).toContain(
|
|
|
|
"mx_EventTile_e2eIcon_warning",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2023-04-05 05:46:17 +03:00
|
|
|
|
|
|
|
describe("event highlighting", () => {
|
|
|
|
const isHighlighted = (container: HTMLElement): boolean =>
|
|
|
|
!!container.getElementsByClassName("mx_EventTile_highlight").length;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(client.getPushActionsForEvent).mockReturnValue(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not highlight message where message matches no push actions", () => {
|
|
|
|
const { container } = getComponent();
|
|
|
|
|
|
|
|
expect(client.getPushActionsForEvent).toHaveBeenCalledWith(mxEvent);
|
|
|
|
expect(isHighlighted(container)).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`does not highlight when message's push actions does not have a highlight tweak`, () => {
|
|
|
|
mocked(client.getPushActionsForEvent).mockReturnValue({ notify: true, tweaks: {} });
|
|
|
|
const { container } = getComponent();
|
|
|
|
|
|
|
|
expect(isHighlighted(container)).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`highlights when message's push actions have a highlight tweak`, () => {
|
|
|
|
mocked(client.getPushActionsForEvent).mockReturnValue({
|
|
|
|
notify: true,
|
|
|
|
tweaks: { [TweakName.Highlight]: true },
|
|
|
|
});
|
|
|
|
const { container } = getComponent();
|
|
|
|
|
|
|
|
expect(isHighlighted(container)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when a message has been edited", () => {
|
|
|
|
let editingEvent: MatrixEvent;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
editingEvent = new MatrixEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room_id: ROOM_ID,
|
|
|
|
sender: "@alice:example.org",
|
|
|
|
content: {
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "* edited body",
|
|
|
|
"m.new_content": {
|
|
|
|
msgtype: "m.text",
|
|
|
|
body: "edited body",
|
|
|
|
},
|
|
|
|
"m.relates_to": {
|
|
|
|
rel_type: "m.replace",
|
|
|
|
event_id: mxEvent.getId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
mxEvent.makeReplaced(editingEvent);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not highlight message where no version of message matches any push actions", () => {
|
|
|
|
const { container } = getComponent();
|
|
|
|
|
|
|
|
// get push actions for both events
|
|
|
|
expect(client.getPushActionsForEvent).toHaveBeenCalledWith(mxEvent);
|
|
|
|
expect(client.getPushActionsForEvent).toHaveBeenCalledWith(editingEvent);
|
|
|
|
expect(isHighlighted(container)).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`does not highlight when no version of message's push actions have a highlight tweak`, () => {
|
|
|
|
mocked(client.getPushActionsForEvent).mockReturnValue({ notify: true, tweaks: {} });
|
|
|
|
const { container } = getComponent();
|
|
|
|
|
|
|
|
expect(isHighlighted(container)).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`highlights when previous version of message's push actions have a highlight tweak`, () => {
|
|
|
|
mocked(client.getPushActionsForEvent).mockImplementation((event: MatrixEvent) => {
|
|
|
|
if (event === mxEvent) {
|
|
|
|
return { notify: true, tweaks: { [TweakName.Highlight]: true } };
|
|
|
|
}
|
|
|
|
return { notify: false, tweaks: {} };
|
|
|
|
});
|
|
|
|
const { container } = getComponent();
|
|
|
|
|
|
|
|
expect(isHighlighted(container)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`highlights when new version of message's push actions have a highlight tweak`, () => {
|
|
|
|
mocked(client.getPushActionsForEvent).mockImplementation((event: MatrixEvent) => {
|
|
|
|
if (event === editingEvent) {
|
|
|
|
return { notify: true, tweaks: { [TweakName.Highlight]: true } };
|
|
|
|
}
|
|
|
|
return { notify: false, tweaks: {} };
|
|
|
|
});
|
|
|
|
const { container } = getComponent();
|
|
|
|
|
|
|
|
expect(isHighlighted(container)).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2024-12-02 13:20:13 +03:00
|
|
|
|
|
|
|
it("should display the not encrypted status for an unencrypted event when the room becomes encrypted", async () => {
|
|
|
|
jest.spyOn(client.getCrypto()!, "getEncryptionInfoForEvent").mockResolvedValue({
|
|
|
|
shieldColour: EventShieldColour.NONE,
|
|
|
|
shieldReason: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
const { rerender } = getComponent();
|
|
|
|
await flushPromises();
|
|
|
|
// The room and the event are unencrypted, the tile should not show the not encrypted status
|
|
|
|
expect(screen.queryByText("Not encrypted")).toBeNull();
|
|
|
|
|
|
|
|
// The room is now encrypted
|
|
|
|
rerender(
|
|
|
|
<WrappedEventTile
|
|
|
|
roomContext={getRoomContext(room, {
|
|
|
|
isRoomEncrypted: true,
|
|
|
|
})}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
// The event tile should now show the not encrypted status
|
|
|
|
await waitFor(() => expect(screen.getByText("Not encrypted")).toBeInTheDocument());
|
|
|
|
});
|
2022-10-24 09:50:21 +03:00
|
|
|
});
|