154 lines
4.5 KiB
TypeScript
154 lines
4.5 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const publicBooking = vi.hoisted(() => ({
|
|
checkRateLimit: vi.fn(),
|
|
isHost: vi.fn(),
|
|
loadPage: vi.fn(),
|
|
submit: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("~/services/admin/admin-public-booking.server", () => ({
|
|
checkAdminPublicBookingRateLimit: publicBooking.checkRateLimit,
|
|
isAdminPublicBookingHost: publicBooking.isHost,
|
|
loadAdminPublicBookingPage: publicBooking.loadPage,
|
|
submitAdminPublicBooking: publicBooking.submit,
|
|
}));
|
|
vi.mock("~/locale/i18next.server", () => ({
|
|
getTranslations: vi.fn(async () => ({ locale: "de-DE" })),
|
|
}));
|
|
|
|
import { action, loader } from "./meet.$slug";
|
|
|
|
const page = {
|
|
pageId: "page_1",
|
|
ownerAdminUserId: "admin_1",
|
|
slug: "erstgespraech",
|
|
title: "Erstgespräch buchen",
|
|
description: "Wir lernen uns kennen.",
|
|
meetingTypeName: "Erstgespräch",
|
|
durationMinutes: 30,
|
|
questions: [],
|
|
date: "2026-08-03",
|
|
slots: [
|
|
{
|
|
startsAt: new Date("2026-08-03T08:00:00.000Z"),
|
|
endsAt: new Date("2026-08-03T08:30:00.000Z"),
|
|
availableHostAdminUserIds: ["admin_1"],
|
|
},
|
|
],
|
|
} as const;
|
|
|
|
describe("meet.$slug public admin booking route", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
process.env.SESSION_SECRET = "s".repeat(32);
|
|
publicBooking.isHost.mockReturnValue(true);
|
|
publicBooking.loadPage.mockResolvedValue(page);
|
|
publicBooking.checkRateLimit.mockResolvedValue({
|
|
allowed: true,
|
|
retryAfterSeconds: 900,
|
|
});
|
|
publicBooking.submit.mockResolvedValue({
|
|
booking: {
|
|
id: "booking_1",
|
|
adminUserId: "admin_1",
|
|
manageTokenVersion: 1,
|
|
manageTokenExpiresAt: new Date("2026-08-15T12:00:00.000Z"),
|
|
},
|
|
selectedHostAdminUserId: "admin_1",
|
|
contactCreated: true,
|
|
});
|
|
});
|
|
|
|
it("loads a minimal public VM without host identities", async () => {
|
|
const result = await loader({
|
|
request: new Request(
|
|
"http://meet.localhost:3000/meet/erstgespraech?date=2026-08-03"
|
|
),
|
|
params: { slug: "erstgespraech" },
|
|
context: {},
|
|
unstable_pattern: "/meet/:slug",
|
|
});
|
|
|
|
expect(result.vm.slots).toHaveLength(1);
|
|
expect(JSON.stringify(result.vm)).not.toContain("admin_1");
|
|
expect(publicBooking.loadPage).toHaveBeenCalledWith({
|
|
slug: "erstgespraech",
|
|
date: "2026-08-03",
|
|
});
|
|
});
|
|
|
|
it("rate-limits and reserves the selected authoritative slot", async () => {
|
|
const form = new URLSearchParams();
|
|
form.set("submissionId", "submission_1");
|
|
form.set("date", "2026-08-03");
|
|
form.set("timeZone", "Europe/Berlin");
|
|
form.set("slot", "2026-08-03T08:00:00.000Z|2026-08-03T08:30:00.000Z");
|
|
form.set("firstName", "Ada");
|
|
form.set("lastName", "Lovelace");
|
|
form.set("email", "ada@example.test");
|
|
const response = await action({
|
|
request: new Request(
|
|
"http://meet.localhost:3000/meet/erstgespraech",
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
origin: "http://meet.localhost:3000",
|
|
"sec-fetch-site": "same-origin",
|
|
},
|
|
body: form,
|
|
}
|
|
),
|
|
params: { slug: "erstgespraech" },
|
|
context: {},
|
|
unstable_pattern: "/meet/:slug",
|
|
});
|
|
|
|
await expect((response as Response).json()).resolves.toEqual({
|
|
confirmed: true,
|
|
errorCode: null,
|
|
manageUrl: expect.stringMatching(
|
|
/^http:\/\/meet\.localhost:3000\/manage\//
|
|
),
|
|
});
|
|
expect(publicBooking.checkRateLimit).toHaveBeenCalled();
|
|
expect(publicBooking.submit).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
slug: "erstgespraech",
|
|
email: "ada@example.test",
|
|
submissionId: "submission_1",
|
|
})
|
|
);
|
|
});
|
|
|
|
it("hides the route on the wrong host and rejects cross-origin posts", async () => {
|
|
publicBooking.isHost.mockReturnValueOnce(false);
|
|
await expect(
|
|
loader({
|
|
request: new Request(
|
|
"http://admin.localhost:3000/meet/erstgespraech"
|
|
),
|
|
params: { slug: "erstgespraech" },
|
|
context: {},
|
|
unstable_pattern: "/meet/:slug",
|
|
})
|
|
).rejects.toMatchObject({ status: 404 });
|
|
|
|
await expect(
|
|
action({
|
|
request: new Request(
|
|
"http://meet.localhost:3000/meet/erstgespraech",
|
|
{
|
|
method: "POST",
|
|
headers: { origin: "https://attacker.invalid" },
|
|
body: new FormData(),
|
|
}
|
|
),
|
|
params: { slug: "erstgespraech" },
|
|
context: {},
|
|
unstable_pattern: "/meet/:slug",
|
|
})
|
|
).rejects.toMatchObject({ status: 404 });
|
|
});
|
|
});
|