2022-05-05 07:02:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
2022-05-05 07:02:48 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
|
|
|
|
import Modal, { ModalManager } from "../../src/Modal";
|
|
|
|
import SettingsStore from "../../src/settings/SettingsStore";
|
|
|
|
import MultiInviter, { CompletionStates } from "../../src/utils/MultiInviter";
|
|
|
|
import * as TestUtilsMatrix from "../test-utils";
|
2022-05-05 07:02:48 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const ROOMID = "!room:server";
|
2022-05-05 07:02:48 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const MXID1 = "@user1:server";
|
|
|
|
const MXID2 = "@user2:server";
|
|
|
|
const MXID3 = "@user3:server";
|
2022-05-05 07:02:48 +00:00
|
|
|
|
|
|
|
const MXID_PROFILE_STATES = {
|
|
|
|
[MXID1]: Promise.resolve({}),
|
2022-12-12 11:24:14 +00:00
|
|
|
[MXID2]: Promise.reject({ errcode: "M_FORBIDDEN" }),
|
|
|
|
[MXID3]: Promise.reject({ errcode: "M_NOT_FOUND" }),
|
2022-05-05 07:02:48 +00:00
|
|
|
};
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.mock("../../src/Modal", () => ({
|
2022-06-14 16:51:51 +00:00
|
|
|
createDialog: jest.fn(),
|
2022-05-05 07:02:48 +00:00
|
|
|
}));
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.mock("../../src/settings/SettingsStore", () => ({
|
2022-05-05 07:02:48 +00:00
|
|
|
getValue: jest.fn(),
|
2022-11-10 08:38:48 +00:00
|
|
|
monitorSetting: jest.fn(),
|
2022-12-06 09:59:17 +00:00
|
|
|
watchSetting: jest.fn(),
|
2022-05-05 07:02:48 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
const mockPromptBeforeInviteUnknownUsers = (value: boolean) => {
|
|
|
|
mocked(SettingsStore.getValue).mockImplementation(
|
|
|
|
(settingName: string, roomId: string = null, _excludeDefault = false): any => {
|
2022-12-12 11:24:14 +00:00
|
|
|
if (settingName === "promptBeforeInviteUnknownUsers" && roomId === ROOMID) {
|
2022-05-05 07:02:48 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const mockCreateTrackedDialog = (callbackName: "onInviteAnyways" | "onGiveUp") => {
|
|
|
|
mocked(Modal.createDialog).mockImplementation((...rest: Parameters<ModalManager["createDialog"]>): any => {
|
|
|
|
rest[1][callbackName]();
|
|
|
|
});
|
2022-05-05 07:02:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const expectAllInvitedResult = (result: CompletionStates) => {
|
|
|
|
expect(result).toEqual({
|
2022-12-12 11:24:14 +00:00
|
|
|
[MXID1]: "invited",
|
|
|
|
[MXID2]: "invited",
|
|
|
|
[MXID3]: "invited",
|
2022-05-05 07:02:48 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("MultiInviter", () => {
|
2022-05-05 07:02:48 +00:00
|
|
|
let client: jest.Mocked<MatrixClient>;
|
|
|
|
let inviter: MultiInviter;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
|
|
|
|
TestUtilsMatrix.stubClient();
|
|
|
|
client = MatrixClientPeg.get() as jest.Mocked<MatrixClient>;
|
|
|
|
|
|
|
|
client.invite = jest.fn();
|
|
|
|
client.invite.mockResolvedValue({});
|
|
|
|
|
|
|
|
client.getProfileInfo = jest.fn();
|
|
|
|
client.getProfileInfo.mockImplementation((userId: string) => {
|
|
|
|
return MXID_PROFILE_STATES[userId] || Promise.reject();
|
|
|
|
});
|
|
|
|
|
|
|
|
inviter = new MultiInviter(ROOMID);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("invite", () => {
|
|
|
|
describe("with promptBeforeInviteUnknownUsers = false", () => {
|
2022-05-05 07:02:48 +00:00
|
|
|
beforeEach(() => mockPromptBeforeInviteUnknownUsers(false));
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should invite all users", async () => {
|
2022-05-05 07:02:48 +00:00
|
|
|
const result = await inviter.invite([MXID1, MXID2, MXID3]);
|
|
|
|
|
|
|
|
expect(client.invite).toHaveBeenCalledTimes(3);
|
2022-10-12 17:59:07 +00:00
|
|
|
expect(client.invite).toHaveBeenNthCalledWith(1, ROOMID, MXID1, undefined);
|
|
|
|
expect(client.invite).toHaveBeenNthCalledWith(2, ROOMID, MXID2, undefined);
|
|
|
|
expect(client.invite).toHaveBeenNthCalledWith(3, ROOMID, MXID3, undefined);
|
2022-05-05 07:02:48 +00:00
|
|
|
|
|
|
|
expectAllInvitedResult(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("with promptBeforeInviteUnknownUsers = true and", () => {
|
2022-05-05 07:02:48 +00:00
|
|
|
beforeEach(() => mockPromptBeforeInviteUnknownUsers(true));
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("confirming the unknown user dialog", () => {
|
|
|
|
beforeEach(() => mockCreateTrackedDialog("onInviteAnyways"));
|
2022-05-05 07:02:48 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should invite all users", async () => {
|
2022-05-05 07:02:48 +00:00
|
|
|
const result = await inviter.invite([MXID1, MXID2, MXID3]);
|
|
|
|
|
|
|
|
expect(client.invite).toHaveBeenCalledTimes(3);
|
2022-10-12 17:59:07 +00:00
|
|
|
expect(client.invite).toHaveBeenNthCalledWith(1, ROOMID, MXID1, undefined);
|
|
|
|
expect(client.invite).toHaveBeenNthCalledWith(2, ROOMID, MXID2, undefined);
|
|
|
|
expect(client.invite).toHaveBeenNthCalledWith(3, ROOMID, MXID3, undefined);
|
2022-05-05 07:02:48 +00:00
|
|
|
|
|
|
|
expectAllInvitedResult(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("declining the unknown user dialog", () => {
|
|
|
|
beforeEach(() => mockCreateTrackedDialog("onGiveUp"));
|
2022-05-05 07:02:48 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should only invite existing users", async () => {
|
2022-05-05 07:02:48 +00:00
|
|
|
const result = await inviter.invite([MXID1, MXID2, MXID3]);
|
|
|
|
|
|
|
|
expect(client.invite).toHaveBeenCalledTimes(1);
|
2022-10-12 17:59:07 +00:00
|
|
|
expect(client.invite).toHaveBeenNthCalledWith(1, ROOMID, MXID1, undefined);
|
2022-05-05 07:02:48 +00:00
|
|
|
|
|
|
|
// The resolved state is 'invited' for all users.
|
|
|
|
// With the above client expectations, the test ensures that only the first user is invited.
|
|
|
|
expectAllInvitedResult(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|