157 lines
5.8 KiB
TypeScript
157 lines
5.8 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
loadChatContext: vi.fn(),
|
|
loadPage: vi.fn(),
|
|
addMember: vi.fn(),
|
|
changeRole: vi.fn(),
|
|
removeMember: vi.fn(),
|
|
changePrivacy: vi.fn(),
|
|
leave: vi.fn(),
|
|
archive: vi.fn(),
|
|
validateCSRFToken: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn(async () => ({ t: (key: string) => key })) }));
|
|
vi.mock("~/modules/chat/routes/chatAccess.server", () => ({ loadChatContext: mocks.loadChatContext }));
|
|
vi.mock("~/modules/chat/services/ChatChannelSettingsService.server", () => ({
|
|
loadChatChannelSettingsPage: mocks.loadPage,
|
|
addChatChannelMember: mocks.addMember,
|
|
changeChatChannelMemberRole: mocks.changeRole,
|
|
removeChatChannelMember: mocks.removeMember,
|
|
changeChatChannelPrivacy: mocks.changePrivacy,
|
|
leaveChatChannel: mocks.leave,
|
|
archiveChatChannel: mocks.archive,
|
|
}));
|
|
vi.mock("~/utils/session.server", () => ({ validateCSRFToken: mocks.validateCSRFToken }));
|
|
vi.mock("~/utils/data/useRootData", () => ({ useRootData: () => ({ csrf: "csrf_1" }) }));
|
|
vi.mock("~/components/screens/chat/ChatChannelSettingsPage", () => ({ ChatChannelSettingsPage: () => null }));
|
|
vi.mock("~/components/ui/errors/ServerError", () => ({ default: () => null }));
|
|
|
|
import { action, loader } from "./chat.$id.settings";
|
|
|
|
const context = {
|
|
tenantId: "tenant_1",
|
|
userId: "user_current",
|
|
tenantUserId: "tenant_user_current",
|
|
permissions: ["chat.read_own_channels"],
|
|
hasModule: true as const,
|
|
};
|
|
|
|
describe("AK-P15b chat channel settings route", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.loadChatContext.mockResolvedValue(context);
|
|
mocks.loadPage.mockResolvedValue({ status: "empty" });
|
|
mocks.validateCSRFToken.mockResolvedValue(undefined);
|
|
});
|
|
|
|
it("loads settings through the tenant chat context with localized fallbacks", async () => {
|
|
const request = new Request("https://app.example.test/demo/chat/chat_1/settings");
|
|
const response = await loader({
|
|
request,
|
|
params: { tenant: "demo", id: "chat_1" },
|
|
context: {},
|
|
unstable_pattern: "app.$tenant/chat.$id.settings",
|
|
});
|
|
|
|
expect(mocks.loadPage).toHaveBeenCalledWith({
|
|
context,
|
|
tenantSlug: "demo",
|
|
chatId: "chat_1",
|
|
labels: {
|
|
directMessage: "chat.channel.fallback.directMessage",
|
|
unnamedGroup: "chat.channel.fallback.group",
|
|
unnamedProject: "chat.channel.fallback.project",
|
|
unknownProjectStage: "chat.channel.fallback.projectStage",
|
|
},
|
|
});
|
|
await expect(response.json()).resolves.toMatchObject({
|
|
tenantSlug: "demo",
|
|
chatId: "chat_1",
|
|
requestId: expect.any(String),
|
|
vm: { status: "empty" },
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
["add_member", { memberUserId: "user_new" }, "addMember", "chat.toast.memberAdded"],
|
|
["set_role", { memberUserId: "user_new", roleInChat: "admin" }, "changeRole", "chat.toast.memberRoleChanged"],
|
|
["remove_member", { memberUserId: "user_new" }, "removeMember", "chat.toast.memberRemoved"],
|
|
["set_privacy", { privacy: "public" }, "changePrivacy", "chat.toast.channelMadePublic"],
|
|
] as const)("handles %s with CSRF and redirects to the settings page", async (intent, values, mockName, toastKey) => {
|
|
const form = new FormData();
|
|
form.set("csrf", "csrf_1");
|
|
form.set("intent", intent);
|
|
form.set("requestId", `request_${intent}`);
|
|
for (const [key, value] of Object.entries(values)) form.set(key, value);
|
|
const request = postRequest(form);
|
|
|
|
const response = await action({
|
|
request,
|
|
params: { tenant: "demo", id: "chat_1" },
|
|
context: {},
|
|
unstable_pattern: "app.$tenant/chat.$id.settings",
|
|
});
|
|
|
|
expect(mocks.validateCSRFToken).toHaveBeenCalledWith(request, { formData: expect.anything() });
|
|
expect(mocks[mockName]).toHaveBeenCalledWith({
|
|
context,
|
|
chatId: "chat_1",
|
|
requestId: `request_${intent}`,
|
|
...values,
|
|
});
|
|
expect(response.status).toBe(302);
|
|
expect(response.headers.get("location")).toBe(`/demo/chat/chat_1/settings?toast=${toastKey}`);
|
|
});
|
|
|
|
it.each([
|
|
["leave", "leave", "chat.toast.channelLeft"],
|
|
["archive", "archive", "chat.toast.channelArchived"],
|
|
] as const)("returns to the channel list after %s", async (intent, mockName, toastKey) => {
|
|
const form = new FormData();
|
|
form.set("csrf", "csrf_1");
|
|
form.set("intent", intent);
|
|
form.set("requestId", `request_${intent}`);
|
|
|
|
const response = await action({
|
|
request: postRequest(form),
|
|
params: { tenant: "demo", id: "chat_1" },
|
|
context: {},
|
|
unstable_pattern: "app.$tenant/chat.$id.settings",
|
|
});
|
|
|
|
expect(mocks[mockName]).toHaveBeenCalledWith({ context, chatId: "chat_1", requestId: `request_${intent}` });
|
|
expect(response.headers.get("location")).toBe(`/demo/chat?toast=${toastKey}`);
|
|
});
|
|
|
|
it("rejects malformed action input before any mutation", async () => {
|
|
const form = new FormData();
|
|
form.set("intent", "set_role");
|
|
form.set("requestId", "request_invalid");
|
|
form.set("memberUserId", "user_new");
|
|
form.set("roleInChat", "owner");
|
|
|
|
const response = await action({
|
|
request: postRequest(form),
|
|
params: { tenant: "demo", id: "chat_1" },
|
|
context: {},
|
|
unstable_pattern: "app.$tenant/chat.$id.settings",
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
await expect(response.json()).resolves.toEqual({ errorKey: "chat.errors.invalidInput" });
|
|
expect(mocks.changeRole).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
function postRequest(formData: FormData): Request {
|
|
const body = new URLSearchParams();
|
|
for (const [key, value] of formData.entries()) body.append(key, value.toString());
|
|
return new Request("https://app.example.test/demo/chat/chat_1/settings", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
body,
|
|
});
|
|
}
|