-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrating deprecated sync
MatrixClient.getKeyBackupEnabled
to async…
… `MatrixClient.CryptoApi.getActiveSessionBackupVersion` in `NewRecoveryMethodDialog`. Rewrite `NewRecoveryMethodDialog` into a functional component to make it easier to handle the new async method.
- Loading branch information
1 parent
2cff2b5
commit c5a1f49
Showing
4 changed files
with
287 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
test/unit-tests/async-components/dialogs/security/NewRecoveryMethodDialog-test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2024 New Vector Ltd. | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only | ||
* 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 { act } from "@testing-library/react-hooks/dom"; | ||
|
||
import NewRecoveryMethodDialog from "../../../../../src/async-components/views/dialogs/security/NewRecoveryMethodDialog"; | ||
import { createTestClient } from "../../../../test-utils"; | ||
import MatrixClientContext from "../../../../../src/contexts/MatrixClientContext.tsx"; | ||
import dis from "../../../../../src/dispatcher/dispatcher.ts"; | ||
import { Action } from "../../../../../src/dispatcher/actions.ts"; | ||
import Modal from "../../../../../src/Modal.tsx"; | ||
|
||
describe("<NewRecoveryMethodDialog />", () => { | ||
let matrixClient: MatrixClient; | ||
beforeEach(() => { | ||
matrixClient = createTestClient(); | ||
jest.spyOn(dis, "fire"); | ||
jest.spyOn(Modal, "createDialog"); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
function renderComponent(onFinished: () => void = jest.fn()) { | ||
return render( | ||
<MatrixClientContext.Provider value={matrixClient}> | ||
<NewRecoveryMethodDialog onFinished={onFinished} /> | ||
</MatrixClientContext.Provider>, | ||
); | ||
} | ||
|
||
test("when cancel is clicked", async () => { | ||
const onFinished = jest.fn(); | ||
act(() => { | ||
renderComponent(onFinished); | ||
}); | ||
|
||
await userEvent.click(screen.getByRole("button", { name: "Go to Settings" })); | ||
expect(onFinished).toHaveBeenCalled(); | ||
expect(dis.fire).toHaveBeenCalledWith(Action.ViewUserSettings); | ||
}); | ||
|
||
test("when key backup is enabled", async () => { | ||
jest.spyOn(matrixClient.getCrypto()!, "getActiveSessionBackupVersion").mockResolvedValue("version"); | ||
|
||
const onFinished = jest.fn(); | ||
|
||
await act(async () => { | ||
const { asFragment } = renderComponent(onFinished); | ||
await waitFor(() => | ||
expect( | ||
screen.getByText("This session is encrypting history using the new recovery method."), | ||
).toBeInTheDocument(), | ||
); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
await userEvent.click(screen.getByRole("button", { name: "Set up Secure Messages" })); | ||
expect(onFinished).toHaveBeenCalled(); | ||
}); | ||
|
||
test("when key backup is disabled", async () => { | ||
const onFinished = jest.fn(); | ||
|
||
const { asFragment } = renderComponent(onFinished); | ||
expect(asFragment()).toMatchSnapshot(); | ||
|
||
await userEvent.click(screen.getByRole("button", { name: "Set up Secure Messages" })); | ||
await waitFor(() => expect(Modal.createDialog).toHaveBeenCalled()); | ||
}); | ||
}); |
146 changes: 146 additions & 0 deletions
146
...sts/async-components/dialogs/security/__snapshots__/NewRecoveryMethodDialog-test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`<NewRecoveryMethodDialog /> when key backup is disabled 1`] = ` | ||
<DocumentFragment> | ||
<div | ||
data-focus-guard="true" | ||
style="width: 1px; height: 0px; padding: 0px; overflow: hidden; position: fixed; top: 1px; left: 1px;" | ||
tabindex="0" | ||
/> | ||
<div | ||
aria-labelledby="mx_BaseDialog_title" | ||
class="mx_KeyBackupFailedDialog mx_Dialog_fixedWidth" | ||
data-focus-lock-disabled="false" | ||
role="dialog" | ||
> | ||
<div | ||
class="mx_Dialog_header" | ||
> | ||
<h1 | ||
class="mx_Heading_h3 mx_Dialog_title" | ||
id="mx_BaseDialog_title" | ||
> | ||
<span | ||
class="mx_KeyBackupFailedDialog_title" | ||
> | ||
New Recovery Method | ||
</span> | ||
</h1> | ||
</div> | ||
<p> | ||
A new Security Phrase and key for Secure Messages have been detected. | ||
</p> | ||
<strong | ||
class="warning" | ||
> | ||
If you didn't set the new recovery method, an attacker may be trying to access your account. Change your account password and set a new recovery method immediately in Settings. | ||
</strong> | ||
<div | ||
class="mx_Dialog_buttons" | ||
> | ||
<span | ||
class="mx_Dialog_buttons_row" | ||
> | ||
<button | ||
data-testid="dialog-cancel-button" | ||
type="button" | ||
> | ||
Go to Settings | ||
</button> | ||
<button | ||
class="mx_Dialog_primary" | ||
data-testid="dialog-primary-button" | ||
type="button" | ||
> | ||
Set up Secure Messages | ||
</button> | ||
</span> | ||
</div> | ||
<div | ||
aria-label="Close dialog" | ||
class="mx_AccessibleButton mx_Dialog_cancelButton" | ||
role="button" | ||
tabindex="0" | ||
/> | ||
</div> | ||
<div | ||
data-focus-guard="true" | ||
style="width: 1px; height: 0px; padding: 0px; overflow: hidden; position: fixed; top: 1px; left: 1px;" | ||
tabindex="0" | ||
/> | ||
</DocumentFragment> | ||
`; | ||
|
||
exports[`<NewRecoveryMethodDialog /> when key backup is enabled 1`] = ` | ||
<DocumentFragment> | ||
<div | ||
data-focus-guard="true" | ||
style="width: 1px; height: 0px; padding: 0px; overflow: hidden; position: fixed; top: 1px; left: 1px;" | ||
tabindex="0" | ||
/> | ||
<div | ||
aria-labelledby="mx_BaseDialog_title" | ||
class="mx_KeyBackupFailedDialog mx_Dialog_fixedWidth" | ||
data-focus-lock-disabled="false" | ||
role="dialog" | ||
> | ||
<div | ||
class="mx_Dialog_header" | ||
> | ||
<h1 | ||
class="mx_Heading_h3 mx_Dialog_title" | ||
id="mx_BaseDialog_title" | ||
> | ||
<span | ||
class="mx_KeyBackupFailedDialog_title" | ||
> | ||
New Recovery Method | ||
</span> | ||
</h1> | ||
</div> | ||
<p> | ||
A new Security Phrase and key for Secure Messages have been detected. | ||
</p> | ||
<p> | ||
This session is encrypting history using the new recovery method. | ||
</p> | ||
<strong | ||
class="warning" | ||
> | ||
If you didn't set the new recovery method, an attacker may be trying to access your account. Change your account password and set a new recovery method immediately in Settings. | ||
</strong> | ||
<div | ||
class="mx_Dialog_buttons" | ||
> | ||
<span | ||
class="mx_Dialog_buttons_row" | ||
> | ||
<button | ||
data-testid="dialog-cancel-button" | ||
type="button" | ||
> | ||
Go to Settings | ||
</button> | ||
<button | ||
class="mx_Dialog_primary" | ||
data-testid="dialog-primary-button" | ||
type="button" | ||
> | ||
Set up Secure Messages | ||
</button> | ||
</span> | ||
</div> | ||
<div | ||
aria-label="Close dialog" | ||
class="mx_AccessibleButton mx_Dialog_cancelButton" | ||
role="button" | ||
tabindex="0" | ||
/> | ||
</div> | ||
<div | ||
data-focus-guard="true" | ||
style="width: 1px; height: 0px; padding: 0px; overflow: hidden; position: fixed; top: 1px; left: 1px;" | ||
tabindex="0" | ||
/> | ||
</DocumentFragment> | ||
`; |