Skip to content

Commit

Permalink
Migrating deprecated sync MatrixClient.getKeyBackupEnabled to async…
Browse files Browse the repository at this point in the history
… `MatrixClient.CryptoApi.getActiveSessionBackupVersion` in `MatrixChat`.
  • Loading branch information
florianduros committed Oct 18, 2024
1 parent c5a1f49 commit 2d95a09
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/components/structures/MatrixChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1631,8 +1631,12 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
cli.on(CryptoEvent.KeyBackupFailed, async (errcode): Promise<void> => {
let haveNewVersion: boolean | undefined;
let newVersionInfo: KeyBackupInfo | null = null;
const keyBackupEnabled = Boolean(
cli.getCrypto() && (await cli.getCrypto()?.getActiveSessionBackupVersion()) !== null,
);

// if key backup is still enabled, there must be a new backup in place
if (cli.getKeyBackupEnabled()) {
if (keyBackupEnabled) {
haveNewVersion = true;
} else {
// otherwise check the server to see if there's a new one
Expand All @@ -1650,7 +1654,6 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
import(
"../../async-components/views/dialogs/security/NewRecoveryMethodDialog"
) as unknown as Promise<typeof NewRecoveryMethodDialog>,
{ newVersionInfo: newVersionInfo! },
);
} else {
Modal.createDialogAsync(
Expand Down
21 changes: 20 additions & 1 deletion test/unit-tests/components/structures/MatrixChat-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { logger } from "matrix-js-sdk/src/logger";
import { OidcError } from "matrix-js-sdk/src/oidc/error";
import { BearerTokenResponse } from "matrix-js-sdk/src/oidc/validate";
import { defer, IDeferred, sleep } from "matrix-js-sdk/src/utils";
import { UserVerificationStatus } from "matrix-js-sdk/src/crypto-api";
import { CryptoEvent, UserVerificationStatus } from "matrix-js-sdk/src/crypto-api";

import MatrixChat from "../../../../src/components/structures/MatrixChat";
import * as StorageAccess from "../../../../src/utils/StorageAccess";
Expand Down Expand Up @@ -135,6 +135,7 @@ describe("<MatrixChat />", () => {
getVersion: jest.fn().mockReturnValue("1"),
setDeviceIsolationMode: jest.fn(),
userHasCrossSigningKeys: jest.fn(),
getActiveSessionBackupVersion: jest.fn().mockResolvedValue(null),
}),
// This needs to not finish immediately because we need to test the screen appears
bootstrapCrossSigning: jest.fn().mockImplementation(() => bootstrapDeferred.promise),
Expand Down Expand Up @@ -1515,4 +1516,22 @@ describe("<MatrixChat />", () => {
expect(screen.getByTestId("mobile-register")).toBeInTheDocument();
});
});

describe("when key backup failed", () => {
it("should show the new recovery method dialog", async () => {
jest.mock("../../../../src/async-components/views/dialogs/security/NewRecoveryMethodDialog", () => ({
__esModule: true,
default: () => <span>mocked dialog</span>,
}));
jest.spyOn(mockClient.getCrypto()!, "getActiveSessionBackupVersion").mockResolvedValue("version");

getComponent({});
defaultDispatcher.dispatch({
action: "will_start_client",
});
await flushPromises();
mockClient.emit(CryptoEvent.KeyBackupFailed, "error code");
await waitFor(() => expect(screen.getByText("mocked dialog")).toBeInTheDocument());
});
});
});

0 comments on commit 2d95a09

Please sign in to comment.