// @vitest-environment node import { beforeEach, describe, expect, it, vi } from "vitest"; import type { ActionFunctionArgs } from "react-router"; const mocks = vi.hoisted(() => ({ checkAuthRateLimit: vi.fn(), resolvePublicBookingPageRlsIdentity: vi.fn(), resolvePublicBookingPage: vi.fn(), submitBooking: vi.fn(), })); vi.mock("~/services/auth/auth-rate-limit.server", () => ({ checkAuthRateLimit: mocks.checkAuthRateLimit })); vi.mock("@hivegrid/db/rls-resolvers.server", () => ({ resolvePublicBookingPageRlsIdentity: mocks.resolvePublicBookingPageRlsIdentity, })); vi.mock("../db/booking.db.server", () => ({ resolvePublicBookingPage: mocks.resolvePublicBookingPage })); vi.mock("../services/SlotQueryService.server", () => ({ loadSlotsForDate: vi.fn() })); vi.mock("../services/slotEngine", () => ({ pickFirstAvailableProvider: vi.fn() })); vi.mock("../services/BookingSubmitService.server", () => ({ BookingSlotTakenError: class BookingSlotTakenError extends Error {}, BookingValidationError: class BookingValidationError extends Error {}, submitBooking: mocks.submitBooking, })); const { publicBookingAction } = await import("./PublicBooking.server"); beforeEach(() => { vi.clearAllMocks(); mocks.checkAuthRateLimit.mockResolvedValue({ allowed: true }); mocks.resolvePublicBookingPageRlsIdentity.mockResolvedValue({ tenantId: "tenant_1", resourceId: "page_1" }); }); describe("public booking abuse protection", () => { it("rate-limits before page lookup, form parsing, or booking writes", async () => { mocks.checkAuthRateLimit.mockResolvedValueOnce({ allowed: false, retryAfterSeconds: 45 }); const request = new Request("https://app.example.test/b/acme/consulting", { method: "POST", body: "attacker-controlled-body", }); const response = await publicBookingAction({ request, params: { tenant: "acme", slug: "consulting" }, context: {}, unstable_pattern: "b.$tenant.$slug", } as ActionFunctionArgs); expect(response.status).toBe(429); expect(response.headers.get("Retry-After")).toBe("45"); expect(response.headers.get("Cache-Control")).toBe("no-store"); expect(mocks.checkAuthRateLimit).toHaveBeenCalledWith(expect.objectContaining({ action: "booking-submit", identifier: "acme/consulting", limit: 10 })); expect(mocks.resolvePublicBookingPage).not.toHaveBeenCalled(); expect(mocks.submitBooking).not.toHaveBeenCalled(); }); it("enters the resolved tenant context before reading the protected booking page", async () => { const { getRlsExecutionContext } = await import("@hivegrid/db/rls-context.server"); mocks.resolvePublicBookingPage.mockImplementationOnce(async () => { expect(getRlsExecutionContext()).toMatchObject({ tenantId: "tenant_1" }); return null; }); const request = new Request("https://app.example.test/b/acme/consulting", { method: "POST", body: new URLSearchParams(), }); await expect( publicBookingAction({ request, params: { tenant: "acme", slug: "consulting" }, context: {}, unstable_pattern: "b.$tenant.$slug", } as ActionFunctionArgs) ).rejects.toMatchObject({ status: 404 }); expect(mocks.resolvePublicBookingPageRlsIdentity).toHaveBeenCalledWith("acme", "consulting"); }); });