Files
element-web/test/unit-tests/components/views/settings/encryption/RecoveryPanel-test.tsx
Florian Duros 13913ba8b2 Add Recovery section in the new user settings Encryption tab (#28673)
* Refine `SettingsSection` & `SettingsTab`

* Add encryption tab

* Add recovery section

* Add device verification

* Rename `Panel` into `State`

* Update & add tests to user settings common

* Add tests to `RecoveryPanel`

* Add tests to `ChangeRecoveryKey`

* Update CreateSecretStorageDialog-test snapshot

* Add tests to `EncryptionUserSettingsTab`

* Update existing screenshots of e2e tests

* Add new encryption tab ownership to `@element-hq/element-crypto-web-reviewers`

* Add e2e tests

* Fix monospace font and add figma link to hardcoded value

* Add unit to Icon

* Improve e2e doc

* Assert that the crypto module is defined

* Add classname doc

* Fix typo

* Use `good` state instead of default

* Rename `ChangeRecoveryKey.isSetupFlow` into `ChangeRecoveryKey.userHasKeyBackup`

* Move `deleteCachedSecrets` fixture in `recovery.spec.ts`

* Use one callback instead of two in `RecoveryPanel`

* Fix docs and naming of `utils.createBot`

* Fix typo in `RecoveryPanel`

* Add more doc to the state of the `EncryptionUserSettingsTab`

* Rename `verification_required` into `set_up_encryption`

* Update test

* ADd new license

* Update comments and doc

* Assert that `recoveryKey.encodedPrivateKey` is always defined

* Add comments to explain how the secrets could be uncached

* Use `matrixClient.secretStorage.getDefaultKeyId` instead of `matrixClient.getCrypto().checkKeyBackupAndEnable` to know if we need to set up a recovery key

* Update existing screenshot to add encryption tab.

* Update tests

* Use new labels when changing the recovery key

* Fix docs

* Don't reset key backup when creating a recovery key

* Fix doc
2025-01-15 12:44:20 +00:00

93 lines
3.8 KiB
TypeScript

/*
* Copyright 2024 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
import React from "react";
import { MatrixClient } from "matrix-js-sdk/src/matrix";
import { render, screen } from "jest-matrix-react";
import { waitFor } from "@testing-library/dom";
import userEvent from "@testing-library/user-event";
import { mocked } from "jest-mock";
import { createTestClient, withClientContextRenderOptions } from "../../../../../test-utils";
import { RecoveryPanel } from "../../../../../../src/components/views/settings/encryption/RecoveryPanel";
import { accessSecretStorage } from "../../../../../../src/SecurityManager";
jest.mock("../../../../../../src/SecurityManager", () => ({
accessSecretStorage: jest.fn(),
}));
describe("<RecoveryPanel />", () => {
let matrixClient: MatrixClient;
beforeEach(() => {
matrixClient = createTestClient();
mocked(accessSecretStorage).mockClear().mockResolvedValue();
});
function renderRecoverPanel(onChangeRecoveryKeyClick = jest.fn()) {
return render(
<RecoveryPanel onChangeRecoveryKeyClick={onChangeRecoveryKeyClick} />,
withClientContextRenderOptions(matrixClient),
);
}
it("should be in loading state when checking the recovery key and the cached keys", () => {
jest.spyOn(matrixClient.secretStorage, "getDefaultKeyId").mockImplementation(() => new Promise(() => {}));
const { asFragment } = renderRecoverPanel();
expect(screen.getByLabelText("Loading…")).toBeInTheDocument();
expect(asFragment()).toMatchSnapshot();
});
it("should ask to set up a recovery key when there is no recovery key", async () => {
const user = userEvent.setup();
const onChangeRecoveryKeyClick = jest.fn();
const { asFragment } = renderRecoverPanel(onChangeRecoveryKeyClick);
await waitFor(() => screen.getByRole("button", { name: "Set up recovery" }));
expect(asFragment()).toMatchSnapshot();
await user.click(screen.getByRole("button", { name: "Set up recovery" }));
expect(onChangeRecoveryKeyClick).toHaveBeenCalledWith(true);
});
it("should ask to enter the recovery key when secrets are not cached", async () => {
jest.spyOn(matrixClient.secretStorage, "getDefaultKeyId").mockResolvedValue("default key");
const user = userEvent.setup();
const { asFragment } = renderRecoverPanel();
await waitFor(() => screen.getByRole("button", { name: "Enter recovery key" }));
expect(asFragment()).toMatchSnapshot();
await user.click(screen.getByRole("button", { name: "Enter recovery key" }));
expect(accessSecretStorage).toHaveBeenCalled();
});
it("should allow to change the recovery key when everything is good", async () => {
jest.spyOn(matrixClient.secretStorage, "getDefaultKeyId").mockResolvedValue("default key");
jest.spyOn(matrixClient.getCrypto()!, "getCrossSigningStatus").mockResolvedValue({
privateKeysInSecretStorage: true,
publicKeysOnDevice: true,
privateKeysCachedLocally: {
masterKey: true,
selfSigningKey: true,
userSigningKey: true,
},
});
const user = userEvent.setup();
const onChangeRecoveryKeyClick = jest.fn();
const { asFragment } = renderRecoverPanel(onChangeRecoveryKeyClick);
await waitFor(() => screen.getByRole("button", { name: "Change recovery key" }));
expect(asFragment()).toMatchSnapshot();
await user.click(screen.getByRole("button", { name: "Change recovery key" }));
expect(onChangeRecoveryKeyClick).toHaveBeenCalledWith(false);
});
});