Fix enabling key backup not working if there is an untrusted key backup (#30707)

* Fix enabling key backup not working if there is an untrusted key backup on the server.

* lint

* Add test for trust situations.

* remove conditional

* Update src/components/viewmodels/settings/encryption/KeyStoragePanelViewModel.ts

Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>

* Update src/components/viewmodels/settings/encryption/KeyStoragePanelViewModel.ts

Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>

---------

Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
This commit is contained in:
Will Hunt
2025-09-15 13:18:34 +01:00
committed by GitHub
parent 2b5dc7bfd5
commit 08487aa945
2 changed files with 63 additions and 7 deletions

View File

@@ -10,7 +10,7 @@ import { act } from "react";
import { mocked } from "jest-mock";
import type { MatrixClient } from "matrix-js-sdk/src/matrix";
import type { KeyBackupCheck, KeyBackupInfo } from "matrix-js-sdk/src/crypto-api";
import type { BackupTrustInfo, KeyBackupCheck, KeyBackupInfo } from "matrix-js-sdk/src/crypto-api";
import { useKeyStoragePanelViewModel } from "../../../../../../src/components/viewmodels/settings/encryption/KeyStoragePanelViewModel";
import { createTestClient, withClientContextRenderOptions } from "../../../../../test-utils";
@@ -49,8 +49,21 @@ describe("KeyStoragePanelViewModel", () => {
expect(mocked(matrixClient.getCrypto()!.resetKeyBackup)).toHaveBeenCalled();
});
it("should not call resetKeyBackup if there is a backup currently", async () => {
mocked(matrixClient.getCrypto()!.checkKeyBackupAndEnable).mockResolvedValue({} as KeyBackupCheck);
it.each<BackupTrustInfo>([
{ trusted: true, matchesDecryptionKey: false },
{ trusted: false, matchesDecryptionKey: true },
{ trusted: true, matchesDecryptionKey: true },
])("should not call resetKeyBackup if there is a backup currently and it is trusted", async (trustInfo) => {
mocked(matrixClient.getCrypto()!.checkKeyBackupAndEnable).mockResolvedValue({
backupInfo: {
version: "1",
algorithm: "foobar",
auth_data: {
public_key: "foobar",
},
},
trustInfo,
});
const { result } = renderHook(
() => useKeyStoragePanelViewModel(),
@@ -61,6 +74,30 @@ describe("KeyStoragePanelViewModel", () => {
expect(mocked(matrixClient.getCrypto()!.resetKeyBackup)).not.toHaveBeenCalled();
});
it("should call resetKeyBackup if there is a backup currently but it is not trusted", async () => {
mocked(matrixClient.getCrypto()!.checkKeyBackupAndEnable).mockResolvedValue({
backupInfo: {
version: "1",
algorithm: "foobar",
auth_data: {
public_key: "foobar",
},
},
trustInfo: {
trusted: false,
matchesDecryptionKey: false,
},
});
const { result } = renderHook(
() => useKeyStoragePanelViewModel(),
withClientContextRenderOptions(matrixClient),
);
await result.current.setEnabled(true);
expect(mocked(matrixClient.getCrypto()!.resetKeyBackup)).toHaveBeenCalled();
});
it("should set account data flag when enabling", async () => {
mocked(matrixClient.getCrypto()!.checkKeyBackupAndEnable).mockResolvedValue(null);