2023-05-05 16:13:50 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-05-05 16:13:50 +08:00
|
|
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2023-05-05 16:13:50 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2023-10-05 11:30:57 +08:00
|
|
|
import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
2023-05-05 16:13:50 +08:00
|
|
|
import {
|
|
|
|
EventType,
|
|
|
|
GuestAccess,
|
|
|
|
HistoryVisibility,
|
|
|
|
JoinRule,
|
|
|
|
MatrixEvent,
|
|
|
|
Room,
|
|
|
|
ClientEvent,
|
|
|
|
RoomMember,
|
2023-09-08 09:00:48 +08:00
|
|
|
MatrixError,
|
|
|
|
Visibility,
|
2023-05-05 16:13:50 +08:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2024-03-18 22:40:52 +08:00
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2023-05-05 16:13:50 +08:00
|
|
|
import { defer, IDeferred } from "matrix-js-sdk/src/utils";
|
|
|
|
|
|
|
|
import {
|
|
|
|
clearAllModals,
|
|
|
|
flushPromises,
|
|
|
|
getMockClientWithEventEmitter,
|
|
|
|
mockClientMethodsUser,
|
|
|
|
} from "../../../test-utils";
|
|
|
|
import { filterBoolean } from "../../../../src/utils/arrays";
|
|
|
|
import JoinRuleSettings, { JoinRuleSettingsProps } from "../../../../src/components/views/settings/JoinRuleSettings";
|
|
|
|
import { PreferredRoomVersions } from "../../../../src/utils/PreferredRoomVersions";
|
|
|
|
import SpaceStore from "../../../../src/stores/spaces/SpaceStore";
|
2023-07-19 18:48:24 +08:00
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
2023-05-05 16:13:50 +08:00
|
|
|
|
|
|
|
describe("<JoinRuleSettings />", () => {
|
|
|
|
const userId = "@alice:server.org";
|
|
|
|
const client = getMockClientWithEventEmitter({
|
|
|
|
...mockClientMethodsUser(userId),
|
|
|
|
getRoom: jest.fn(),
|
2023-10-05 11:30:57 +08:00
|
|
|
getDomain: jest.fn(),
|
2023-05-05 16:13:50 +08:00
|
|
|
getLocalAliases: jest.fn().mockReturnValue([]),
|
|
|
|
sendStateEvent: jest.fn(),
|
|
|
|
upgradeRoom: jest.fn(),
|
|
|
|
getProfileInfo: jest.fn(),
|
|
|
|
invite: jest.fn().mockResolvedValue(undefined),
|
|
|
|
isRoomEncrypted: jest.fn().mockReturnValue(false),
|
2023-09-08 09:00:48 +08:00
|
|
|
getRoomDirectoryVisibility: jest.fn(),
|
|
|
|
setRoomDirectoryVisibility: jest.fn(),
|
2023-05-05 16:13:50 +08:00
|
|
|
});
|
|
|
|
const roomId = "!room:server.org";
|
|
|
|
const newRoomId = "!roomUpgraded:server.org";
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
room: new Room(roomId, client, userId),
|
|
|
|
closeSettingsFn: jest.fn(),
|
|
|
|
onError: jest.fn(),
|
|
|
|
};
|
|
|
|
const getComponent = (props: Partial<JoinRuleSettingsProps> = {}) =>
|
|
|
|
render(<JoinRuleSettings {...defaultProps} {...props} />);
|
|
|
|
|
|
|
|
const setRoomStateEvents = (
|
|
|
|
room: Room,
|
2023-07-19 18:48:24 +08:00
|
|
|
roomVersion: string,
|
2023-05-05 16:13:50 +08:00
|
|
|
joinRule?: JoinRule,
|
|
|
|
guestAccess?: GuestAccess,
|
|
|
|
history?: HistoryVisibility,
|
|
|
|
): void => {
|
|
|
|
const events = filterBoolean<MatrixEvent>([
|
|
|
|
new MatrixEvent({
|
|
|
|
type: EventType.RoomCreate,
|
2023-07-19 18:48:24 +08:00
|
|
|
content: { room_version: roomVersion },
|
2023-05-05 16:13:50 +08:00
|
|
|
sender: userId,
|
|
|
|
state_key: "",
|
|
|
|
room_id: room.roomId,
|
|
|
|
}),
|
|
|
|
guestAccess &&
|
|
|
|
new MatrixEvent({
|
|
|
|
type: EventType.RoomGuestAccess,
|
|
|
|
content: { guest_access: guestAccess },
|
|
|
|
sender: userId,
|
|
|
|
state_key: "",
|
|
|
|
room_id: room.roomId,
|
|
|
|
}),
|
|
|
|
history &&
|
|
|
|
new MatrixEvent({
|
|
|
|
type: EventType.RoomHistoryVisibility,
|
|
|
|
content: { history_visibility: history },
|
|
|
|
sender: userId,
|
|
|
|
state_key: "",
|
|
|
|
room_id: room.roomId,
|
|
|
|
}),
|
|
|
|
joinRule &&
|
|
|
|
new MatrixEvent({
|
|
|
|
type: EventType.RoomJoinRules,
|
|
|
|
content: { join_rule: joinRule },
|
|
|
|
sender: userId,
|
|
|
|
state_key: "",
|
|
|
|
room_id: room.roomId,
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
|
|
|
room.currentState.setStateEvents(events);
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client.sendStateEvent.mockReset().mockResolvedValue({ event_id: "test" });
|
|
|
|
client.isRoomEncrypted.mockReturnValue(false);
|
|
|
|
client.upgradeRoom.mockResolvedValue({ replacement_room: newRoomId });
|
|
|
|
client.getRoom.mockReturnValue(null);
|
2023-07-19 18:48:24 +08:00
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((setting) => setting === "feature_ask_to_join");
|
2023-05-05 16:13:50 +08:00
|
|
|
});
|
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
type TestCase = [string, { label: string; unsupportedRoomVersion: string; preferredRoomVersion: string }];
|
|
|
|
const testCases: TestCase[] = [
|
|
|
|
[
|
|
|
|
JoinRule.Knock,
|
|
|
|
{
|
|
|
|
label: "Ask to join",
|
|
|
|
unsupportedRoomVersion: "6",
|
|
|
|
preferredRoomVersion: PreferredRoomVersions.KnockRooms,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
JoinRule.Restricted,
|
|
|
|
{
|
|
|
|
label: "Space members",
|
|
|
|
unsupportedRoomVersion: "8",
|
|
|
|
preferredRoomVersion: PreferredRoomVersions.RestrictedRooms,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
describe.each(testCases)("%s rooms", (joinRule, { label, unsupportedRoomVersion, preferredRoomVersion }) => {
|
2023-05-05 16:13:50 +08:00
|
|
|
afterEach(async () => {
|
|
|
|
await clearAllModals();
|
|
|
|
});
|
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
describe(`when room does not support join rule ${joinRule}`, () => {
|
|
|
|
it(`should not show ${joinRule} room join rule when upgrade is disabled`, () => {
|
|
|
|
// room that doesn't support the join rule
|
|
|
|
const room = new Room(roomId, client, userId);
|
|
|
|
setRoomStateEvents(room, unsupportedRoomVersion);
|
2023-05-05 16:13:50 +08:00
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
getComponent({ room: room, promptUpgrade: false });
|
|
|
|
|
|
|
|
expect(screen.queryByText(label)).not.toBeInTheDocument();
|
2023-05-05 16:13:50 +08:00
|
|
|
});
|
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
it(`should show ${joinRule} room join rule when upgrade is enabled`, () => {
|
|
|
|
// room that doesn't support the join rule
|
|
|
|
const room = new Room(roomId, client, userId);
|
|
|
|
setRoomStateEvents(room, unsupportedRoomVersion);
|
2023-05-05 16:13:50 +08:00
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
getComponent({ room: room, promptUpgrade: true });
|
2023-05-05 16:13:50 +08:00
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
expect(within(screen.getByText(label)).getByText("Upgrade required")).toBeInTheDocument();
|
2023-05-05 16:13:50 +08:00
|
|
|
});
|
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
it(`upgrades room when changing join rule to ${joinRule}`, async () => {
|
2023-05-05 16:13:50 +08:00
|
|
|
const deferredInvites: IDeferred<any>[] = [];
|
2023-07-19 18:48:24 +08:00
|
|
|
// room that doesn't support the join rule
|
|
|
|
const room = new Room(roomId, client, userId);
|
2023-05-05 16:13:50 +08:00
|
|
|
const parentSpace = new Room("!parentSpace:server.org", client, userId);
|
|
|
|
jest.spyOn(SpaceStore.instance, "getKnownParents").mockReturnValue(new Set([parentSpace.roomId]));
|
2023-07-19 18:48:24 +08:00
|
|
|
setRoomStateEvents(room, unsupportedRoomVersion);
|
2023-05-05 16:13:50 +08:00
|
|
|
const memberAlice = new RoomMember(roomId, "@alice:server.org");
|
|
|
|
const memberBob = new RoomMember(roomId, "@bob:server.org");
|
|
|
|
const memberCharlie = new RoomMember(roomId, "@charlie:server.org");
|
2023-07-19 18:48:24 +08:00
|
|
|
jest.spyOn(room, "getMembersWithMembership").mockImplementation((membership) =>
|
2024-03-12 22:52:54 +08:00
|
|
|
membership === KnownMembership.Join ? [memberAlice, memberBob] : [memberCharlie],
|
2023-05-05 16:13:50 +08:00
|
|
|
);
|
|
|
|
const upgradedRoom = new Room(newRoomId, client, userId);
|
2023-07-19 18:48:24 +08:00
|
|
|
setRoomStateEvents(upgradedRoom, preferredRoomVersion);
|
2023-05-05 16:13:50 +08:00
|
|
|
client.getRoom.mockImplementation((id) => {
|
2023-07-19 18:48:24 +08:00
|
|
|
if (roomId === id) return room;
|
2023-05-05 16:13:50 +08:00
|
|
|
if (parentSpace.roomId === id) return parentSpace;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
// resolve invites by hand
|
|
|
|
// flushPromises is too blunt to test reliably
|
|
|
|
client.invite.mockImplementation(() => {
|
|
|
|
const p = defer<{}>();
|
|
|
|
deferredInvites.push(p);
|
|
|
|
return p.promise;
|
|
|
|
});
|
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
getComponent({ room: room, promptUpgrade: true });
|
2023-05-05 16:13:50 +08:00
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
fireEvent.click(screen.getByText(label));
|
2023-05-05 16:13:50 +08:00
|
|
|
|
|
|
|
const dialog = await screen.findByRole("dialog");
|
|
|
|
|
|
|
|
fireEvent.click(within(dialog).getByText("Upgrade"));
|
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
expect(client.upgradeRoom).toHaveBeenCalledWith(roomId, preferredRoomVersion);
|
2023-05-05 16:13:50 +08:00
|
|
|
|
|
|
|
expect(within(dialog).getByText("Upgrading room")).toBeInTheDocument();
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
expect(within(dialog).getByText("Loading new room")).toBeInTheDocument();
|
|
|
|
|
|
|
|
// "create" our new room, have it come thru sync
|
|
|
|
client.getRoom.mockImplementation((id) => {
|
2023-07-19 18:48:24 +08:00
|
|
|
if (roomId === id) return room;
|
2023-05-05 16:13:50 +08:00
|
|
|
if (newRoomId === id) return upgradedRoom;
|
|
|
|
if (parentSpace.roomId === id) return parentSpace;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
client.emit(ClientEvent.Room, upgradedRoom);
|
|
|
|
|
|
|
|
// invite users
|
|
|
|
expect(await screen.findByText("Sending invites... (0 out of 2)")).toBeInTheDocument();
|
|
|
|
deferredInvites.pop()!.resolve({});
|
|
|
|
expect(await screen.findByText("Sending invites... (1 out of 2)")).toBeInTheDocument();
|
|
|
|
deferredInvites.pop()!.resolve({});
|
|
|
|
|
2023-10-05 11:30:57 +08:00
|
|
|
// Usually we see "Updating space..." in the UI here, but we
|
|
|
|
// removed the assertion about it, because it sometimes fails,
|
|
|
|
// presumably because it disappeared too quickly to be visible.
|
2023-05-05 16:13:50 +08:00
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
// done, modal closed
|
2023-10-05 11:30:57 +08:00
|
|
|
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
|
2023-05-05 16:13:50 +08:00
|
|
|
});
|
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
it(`upgrades room with no parent spaces or members when changing join rule to ${joinRule}`, async () => {
|
|
|
|
// room that doesn't support the join rule
|
|
|
|
const room = new Room(roomId, client, userId);
|
|
|
|
setRoomStateEvents(room, unsupportedRoomVersion);
|
2023-05-05 16:13:50 +08:00
|
|
|
const upgradedRoom = new Room(newRoomId, client, userId);
|
2023-07-19 18:48:24 +08:00
|
|
|
setRoomStateEvents(upgradedRoom, preferredRoomVersion);
|
2023-05-05 16:13:50 +08:00
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
getComponent({ room: room, promptUpgrade: true });
|
2023-05-05 16:13:50 +08:00
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
fireEvent.click(screen.getByText(label));
|
2023-05-05 16:13:50 +08:00
|
|
|
|
|
|
|
const dialog = await screen.findByRole("dialog");
|
|
|
|
|
|
|
|
fireEvent.click(within(dialog).getByText("Upgrade"));
|
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
expect(client.upgradeRoom).toHaveBeenCalledWith(roomId, preferredRoomVersion);
|
2023-05-05 16:13:50 +08:00
|
|
|
|
|
|
|
expect(within(dialog).getByText("Upgrading room")).toBeInTheDocument();
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
expect(within(dialog).getByText("Loading new room")).toBeInTheDocument();
|
|
|
|
|
|
|
|
// "create" our new room, have it come thru sync
|
|
|
|
client.getRoom.mockImplementation((id) => {
|
2023-07-19 18:48:24 +08:00
|
|
|
if (roomId === id) return room;
|
2023-05-05 16:13:50 +08:00
|
|
|
if (newRoomId === id) return upgradedRoom;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
client.emit(ClientEvent.Room, upgradedRoom);
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
// done, modal closed
|
|
|
|
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-07-19 18:48:24 +08:00
|
|
|
|
2023-09-08 09:00:48 +08:00
|
|
|
describe("knock rooms directory visibility", () => {
|
|
|
|
const getCheckbox = () => screen.getByRole("checkbox");
|
|
|
|
let room: Room;
|
|
|
|
|
|
|
|
beforeEach(() => (room = new Room(roomId, client, userId)));
|
|
|
|
|
|
|
|
describe("when join rule is knock", () => {
|
|
|
|
beforeEach(() => setRoomStateEvents(room, PreferredRoomVersions.KnockRooms, JoinRule.Knock));
|
|
|
|
|
|
|
|
it("should set the visibility to public", async () => {
|
|
|
|
jest.spyOn(client, "getRoomDirectoryVisibility").mockResolvedValue({ visibility: Visibility.Private });
|
|
|
|
jest.spyOn(client, "setRoomDirectoryVisibility").mockResolvedValue({});
|
|
|
|
getComponent({ room });
|
|
|
|
fireEvent.click(getCheckbox());
|
|
|
|
await act(async () => await flushPromises());
|
|
|
|
expect(client.setRoomDirectoryVisibility).toHaveBeenCalledWith(roomId, Visibility.Public);
|
|
|
|
expect(getCheckbox()).toBeChecked();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should set the visibility to private", async () => {
|
|
|
|
jest.spyOn(client, "getRoomDirectoryVisibility").mockResolvedValue({ visibility: Visibility.Public });
|
|
|
|
jest.spyOn(client, "setRoomDirectoryVisibility").mockResolvedValue({});
|
|
|
|
getComponent({ room });
|
|
|
|
await act(async () => await flushPromises());
|
|
|
|
fireEvent.click(getCheckbox());
|
|
|
|
await act(async () => await flushPromises());
|
|
|
|
expect(client.setRoomDirectoryVisibility).toHaveBeenCalledWith(roomId, Visibility.Private);
|
|
|
|
expect(getCheckbox()).not.toBeChecked();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call onError if setting visibility fails", async () => {
|
|
|
|
const error = new MatrixError();
|
|
|
|
jest.spyOn(client, "getRoomDirectoryVisibility").mockResolvedValue({ visibility: Visibility.Private });
|
|
|
|
jest.spyOn(client, "setRoomDirectoryVisibility").mockRejectedValue(error);
|
|
|
|
getComponent({ room });
|
|
|
|
fireEvent.click(getCheckbox());
|
|
|
|
await act(async () => await flushPromises());
|
|
|
|
expect(getCheckbox()).not.toBeChecked();
|
|
|
|
expect(defaultProps.onError).toHaveBeenCalledWith(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when the room version is unsupported and upgrade is enabled", () => {
|
|
|
|
it("should disable the checkbox", () => {
|
|
|
|
setRoomStateEvents(room, "6", JoinRule.Invite);
|
|
|
|
getComponent({ promptUpgrade: true, room });
|
|
|
|
expect(getCheckbox()).toBeDisabled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when join rule is not knock", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setRoomStateEvents(room, PreferredRoomVersions.KnockRooms, JoinRule.Invite);
|
|
|
|
getComponent({ room });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should disable the checkbox", () => {
|
|
|
|
expect(getCheckbox()).toBeDisabled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should set the visibility to private by default", () => {
|
|
|
|
expect(getCheckbox()).not.toBeChecked();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-07-19 18:48:24 +08:00
|
|
|
it("should not show knock room join rule", async () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
|
|
|
|
const room = new Room(newRoomId, client, userId);
|
2023-09-08 09:00:48 +08:00
|
|
|
setRoomStateEvents(room, PreferredRoomVersions.KnockRooms);
|
|
|
|
getComponent({ room });
|
2023-07-19 18:48:24 +08:00
|
|
|
expect(screen.queryByText("Ask to join")).not.toBeInTheDocument();
|
|
|
|
});
|
2023-05-05 16:13:50 +08:00
|
|
|
});
|