From d1928079ccc020588dc3f73c1eda579d7845a5c2 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Fri, 9 Feb 2024 13:13:26 +0000 Subject: [PATCH] Use the non-deprecated importRoomKeys method on CryptoApi (#12232) * Use the non-deprecated importRoomKeys method on CryptoApi * Assert that we have crypto in import dialog --- .../dialogs/security/ImportE2eKeysDialog.tsx | 2 +- .../security/ImportE2eKeysDialog-test.tsx | 30 ++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/async-components/views/dialogs/security/ImportE2eKeysDialog.tsx b/src/async-components/views/dialogs/security/ImportE2eKeysDialog.tsx index 42ba0b7c6b..f10974ee04 100644 --- a/src/async-components/views/dialogs/security/ImportE2eKeysDialog.tsx +++ b/src/async-components/views/dialogs/security/ImportE2eKeysDialog.tsx @@ -108,7 +108,7 @@ export default class ImportE2eKeysDialog extends React.Component return MegolmExportEncryption.decryptMegolmKeyFile(arrayBuffer, passphrase); }) .then((keys) => { - return this.props.matrixClient.importRoomKeys(JSON.parse(keys)); + return this.props.matrixClient.getCrypto()!.importRoomKeys(JSON.parse(keys)); }) .then(() => { // TODO: it would probably be nice to give some feedback about what we've imported here. diff --git a/test/components/views/dialogs/security/ImportE2eKeysDialog-test.tsx b/test/components/views/dialogs/security/ImportE2eKeysDialog-test.tsx index 86ccb7b154..99e61abf61 100644 --- a/test/components/views/dialogs/security/ImportE2eKeysDialog-test.tsx +++ b/test/components/views/dialogs/security/ImportE2eKeysDialog-test.tsx @@ -15,10 +15,12 @@ limitations under the License. */ import React from "react"; -import { fireEvent, render } from "@testing-library/react"; +import { fireEvent, render, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; +import { CryptoApi } from "matrix-js-sdk/src/matrix"; import ImportE2eKeysDialog from "../../../../../src/async-components/views/dialogs/security/ImportE2eKeysDialog"; +import * as MegolmExportEncryption from "../../../../../src/utils/MegolmExportEncryption"; import { createTestClient } from "../../../../test-utils"; describe("ImportE2eKeysDialog", () => { @@ -64,4 +66,30 @@ describe("ImportE2eKeysDialog", () => { await userEvent.paste("passphrase"); expect(container.querySelector("[type=submit]")!).toBeEnabled(); }); + + it("should import exported keys on submit", async () => { + const cli = createTestClient(); + const onFinished = jest.fn(); + const file = new File(["test"], "file.txt", { type: "text/plain" }); + const importRoomKeys = jest.fn(); + cli.getCrypto = () => { + return { + importRoomKeys, + } as unknown as CryptoApi; + }; + + // Mock the result of decrypting the sessions, to avoid needing to + // create encrypted input data. + jest.spyOn(MegolmExportEncryption, "decryptMegolmKeyFile").mockResolvedValue("[]"); + + const { container } = render(); + fireEvent.change(container.querySelector("[type=file]")!, { + target: { files: [file] }, + }); + await userEvent.click(container.querySelector("[type=password]")!); + await userEvent.paste("passphrase"); + fireEvent.click(container.querySelector("[type=submit]")!); + + await waitFor(() => expect(importRoomKeys).toHaveBeenCalled()); + }); });