Handle cross-signing keys missing locally and/or from secret storage (#31367)
* show correct toast when cross-signing keys missing If cross-signing keys are missing both locally and in 4S, show a new toast saying that identity needs resetting, rather than saying that the device needs to be verified. * refactor: make DeviceListener in charge of device state - move enum from SetupEncryptionToast to DeviceListener - DeviceListener has public method to get device state - DeviceListener emits events to update device state * reset key backup when needed in RecoveryPanelOutOfSync brings RecoveryPanelOutOfSync in line with SetupEncryptionToast behaviour * update strings to agree with designs from Figma * use DeviceListener to determine EncryptionUserSettingsTab display rather than using its own logic * prompt to reset identity in Encryption Settings when needed * fix type * calculate device state even if we aren't going to show a toast * update snapshot * make logs more accurate * add tests * make the bot use a different access token/device * only log in a new session when requested * Mark properties as read-only Co-authored-by: Skye Elliot <actuallyori@gmail.com> * remove some duplicate strings * make accessToken optional instead of using empty string * switch from enum to string union as per review * apply other changes from review * handle errors in accessSecretStorage * remove incorrect testid --------- Co-authored-by: Skye Elliot <actuallyori@gmail.com>
This commit is contained in:
@@ -9,19 +9,45 @@ import React from "react";
|
||||
import { render, screen } from "jest-matrix-react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { mocked } from "jest-mock";
|
||||
import { type MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import { RecoveryPanelOutOfSync } from "../../../../../../src/components/views/settings/encryption/RecoveryPanelOutOfSync";
|
||||
import { accessSecretStorage } from "../../../../../../src/SecurityManager";
|
||||
import { AccessCancelledError, accessSecretStorage } from "../../../../../../src/SecurityManager";
|
||||
import DeviceListener from "../../../../../../src/DeviceListener";
|
||||
import { createTestClient, withClientContextRenderOptions } from "../../../../../test-utils";
|
||||
|
||||
jest.mock("../../../../../../src/SecurityManager", () => ({
|
||||
accessSecretStorage: jest.fn(),
|
||||
}));
|
||||
jest.mock("../../../../../../src/SecurityManager", () => {
|
||||
const originalModule = jest.requireActual("../../../../../../src/SecurityManager");
|
||||
|
||||
return {
|
||||
...originalModule,
|
||||
accessSecretStorage: jest.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
describe("<RecoveyPanelOutOfSync />", () => {
|
||||
function renderComponent(onFinish = jest.fn(), onForgotRecoveryKey = jest.fn()) {
|
||||
return render(<RecoveryPanelOutOfSync onFinish={onFinish} onForgotRecoveryKey={onForgotRecoveryKey} />);
|
||||
let matrixClient: MatrixClient;
|
||||
|
||||
function renderComponent(
|
||||
onFinish = jest.fn(),
|
||||
onForgotRecoveryKey = jest.fn(),
|
||||
onAccessSecretStorageFailed = jest.fn(),
|
||||
) {
|
||||
matrixClient = createTestClient();
|
||||
return render(
|
||||
<RecoveryPanelOutOfSync
|
||||
onFinish={onFinish}
|
||||
onForgotRecoveryKey={onForgotRecoveryKey}
|
||||
onAccessSecretStorageFailed={onAccessSecretStorageFailed}
|
||||
/>,
|
||||
withClientContextRenderOptions(matrixClient),
|
||||
);
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should render", () => {
|
||||
const { asFragment } = renderComponent();
|
||||
expect(asFragment()).toMatchSnapshot();
|
||||
@@ -38,8 +64,12 @@ describe("<RecoveyPanelOutOfSync />", () => {
|
||||
});
|
||||
|
||||
it("should access to 4S and call onFinish when 'Enter recovery key' is clicked", async () => {
|
||||
jest.spyOn(DeviceListener.sharedInstance(), "keyStorageOutOfSyncNeedsBackupReset").mockResolvedValue(false);
|
||||
|
||||
const user = userEvent.setup();
|
||||
mocked(accessSecretStorage).mockClear().mockResolvedValue();
|
||||
mocked(accessSecretStorage).mockImplementation(async (func = async (): Promise<void> => {}) => {
|
||||
return await func();
|
||||
});
|
||||
|
||||
const onFinish = jest.fn();
|
||||
renderComponent(onFinish);
|
||||
@@ -47,5 +77,59 @@ describe("<RecoveyPanelOutOfSync />", () => {
|
||||
await user.click(screen.getByRole("button", { name: "Enter recovery key" }));
|
||||
expect(accessSecretStorage).toHaveBeenCalled();
|
||||
expect(onFinish).toHaveBeenCalled();
|
||||
|
||||
expect(matrixClient.getCrypto()!.resetKeyBackup).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should reset key backup if needed", async () => {
|
||||
jest.spyOn(DeviceListener.sharedInstance(), "keyStorageOutOfSyncNeedsBackupReset").mockResolvedValue(true);
|
||||
|
||||
const user = userEvent.setup();
|
||||
mocked(accessSecretStorage).mockImplementation(async (func = async (): Promise<void> => {}) => {
|
||||
return await func();
|
||||
});
|
||||
|
||||
const onFinish = jest.fn();
|
||||
renderComponent(onFinish);
|
||||
|
||||
await user.click(screen.getByRole("button", { name: "Enter recovery key" }));
|
||||
expect(accessSecretStorage).toHaveBeenCalled();
|
||||
expect(onFinish).toHaveBeenCalled();
|
||||
|
||||
expect(matrixClient.getCrypto()!.resetKeyBackup).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should call onAccessSecretStorageFailed on failure", async () => {
|
||||
jest.spyOn(DeviceListener.sharedInstance(), "keyStorageOutOfSyncNeedsBackupReset").mockResolvedValue(true);
|
||||
|
||||
const user = userEvent.setup();
|
||||
mocked(accessSecretStorage).mockImplementation(async (func = async (): Promise<void> => {}) => {
|
||||
throw new Error("Error");
|
||||
});
|
||||
|
||||
const onAccessSecretStorageFailed = jest.fn();
|
||||
renderComponent(jest.fn(), jest.fn(), onAccessSecretStorageFailed);
|
||||
|
||||
await user.click(screen.getByRole("button", { name: "Enter recovery key" }));
|
||||
expect(accessSecretStorage).toHaveBeenCalled();
|
||||
expect(onAccessSecretStorageFailed).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should not call onAccessSecretStorageFailed when cancelled", async () => {
|
||||
jest.spyOn(DeviceListener.sharedInstance(), "keyStorageOutOfSyncNeedsBackupReset").mockResolvedValue(true);
|
||||
|
||||
const user = userEvent.setup();
|
||||
mocked(accessSecretStorage).mockImplementation(async (func = async (): Promise<void> => {}) => {
|
||||
throw new AccessCancelledError();
|
||||
});
|
||||
|
||||
const onFinish = jest.fn();
|
||||
const onAccessSecretStorageFailed = jest.fn();
|
||||
renderComponent(onFinish, jest.fn(), onAccessSecretStorageFailed);
|
||||
|
||||
await user.click(screen.getByRole("button", { name: "Enter recovery key" }));
|
||||
expect(accessSecretStorage).toHaveBeenCalled();
|
||||
expect(onFinish).not.toHaveBeenCalled();
|
||||
expect(onAccessSecretStorageFailed).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user