|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { mocked } from "jest-mock"; |
| 18 | +import { MatrixClient } from "matrix-js-sdk/src/matrix"; |
| 19 | +import { IDevice } from "matrix-js-sdk/src/crypto/deviceinfo"; |
| 20 | +import { RoomType } from "matrix-js-sdk/src/@types/event"; |
| 21 | + |
| 22 | +import { stubClient, setupAsyncStoreWithClient } from "./test-utils"; |
| 23 | +import { MatrixClientPeg } from "../src/MatrixClientPeg"; |
| 24 | +import WidgetStore from "../src/stores/WidgetStore"; |
| 25 | +import WidgetUtils from "../src/utils/WidgetUtils"; |
| 26 | +import { VIDEO_CHANNEL, VIDEO_CHANNEL_MEMBER } from "../src/utils/VideoChannelUtils"; |
| 27 | +import createRoom, { canEncryptToAllUsers } from '../src/createRoom'; |
| 28 | + |
| 29 | +describe("createRoom", () => { |
| 30 | + let client: MatrixClient; |
| 31 | + beforeEach(() => { |
| 32 | + stubClient(); |
| 33 | + client = MatrixClientPeg.get(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("sets up video rooms correctly", async () => { |
| 37 | + setupAsyncStoreWithClient(WidgetStore.instance, client); |
| 38 | + jest.spyOn(WidgetUtils, "waitForRoomWidget").mockResolvedValue(); |
| 39 | + |
| 40 | + const userId = client.getUserId(); |
| 41 | + const roomId = await createRoom({ roomType: RoomType.ElementVideo }); |
| 42 | + |
| 43 | + const [[{ |
| 44 | + power_level_content_override: { |
| 45 | + users: { |
| 46 | + [userId]: userPower, |
| 47 | + }, |
| 48 | + events: { |
| 49 | + "im.vector.modular.widgets": widgetPower, |
| 50 | + [VIDEO_CHANNEL_MEMBER]: videoMemberPower, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }]] = mocked(client.createRoom).mock.calls as any; |
| 54 | + const [[widgetRoomId, widgetStateKey, , widgetId]] = mocked(client.sendStateEvent).mock.calls; |
| 55 | + |
| 56 | + // We should have had enough power to be able to set up the Jitsi widget |
| 57 | + expect(userPower).toBeGreaterThanOrEqual(widgetPower); |
| 58 | + // and should have actually set it up |
| 59 | + expect(widgetRoomId).toEqual(roomId); |
| 60 | + expect(widgetStateKey).toEqual("im.vector.modular.widgets"); |
| 61 | + expect(widgetId).toEqual(VIDEO_CHANNEL); |
| 62 | + |
| 63 | + // All members should be able to update their connected devices |
| 64 | + expect(videoMemberPower).toEqual(0); |
| 65 | + // Jitsi widget should be immutable for admins |
| 66 | + expect(widgetPower).toBeGreaterThan(100); |
| 67 | + // and we should have been reset back to admin |
| 68 | + expect(client.setPowerLevel).toHaveBeenCalledWith(roomId, userId, 100, undefined); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe("canEncryptToAllUsers", () => { |
| 73 | + const trueUser = { |
| 74 | + "@goodUser:localhost": { |
| 75 | + "DEV1": {} as unknown as IDevice, |
| 76 | + "DEV2": {} as unknown as IDevice, |
| 77 | + }, |
| 78 | + }; |
| 79 | + const falseUser = { |
| 80 | + "@badUser:localhost": {}, |
| 81 | + }; |
| 82 | + |
| 83 | + let client: MatrixClient; |
| 84 | + beforeEach(() => { |
| 85 | + stubClient(); |
| 86 | + client = MatrixClientPeg.get(); |
| 87 | + }); |
| 88 | + |
| 89 | + it("returns true if all devices have crypto", async () => { |
| 90 | + mocked(client.downloadKeys).mockResolvedValue(trueUser); |
| 91 | + const response = await canEncryptToAllUsers(client, ["@goodUser:localhost"]); |
| 92 | + expect(response).toBe(true); |
| 93 | + }); |
| 94 | + |
| 95 | + it("returns false if not all users have crypto", async () => { |
| 96 | + mocked(client.downloadKeys).mockResolvedValue({ ...trueUser, ...falseUser }); |
| 97 | + const response = await canEncryptToAllUsers(client, ["@goodUser:localhost", "@badUser:localhost"]); |
| 98 | + expect(response).toBe(false); |
| 99 | + }); |
| 100 | +}); |
0 commit comments