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

Commit

Permalink
Don't start key backups when opening settings (#11640)
Browse files Browse the repository at this point in the history
* SecureBackupPanel: stop calling `checkKeyBackup`

`checkKeyBackup` will start key backups if they aren't already running. In my
not-so-humble opinion, the mere act of opening a settings panel shouldn't change anything.

* fix SecurityUserSettingsTab test
  • Loading branch information
richvdh authored Sep 21, 2023
1 parent c6fec9b commit c879882
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 52 deletions.
24 changes: 1 addition & 23 deletions src/components/views/settings/SecureBackupPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class SecureBackupPanel extends React.PureComponent<{}, IState> {
}

public componentDidMount(): void {
this.checkKeyBackupStatus();
this.loadBackupStatus();

MatrixClientPeg.safeGet().on(CryptoEvent.KeyBackupStatus, this.onKeyBackupStatus);
MatrixClientPeg.safeGet().on(CryptoEvent.KeyBackupSessionsRemaining, this.onKeyBackupSessionsRemaining);
Expand Down Expand Up @@ -97,28 +97,6 @@ export default class SecureBackupPanel extends React.PureComponent<{}, IState> {
this.loadBackupStatus();
};

private async checkKeyBackupStatus(): Promise<void> {
this.getUpdatedDiagnostics();
try {
const keyBackupResult = await MatrixClientPeg.safeGet().checkKeyBackup();
this.setState({
loading: false,
error: false,
backupInfo: keyBackupResult?.backupInfo ?? null,
backupSigStatus: keyBackupResult?.trustInfo ?? null,
});
} catch (e) {
logger.log("Unable to fetch check backup status", e);
if (this.unmounted) return;
this.setState({
loading: false,
error: true,
backupInfo: null,
backupSigStatus: null,
});
}
}

private async loadBackupStatus(): Promise<void> {
this.setState({ loading: true });
this.getUpdatedDiagnostics();
Expand Down
55 changes: 27 additions & 28 deletions test/components/views/settings/SecureBackupPanel-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,17 @@ describe("<SecureBackupPanel />", () => {
const getComponent = () => render(<SecureBackupPanel />);

beforeEach(() => {
client.checkKeyBackup.mockResolvedValue({
backupInfo: {
version: "1",
algorithm: "test",
auth_data: {
public_key: "1234",
},
},
trustInfo: {
usable: false,
sigs: [],
client.getKeyBackupVersion.mockResolvedValue({
version: "1",
algorithm: "test",
auth_data: {
public_key: "1234",
},
});
client.isKeyBackupTrusted.mockResolvedValue({
usable: false,
sigs: [],
});

mocked(client.secretStorage.hasKey).mockClear().mockResolvedValue(false);
client.deleteKeyBackupVersion.mockClear().mockResolvedValue();
Expand All @@ -75,14 +73,21 @@ describe("<SecureBackupPanel />", () => {
expect(screen.queryByRole("progressbar")).not.toBeInTheDocument();
});

it("handles null backup info", async () => {
// checkKeyBackup can fail and return null for various reasons
client.checkKeyBackup.mockResolvedValue(null);
it("handles error fetching backup", async () => {
// getKeyBackupVersion can fail for various reasons
client.getKeyBackupVersion.mockImplementation(async () => {
throw new Error("beep beep");
});
const renderResult = getComponent();
await renderResult.findByText("Unable to load key backup status");
expect(renderResult.container).toMatchSnapshot();
});

it("handles absence of backup", async () => {
client.getKeyBackupVersion.mockResolvedValue(null);
getComponent();
// flush checkKeyBackup promise
// flush getKeyBackupVersion promise
await flushPromises();

// no backup info
expect(screen.getByText("Back up your keys before signing out to avoid losing them.")).toBeInTheDocument();
});

Expand Down Expand Up @@ -124,18 +129,12 @@ describe("<SecureBackupPanel />", () => {
});

it("deletes backup after confirmation", async () => {
client.checkKeyBackup
client.getKeyBackupVersion
.mockResolvedValueOnce({
backupInfo: {
version: "1",
algorithm: "test",
auth_data: {
public_key: "1234",
},
},
trustInfo: {
usable: false,
sigs: [],
version: "1",
algorithm: "test",
auth_data: {
public_key: "1234",
},
})
.mockResolvedValue(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,70 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<SecureBackupPanel /> handles error fetching backup 1`] = `
<div>
<div
class="mx_SettingsSubsection_text"
>
Back up your encryption keys with your account data in case you lose access to your sessions. Your keys will be secured with a unique Security Key.
</div>
<div
class="mx_SettingsSubsection_text"
>
Unable to load key backup status
</div>
<details>
<summary>
Advanced
</summary>
<table
class="mx_SecureBackupPanel_statusList"
>
<tr>
<th
scope="row"
>
Backup key stored:
</th>
<td>
not stored
</td>
</tr>
<tr>
<th
scope="row"
>
Backup key cached:
</th>
<td>
not found locally
</td>
</tr>
<tr>
<th
scope="row"
>
Secret storage public key:
</th>
<td>
not found
</td>
</tr>
<tr>
<th
scope="row"
>
Secret storage:
</th>
<td>
not ready
</td>
</tr>
</table>
</details>
</div>
`;

exports[`<SecureBackupPanel /> suggests connecting session to key backup when backup exists 1`] = `
<div>
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe("<SecurityUserSettingsTab />", () => {
...mockClientMethodsCrypto(),
getRooms: jest.fn().mockReturnValue([]),
getIgnoredUsers: jest.fn(),
getKeyBackupVersion: jest.fn(),
});

const getComponent = () => (
Expand Down
1 change: 0 additions & 1 deletion test/test-utils/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ export const mockClientMethodsCrypto = (): Partial<
isKeyBackupKeyStored: jest.fn(),
getCrossSigningCacheCallbacks: jest.fn().mockReturnValue({ getCrossSigningKeyCache: jest.fn() }),
getStoredCrossSigningForUser: jest.fn(),
checkKeyBackup: jest.fn().mockReturnValue({}),
secretStorage: { hasKey: jest.fn() },
getCrypto: jest.fn().mockReturnValue({
getUserDeviceInfo: jest.fn(),
Expand Down

0 comments on commit c879882

Please sign in to comment.