Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
44 lines
1.8 KiB
TypeScript
44 lines
1.8 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { NextRequest } from "next/server";
|
|
import { guardDecision } from "./app/lib/routeGuard";
|
|
import { middleware } from "./middleware";
|
|
|
|
// add-qr-self-registration: the QR landing page and its proxy are reachable signed out; admin paths still are not.
|
|
const get = (path: string) => middleware(new NextRequest(new URL(path, "https://portal.test")));
|
|
|
|
describe("middleware: public registration paths", () => {
|
|
it("lets a signed-out visitor open /r/<token>", async () => {
|
|
const res = await get("/r/abc123");
|
|
expect(res.headers.get("x-middleware-next")).toBe("1");
|
|
expect(res.headers.get("location")).toBeNull();
|
|
});
|
|
|
|
it("lets a signed-out visitor open /i/<token> and /c/<token>", async () => {
|
|
for (const path of ["/i/abc123", "/c/abc123"]) {
|
|
const res = await get(path);
|
|
expect(res.headers.get("x-middleware-next"), path).toBe("1");
|
|
}
|
|
});
|
|
|
|
it("lets a signed-out visitor reach the public registration proxy", async () => {
|
|
const res = await get("/api/public/registration/abc123");
|
|
expect(res.headers.get("x-middleware-next")).toBe("1");
|
|
});
|
|
|
|
it("still redirects a signed-out visitor from admin paths", async () => {
|
|
for (const path of ["/events", "/my-qr", "/r", "/r/", "/i", "/c/"]) {
|
|
const res = await get(path);
|
|
expect(res.headers.get("location"), path).toContain("/login");
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("routeGuard: /r/<token> with a pending password reset", () => {
|
|
it("stays reachable; admin paths still go to /reset-password", () => {
|
|
const s = { present: true, resetRequired: true };
|
|
expect(guardDecision("/r/abc123", s)).toBeNull();
|
|
expect(guardDecision("/events", s)).toBe("/reset-password");
|
|
expect(guardDecision("/r", s)).toBe("/reset-password");
|
|
});
|
|
});
|