2022-07-25 08:17:40 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-07-25 08:17:40 +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-25 08:17:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { mocked } from "jest-mock";
|
2024-03-18 14:40:52 +00:00
|
|
|
import { EventType, KNOWN_SAFE_ROOM_VERSION, MatrixClient } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2022-07-25 08:17:40 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import { canEncryptToAllUsers } from "../../../../src/createRoom";
|
|
|
|
import { LocalRoom, LOCAL_ROOM_ID_PREFIX } from "../../../../src/models/LocalRoom";
|
|
|
|
import { DirectoryMember, Member, ThreepidMember } from "../../../../src/utils/direct-messages";
|
|
|
|
import { createDmLocalRoom } from "../../../../src/utils/dm/createDmLocalRoom";
|
|
|
|
import { privateShouldBeEncrypted } from "../../../../src/utils/rooms";
|
|
|
|
import { createTestClient } from "../../../test-utils";
|
2022-07-25 08:17:40 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
jest.mock("../../../../src/utils/rooms", () => ({
|
2022-07-25 08:17:40 +00:00
|
|
|
privateShouldBeEncrypted: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
jest.mock("../../../../src/createRoom", () => ({
|
2022-07-25 08:17:40 +00:00
|
|
|
canEncryptToAllUsers: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
function assertLocalRoom(room: LocalRoom, targets: Member[], encrypted: boolean) {
|
|
|
|
expect(room.roomId).toBe(LOCAL_ROOM_ID_PREFIX + "t1");
|
|
|
|
expect(room.name).toBe(targets.length ? targets[0].name : "Empty Room");
|
|
|
|
expect(room.encrypted).toBe(encrypted);
|
|
|
|
expect(room.targets).toEqual(targets);
|
2024-03-12 14:52:54 +00:00
|
|
|
expect(room.getMyMembership()).toBe(KnownMembership.Join);
|
2022-07-25 08:17:40 +00:00
|
|
|
|
|
|
|
const roomCreateEvent = room.currentState.getStateEvents(EventType.RoomCreate)[0];
|
|
|
|
expect(roomCreateEvent).toBeDefined();
|
|
|
|
expect(roomCreateEvent.getContent()["room_version"]).toBe(KNOWN_SAFE_ROOM_VERSION);
|
|
|
|
|
|
|
|
// check that the user and all targets are joined
|
2024-03-12 14:52:54 +00:00
|
|
|
expect(room.getMember("@userId:matrix.org")?.membership).toBe(KnownMembership.Join);
|
2022-07-25 08:17:40 +00:00
|
|
|
targets.forEach((target: Member) => {
|
2024-03-12 14:52:54 +00:00
|
|
|
expect(room.getMember(target.userId)?.membership).toBe(KnownMembership.Join);
|
2022-07-25 08:17:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (encrypted) {
|
|
|
|
const encryptionEvent = room.currentState.getStateEvents(EventType.RoomEncryption)[0];
|
|
|
|
expect(encryptionEvent).toBeDefined();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("createDmLocalRoom", () => {
|
|
|
|
let mockClient: MatrixClient;
|
|
|
|
const userId1 = "@user1:example.com";
|
|
|
|
const member1 = new DirectoryMember({ user_id: userId1 });
|
|
|
|
const member2 = new ThreepidMember("user2");
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockClient = createTestClient();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when rooms should be encrypted", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(privateShouldBeEncrypted).mockReturnValue(true);
|
|
|
|
});
|
|
|
|
|
2023-03-06 11:08:04 +00:00
|
|
|
it("should create an encrytped room for 3PID targets", async () => {
|
2022-07-25 08:17:40 +00:00
|
|
|
const room = await createDmLocalRoom(mockClient, [member2]);
|
|
|
|
expect(mockClient.store.storeRoom).toHaveBeenCalledWith(room);
|
2023-03-06 11:08:04 +00:00
|
|
|
assertLocalRoom(room, [member2], true);
|
2022-07-25 08:17:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("for MXID targets with encryption available", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(canEncryptToAllUsers).mockResolvedValue(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should create an encrypted room", async () => {
|
|
|
|
const room = await createDmLocalRoom(mockClient, [member1]);
|
|
|
|
expect(mockClient.store.storeRoom).toHaveBeenCalledWith(room);
|
|
|
|
assertLocalRoom(room, [member1], true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("for MXID targets with encryption unavailable", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(canEncryptToAllUsers).mockResolvedValue(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should create an unencrypted room", async () => {
|
|
|
|
const room = await createDmLocalRoom(mockClient, [member1]);
|
|
|
|
expect(mockClient.store.storeRoom).toHaveBeenCalledWith(room);
|
|
|
|
assertLocalRoom(room, [member1], false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("if rooms should not be encrypted", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(privateShouldBeEncrypted).mockReturnValue(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should create an unencrypted room", async () => {
|
|
|
|
const room = await createDmLocalRoom(mockClient, [member1]);
|
|
|
|
assertLocalRoom(room, [member1], false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|