Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Use the non-deprecated exportRoomKeys method on CryptoApi (#12231)
Browse files Browse the repository at this point in the history
* Expand the export test to check encryptMegolmKeyFile was called

* Use the non-deprecated exportRoomKeys method on CryptoApi
  • Loading branch information
andybalaam authored Feb 6, 2024
1 parent cdfcd37 commit a664172
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default class ExportE2eKeysDialog extends React.Component<IProps, IState>
// asynchronous ones.
Promise.resolve()
.then(() => {
return this.props.matrixClient.exportRoomKeys();
return this.props.matrixClient.getCrypto()!.exportRoomKeys();
})
.then((k) => {
return MegolmExportEncryption.encryptMegolmKeyFile(JSON.stringify(k), passphrase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
import React from "react";
import { screen, fireEvent, render, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { CryptoApi, IMegolmSessionData } from "matrix-js-sdk/src/matrix";

import * as MegolmExportEncryption from "../../../../../src/utils/MegolmExportEncryption";
import ExportE2eKeysDialog from "../../../../../src/async-components/views/dialogs/security/ExportE2eKeysDialog";
import { createTestClient } from "../../../../test-utils";

Expand Down Expand Up @@ -60,13 +62,32 @@ describe("ExportE2eKeysDialog", () => {
});

it("should export if everything is fine", async () => {
// Given a client able to export keys
const cli = createTestClient();
const onFinished = jest.fn();
const keys: IMegolmSessionData[] = [];
const passphrase = "ThisIsAMoreSecurePW123$$";
const exportRoomKeys = jest.fn().mockResolvedValue(keys);
cli.getCrypto = () => {
return {
exportRoomKeys,
} as unknown as CryptoApi;
};

const { container } = render(<ExportE2eKeysDialog matrixClient={cli} onFinished={onFinished} />);
await userEvent.type(screen.getByLabelText("Enter passphrase"), "ThisIsAMoreSecurePW123$$");
await userEvent.type(screen.getByLabelText("Confirm passphrase"), "ThisIsAMoreSecurePW123$$");
// Mock the result of encrypting the sessions. If we don't do this, the
// encryption process fails, possibly because we didn't initialise
// something.
jest.spyOn(MegolmExportEncryption, "encryptMegolmKeyFile").mockResolvedValue(new ArrayBuffer(3));

// When we tell the dialog to export
const { container } = render(<ExportE2eKeysDialog matrixClient={cli} onFinished={jest.fn()} />);
await userEvent.type(screen.getByLabelText("Enter passphrase"), passphrase);
await userEvent.type(screen.getByLabelText("Confirm passphrase"), passphrase);
fireEvent.click(container.querySelector("[type=submit]")!);
await waitFor(() => expect(cli.exportRoomKeys).toHaveBeenCalled());

// Then it exports keys and encrypts them
await waitFor(() => expect(exportRoomKeys).toHaveBeenCalled());
await waitFor(() =>
expect(MegolmExportEncryption.encryptMegolmKeyFile).toHaveBeenCalledWith(JSON.stringify(keys), passphrase),
);
});
});

0 comments on commit a664172

Please sign in to comment.