Files
co-lite/solvway-admin-portal/middleware.public.test.ts
T
ngqileandClaude Opus 5.5 d532bdcda4 fix(qr): inactive answer for malformed codes, undated events, switched-off badge, reset-pending sessions
A code of the wrong shape now shows "no longer active" instead of failing,
My QR codes skips events with no end date and marks switched-off codes,
and a session waiting on a password reset can still open a QR page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-12-01 14:04:12 +08:00

37 lines
1.5 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 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/"]) {
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");
});
});