MSC4380: Invite blocking (#31268)
* Initial implementation of MSC4380 * fix lint * Update InviteRulesAccountSetting-test * add some docs * `block_all` -> `default_action` * Add a unit test for BlockInvitesConfigController
This commit is contained in:
committed by
GitHub
parent
5869c519ed
commit
1c684489da
@@ -13,16 +13,25 @@ import SettingsStore from "../../../../../../../src/settings/SettingsStore";
|
||||
import { type ComputedInviteConfig } from "../../../../../../../src/@types/invite-rules";
|
||||
import { SettingLevel } from "../../../../../../../src/settings/SettingLevel";
|
||||
|
||||
function mockSetting(mediaPreviews: ComputedInviteConfig, supported = true) {
|
||||
function mockSetting(inviteRules: ComputedInviteConfig, blockInvites: boolean) {
|
||||
jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName) => {
|
||||
if (settingName === "inviteRules") {
|
||||
return mediaPreviews;
|
||||
return inviteRules;
|
||||
}
|
||||
if (settingName === "blockInvites") {
|
||||
return blockInvites;
|
||||
}
|
||||
throw Error(`Unexpected setting ${settingName}`);
|
||||
});
|
||||
}
|
||||
|
||||
function mockDisabledMessage(msc4155supported: boolean, msc4380Supported: boolean) {
|
||||
jest.spyOn(SettingsStore, "disabledMessage").mockImplementation((settingName) => {
|
||||
if (settingName === "inviteRules") {
|
||||
return supported ? undefined : "test-not-supported";
|
||||
return msc4155supported ? undefined : "test-not-supported";
|
||||
}
|
||||
if (settingName === "blockInvites") {
|
||||
return msc4380Supported ? undefined : "test-not-supported";
|
||||
}
|
||||
throw Error(`Unexpected setting ${settingName}`);
|
||||
});
|
||||
@@ -33,44 +42,103 @@ describe("InviteRulesAccountSetting", () => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("does not render if not supported", async () => {
|
||||
it("does not render if neither MSC4155 nor MSC4380 are supported", async () => {
|
||||
mockSetting({ allBlocked: false }, false);
|
||||
mockDisabledMessage(false, false);
|
||||
const { findByText, findByLabelText } = render(<InviteRulesAccountSetting />);
|
||||
const input = await findByLabelText("Allow users to invite you to rooms");
|
||||
await userEvent.hover(input);
|
||||
const result = await findByText("test-not-supported");
|
||||
expect(result).toBeInTheDocument();
|
||||
});
|
||||
it("renders correct state when invites are not blocked", async () => {
|
||||
mockSetting({ allBlocked: false }, true);
|
||||
const { findByLabelText } = render(<InviteRulesAccountSetting />);
|
||||
const result = await findByLabelText("Allow users to invite you to rooms");
|
||||
expect(result).toBeChecked();
|
||||
});
|
||||
it("renders correct state when invites are blocked", async () => {
|
||||
mockSetting({ allBlocked: true }, true);
|
||||
const { findByLabelText } = render(<InviteRulesAccountSetting />);
|
||||
const result = await findByLabelText("Allow users to invite you to rooms");
|
||||
expect(result).not.toBeChecked();
|
||||
});
|
||||
it("handles disabling all invites", async () => {
|
||||
mockSetting({ allBlocked: false }, true);
|
||||
jest.spyOn(SettingsStore, "setValue").mockImplementation();
|
||||
const { findByLabelText } = render(<InviteRulesAccountSetting />);
|
||||
const result = await findByLabelText("Allow users to invite you to rooms");
|
||||
await userEvent.click(result);
|
||||
expect(SettingsStore.setValue).toHaveBeenCalledWith("inviteRules", null, SettingLevel.ACCOUNT, {
|
||||
allBlocked: true,
|
||||
|
||||
describe("when MSC4155 is supported", () => {
|
||||
beforeEach(() => {
|
||||
mockDisabledMessage(true, false);
|
||||
});
|
||||
|
||||
it("renders correct state when invites are not blocked", async () => {
|
||||
mockSetting({ allBlocked: false }, false);
|
||||
const { findByLabelText } = render(<InviteRulesAccountSetting />);
|
||||
const result = await findByLabelText("Allow users to invite you to rooms");
|
||||
expect(result).toBeChecked();
|
||||
});
|
||||
|
||||
it("renders correct state when invites are blocked", async () => {
|
||||
mockSetting({ allBlocked: true }, false);
|
||||
const { findByLabelText } = render(<InviteRulesAccountSetting />);
|
||||
const result = await findByLabelText("Allow users to invite you to rooms");
|
||||
expect(result).not.toBeChecked();
|
||||
});
|
||||
|
||||
it("handles disabling all invites", async () => {
|
||||
mockSetting({ allBlocked: false }, false);
|
||||
jest.spyOn(SettingsStore, "setValue").mockImplementation();
|
||||
const { findByLabelText } = render(<InviteRulesAccountSetting />);
|
||||
const result = await findByLabelText("Allow users to invite you to rooms");
|
||||
await userEvent.click(result);
|
||||
expect(SettingsStore.setValue).toHaveBeenCalledTimes(1);
|
||||
expect(SettingsStore.setValue).toHaveBeenCalledWith("inviteRules", null, SettingLevel.ACCOUNT, {
|
||||
allBlocked: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("handles enabling all invites", async () => {
|
||||
mockSetting({ allBlocked: true }, true);
|
||||
jest.spyOn(SettingsStore, "setValue").mockImplementation();
|
||||
const { findByLabelText } = render(<InviteRulesAccountSetting />);
|
||||
const result = await findByLabelText("Allow users to invite you to rooms");
|
||||
await userEvent.click(result);
|
||||
// Should clear both MSC4155 and MSC4380 settings
|
||||
expect(SettingsStore.setValue).toHaveBeenCalledTimes(2);
|
||||
expect(SettingsStore.setValue).toHaveBeenCalledWith("inviteRules", null, SettingLevel.ACCOUNT, {
|
||||
allBlocked: false,
|
||||
});
|
||||
expect(SettingsStore.setValue).toHaveBeenCalledWith("blockInvites", null, SettingLevel.ACCOUNT, false);
|
||||
});
|
||||
});
|
||||
it("handles enabling all invites", async () => {
|
||||
mockSetting({ allBlocked: true }, true);
|
||||
jest.spyOn(SettingsStore, "setValue").mockImplementation();
|
||||
const { findByLabelText } = render(<InviteRulesAccountSetting />);
|
||||
const result = await findByLabelText("Allow users to invite you to rooms");
|
||||
await userEvent.click(result);
|
||||
expect(SettingsStore.setValue).toHaveBeenCalledWith("inviteRules", null, SettingLevel.ACCOUNT, {
|
||||
allBlocked: false,
|
||||
|
||||
describe("when MSC4380 is supported", () => {
|
||||
beforeEach(() => {
|
||||
mockDisabledMessage(false, true);
|
||||
});
|
||||
|
||||
it("renders correct state when invites are not blocked", async () => {
|
||||
mockSetting({ allBlocked: false }, false);
|
||||
const { findByLabelText } = render(<InviteRulesAccountSetting />);
|
||||
const result = await findByLabelText("Allow users to invite you to rooms");
|
||||
expect(result).toBeChecked();
|
||||
});
|
||||
|
||||
it("renders correct state when invites are blocked", async () => {
|
||||
mockSetting({ allBlocked: false }, true);
|
||||
const { findByLabelText } = render(<InviteRulesAccountSetting />);
|
||||
const result = await findByLabelText("Allow users to invite you to rooms");
|
||||
expect(result).not.toBeChecked();
|
||||
});
|
||||
|
||||
it("handles disabling all invites", async () => {
|
||||
mockSetting({ allBlocked: false }, false);
|
||||
jest.spyOn(SettingsStore, "setValue").mockImplementation();
|
||||
const { findByLabelText } = render(<InviteRulesAccountSetting />);
|
||||
const result = await findByLabelText("Allow users to invite you to rooms");
|
||||
await userEvent.click(result);
|
||||
expect(SettingsStore.setValue).toHaveBeenCalledTimes(1);
|
||||
expect(SettingsStore.setValue).toHaveBeenCalledWith("blockInvites", null, SettingLevel.ACCOUNT, true);
|
||||
});
|
||||
|
||||
it("handles enabling all invites", async () => {
|
||||
mockSetting({ allBlocked: true }, true);
|
||||
jest.spyOn(SettingsStore, "setValue").mockImplementation();
|
||||
const { findByLabelText } = render(<InviteRulesAccountSetting />);
|
||||
const result = await findByLabelText("Allow users to invite you to rooms");
|
||||
await userEvent.click(result);
|
||||
// Should clear both MSC4155 and MSC4380 settings
|
||||
expect(SettingsStore.setValue).toHaveBeenCalledTimes(2);
|
||||
expect(SettingsStore.setValue).toHaveBeenCalledWith("inviteRules", null, SettingLevel.ACCOUNT, {
|
||||
allBlocked: false,
|
||||
});
|
||||
expect(SettingsStore.setValue).toHaveBeenCalledWith("blockInvites", null, SettingLevel.ACCOUNT, false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user