Replace `MatrixClient.isRoomEncrypted` by `MatrixClient.CryptoApi.isEncryptionEnabledInRoom` in `SecurityRoomSettingsTab` (#28281)

pull/28287/head
Florian Duros 2024-10-24 10:34:07 +02:00 committed by GitHub
parent e73a832008
commit 6d0d237c79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 30 deletions

View File

@ -78,14 +78,18 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
HistoryVisibility.Shared, HistoryVisibility.Shared,
), ),
hasAliases: false, // async loaded in componentDidMount hasAliases: false, // async loaded in componentDidMount
encrypted: context.isRoomEncrypted(this.props.room.roomId), encrypted: false, // async loaded in componentDidMount
showAdvancedSection: false, showAdvancedSection: false,
}; };
} }
public componentDidMount(): void { public async componentDidMount(): Promise<void> {
this.context.on(RoomStateEvent.Events, this.onStateEvent); 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<T>(event: MatrixEvent | null | undefined, key: string, defaultValue: T): T { private pullContentPropertyFromEvent<T>(event: MatrixEvent | null | undefined, key: string, defaultValue: T): T {

View File

@ -269,6 +269,7 @@ export function createTestClient(): MatrixClient {
getAuthIssuer: jest.fn(), getAuthIssuer: jest.fn(),
getOrCreateFilter: jest.fn(), getOrCreateFilter: jest.fn(),
sendStickerMessage: jest.fn(), sendStickerMessage: jest.fn(),
getLocalAliases: jest.fn().mockReturnValue([]),
} as unknown as MatrixClient; } as unknown as MatrixClient;
client.reEmitter = new ReEmitter(client); client.reEmitter = new ReEmitter(client);

View File

@ -10,28 +10,17 @@ import React from "react";
import { fireEvent, render, screen, waitFor, within } from "jest-matrix-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 { EventType, GuestAccess, HistoryVisibility, JoinRule, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import { logger } from "matrix-js-sdk/src/logger"; import { logger } from "matrix-js-sdk/src/logger";
import { mocked } from "jest-mock";
import SecurityRoomSettingsTab from "../../../../../../../src/components/views/settings/tabs/room/SecurityRoomSettingsTab"; import SecurityRoomSettingsTab from "../../../../../../../src/components/views/settings/tabs/room/SecurityRoomSettingsTab";
import MatrixClientContext from "../../../../../../../src/contexts/MatrixClientContext"; import MatrixClientContext from "../../../../../../../src/contexts/MatrixClientContext";
import SettingsStore from "../../../../../../../src/settings/SettingsStore"; import SettingsStore from "../../../../../../../src/settings/SettingsStore";
import { import { clearAllModals, flushPromises, stubClient } from "../../../../../../test-utils";
clearAllModals,
flushPromises,
getMockClientWithEventEmitter,
mockClientMethodsUser,
} from "../../../../../../test-utils";
import { filterBoolean } from "../../../../../../../src/utils/arrays"; import { filterBoolean } from "../../../../../../../src/utils/arrays";
describe("<SecurityRoomSettingsTab />", () => { describe("<SecurityRoomSettingsTab />", () => {
const userId = "@alice:server.org"; const userId = "@alice:server.org";
const client = getMockClientWithEventEmitter({ const client = mocked(stubClient());
...mockClientMethodsUser(userId),
getRoom: jest.fn(),
isRoomEncrypted: jest.fn(),
getLocalAliases: jest.fn().mockReturnValue([]),
sendStateEvent: jest.fn(),
getClientWellKnown: jest.fn(),
});
const roomId = "!room:server.org"; const roomId = "!room:server.org";
const getComponent = (room: Room, closeSettingsFn = jest.fn()) => const getComponent = (room: Room, closeSettingsFn = jest.fn()) =>
@ -96,11 +85,12 @@ describe("<SecurityRoomSettingsTab />", () => {
describe("join rule", () => { describe("join rule", () => {
it("warns when trying to make an encrypted room public", async () => { it("warns when trying to make an encrypted room public", async () => {
const room = new Room(roomId, client, userId); const room = new Room(roomId, client, userId);
client.isRoomEncrypted.mockReturnValue(true); jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
setRoomStateEvents(room, JoinRule.Invite); setRoomStateEvents(room, JoinRule.Invite);
getComponent(room); getComponent(room);
await waitFor(() => expect(screen.getByLabelText("Encrypted")).toBeChecked());
fireEvent.click(screen.getByLabelText("Public")); fireEvent.click(screen.getByLabelText("Public"));
const modal = await screen.findByRole("dialog"); const modal = await screen.findByRole("dialog");
@ -244,19 +234,21 @@ describe("<SecurityRoomSettingsTab />", () => {
expect(screen.getByDisplayValue(HistoryVisibility.Shared)).toBeChecked(); 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); const room = new Room(roomId, client, userId);
client.isRoomEncrypted.mockReturnValue(true); jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
setRoomStateEvents(room); setRoomStateEvents(room);
getComponent(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", () => { it("renders world readable option when room is encrypted and history is already set to world readable", () => {
const room = new Room(roomId, client, userId); const room = new Room(roomId, client, userId);
client.isRoomEncrypted.mockReturnValue(true); jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
setRoomStateEvents(room, undefined, undefined, HistoryVisibility.WorldReadable); setRoomStateEvents(room, undefined, undefined, HistoryVisibility.WorldReadable);
getComponent(room); getComponent(room);
@ -305,13 +297,13 @@ describe("<SecurityRoomSettingsTab />", () => {
}); });
describe("encryption", () => { describe("encryption", () => {
it("displays encryption as enabled", () => { it("displays encryption as enabled", async () => {
const room = new Room(roomId, client, userId); const room = new Room(roomId, client, userId);
client.isRoomEncrypted.mockReturnValue(true); jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
setRoomStateEvents(room); setRoomStateEvents(room);
getComponent(room); getComponent(room);
expect(screen.getByLabelText("Encrypted")).toBeChecked(); await waitFor(() => expect(screen.getByLabelText("Encrypted")).toBeChecked());
// can't disable encryption once enabled // can't disable encryption once enabled
expect(screen.getByLabelText("Encrypted").getAttribute("aria-disabled")).toEqual("true"); expect(screen.getByLabelText("Encrypted").getAttribute("aria-disabled")).toEqual("true");
}); });
@ -356,7 +348,7 @@ describe("<SecurityRoomSettingsTab />", () => {
it("renders world readable option when room is encrypted and history is already set to world readable", () => { it("renders world readable option when room is encrypted and history is already set to world readable", () => {
const room = new Room(roomId, client, userId); const room = new Room(roomId, client, userId);
client.isRoomEncrypted.mockReturnValue(true); jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
setRoomStateEvents(room, undefined, undefined, HistoryVisibility.WorldReadable); setRoomStateEvents(room, undefined, undefined, HistoryVisibility.WorldReadable);
getComponent(room); getComponent(room);
@ -412,21 +404,20 @@ describe("<SecurityRoomSettingsTab />", () => {
}); });
}); });
it("displays encrypted rooms as encrypted", () => { it("displays encrypted rooms as encrypted", async () => {
// rooms that are already encrypted still show encrypted // rooms that are already encrypted still show encrypted
const room = new Room(roomId, client, userId); const room = new Room(roomId, client, userId);
client.isRoomEncrypted.mockReturnValue(true); jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
setRoomStateEvents(room); setRoomStateEvents(room);
getComponent(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.getByLabelText("Encrypted").getAttribute("aria-disabled")).toEqual("true");
expect(screen.getByText("Once enabled, encryption cannot be disabled.")).toBeInTheDocument(); expect(screen.getByText("Once enabled, encryption cannot be disabled.")).toBeInTheDocument();
}); });
it("displays unencrypted rooms with toggle disabled", () => { it("displays unencrypted rooms with toggle disabled", () => {
const room = new Room(roomId, client, userId); const room = new Room(roomId, client, userId);
client.isRoomEncrypted.mockReturnValue(false);
setRoomStateEvents(room); setRoomStateEvents(room);
getComponent(room); getComponent(room);