From 6d0d237c793126d903c94b40c9037235c1aaccf0 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Thu, 24 Oct 2024 10:34:07 +0200 Subject: [PATCH] Replace `MatrixClient.isRoomEncrypted` by `MatrixClient.CryptoApi.isEncryptionEnabledInRoom` in `SecurityRoomSettingsTab` (#28281) --- .../tabs/room/SecurityRoomSettingsTab.tsx | 10 +++-- test/test-utils/test-utils.ts | 1 + .../room/SecurityRoomSettingsTab-test.tsx | 45 ++++++++----------- 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/components/views/settings/tabs/room/SecurityRoomSettingsTab.tsx b/src/components/views/settings/tabs/room/SecurityRoomSettingsTab.tsx index 064422b4518..1ade503bb29 100644 --- a/src/components/views/settings/tabs/room/SecurityRoomSettingsTab.tsx +++ b/src/components/views/settings/tabs/room/SecurityRoomSettingsTab.tsx @@ -78,14 +78,18 @@ export default class SecurityRoomSettingsTab extends React.Component { this.context.on(RoomStateEvent.Events, this.onStateEvent); - this.hasAliases().then((hasAliases) => this.setState({ hasAliases })); + + this.setState({ + hasAliases: await this.hasAliases(), + encrypted: Boolean(await this.context.getCrypto()?.isEncryptionEnabledInRoom(this.props.room.roomId)), + }); } private pullContentPropertyFromEvent(event: MatrixEvent | null | undefined, key: string, defaultValue: T): T { diff --git a/test/test-utils/test-utils.ts b/test/test-utils/test-utils.ts index 73ff714ae0f..c36b1f50549 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -269,6 +269,7 @@ export function createTestClient(): MatrixClient { getAuthIssuer: jest.fn(), getOrCreateFilter: jest.fn(), sendStickerMessage: jest.fn(), + getLocalAliases: jest.fn().mockReturnValue([]), } as unknown as MatrixClient; client.reEmitter = new ReEmitter(client); diff --git a/test/unit-tests/components/views/settings/tabs/room/SecurityRoomSettingsTab-test.tsx b/test/unit-tests/components/views/settings/tabs/room/SecurityRoomSettingsTab-test.tsx index 4eefc2d5d56..6534a340045 100644 --- a/test/unit-tests/components/views/settings/tabs/room/SecurityRoomSettingsTab-test.tsx +++ b/test/unit-tests/components/views/settings/tabs/room/SecurityRoomSettingsTab-test.tsx @@ -10,28 +10,17 @@ import React from "react"; import { fireEvent, render, screen, waitFor, within } from "jest-matrix-react"; import { EventType, GuestAccess, HistoryVisibility, JoinRule, MatrixEvent, Room } from "matrix-js-sdk/src/matrix"; import { logger } from "matrix-js-sdk/src/logger"; +import { mocked } from "jest-mock"; import SecurityRoomSettingsTab from "../../../../../../../src/components/views/settings/tabs/room/SecurityRoomSettingsTab"; import MatrixClientContext from "../../../../../../../src/contexts/MatrixClientContext"; import SettingsStore from "../../../../../../../src/settings/SettingsStore"; -import { - clearAllModals, - flushPromises, - getMockClientWithEventEmitter, - mockClientMethodsUser, -} from "../../../../../../test-utils"; +import { clearAllModals, flushPromises, stubClient } from "../../../../../../test-utils"; import { filterBoolean } from "../../../../../../../src/utils/arrays"; describe("", () => { const userId = "@alice:server.org"; - const client = getMockClientWithEventEmitter({ - ...mockClientMethodsUser(userId), - getRoom: jest.fn(), - isRoomEncrypted: jest.fn(), - getLocalAliases: jest.fn().mockReturnValue([]), - sendStateEvent: jest.fn(), - getClientWellKnown: jest.fn(), - }); + const client = mocked(stubClient()); const roomId = "!room:server.org"; const getComponent = (room: Room, closeSettingsFn = jest.fn()) => @@ -96,11 +85,12 @@ describe("", () => { describe("join rule", () => { it("warns when trying to make an encrypted room public", async () => { const room = new Room(roomId, client, userId); - client.isRoomEncrypted.mockReturnValue(true); + jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true); setRoomStateEvents(room, JoinRule.Invite); getComponent(room); + await waitFor(() => expect(screen.getByLabelText("Encrypted")).toBeChecked()); fireEvent.click(screen.getByLabelText("Public")); const modal = await screen.findByRole("dialog"); @@ -244,19 +234,21 @@ describe("", () => { expect(screen.getByDisplayValue(HistoryVisibility.Shared)).toBeChecked(); }); - it("does not render world readable option when room is encrypted", () => { + it("does not render world readable option when room is encrypted", async () => { const room = new Room(roomId, client, userId); - client.isRoomEncrypted.mockReturnValue(true); + jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true); setRoomStateEvents(room); getComponent(room); - expect(screen.queryByDisplayValue(HistoryVisibility.WorldReadable)).not.toBeInTheDocument(); + await waitFor(() => + expect(screen.queryByDisplayValue(HistoryVisibility.WorldReadable)).not.toBeInTheDocument(), + ); }); it("renders world readable option when room is encrypted and history is already set to world readable", () => { const room = new Room(roomId, client, userId); - client.isRoomEncrypted.mockReturnValue(true); + jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true); setRoomStateEvents(room, undefined, undefined, HistoryVisibility.WorldReadable); getComponent(room); @@ -305,13 +297,13 @@ describe("", () => { }); describe("encryption", () => { - it("displays encryption as enabled", () => { + it("displays encryption as enabled", async () => { const room = new Room(roomId, client, userId); - client.isRoomEncrypted.mockReturnValue(true); + jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true); setRoomStateEvents(room); getComponent(room); - expect(screen.getByLabelText("Encrypted")).toBeChecked(); + await waitFor(() => expect(screen.getByLabelText("Encrypted")).toBeChecked()); // can't disable encryption once enabled expect(screen.getByLabelText("Encrypted").getAttribute("aria-disabled")).toEqual("true"); }); @@ -356,7 +348,7 @@ describe("", () => { it("renders world readable option when room is encrypted and history is already set to world readable", () => { const room = new Room(roomId, client, userId); - client.isRoomEncrypted.mockReturnValue(true); + jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true); setRoomStateEvents(room, undefined, undefined, HistoryVisibility.WorldReadable); getComponent(room); @@ -412,21 +404,20 @@ describe("", () => { }); }); - it("displays encrypted rooms as encrypted", () => { + it("displays encrypted rooms as encrypted", async () => { // rooms that are already encrypted still show encrypted const room = new Room(roomId, client, userId); - client.isRoomEncrypted.mockReturnValue(true); + jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true); setRoomStateEvents(room); getComponent(room); - expect(screen.getByLabelText("Encrypted")).toBeChecked(); + await waitFor(() => expect(screen.getByLabelText("Encrypted")).toBeChecked()); expect(screen.getByLabelText("Encrypted").getAttribute("aria-disabled")).toEqual("true"); expect(screen.getByText("Once enabled, encryption cannot be disabled.")).toBeInTheDocument(); }); it("displays unencrypted rooms with toggle disabled", () => { const room = new Room(roomId, client, userId); - client.isRoomEncrypted.mockReturnValue(false); setRoomStateEvents(room); getComponent(room);