From 593cd3e489a652481d944773d1f0d9fc509a9dc4 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Mon, 20 Jan 2025 10:34:52 +0100 Subject: [PATCH] test(crypto api): Cleaner way to handle key backup removal --- spec/unit/rust-crypto/rust-crypto.spec.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/spec/unit/rust-crypto/rust-crypto.spec.ts b/spec/unit/rust-crypto/rust-crypto.spec.ts index bcbaaec7ac..632a39df98 100644 --- a/spec/unit/rust-crypto/rust-crypto.spec.ts +++ b/spec/unit/rust-crypto/rust-crypto.spec.ts @@ -1912,9 +1912,16 @@ describe("RustCrypto", () => { }); it("key backup should be re-enabled after reset", async () => { - fetchMock.get("path:/_matrix/client/v3/room_keys/version", testData.SIGNED_BACKUP_DATA); // When we will delete the key backup - fetchMock.delete("path:/_matrix/client/v3/room_keys/version/1", {}); + let backupIsDeleted = false; + fetchMock.delete("path:/_matrix/client/v3/room_keys/version/1", () => { + backupIsDeleted = true; + return {}; + }); + // If the backup is deleted, we will return an empty object + fetchMock.get("path:/_matrix/client/v3/room_keys/version", () => { + return backupIsDeleted ? {} : testData.SIGNED_BACKUP_DATA; + }); // We consider the key backup as trusted jest.spyOn(RustBackupManager.prototype, "isKeyBackupTrusted").mockResolvedValue({ @@ -1926,18 +1933,6 @@ describe("RustCrypto", () => { // We have a key backup expect(await rustCrypto.getActiveSessionBackupVersion()).not.toBeNull(); - let nbCalls = 0; - fetchMock.get( - "path:/_matrix/client/v3/room_keys/version", - () => { - // First call is when we check if the key backup is enabled. - // Second call is when we get the next backup after we deleted the first key backup, - // we return an empty object because we don't have a key backup anymore. - return ++nbCalls === 1 ? testData.SIGNED_BACKUP_DATA : {}; - }, - { overwriteRoutes: true }, - ); - // A new key backup should be created after the reset let newKeyBackupInfo!: KeyBackupInfo; fetchMock.post("path:/_matrix/client/v3/room_keys/version", (res, options) => {