mirror of
https://github.com/open-reception/appointment-booking-software.git
synced 2026-09-23 01:14:52 +02:00
191 lines
5.6 KiB
TypeScript
191 lines
5.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
import { handle } from "./hooks.server.js";
|
|
|
|
// Mock the Date.now function for rate limiting tests
|
|
const mockDateNow = vi.fn();
|
|
vi.stubGlobal("Date", { ...Date, now: mockDateNow });
|
|
|
|
describe("hooks.server", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mockDateNow.mockReturnValue(1000000); // Fixed timestamp for consistent testing
|
|
});
|
|
|
|
afterEach(() => {
|
|
// Reset any global state
|
|
vi.clearAllTimers();
|
|
});
|
|
|
|
describe("rate limiting", () => {
|
|
const mockResolve = vi.fn();
|
|
const createEvent = (ip: string = "192.168.1.1", method: string = "GET") => ({
|
|
url: new URL("http://localhost/api/test"),
|
|
request: new Request("http://localhost/api/test", {
|
|
method,
|
|
headers: {
|
|
"x-forwarded-for": ip
|
|
}
|
|
}),
|
|
cookies: {} as any,
|
|
fetch: {} as any,
|
|
getClientAddress: () => ip,
|
|
locals: {},
|
|
params: {},
|
|
route: { id: null },
|
|
setHeaders: vi.fn(),
|
|
isDataRequest: false,
|
|
isSubRequest: false,
|
|
platform: {} as any
|
|
});
|
|
|
|
beforeEach(() => {
|
|
mockResolve.mockResolvedValue(new Response("OK"));
|
|
});
|
|
|
|
it("should allow requests within rate limit", async () => {
|
|
const event = createEvent();
|
|
|
|
const response = await handle({ event, resolve: mockResolve });
|
|
|
|
expect(response.status).not.toBe(429);
|
|
expect(mockResolve).toHaveBeenCalled();
|
|
});
|
|
|
|
it("should block requests when rate limit exceeded", async () => {
|
|
const event = createEvent();
|
|
|
|
// Make multiple requests to exceed rate limit
|
|
for (let i = 0; i < 10; i++) {
|
|
await handle({ event, resolve: mockResolve });
|
|
}
|
|
|
|
// This request should be rate limited
|
|
const response = await handle({ event, resolve: mockResolve });
|
|
|
|
expect(response.status).toBe(429);
|
|
expect(await response.text()).toBe("Too Many Requests");
|
|
});
|
|
|
|
it("should reset rate limit after window expires", async () => {
|
|
const event = createEvent();
|
|
|
|
// Exceed rate limit
|
|
for (let i = 0; i < 11; i++) {
|
|
await handle({ event, resolve: mockResolve });
|
|
}
|
|
|
|
// Mock time passing (would need to mock Date.now in real implementation)
|
|
// For now, we'll test that different IPs are treated separately
|
|
const differentIPEvent = createEvent("192.168.1.2");
|
|
const response = await handle({ event: differentIPEvent, resolve: mockResolve });
|
|
|
|
expect(response.status).not.toBe(429);
|
|
});
|
|
});
|
|
|
|
describe("CORS handling", () => {
|
|
const mockResolve = vi.fn();
|
|
|
|
const createCORSEvent = (method: string = "GET", path: string = "/api/test") => ({
|
|
url: new URL(`http://localhost${path}`),
|
|
request: new Request(`http://localhost${path}`, {
|
|
method,
|
|
headers: {
|
|
"x-forwarded-for": "192.168.2.1" // Different IP for CORS tests
|
|
}
|
|
}),
|
|
cookies: {} as any,
|
|
fetch: {} as any,
|
|
getClientAddress: () => "192.168.2.1",
|
|
locals: {},
|
|
params: {},
|
|
route: { id: null },
|
|
setHeaders: vi.fn(),
|
|
isDataRequest: false,
|
|
isSubRequest: false,
|
|
platform: {} as any
|
|
});
|
|
|
|
beforeEach(() => {
|
|
mockResolve.mockResolvedValue(new Response("OK"));
|
|
});
|
|
|
|
it("should handle OPTIONS preflight requests", async () => {
|
|
const event = createCORSEvent("OPTIONS");
|
|
|
|
const response = await handle({ event, resolve: mockResolve });
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.headers.get("Access-Control-Allow-Origin")).toBe("*");
|
|
expect(response.headers.get("Access-Control-Allow-Methods")).toContain("GET");
|
|
expect(mockResolve).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should add CORS headers to API routes", async () => {
|
|
const event = createCORSEvent();
|
|
|
|
const response = await handle({ event, resolve: mockResolve });
|
|
|
|
expect(response.headers.get("Access-Control-Allow-Origin")).toBe("*");
|
|
expect(response.headers.get("Access-Control-Allow-Methods")).toContain("GET");
|
|
});
|
|
});
|
|
|
|
describe("security headers", () => {
|
|
const mockResolve = vi.fn();
|
|
|
|
const createSecurityEvent = (protocol: string = "http", path: string = "/test") => ({
|
|
url: new URL(`${protocol}://localhost${path}`),
|
|
request: new Request(`${protocol}://localhost${path}`, {
|
|
headers: {
|
|
"x-forwarded-for": "192.168.3.1" // Different IP for security tests
|
|
}
|
|
}),
|
|
cookies: {} as any,
|
|
fetch: {} as any,
|
|
getClientAddress: () => "192.168.3.1",
|
|
locals: {},
|
|
params: {},
|
|
route: { id: null },
|
|
setHeaders: vi.fn(),
|
|
isDataRequest: false,
|
|
isSubRequest: false,
|
|
platform: {} as any
|
|
});
|
|
|
|
beforeEach(() => {
|
|
mockResolve.mockResolvedValue(new Response("OK"));
|
|
});
|
|
|
|
it("should add security headers to all responses", async () => {
|
|
const event = createSecurityEvent();
|
|
|
|
const response = await handle({ event, resolve: mockResolve });
|
|
|
|
expect(response.headers.get("X-Frame-Options")).toBe("DENY");
|
|
expect(response.headers.get("X-Content-Type-Options")).toBe("nosniff");
|
|
expect(response.headers.get("X-XSS-Protection")).toBe("1; mode=block");
|
|
expect(response.headers.get("Referrer-Policy")).toBe("strict-origin-when-cross-origin");
|
|
expect(response.headers.get("Content-Security-Policy")).toContain("default-src 'self'");
|
|
});
|
|
|
|
it("should add HSTS header for HTTPS requests", async () => {
|
|
const event = createSecurityEvent("https");
|
|
|
|
const response = await handle({ event, resolve: mockResolve });
|
|
|
|
expect(response.headers.get("Strict-Transport-Security")).toBe(
|
|
"max-age=31536000; includeSubDomains; preload"
|
|
);
|
|
});
|
|
|
|
it("should not add HSTS header for HTTP requests", async () => {
|
|
const event = createSecurityEvent("http");
|
|
|
|
const response = await handle({ event, resolve: mockResolve });
|
|
|
|
expect(response.headers.get("Strict-Transport-Security")).toBeNull();
|
|
});
|
|
});
|
|
});
|