2022-09-16 09:03:17 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-09-16 09:03:17 +01:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-09-16 09:03:17 +01:00
|
|
|
*/
|
|
|
|
|
|
2026-07-22 13:18:04 +00:00
|
|
|
// @vitest-environment happy-dom
|
|
|
|
|
|
|
|
|
|
import { vi, describe, it, expect, afterAll, beforeEach, afterEach, type Mocked } from "vitest";
|
|
|
|
|
|
2022-09-16 09:03:17 +01:00
|
|
|
import React from "react";
|
2026-07-22 13:18:04 +00:00
|
|
|
import { findByText, fireEvent, render, screen } from "test-utils-rtl";
|
2023-02-28 10:39:35 +01:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2026-04-22 21:05:31 +01:00
|
|
|
import { type MatrixClient, MatrixError, Room, RoomType } from "matrix-js-sdk/src/matrix";
|
2024-03-18 14:40:52 +00:00
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2023-04-05 13:13:51 +02:00
|
|
|
import { sleep } from "matrix-js-sdk/src/utils";
|
2026-04-22 21:05:31 +01:00
|
|
|
import { UserVerificationStatus } from "matrix-js-sdk/src/crypto-api";
|
2023-04-05 13:13:51 +02:00
|
|
|
import {
|
2024-07-05 14:39:13 +01:00
|
|
|
clearAllModals,
|
2023-04-05 13:13:51 +02:00
|
|
|
filterConsole,
|
2023-06-15 11:07:26 +02:00
|
|
|
flushPromises,
|
2023-04-05 13:13:51 +02:00
|
|
|
getMockClientWithEventEmitter,
|
|
|
|
|
mkMembership,
|
|
|
|
|
mkMessage,
|
|
|
|
|
mkRoomCreateEvent,
|
2026-07-22 13:18:04 +00:00
|
|
|
TestSDKContext,
|
|
|
|
|
} from "test-utils";
|
2022-09-16 09:03:17 +01:00
|
|
|
|
2026-07-22 13:18:04 +00:00
|
|
|
import InviteDialog from "./InviteDialog";
|
|
|
|
|
import { InviteKind } from "./InviteDialogTypes";
|
|
|
|
|
import DMRoomMap from "../../../utils/DMRoomMap";
|
|
|
|
|
import SdkConfig from "../../../SdkConfig";
|
|
|
|
|
import { type ValidatedServerConfig } from "../../../utils/ValidatedServerConfig";
|
|
|
|
|
import { type IConfigOptions } from "../../../IConfigOptions";
|
|
|
|
|
import { SDKContextClass } from "../../../contexts/SDKContextClass";
|
|
|
|
|
import { type IProfileInfo } from "../../../hooks/useProfileInfo";
|
|
|
|
|
import { DirectoryMember, startDmOnFirstMessage } from "../../../utils/direct-messages";
|
|
|
|
|
import { MatrixClientPeg } from "../../../MatrixClientPeg.ts";
|
2022-10-12 18:59:07 +01:00
|
|
|
|
2026-07-22 13:18:04 +00:00
|
|
|
const mockGetAccessToken = vi.fn().mockResolvedValue("getAccessToken");
|
|
|
|
|
vi.mock("../../../IdentityAuthClient", () => ({
|
|
|
|
|
default: vi.fn().mockImplementation(function () {
|
|
|
|
|
return {
|
|
|
|
|
getAccessToken: mockGetAccessToken,
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../../../utils/direct-messages", async () => ({
|
|
|
|
|
...(await vi.importActual("../../../utils/direct-messages")),
|
|
|
|
|
startDmOnFirstMessage: vi.fn(),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../../../dispatcher/dispatcher");
|
|
|
|
|
|
|
|
|
|
vi.mock("lodash", async () => ({
|
|
|
|
|
...(await vi.importActual("lodash")),
|
|
|
|
|
// Stub out the debounce to prevent async leaks
|
|
|
|
|
debounce: vi.fn((fn) => {
|
|
|
|
|
fn.cancel = vi.fn();
|
|
|
|
|
return fn;
|
|
|
|
|
}),
|
2023-04-05 13:13:51 +02:00
|
|
|
}));
|
|
|
|
|
|
2023-03-06 12:08:17 +01:00
|
|
|
const getSearchField = () => screen.getByTestId("invite-dialog-input");
|
|
|
|
|
|
|
|
|
|
const enterIntoSearchField = async (value: string) => {
|
|
|
|
|
const searchField = getSearchField();
|
|
|
|
|
await userEvent.clear(searchField);
|
|
|
|
|
await userEvent.type(searchField, value + "{enter}");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const pasteIntoSearchField = async (value: string) => {
|
|
|
|
|
const searchField = getSearchField();
|
|
|
|
|
await userEvent.clear(searchField);
|
|
|
|
|
searchField.focus();
|
|
|
|
|
await userEvent.paste(value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const expectPill = (value: string) => {
|
|
|
|
|
expect(screen.getByText(value)).toBeInTheDocument();
|
|
|
|
|
expect(getSearchField()).toHaveValue("");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const expectNoPill = (value: string) => {
|
|
|
|
|
expect(screen.queryByText(value)).not.toBeInTheDocument();
|
|
|
|
|
expect(getSearchField()).toHaveValue(value);
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-11 09:22:10 +01:00
|
|
|
const serverDomain = "example.org";
|
2023-04-05 13:13:51 +02:00
|
|
|
const roomId = "!111111111111111111:example.org";
|
|
|
|
|
const aliceId = "@alice:example.org";
|
|
|
|
|
const aliceEmail = "foobar@email.com";
|
|
|
|
|
const bobId = "@bob:example.org";
|
|
|
|
|
const bobEmail = "bobbob@example.com"; // bob@example.com is already used as an example in the invite dialog
|
|
|
|
|
const carolId = "@carol:example.com";
|
2023-08-04 09:19:56 +02:00
|
|
|
const bobbob = "bobbob";
|
2023-04-05 13:13:51 +02:00
|
|
|
|
|
|
|
|
const aliceProfileInfo: IProfileInfo = {
|
|
|
|
|
user_id: aliceId,
|
|
|
|
|
display_name: "Alice",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const bobProfileInfo: IProfileInfo = {
|
|
|
|
|
user_id: bobId,
|
|
|
|
|
display_name: "Bob",
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-16 09:03:17 +01:00
|
|
|
describe("InviteDialog", () => {
|
2023-04-05 13:13:51 +02:00
|
|
|
let mockClient: Mocked<MatrixClient>;
|
2023-02-14 13:21:29 +01:00
|
|
|
let room: Room;
|
2026-07-06 17:09:21 +01:00
|
|
|
let sdkContext: TestSDKContext;
|
2022-09-16 09:03:17 +01:00
|
|
|
|
2023-04-05 13:13:51 +02:00
|
|
|
filterConsole(
|
|
|
|
|
"Error retrieving profile for userId @carol:example.com",
|
|
|
|
|
"Error retrieving profile for userId @localpart:server.tld",
|
|
|
|
|
"Error retrieving profile for userId @localpart:server:tld",
|
|
|
|
|
"[Invite:Recents] Excluding @alice:example.org from recents",
|
|
|
|
|
);
|
|
|
|
|
|
2022-09-16 09:03:17 +01:00
|
|
|
beforeEach(() => {
|
2023-04-05 13:13:51 +02:00
|
|
|
mockClient = getMockClientWithEventEmitter({
|
2026-07-22 13:18:04 +00:00
|
|
|
getCrypto: vi.fn().mockReturnValue({
|
|
|
|
|
getUserVerificationStatus: vi
|
2026-04-22 21:05:31 +01:00
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValue(new UserVerificationStatus(false, false, true, false)),
|
|
|
|
|
}),
|
2026-07-22 13:18:04 +00:00
|
|
|
getDomain: vi.fn().mockReturnValue(serverDomain),
|
|
|
|
|
getUserId: vi.fn().mockReturnValue(bobId),
|
|
|
|
|
getSafeUserId: vi.fn().mockReturnValue(bobId),
|
|
|
|
|
isGuest: vi.fn().mockReturnValue(false),
|
|
|
|
|
getVisibleRooms: vi.fn().mockReturnValue([]),
|
|
|
|
|
getRoom: vi.fn(),
|
|
|
|
|
getRooms: vi.fn(),
|
|
|
|
|
getAccountData: vi.fn(),
|
|
|
|
|
getPushActionsForEvent: vi.fn(),
|
|
|
|
|
mxcUrlToHttp: vi.fn().mockReturnValue(""),
|
|
|
|
|
isRoomEncrypted: vi.fn().mockReturnValue(false),
|
|
|
|
|
getProfileInfo: vi.fn().mockImplementation(async (userId: string) => {
|
2023-04-05 13:13:51 +02:00
|
|
|
if (userId === aliceId) return aliceProfileInfo;
|
|
|
|
|
if (userId === bobId) return bobProfileInfo;
|
|
|
|
|
|
|
|
|
|
throw new MatrixError({
|
2026-04-02 16:31:29 +01:00
|
|
|
errcode: "M_UNKNOWN",
|
2023-04-05 13:13:51 +02:00
|
|
|
error: "Profile not found",
|
|
|
|
|
});
|
|
|
|
|
}),
|
2026-07-22 13:18:04 +00:00
|
|
|
getIdentityServerUrl: vi.fn(),
|
|
|
|
|
searchUserDirectory: vi.fn().mockResolvedValue({}),
|
|
|
|
|
lookupThreePid: vi.fn(),
|
|
|
|
|
registerWithIdentityServer: vi.fn().mockResolvedValue({
|
2023-04-05 13:13:51 +02:00
|
|
|
access_token: "access_token",
|
|
|
|
|
token: "token",
|
|
|
|
|
}),
|
2026-07-22 13:18:04 +00:00
|
|
|
getOpenIdToken: vi.fn().mockResolvedValue({}),
|
|
|
|
|
getIdentityAccount: vi.fn().mockResolvedValue({}),
|
|
|
|
|
getTerms: vi.fn().mockResolvedValue({ policies: [] }),
|
|
|
|
|
supportsThreads: vi.fn().mockReturnValue(false),
|
|
|
|
|
isInitialSyncComplete: vi.fn().mockReturnValue(true),
|
|
|
|
|
getClientWellKnown: vi.fn().mockResolvedValue({}),
|
|
|
|
|
invite: vi.fn(),
|
2023-04-05 13:13:51 +02:00
|
|
|
});
|
2022-09-16 09:03:17 +01:00
|
|
|
SdkConfig.put({ validated_server_config: {} as ValidatedServerConfig } as IConfigOptions);
|
2023-05-23 16:24:12 +01:00
|
|
|
DMRoomMap.makeShared(mockClient);
|
2026-07-22 13:18:04 +00:00
|
|
|
vi.clearAllMocks();
|
|
|
|
|
vi.spyOn(MatrixClientPeg, "safeGet").mockReturnValue(mockClient);
|
2022-09-16 09:03:17 +01:00
|
|
|
|
2023-02-14 13:21:29 +01:00
|
|
|
room = new Room(roomId, mockClient, mockClient.getSafeUserId());
|
2024-11-06 14:55:21 +00:00
|
|
|
room.addLiveEvents(
|
|
|
|
|
[
|
|
|
|
|
mkMessage({
|
|
|
|
|
msg: "Hello",
|
|
|
|
|
relatesTo: undefined,
|
|
|
|
|
event: true,
|
|
|
|
|
room: roomId,
|
|
|
|
|
user: mockClient.getSafeUserId(),
|
|
|
|
|
ts: Date.now(),
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
{ addToState: true },
|
|
|
|
|
);
|
2023-02-14 13:21:29 +01:00
|
|
|
room.currentState.setStateEvents([
|
|
|
|
|
mkRoomCreateEvent(bobId, roomId),
|
|
|
|
|
mkMembership({
|
|
|
|
|
event: true,
|
|
|
|
|
room: roomId,
|
2024-03-12 14:52:54 +00:00
|
|
|
mship: KnownMembership.Join,
|
2023-02-14 13:21:29 +01:00
|
|
|
user: aliceId,
|
|
|
|
|
skey: aliceId,
|
|
|
|
|
}),
|
|
|
|
|
]);
|
2026-07-22 13:18:04 +00:00
|
|
|
vi.spyOn(DMRoomMap.shared(), "getUniqueRoomsWithIndividuals").mockReturnValue({
|
2023-02-14 13:21:29 +01:00
|
|
|
[aliceId]: room,
|
|
|
|
|
});
|
2022-09-16 09:03:17 +01:00
|
|
|
mockClient.getRooms.mockReturnValue([room]);
|
|
|
|
|
mockClient.getRoom.mockReturnValue(room);
|
2023-04-05 13:13:51 +02:00
|
|
|
|
2026-07-06 17:09:21 +01:00
|
|
|
sdkContext = new TestSDKContext();
|
|
|
|
|
// @ts-ignore UserMenuViewModel uses SDKContext in the constructor
|
|
|
|
|
SDKContextClass.instance = sdkContext;
|
|
|
|
|
sdkContext._client = mockClient;
|
2023-04-05 13:13:51 +02:00
|
|
|
});
|
|
|
|
|
|
2024-07-05 14:39:13 +01:00
|
|
|
afterEach(async () => {
|
|
|
|
|
await clearAllModals();
|
2026-07-01 13:49:18 +01:00
|
|
|
SDKContextClass.instance.onLoggedOut();
|
2022-09-16 09:03:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterAll(() => {
|
2026-07-22 13:18:04 +00:00
|
|
|
vi.restoreAllMocks();
|
2022-09-16 09:03:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should label with space name", () => {
|
2026-07-22 13:18:04 +00:00
|
|
|
room.isSpaceRoom = vi.fn().mockReturnValue(true);
|
|
|
|
|
room.getType = vi.fn().mockReturnValue(RoomType.Space);
|
2023-02-14 13:21:29 +01:00
|
|
|
room.name = "Space";
|
2026-07-22 13:18:04 +00:00
|
|
|
render(<InviteDialog kind={InviteKind.Invite} roomId={roomId} onFinished={vi.fn()} />);
|
2022-09-16 09:03:17 +01:00
|
|
|
|
|
|
|
|
expect(screen.queryByText("Invite to Space")).toBeTruthy();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should label with room name", () => {
|
2026-07-22 13:18:04 +00:00
|
|
|
render(<InviteDialog kind={InviteKind.Invite} roomId={roomId} onFinished={vi.fn()} />);
|
2023-02-14 13:21:29 +01:00
|
|
|
expect(screen.getByText(`Invite to ${roomId}`)).toBeInTheDocument();
|
2022-09-16 09:03:17 +01:00
|
|
|
});
|
|
|
|
|
|
2023-06-15 11:07:26 +02:00
|
|
|
it("should not suggest valid unknown MXIDs", async () => {
|
2022-09-16 09:03:17 +01:00
|
|
|
render(
|
|
|
|
|
<InviteDialog
|
2023-02-28 10:31:48 +00:00
|
|
|
kind={InviteKind.Invite}
|
2022-09-16 09:03:17 +01:00
|
|
|
roomId={roomId}
|
2026-07-22 13:18:04 +00:00
|
|
|
onFinished={vi.fn()}
|
2022-09-16 09:03:17 +01:00
|
|
|
initialText="@localpart:server.tld"
|
|
|
|
|
/>,
|
|
|
|
|
);
|
2023-06-15 11:07:26 +02:00
|
|
|
await flushPromises();
|
|
|
|
|
expect(screen.queryByText("@localpart:server.tld")).not.toBeInTheDocument();
|
2022-09-16 09:03:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should not suggest invalid MXIDs", () => {
|
|
|
|
|
render(
|
|
|
|
|
<InviteDialog
|
2023-02-28 10:31:48 +00:00
|
|
|
kind={InviteKind.Invite}
|
2022-09-16 09:03:17 +01:00
|
|
|
roomId={roomId}
|
2026-07-22 13:18:04 +00:00
|
|
|
onFinished={vi.fn()}
|
2022-09-16 09:03:17 +01:00
|
|
|
initialText="@localpart:server:tld"
|
|
|
|
|
/>,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(screen.queryByText("@localpart:server:tld")).toBeFalsy();
|
|
|
|
|
});
|
2022-10-12 18:59:07 +01:00
|
|
|
|
2023-02-28 10:31:48 +00:00
|
|
|
it.each([[InviteKind.Dm], [InviteKind.Invite]] as [typeof InviteKind.Dm | typeof InviteKind.Invite][])(
|
2023-02-14 13:21:29 +01:00
|
|
|
"should lookup inputs which look like email addresses (%s)",
|
2023-02-28 10:31:48 +00:00
|
|
|
async (kind: typeof InviteKind.Dm | typeof InviteKind.Invite) => {
|
2023-02-14 13:21:29 +01:00
|
|
|
mockClient.getIdentityServerUrl.mockReturnValue("https://identity-server");
|
|
|
|
|
mockClient.lookupThreePid.mockResolvedValue({
|
|
|
|
|
address: aliceEmail,
|
|
|
|
|
medium: "email",
|
|
|
|
|
mxid: aliceId,
|
|
|
|
|
});
|
|
|
|
|
mockClient.getProfileInfo.mockResolvedValue({
|
|
|
|
|
displayname: "Mrs Alice",
|
|
|
|
|
avatar_url: "mxc://foo/bar",
|
|
|
|
|
});
|
2022-10-12 18:59:07 +01:00
|
|
|
|
2023-02-14 13:21:29 +01:00
|
|
|
render(
|
|
|
|
|
<InviteDialog
|
|
|
|
|
kind={kind}
|
2023-02-28 10:31:48 +00:00
|
|
|
roomId={kind === InviteKind.Invite ? roomId : ""}
|
2026-07-22 13:18:04 +00:00
|
|
|
onFinished={vi.fn()}
|
2023-02-14 13:21:29 +01:00
|
|
|
initialText={aliceEmail}
|
|
|
|
|
/>,
|
|
|
|
|
);
|
2022-10-12 18:59:07 +01:00
|
|
|
|
2023-02-14 13:21:29 +01:00
|
|
|
await screen.findByText("Mrs Alice");
|
|
|
|
|
// expect the email and MXID to be visible
|
|
|
|
|
await screen.findByText(aliceId);
|
|
|
|
|
await screen.findByText(aliceEmail);
|
|
|
|
|
expect(mockClient.lookupThreePid).toHaveBeenCalledWith("email", aliceEmail, expect.anything());
|
|
|
|
|
expect(mockClient.getProfileInfo).toHaveBeenCalledWith(aliceId);
|
|
|
|
|
},
|
|
|
|
|
);
|
2022-10-12 18:59:07 +01:00
|
|
|
|
|
|
|
|
it("should suggest e-mail even if lookup fails", async () => {
|
|
|
|
|
mockClient.getIdentityServerUrl.mockReturnValue("https://identity-server");
|
|
|
|
|
mockClient.lookupThreePid.mockResolvedValue({});
|
|
|
|
|
|
|
|
|
|
render(
|
2023-02-28 10:31:48 +00:00
|
|
|
<InviteDialog
|
|
|
|
|
kind={InviteKind.Invite}
|
|
|
|
|
roomId={roomId}
|
2026-07-22 13:18:04 +00:00
|
|
|
onFinished={vi.fn()}
|
2023-02-28 10:31:48 +00:00
|
|
|
initialText="foobar@email.com"
|
|
|
|
|
/>,
|
2022-10-12 18:59:07 +01:00
|
|
|
);
|
|
|
|
|
|
2026-07-22 13:18:04 +00:00
|
|
|
await expect(screen.findByText("foobar@email.com")).resolves.toBeVisible();
|
|
|
|
|
await expect(screen.findByText("Invite by email")).resolves.toBeVisible();
|
2022-10-12 18:59:07 +01:00
|
|
|
});
|
2023-02-28 10:39:35 +01:00
|
|
|
|
|
|
|
|
it("should add pasted values", async () => {
|
|
|
|
|
mockClient.getIdentityServerUrl.mockReturnValue("https://identity-server");
|
|
|
|
|
mockClient.lookupThreePid.mockResolvedValue({});
|
|
|
|
|
|
2026-07-22 13:18:04 +00:00
|
|
|
render(<InviteDialog kind={InviteKind.Invite} roomId={roomId} onFinished={vi.fn()} />);
|
2023-02-28 10:39:35 +01:00
|
|
|
|
|
|
|
|
const input = screen.getByTestId("invite-dialog-input");
|
|
|
|
|
input.focus();
|
|
|
|
|
await userEvent.paste(`${bobId} ${aliceEmail}`);
|
|
|
|
|
|
2023-04-05 13:13:51 +02:00
|
|
|
await screen.findAllByText(bobId);
|
2023-02-28 10:39:35 +01:00
|
|
|
await screen.findByText(aliceEmail);
|
|
|
|
|
expect(input).toHaveValue("");
|
|
|
|
|
});
|
2023-08-04 09:19:56 +02:00
|
|
|
it("should support pasting one username that is not a mx id or email", async () => {
|
|
|
|
|
mockClient.getIdentityServerUrl.mockReturnValue("https://identity-server");
|
|
|
|
|
mockClient.lookupThreePid.mockResolvedValue({});
|
|
|
|
|
|
2026-07-22 13:18:04 +00:00
|
|
|
render(<InviteDialog kind={InviteKind.Invite} roomId={roomId} onFinished={vi.fn()} />);
|
2023-08-04 09:19:56 +02:00
|
|
|
|
|
|
|
|
const input = screen.getByTestId("invite-dialog-input");
|
|
|
|
|
input.focus();
|
|
|
|
|
await userEvent.paste(`${bobbob}`);
|
|
|
|
|
|
|
|
|
|
await screen.findAllByText(bobId);
|
|
|
|
|
expect(input).toHaveValue(`${bobbob}`);
|
|
|
|
|
});
|
2023-03-06 12:08:17 +01:00
|
|
|
|
|
|
|
|
it("should allow to invite multiple emails to a room", async () => {
|
2026-07-22 13:18:04 +00:00
|
|
|
render(<InviteDialog kind={InviteKind.Invite} roomId={roomId} onFinished={vi.fn()} />);
|
2023-03-06 12:08:17 +01:00
|
|
|
|
|
|
|
|
await enterIntoSearchField(aliceEmail);
|
|
|
|
|
expectPill(aliceEmail);
|
|
|
|
|
|
|
|
|
|
await enterIntoSearchField(bobEmail);
|
|
|
|
|
expectPill(bobEmail);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("when encryption by default is disabled", () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
mockClient.getClientWellKnown.mockReturnValue({
|
|
|
|
|
"io.element.e2ee": {
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should allow to invite more than one email to a DM", async () => {
|
2026-07-22 13:18:04 +00:00
|
|
|
render(<InviteDialog kind={InviteKind.Dm} onFinished={vi.fn()} />);
|
2023-03-06 12:08:17 +01:00
|
|
|
|
|
|
|
|
await enterIntoSearchField(aliceEmail);
|
|
|
|
|
expectPill(aliceEmail);
|
|
|
|
|
|
|
|
|
|
await enterIntoSearchField(bobEmail);
|
|
|
|
|
expectPill(bobEmail);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should not allow to invite more than one email to a DM", async () => {
|
2026-07-22 13:18:04 +00:00
|
|
|
render(<InviteDialog kind={InviteKind.Dm} onFinished={vi.fn()} />);
|
2023-03-06 12:08:17 +01:00
|
|
|
|
|
|
|
|
// Start with an email → should convert to a pill
|
|
|
|
|
await enterIntoSearchField(aliceEmail);
|
|
|
|
|
expect(screen.getByText("Invites by email can only be sent one at a time")).toBeInTheDocument();
|
|
|
|
|
expectPill(aliceEmail);
|
|
|
|
|
|
|
|
|
|
// Everything else from now on should not convert to a pill
|
|
|
|
|
|
|
|
|
|
await enterIntoSearchField(bobEmail);
|
|
|
|
|
expectNoPill(bobEmail);
|
|
|
|
|
|
|
|
|
|
await enterIntoSearchField(aliceId);
|
|
|
|
|
expectNoPill(aliceId);
|
|
|
|
|
|
|
|
|
|
await pasteIntoSearchField(bobEmail);
|
|
|
|
|
expectNoPill(bobEmail);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should not allow to invite a MXID and an email to a DM", async () => {
|
2026-07-22 13:18:04 +00:00
|
|
|
render(<InviteDialog kind={InviteKind.Dm} onFinished={vi.fn()} />);
|
2023-03-06 12:08:17 +01:00
|
|
|
|
|
|
|
|
// Start with a MXID → should convert to a pill
|
2023-04-05 13:13:51 +02:00
|
|
|
await enterIntoSearchField(carolId);
|
2023-03-06 12:08:17 +01:00
|
|
|
expect(screen.queryByText("Invites by email can only be sent one at a time")).not.toBeInTheDocument();
|
2023-04-05 13:13:51 +02:00
|
|
|
expectPill(carolId);
|
2023-03-06 12:08:17 +01:00
|
|
|
|
|
|
|
|
// Add an email → should not convert to a pill
|
|
|
|
|
await enterIntoSearchField(bobEmail);
|
|
|
|
|
expect(screen.getByText("Invites by email can only be sent one at a time")).toBeInTheDocument();
|
|
|
|
|
expectNoPill(bobEmail);
|
|
|
|
|
});
|
2023-04-05 13:13:51 +02:00
|
|
|
|
|
|
|
|
it("should start a DM if the profile is available", async () => {
|
2026-07-22 13:18:04 +00:00
|
|
|
render(<InviteDialog kind={InviteKind.Dm} onFinished={vi.fn()} />);
|
2023-04-05 13:13:51 +02:00
|
|
|
await enterIntoSearchField(aliceId);
|
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: "Go" }));
|
|
|
|
|
expect(startDmOnFirstMessage).toHaveBeenCalledWith(mockClient, [
|
|
|
|
|
new DirectoryMember({
|
|
|
|
|
user_id: aliceId,
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2023-07-24 10:12:37 +01:00
|
|
|
it("should not allow pasting the same user multiple times", async () => {
|
2026-07-22 13:18:04 +00:00
|
|
|
render(<InviteDialog kind={InviteKind.Invite} roomId={roomId} onFinished={vi.fn()} />);
|
2023-07-24 10:12:37 +01:00
|
|
|
|
|
|
|
|
const input = screen.getByTestId("invite-dialog-input");
|
|
|
|
|
input.focus();
|
|
|
|
|
await userEvent.paste(`${bobId}`);
|
|
|
|
|
await userEvent.paste(`${bobId}`);
|
|
|
|
|
await userEvent.paste(`${bobId}`);
|
|
|
|
|
|
|
|
|
|
expect(input).toHaveValue("");
|
|
|
|
|
await expect(screen.findAllByText(bobId, { selector: "a" })).resolves.toHaveLength(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should add to selection on click of user tile", async () => {
|
2026-07-22 13:18:04 +00:00
|
|
|
render(<InviteDialog kind={InviteKind.Invite} roomId={roomId} onFinished={vi.fn()} />);
|
2023-07-24 10:12:37 +01:00
|
|
|
|
|
|
|
|
const input = screen.getByTestId("invite-dialog-input");
|
|
|
|
|
input.focus();
|
|
|
|
|
await userEvent.keyboard(`${aliceId}`);
|
|
|
|
|
|
2025-09-23 14:29:22 +02:00
|
|
|
const btn = await screen.findByRole("option", { name: aliceId });
|
2023-07-24 10:12:37 +01:00
|
|
|
fireEvent.click(btn);
|
|
|
|
|
|
2025-10-01 11:03:43 +02:00
|
|
|
const tile = await findByText(screen.getByTestId("invite-dialog-input-wrapper"), aliceId);
|
2023-07-24 10:12:37 +01:00
|
|
|
expect(tile).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-22 16:10:42 +01:00
|
|
|
describe("while the invite is in progress", () => {
|
|
|
|
|
it("should show a spinner", async () => {
|
|
|
|
|
mockClient.invite.mockReturnValue(new Promise(() => {}));
|
|
|
|
|
|
2026-07-22 13:18:04 +00:00
|
|
|
render(<InviteDialog kind={InviteKind.Invite} roomId={roomId} onFinished={vi.fn()} />);
|
2025-08-22 16:10:42 +01:00
|
|
|
await enterIntoSearchField(bobId);
|
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: "Invite" }));
|
|
|
|
|
|
2026-07-22 13:18:04 +00:00
|
|
|
await expect(screen.findByText("Preparing invitations...")).resolves.toBeVisible();
|
2025-08-22 16:10:42 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-04-05 13:13:51 +02:00
|
|
|
describe("when inviting a user with an unknown profile", () => {
|
|
|
|
|
beforeEach(async () => {
|
2026-07-22 13:18:04 +00:00
|
|
|
vi.mocked(startDmOnFirstMessage).mockClear();
|
|
|
|
|
render(<InviteDialog kind={InviteKind.Dm} onFinished={vi.fn()} />);
|
2023-04-05 13:13:51 +02:00
|
|
|
await enterIntoSearchField(carolId);
|
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: "Go" }));
|
|
|
|
|
// modal rendering has some weird sleeps - fake timers will mess up the entire test
|
|
|
|
|
await sleep(100);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should start the DM directly", () => {
|
|
|
|
|
expect(startDmOnFirstMessage).toHaveBeenCalledWith(mockClient, [
|
|
|
|
|
new DirectoryMember({
|
|
|
|
|
user_id: carolId,
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-10-25 12:08:10 +02:00
|
|
|
|
|
|
|
|
it("should not suggest users from other server when room has m.federate=false", async () => {
|
|
|
|
|
room.currentState.setStateEvents([mkRoomCreateEvent(bobId, roomId, { "m.federate": false })]);
|
|
|
|
|
|
|
|
|
|
render(
|
|
|
|
|
<InviteDialog
|
|
|
|
|
kind={InviteKind.Invite}
|
|
|
|
|
roomId={roomId}
|
2026-07-22 13:18:04 +00:00
|
|
|
onFinished={vi.fn()}
|
2023-10-25 12:08:10 +02:00
|
|
|
initialText="@localpart:server.tld"
|
|
|
|
|
/>,
|
|
|
|
|
);
|
|
|
|
|
await flushPromises();
|
|
|
|
|
expect(screen.queryByText("@localpart:server.tld")).not.toBeInTheDocument();
|
|
|
|
|
});
|
2026-04-22 21:05:31 +01:00
|
|
|
|
|
|
|
|
describe("when inviting a user whose cryptographic identity we do not know", () => {
|
|
|
|
|
beforeEach(() => {
|
2026-07-22 13:18:04 +00:00
|
|
|
vi.mocked(mockClient.getCrypto()!.getUserVerificationStatus).mockImplementation(async (u) => {
|
2026-04-22 21:05:31 +01:00
|
|
|
return new UserVerificationStatus(false, false, false, false);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe.each([InviteKind.Invite, InviteKind.Dm])("with invitekind '%s'", (kind) => {
|
|
|
|
|
const goButtonName = kind == InviteKind.Invite ? "Invite" : "Go";
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
render(
|
|
|
|
|
<InviteDialog
|
|
|
|
|
kind={kind as InviteKind.Invite | InviteKind.Dm}
|
|
|
|
|
roomId={roomId}
|
2026-07-22 13:18:04 +00:00
|
|
|
onFinished={vi.fn()}
|
2026-04-22 21:05:31 +01:00
|
|
|
/>,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should show a warning when inviting by user id", async () => {
|
|
|
|
|
await enterIntoSearchField(aliceId);
|
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: goButtonName }));
|
|
|
|
|
await screen.findByText("Confirm inviting them", { exact: false });
|
|
|
|
|
|
2026-07-22 13:18:04 +00:00
|
|
|
expect(vi.mocked(mockClient.getCrypto()!.getUserVerificationStatus)).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(vi.mocked(mockClient.getCrypto()!.getUserVerificationStatus)).toHaveBeenCalledWith(aliceId);
|
2026-04-22 21:05:31 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should show a warning when inviting by email address", async () => {
|
|
|
|
|
await enterIntoSearchField("aaa@bbb");
|
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: goButtonName }));
|
|
|
|
|
await screen.findByText("Confirm inviting them", { exact: false });
|
|
|
|
|
|
|
|
|
|
// We shouldn't call getUserVerificationStatus on an email address
|
2026-07-22 13:18:04 +00:00
|
|
|
expect(vi.mocked(mockClient.getCrypto()!.getUserVerificationStatus)).not.toHaveBeenCalled();
|
2026-04-22 21:05:31 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2022-09-16 09:03:17 +01:00
|
|
|
});
|