From a597da26a015524ca4ce5804f07263d1d24f19b1 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Fri, 12 May 2023 16:55:01 +0100 Subject: [PATCH] DeviceListener: replace calls to deprecated APIs (#10883) * DeviceListener: replace calls to deprecated APIs * fix tests --- src/DeviceListener.ts | 20 ++++++++++---------- test/DeviceListener-test.ts | 36 ++++++++++++++++-------------------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/DeviceListener.ts b/src/DeviceListener.ts index ff16a8237a..b1b3208230 100644 --- a/src/DeviceListener.ts +++ b/src/DeviceListener.ts @@ -267,14 +267,16 @@ export default class DeviceListener { // cross-signing support was added to Matrix in MSC1756, which landed in spec v1.1 if (!(await cli.isVersionSupported("v1.1"))) return; - if (!cli.isCryptoEnabled()) return; + const crypto = cli.getCrypto(); + if (!crypto) return; + // don't recheck until the initial sync is complete: lots of account data events will fire // while the initial sync is processing and we don't need to recheck on each one of them // (we add a listener on sync to do once check after the initial sync is done) if (!cli.isInitialSyncComplete()) return; - const crossSigningReady = await cli.isCrossSigningReady(); - const secretStorageReady = await cli.isSecretStorageReady(); + const crossSigningReady = await crypto.isCrossSigningReady(); + const secretStorageReady = await crypto.isSecretStorageReady(); const allSystemsReady = crossSigningReady && secretStorageReady; if (this.dismissedThisDeviceToast || allSystemsReady) { @@ -283,7 +285,8 @@ export default class DeviceListener { this.checkKeyBackupStatus(); } else if (this.shouldShowSetupEncryptionToast()) { // make sure our keys are finished downloading - await cli.downloadKeys([cli.getUserId()!]); + await crypto.getUserDeviceInfo([cli.getUserId()!]); + // cross signing isn't enabled - nag to enable it // There are 3 different toasts for: if (!cli.getCrossSigningId() && cli.getStoredCrossSigningForUser(cli.getUserId()!)) { @@ -310,7 +313,7 @@ export default class DeviceListener { } } - // This needs to be done after awaiting on downloadKeys() above, so + // This needs to be done after awaiting on getUserDeviceInfo() above, so // we make sure we get the devices after the fetch is done. await this.ensureDeviceIdsAtStartPopulated(); @@ -324,10 +327,7 @@ export default class DeviceListener { const isCurrentDeviceTrusted = crossSigningReady && - Boolean( - (await cli.getCrypto()?.getDeviceVerificationStatus(cli.getUserId()!, cli.deviceId!)) - ?.crossSigningVerified, - ); + Boolean((await crypto.getDeviceVerificationStatus(cli.getUserId()!, cli.deviceId!))?.crossSigningVerified); // as long as cross-signing isn't ready, // you can't see or dismiss any device toasts @@ -336,7 +336,7 @@ export default class DeviceListener { for (const deviceId of devices) { if (deviceId === cli.deviceId) continue; - const deviceTrust = await cli.getCrypto()!.getDeviceVerificationStatus(cli.getUserId()!, deviceId); + const deviceTrust = await crypto.getDeviceVerificationStatus(cli.getUserId()!, deviceId); if (!deviceTrust?.crossSigningVerified && !this.dismissed.has(deviceId)) { if (this.ourDeviceIdsAtStart?.has(deviceId)) { oldUnverifiedDeviceIds.add(deviceId); diff --git a/test/DeviceListener-test.ts b/test/DeviceListener-test.ts index b0b5ea22dc..95214ef006 100644 --- a/test/DeviceListener-test.ts +++ b/test/DeviceListener-test.ts @@ -81,6 +81,8 @@ describe("DeviceListener", () => { crossSigningVerified: false, }), getUserDeviceInfo: jest.fn().mockResolvedValue(new Map()), + isCrossSigningReady: jest.fn().mockResolvedValue(true), + isSecretStorageReady: jest.fn().mockResolvedValue(true), } as unknown as Mocked; mockClient = getMockClientWithEventEmitter({ isGuest: jest.fn(), @@ -89,15 +91,11 @@ describe("DeviceListener", () => { getKeyBackupVersion: jest.fn().mockResolvedValue(undefined), getRooms: jest.fn().mockReturnValue([]), isVersionSupported: jest.fn().mockResolvedValue(true), - isCrossSigningReady: jest.fn().mockResolvedValue(true), - isSecretStorageReady: jest.fn().mockResolvedValue(true), - isCryptoEnabled: jest.fn().mockReturnValue(true), isInitialSyncComplete: jest.fn().mockReturnValue(true), getKeyBackupEnabled: jest.fn(), getCrossSigningId: jest.fn(), getStoredCrossSigningForUser: jest.fn(), waitForClientWellKnown: jest.fn(), - downloadKeys: jest.fn(), isRoomEncrypted: jest.fn(), getClientWellKnown: jest.fn(), getDeviceId: jest.fn().mockReturnValue(deviceId), @@ -245,34 +243,34 @@ describe("DeviceListener", () => { await createAndStart(); expect(mockClient!.isVersionSupported).toHaveBeenCalledWith("v1.1"); - expect(mockClient!.isCrossSigningReady).not.toHaveBeenCalled(); + expect(mockCrypto!.isCrossSigningReady).not.toHaveBeenCalled(); }); it("does nothing when crypto is not enabled", async () => { - mockClient!.isCryptoEnabled.mockReturnValue(false); + mockClient!.getCrypto.mockReturnValue(undefined); await createAndStart(); - expect(mockClient!.isCrossSigningReady).not.toHaveBeenCalled(); + expect(mockCrypto!.isCrossSigningReady).not.toHaveBeenCalled(); }); it("does nothing when initial sync is not complete", async () => { mockClient!.isInitialSyncComplete.mockReturnValue(false); await createAndStart(); - expect(mockClient!.isCrossSigningReady).not.toHaveBeenCalled(); + expect(mockCrypto!.isCrossSigningReady).not.toHaveBeenCalled(); }); describe("set up encryption", () => { const rooms = [{ roomId: "!room1" }, { roomId: "!room2" }] as unknown as Room[]; beforeEach(() => { - mockClient!.isCrossSigningReady.mockResolvedValue(false); - mockClient!.isSecretStorageReady.mockResolvedValue(false); + mockCrypto!.isCrossSigningReady.mockResolvedValue(false); + mockCrypto!.isSecretStorageReady.mockResolvedValue(false); mockClient!.getRooms.mockReturnValue(rooms); mockClient!.isRoomEncrypted.mockReturnValue(true); }); it("hides setup encryption toast when cross signing and secret storage are ready", async () => { - mockClient!.isCrossSigningReady.mockResolvedValue(true); - mockClient!.isSecretStorageReady.mockResolvedValue(true); + mockCrypto!.isCrossSigningReady.mockResolvedValue(true); + mockCrypto!.isSecretStorageReady.mockResolvedValue(true); await createAndStart(); expect(SetupEncryptionToast.hideToast).toHaveBeenCalled(); }); @@ -284,19 +282,17 @@ describe("DeviceListener", () => { expect(SetupEncryptionToast.hideToast).toHaveBeenCalled(); }); - it("does not do any checks or show any toasts when secret storage is being accessed", async () => { + it("does not show any toasts when secret storage is being accessed", async () => { mocked(isSecretStorageBeingAccessed).mockReturnValue(true); await createAndStart(); - expect(mockClient!.downloadKeys).not.toHaveBeenCalled(); expect(SetupEncryptionToast.showToast).not.toHaveBeenCalled(); }); - it("does not do any checks or show any toasts when no rooms are encrypted", async () => { + it("does not show any toasts when no rooms are encrypted", async () => { mockClient!.isRoomEncrypted.mockReturnValue(false); await createAndStart(); - expect(mockClient!.downloadKeys).not.toHaveBeenCalled(); expect(SetupEncryptionToast.showToast).not.toHaveBeenCalled(); }); @@ -309,7 +305,7 @@ describe("DeviceListener", () => { mockClient!.getStoredCrossSigningForUser.mockReturnValue(new CrossSigningInfo(userId)); await createAndStart(); - expect(mockClient!.downloadKeys).toHaveBeenCalled(); + expect(mockCrypto!.getUserDeviceInfo).toHaveBeenCalled(); expect(SetupEncryptionToast.showToast).toHaveBeenCalledWith( SetupEncryptionToast.Kind.VERIFY_THIS_SESSION, ); @@ -350,7 +346,7 @@ describe("DeviceListener", () => { }); it("checks keybackup status when setup encryption toast has been dismissed", async () => { - mockClient!.isCrossSigningReady.mockResolvedValue(false); + mockCrypto!.isCrossSigningReady.mockResolvedValue(false); const instance = await createAndStart(); instance.dismissEncryptionSetup(); @@ -402,7 +398,7 @@ describe("DeviceListener", () => { const deviceTrustUnverified = new DeviceVerificationStatus({}); beforeEach(() => { - mockClient!.isCrossSigningReady.mockResolvedValue(true); + mockCrypto!.isCrossSigningReady.mockResolvedValue(true); mockCrypto!.getUserDeviceInfo.mockResolvedValue( new Map([[userId, new Map([currentDevice, device2, device3].map((d) => [d.deviceId, d]))]]), ); @@ -415,7 +411,7 @@ describe("DeviceListener", () => { }); describe("bulk unverified sessions toasts", () => { it("hides toast when cross signing is not ready", async () => { - mockClient!.isCrossSigningReady.mockResolvedValue(false); + mockCrypto!.isCrossSigningReady.mockResolvedValue(false); await createAndStart(); expect(BulkUnverifiedSessionsToast.hideToast).toHaveBeenCalled(); expect(BulkUnverifiedSessionsToast.showToast).not.toHaveBeenCalled();