Skip to content

Commit 6d0d237

Browse files
authored
Replace MatrixClient.isRoomEncrypted by MatrixClient.CryptoApi.isEncryptionEnabledInRoom in SecurityRoomSettingsTab (#28281)
1 parent e73a832 commit 6d0d237

3 files changed

Lines changed: 26 additions & 30 deletions

File tree

src/components/views/settings/tabs/room/SecurityRoomSettingsTab.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,18 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
7878
HistoryVisibility.Shared,
7979
),
8080
hasAliases: false, // async loaded in componentDidMount
81-
encrypted: context.isRoomEncrypted(this.props.room.roomId),
81+
encrypted: false, // async loaded in componentDidMount
8282
showAdvancedSection: false,
8383
};
8484
}
8585

86-
public componentDidMount(): void {
86+
public async componentDidMount(): Promise<void> {
8787
this.context.on(RoomStateEvent.Events, this.onStateEvent);
88-
this.hasAliases().then((hasAliases) => this.setState({ hasAliases }));
88+
89+
this.setState({
90+
hasAliases: await this.hasAliases(),
91+
encrypted: Boolean(await this.context.getCrypto()?.isEncryptionEnabledInRoom(this.props.room.roomId)),
92+
});
8993
}
9094

9195
private pullContentPropertyFromEvent<T>(event: MatrixEvent | null | undefined, key: string, defaultValue: T): T {

test/test-utils/test-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ export function createTestClient(): MatrixClient {
269269
getAuthIssuer: jest.fn(),
270270
getOrCreateFilter: jest.fn(),
271271
sendStickerMessage: jest.fn(),
272+
getLocalAliases: jest.fn().mockReturnValue([]),
272273
} as unknown as MatrixClient;
273274

274275
client.reEmitter = new ReEmitter(client);

test/unit-tests/components/views/settings/tabs/room/SecurityRoomSettingsTab-test.tsx

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,17 @@ import React from "react";
1010
import { fireEvent, render, screen, waitFor, within } from "jest-matrix-react";
1111
import { EventType, GuestAccess, HistoryVisibility, JoinRule, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
1212
import { logger } from "matrix-js-sdk/src/logger";
13+
import { mocked } from "jest-mock";
1314

1415
import SecurityRoomSettingsTab from "../../../../../../../src/components/views/settings/tabs/room/SecurityRoomSettingsTab";
1516
import MatrixClientContext from "../../../../../../../src/contexts/MatrixClientContext";
1617
import SettingsStore from "../../../../../../../src/settings/SettingsStore";
17-
import {
18-
clearAllModals,
19-
flushPromises,
20-
getMockClientWithEventEmitter,
21-
mockClientMethodsUser,
22-
} from "../../../../../../test-utils";
18+
import { clearAllModals, flushPromises, stubClient } from "../../../../../../test-utils";
2319
import { filterBoolean } from "../../../../../../../src/utils/arrays";
2420

2521
describe("<SecurityRoomSettingsTab />", () => {
2622
const userId = "@alice:server.org";
27-
const client = getMockClientWithEventEmitter({
28-
...mockClientMethodsUser(userId),
29-
getRoom: jest.fn(),
30-
isRoomEncrypted: jest.fn(),
31-
getLocalAliases: jest.fn().mockReturnValue([]),
32-
sendStateEvent: jest.fn(),
33-
getClientWellKnown: jest.fn(),
34-
});
23+
const client = mocked(stubClient());
3524
const roomId = "!room:server.org";
3625

3726
const getComponent = (room: Room, closeSettingsFn = jest.fn()) =>
@@ -96,11 +85,12 @@ describe("<SecurityRoomSettingsTab />", () => {
9685
describe("join rule", () => {
9786
it("warns when trying to make an encrypted room public", async () => {
9887
const room = new Room(roomId, client, userId);
99-
client.isRoomEncrypted.mockReturnValue(true);
88+
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
10089
setRoomStateEvents(room, JoinRule.Invite);
10190

10291
getComponent(room);
10392

93+
await waitFor(() => expect(screen.getByLabelText("Encrypted")).toBeChecked());
10494
fireEvent.click(screen.getByLabelText("Public"));
10595

10696
const modal = await screen.findByRole("dialog");
@@ -244,19 +234,21 @@ describe("<SecurityRoomSettingsTab />", () => {
244234
expect(screen.getByDisplayValue(HistoryVisibility.Shared)).toBeChecked();
245235
});
246236

247-
it("does not render world readable option when room is encrypted", () => {
237+
it("does not render world readable option when room is encrypted", async () => {
248238
const room = new Room(roomId, client, userId);
249-
client.isRoomEncrypted.mockReturnValue(true);
239+
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
250240
setRoomStateEvents(room);
251241

252242
getComponent(room);
253243

254-
expect(screen.queryByDisplayValue(HistoryVisibility.WorldReadable)).not.toBeInTheDocument();
244+
await waitFor(() =>
245+
expect(screen.queryByDisplayValue(HistoryVisibility.WorldReadable)).not.toBeInTheDocument(),
246+
);
255247
});
256248

257249
it("renders world readable option when room is encrypted and history is already set to world readable", () => {
258250
const room = new Room(roomId, client, userId);
259-
client.isRoomEncrypted.mockReturnValue(true);
251+
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
260252
setRoomStateEvents(room, undefined, undefined, HistoryVisibility.WorldReadable);
261253

262254
getComponent(room);
@@ -305,13 +297,13 @@ describe("<SecurityRoomSettingsTab />", () => {
305297
});
306298

307299
describe("encryption", () => {
308-
it("displays encryption as enabled", () => {
300+
it("displays encryption as enabled", async () => {
309301
const room = new Room(roomId, client, userId);
310-
client.isRoomEncrypted.mockReturnValue(true);
302+
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
311303
setRoomStateEvents(room);
312304
getComponent(room);
313305

314-
expect(screen.getByLabelText("Encrypted")).toBeChecked();
306+
await waitFor(() => expect(screen.getByLabelText("Encrypted")).toBeChecked());
315307
// can't disable encryption once enabled
316308
expect(screen.getByLabelText("Encrypted").getAttribute("aria-disabled")).toEqual("true");
317309
});
@@ -356,7 +348,7 @@ describe("<SecurityRoomSettingsTab />", () => {
356348

357349
it("renders world readable option when room is encrypted and history is already set to world readable", () => {
358350
const room = new Room(roomId, client, userId);
359-
client.isRoomEncrypted.mockReturnValue(true);
351+
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
360352
setRoomStateEvents(room, undefined, undefined, HistoryVisibility.WorldReadable);
361353

362354
getComponent(room);
@@ -412,21 +404,20 @@ describe("<SecurityRoomSettingsTab />", () => {
412404
});
413405
});
414406

415-
it("displays encrypted rooms as encrypted", () => {
407+
it("displays encrypted rooms as encrypted", async () => {
416408
// rooms that are already encrypted still show encrypted
417409
const room = new Room(roomId, client, userId);
418-
client.isRoomEncrypted.mockReturnValue(true);
410+
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
419411
setRoomStateEvents(room);
420412
getComponent(room);
421413

422-
expect(screen.getByLabelText("Encrypted")).toBeChecked();
414+
await waitFor(() => expect(screen.getByLabelText("Encrypted")).toBeChecked());
423415
expect(screen.getByLabelText("Encrypted").getAttribute("aria-disabled")).toEqual("true");
424416
expect(screen.getByText("Once enabled, encryption cannot be disabled.")).toBeInTheDocument();
425417
});
426418

427419
it("displays unencrypted rooms with toggle disabled", () => {
428420
const room = new Room(roomId, client, userId);
429-
client.isRoomEncrypted.mockReturnValue(false);
430421
setRoomStateEvents(room);
431422
getComponent(room);
432423

0 commit comments

Comments
 (0)