Prepare utils for local rooms (#9084)

* Prepare utils for local rooms

* Split up direct-messages module
This commit is contained in:
Michael Weimann
2022-07-25 10:17:40 +02:00
committed by GitHub
parent 0a8adb6bfe
commit c5eaeafe8e
20 changed files with 1016 additions and 220 deletions
+114
View File
@@ -0,0 +1,114 @@
/*
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.
*/
import { mocked } from "jest-mock";
import { EventType, KNOWN_SAFE_ROOM_VERSION, MatrixClient } from "matrix-js-sdk/src/matrix";
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";
jest.mock("../../../src/utils/rooms", () => ({
privateShouldBeEncrypted: jest.fn(),
}));
jest.mock("../../../src/createRoom", () => ({
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);
expect(room.getMyMembership()).toBe("join");
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
expect(room.getMember("@userId:matrix.org").membership).toBe("join");
targets.forEach((target: Member) => {
expect(room.getMember(target.userId).membership).toBe("join");
});
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);
});
it("should create an unencrypted room for 3PID targets", async () => {
const room = await createDmLocalRoom(mockClient, [member2]);
expect(mockClient.store.storeRoom).toHaveBeenCalledWith(room);
assertLocalRoom(room, [member2], false);
});
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);
});
});
});
+72
View File
@@ -0,0 +1,72 @@
/*
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.
*/
import { mocked } from "jest-mock";
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
import { DirectoryMember, ThreepidMember } from "../../../src/utils/direct-messages";
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
import DMRoomMap from "../../../src/utils/DMRoomMap";
import { createTestClient } from "../../test-utils";
import { findDMRoom } from "../../../src/utils/dm/findDMRoom";
import { findDMForUser } from "../../../src/utils/dm/findDMForUser";
jest.mock("../../../src/utils/dm/findDMForUser", () => ({
findDMForUser: jest.fn(),
}));
describe("findDMRoom", () => {
const userId1 = "@user1:example.com";
const member1 = new DirectoryMember({ user_id: userId1 });
const member2 = new ThreepidMember("user2");
let mockClient: MatrixClient;
let room1: Room;
let dmRoomMap: DMRoomMap;
beforeEach(() => {
mockClient = createTestClient();
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient);
room1 = new Room("!room1:example.com", mockClient, userId1);
room1.getMyMembership = () => "join";
dmRoomMap = {
getDMRoomForIdentifiers: jest.fn(),
getDMRoomsForUserId: jest.fn(),
} as unknown as DMRoomMap;
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
});
it("should return the room for a single target with a room", () => {
mocked(findDMForUser).mockReturnValue(room1);
expect(findDMRoom(mockClient, [member1])).toBe(room1);
});
it("should return null for a single target without a room", () => {
mocked(findDMForUser).mockReturnValue(null);
expect(findDMRoom(mockClient, [member1])).toBeNull();
});
it("should return the room for 2 targets with a room", () => {
mocked(dmRoomMap.getDMRoomForIdentifiers).mockReturnValue(room1);
expect(findDMRoom(mockClient, [member1, member2])).toBe(room1);
});
it("should return null for 2 targets without a room", () => {
mocked(dmRoomMap.getDMRoomForIdentifiers).mockReturnValue(null);
expect(findDMRoom(mockClient, [member1, member2])).toBeNull();
});
});
+70
View File
@@ -0,0 +1,70 @@
/*
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.
*/
import { mocked } from "jest-mock";
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
import { DirectoryMember, ThreepidMember } from "../../../src/utils/direct-messages";
import { findDMForUser } from "../../../src/utils/dm/findDMForUser";
import { findDMRoom } from "../../../src/utils/dm/findDMRoom";
import DMRoomMap from "../../../src/utils/DMRoomMap";
import { createTestClient } from "../../test-utils";
jest.mock("../../../src/utils/dm/findDMForUser", () => ({
findDMForUser: jest.fn(),
}));
describe("findDMRoom", () => {
const userId1 = "@user1:example.com";
const member1 = new DirectoryMember({ user_id: userId1 });
const member2 = new ThreepidMember("user2");
let mockClient: MatrixClient;
let room1: Room;
let dmRoomMap: DMRoomMap;
beforeEach(() => {
mockClient = createTestClient();
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient);
room1 = new Room("!room1:example.com", mockClient, userId1);
dmRoomMap = {
getDMRoomForIdentifiers: jest.fn(),
getDMRoomsForUserId: jest.fn(),
} as unknown as DMRoomMap;
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
});
it("should return the room for a single target with a room", () => {
mocked(findDMForUser).mockReturnValue(room1);
expect(findDMRoom(mockClient, [member1])).toBe(room1);
});
it("should return null for a single target without a room", () => {
mocked(findDMForUser).mockReturnValue(null);
expect(findDMRoom(mockClient, [member1])).toBeNull();
});
it("should return the room for 2 targets with a room", () => {
mocked(dmRoomMap.getDMRoomForIdentifiers).mockReturnValue(room1);
expect(findDMRoom(mockClient, [member1, member2])).toBe(room1);
});
it("should return null for 2 targets without a room", () => {
mocked(dmRoomMap.getDMRoomForIdentifiers).mockReturnValue(null);
expect(findDMRoom(mockClient, [member1, member2])).toBeNull();
});
});