2022-03-25 00:32:22 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-03-25 00:32:22 +03:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 16:57:16 +03:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-03-25 00:32:22 +03:00
|
|
|
*/
|
|
|
|
|
2024-03-18 17:40:52 +03:00
|
|
|
import { MatrixClient, MatrixEvent, Room, RoomMember, RoomState, RoomStateEvent } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2023-06-19 17:11:25 +03:00
|
|
|
import { mocked } from "jest-mock";
|
2022-03-25 00:32:22 +03:00
|
|
|
|
2023-09-19 14:24:35 +03:00
|
|
|
import { isKnockDenied, waitForMember } from "../../src/utils/membership";
|
|
|
|
import { createTestClient, mkRoomMember, stubClient } from "../test-utils";
|
|
|
|
|
|
|
|
describe("isKnockDenied", () => {
|
|
|
|
const userId = "alice";
|
|
|
|
let client: jest.Mocked<MatrixClient>;
|
|
|
|
let room: Room;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient() as jest.Mocked<MatrixClient>;
|
|
|
|
room = new Room("!room-id:example.com", client, "@user:example.com");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("checks that the user knock has been denied", () => {
|
2024-03-12 17:52:54 +03:00
|
|
|
const roomMember = mkRoomMember(room.roomId, userId, KnownMembership.Leave, true, {
|
|
|
|
membership: KnownMembership.Knock,
|
|
|
|
});
|
2023-09-19 14:24:35 +03:00
|
|
|
jest.spyOn(room, "getMember").mockReturnValue(roomMember);
|
|
|
|
expect(isKnockDenied(room)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2024-03-12 17:52:54 +03:00
|
|
|
{ membership: KnownMembership.Leave, isKicked: false, prevMembership: KnownMembership.Invite },
|
|
|
|
{ membership: KnownMembership.Leave, isKicked: true, prevMembership: KnownMembership.Invite },
|
|
|
|
{ membership: KnownMembership.Leave, isKicked: false, prevMembership: KnownMembership.Join },
|
|
|
|
{ membership: KnownMembership.Leave, isKicked: true, prevMembership: KnownMembership.Join },
|
2023-09-19 14:24:35 +03:00
|
|
|
])("checks that the user knock has been not denied", ({ membership, isKicked, prevMembership }) => {
|
|
|
|
const roomMember = mkRoomMember(room.roomId, userId, membership, isKicked, { membership: prevMembership });
|
|
|
|
jest.spyOn(room, "getMember").mockReturnValue(roomMember);
|
|
|
|
expect(isKnockDenied(room)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
2022-03-25 00:32:22 +03:00
|
|
|
|
|
|
|
/* Shorter timeout, we've got tests to run */
|
|
|
|
const timeout = 30;
|
|
|
|
|
|
|
|
describe("waitForMember", () => {
|
2023-06-19 17:11:25 +03:00
|
|
|
const STUB_ROOM_ID = "!stub_room:domain";
|
|
|
|
const STUB_MEMBER_ID = "!stub_member:domain";
|
|
|
|
|
|
|
|
let client: MatrixClient;
|
2022-03-25 00:32:22 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-06-19 17:11:25 +03:00
|
|
|
client = createTestClient();
|
|
|
|
|
|
|
|
// getRoom() only knows about !stub_room, which has only one member
|
|
|
|
const stubRoom = {
|
|
|
|
getMember: jest.fn().mockImplementation((userId) => {
|
|
|
|
return userId === STUB_MEMBER_ID ? ({} as RoomMember) : null;
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
mocked(client.getRoom).mockImplementation((roomId) => {
|
|
|
|
return roomId === STUB_ROOM_ID ? (stubRoom as unknown as Room) : null;
|
|
|
|
});
|
2022-03-25 00:32:22 +03:00
|
|
|
});
|
|
|
|
|
2023-03-01 18:23:35 +03:00
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
2022-03-25 00:32:22 +03:00
|
|
|
});
|
|
|
|
|
2023-03-01 18:23:35 +03:00
|
|
|
it("resolves with false if the timeout is reached", async () => {
|
2023-06-19 17:11:25 +03:00
|
|
|
const result = await waitForMember(client, "", "", { timeout: 0 });
|
2023-03-01 18:23:35 +03:00
|
|
|
expect(result).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("resolves with false if the timeout is reached, even if other RoomState.newMember events fire", async () => {
|
|
|
|
jest.useFakeTimers();
|
2022-03-25 00:32:22 +03:00
|
|
|
const roomId = "!roomId:domain";
|
|
|
|
const userId = "@clientId:domain";
|
2023-06-19 17:11:25 +03:00
|
|
|
const resultProm = waitForMember(client, roomId, userId, { timeout });
|
2023-03-01 18:23:35 +03:00
|
|
|
jest.advanceTimersByTime(50);
|
|
|
|
expect(await resultProm).toBe(false);
|
2023-06-19 17:11:25 +03:00
|
|
|
client.emit(
|
|
|
|
RoomStateEvent.NewMember,
|
|
|
|
undefined as unknown as MatrixEvent,
|
|
|
|
undefined as unknown as RoomState,
|
|
|
|
{
|
|
|
|
roomId,
|
|
|
|
userId: "@anotherClient:domain",
|
|
|
|
} as RoomMember,
|
|
|
|
);
|
2023-03-01 18:23:35 +03:00
|
|
|
jest.useRealTimers();
|
2022-03-25 00:32:22 +03:00
|
|
|
});
|
|
|
|
|
2023-03-01 18:23:35 +03:00
|
|
|
it("resolves with true if RoomState.newMember fires", async () => {
|
2022-03-25 00:32:22 +03:00
|
|
|
const roomId = "!roomId:domain";
|
|
|
|
const userId = "@clientId:domain";
|
2023-06-19 17:11:25 +03:00
|
|
|
const resultProm = waitForMember(client, roomId, userId, { timeout });
|
|
|
|
client.emit(
|
|
|
|
RoomStateEvent.NewMember,
|
|
|
|
undefined as unknown as MatrixEvent,
|
|
|
|
undefined as unknown as RoomState,
|
|
|
|
{ roomId, userId } as RoomMember,
|
|
|
|
);
|
2023-03-01 18:23:35 +03:00
|
|
|
expect(await resultProm).toBe(true);
|
2022-03-25 00:32:22 +03:00
|
|
|
});
|
2023-06-19 17:11:25 +03:00
|
|
|
|
|
|
|
it("resolves immediately if the user is already a member", async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const resultProm = waitForMember(client, STUB_ROOM_ID, STUB_MEMBER_ID, { timeout });
|
|
|
|
expect(await resultProm).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("waits for the timeout if the room is known but the user is not", async () => {
|
|
|
|
const result = await waitForMember(client, STUB_ROOM_ID, "@other_user", { timeout: 0 });
|
|
|
|
expect(result).toBe(false);
|
|
|
|
});
|
2022-03-25 00:32:22 +03:00
|
|
|
});
|