From bb8a894105d068c27c0d5a92b4c224ffe146e6ce Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Mon, 9 Oct 2023 15:23:45 +0200 Subject: [PATCH] Call `RustBackupManager.checkKeyBackupAndEnable` when `RustCrypto` is created (#3784) --- spec/unit/rust-crypto/rust-crypto.spec.ts | 21 +++++++++++++++++---- src/rust-crypto/rust-crypto.ts | 3 +++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/spec/unit/rust-crypto/rust-crypto.spec.ts b/spec/unit/rust-crypto/rust-crypto.spec.ts index d0b6383b0..6bc671f75 100644 --- a/spec/unit/rust-crypto/rust-crypto.spec.ts +++ b/spec/unit/rust-crypto/rust-crypto.spec.ts @@ -44,6 +44,7 @@ import { EventShieldColour, EventShieldReason, ImportRoomKeysOpts, + KeyBackupCheck, VerificationRequest, } from "../../../src/crypto-api"; import * as testData from "../../test-utils/test-data"; @@ -864,8 +865,23 @@ describe("RustCrypto", () => { }); }); - describe("onUserIdentityUpdated", () => { + describe("key backup", () => { + it("is started when rust crypto is created", async () => { + // `RustCrypto.checkKeyBackupAndEnable` async call is made in background in the RustCrypto constructor. + // We don't have an instance of the rust crypto yet, we spy directly in the prototype. + const spyCheckKeyBackupAndEnable = jest + .spyOn(RustCrypto.prototype, "checkKeyBackupAndEnable") + .mockResolvedValue({} as KeyBackupCheck); + + await makeTestRustCrypto(); + + expect(spyCheckKeyBackupAndEnable).toHaveBeenCalled(); + }); + it("raises KeyBackupStatus event when identify change", async () => { + // Return the key backup + fetchMock.get("path:/_matrix/client/v3/room_keys/version", testData.SIGNED_BACKUP_DATA); + const mockHttpApi = new MatrixHttpApi(new TypedEventEmitter(), { baseUrl: "http://server/", prefix: "", @@ -890,9 +906,6 @@ describe("RustCrypto", () => { {} as CryptoCallbacks, ); - // Return the key backup - fetchMock.get("path:/_matrix/client/v3/room_keys/version", testData.SIGNED_BACKUP_DATA); - // Wait for the key backup to be available const keyBackupStatusPromise = new Promise((resolve) => rustCrypto.once(CryptoEvent.KeyBackupStatus, resolve), diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index 22f6f70be..8e53941c4 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -153,6 +153,9 @@ export class RustCrypto extends TypedEventEmitter