|
| 1 | +/* |
| 2 | +Copyright 2023 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, mocked } from "jest-mock"; |
| 18 | +import { Device, MatrixClient } from "matrix-js-sdk/src/matrix"; |
| 19 | + |
| 20 | +import { getDeviceCryptoInfo, getUserDeviceIds } from "../../../src/utils/crypto/deviceInfo"; |
| 21 | +import { getMockClientWithEventEmitter, mockClientMethodsCrypto } from "../../test-utils"; |
| 22 | + |
| 23 | +describe("getDeviceCryptoInfo()", () => { |
| 24 | + let mockClient: Mocked<MatrixClient>; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + mockClient = getMockClientWithEventEmitter({ ...mockClientMethodsCrypto() }); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should return undefined on clients with no crypto", async () => { |
| 31 | + jest.spyOn(mockClient, "getCrypto").mockReturnValue(undefined); |
| 32 | + await expect(getDeviceCryptoInfo(mockClient, "@user:id", "device_id")).resolves.toBeUndefined(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should return undefined for unknown users", async () => { |
| 36 | + mocked(mockClient.getCrypto()!.getUserDeviceInfo).mockResolvedValue(new Map()); |
| 37 | + await expect(getDeviceCryptoInfo(mockClient, "@user:id", "device_id")).resolves.toBeUndefined(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("should return undefined for unknown devices", async () => { |
| 41 | + mocked(mockClient.getCrypto()!.getUserDeviceInfo).mockResolvedValue(new Map([["@user:id", new Map()]])); |
| 42 | + await expect(getDeviceCryptoInfo(mockClient, "@user:id", "device_id")).resolves.toBeUndefined(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should return the right result for known devices", async () => { |
| 46 | + const mockDevice = { deviceId: "device_id" } as Device; |
| 47 | + mocked(mockClient.getCrypto()!.getUserDeviceInfo).mockResolvedValue( |
| 48 | + new Map([["@user:id", new Map([["device_id", mockDevice]])]]), |
| 49 | + ); |
| 50 | + await expect(getDeviceCryptoInfo(mockClient, "@user:id", "device_id")).resolves.toBe(mockDevice); |
| 51 | + expect(mockClient.getCrypto()!.getUserDeviceInfo).toHaveBeenCalledWith(["@user:id"], undefined); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe("getUserDeviceIds", () => { |
| 56 | + let mockClient: Mocked<MatrixClient>; |
| 57 | + |
| 58 | + beforeEach(() => { |
| 59 | + mockClient = getMockClientWithEventEmitter({ ...mockClientMethodsCrypto() }); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should return empty set on clients with no crypto", async () => { |
| 63 | + jest.spyOn(mockClient, "getCrypto").mockReturnValue(undefined); |
| 64 | + await expect(getUserDeviceIds(mockClient, "@user:id")).resolves.toEqual(new Set()); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should return empty set for unknown users", async () => { |
| 68 | + mocked(mockClient.getCrypto()!.getUserDeviceInfo).mockResolvedValue(new Map()); |
| 69 | + await expect(getUserDeviceIds(mockClient, "@user:id")).resolves.toEqual(new Set()); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should return the right result for known users", async () => { |
| 73 | + const mockDevice = { deviceId: "device_id" } as Device; |
| 74 | + mocked(mockClient.getCrypto()!.getUserDeviceInfo).mockResolvedValue( |
| 75 | + new Map([["@user:id", new Map([["device_id", mockDevice]])]]), |
| 76 | + ); |
| 77 | + await expect(getUserDeviceIds(mockClient, "@user:id")).resolves.toEqual(new Set(["device_id"])); |
| 78 | + expect(mockClient.getCrypto()!.getUserDeviceInfo).toHaveBeenCalledWith(["@user:id"]); |
| 79 | + }); |
| 80 | +}); |
0 commit comments