Files
co-lite/solvway-admin-portal/app/lib/routeGuard.test.ts
T
ngqileandClaude Opus 5.5 bb2c5c5db7 feat(portal): landing pages list, editor, preview and public page
Pages list and Tiptap editor (ported from babyboo) with image upload on
save, the FORM event picker, field errors at their fields, delete
confirmation, unsaved-edits guard and a draft preview. The public page
/p/<company>/<slug> renders on the server with metadata, sanitize-html,
the company's logo and accent, and the sign-up form with UTM tags.
Companies get branding; the event lead detail shows the web page and
campaign. Labels in English and Chinese.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
2026-12-02 16:30:54 +08:00

74 lines
3.0 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { guardDecision, isPublicPath, PUBLIC_PATHS } from "./routeGuard";
describe("isPublicPath", () => {
it("recognizes the public auth routes", () => {
expect(isPublicPath("/login")).toBe(true);
expect(isPublicPath("/")).toBe(false);
expect(isPublicPath("/members")).toBe(false);
});
it("includes a reset-password placeholder for a future forced-reset flow", () => {
expect(isPublicPath("/reset-password")).toBe(true);
expect([...PUBLIC_PATHS].sort()).toEqual(["/forgot-password", "/login", "/reset-password"]);
});
it("lets signed-out visitors open /forgot-password and signed-in users too", () => {
expect(isPublicPath("/forgot-password")).toBe(true);
expect(guardDecision("/forgot-password", null)).toBeNull();
expect(guardDecision("/forgot-password", { present: true })).toBeNull();
});
});
describe("guardDecision", () => {
it("redirects an unauthenticated request on a non-public path to /login", () => {
expect(guardDecision("/", null)).toBe("/login");
expect(guardDecision("/members", null)).toBe("/login");
expect(guardDecision("/reports", { present: false })).toBe("/login");
});
it("allows an unauthenticated request on a public path", () => {
expect(guardDecision("/login", null)).toBeNull();
expect(guardDecision("/reset-password", null)).toBeNull();
});
it("allows an authenticated request on any path", () => {
const s = { present: true };
expect(guardDecision("/", s)).toBeNull();
expect(guardDecision("/members", s)).toBeNull();
expect(guardDecision("/login", s)).toBeNull();
});
it("sends a session with a pending password reset to /reset-password, except on it and /login", () => {
const s = { present: true, resetRequired: true };
expect(guardDecision("/", s)).toBe("/reset-password");
expect(guardDecision("/members", s)).toBe("/reset-password");
expect(guardDecision("/reset-password", s)).toBeNull();
expect(guardDecision("/login", s)).toBeNull();
});
});
describe("visitor pages /r, /i, /c", () => {
it("pass signed out and with a reset pending; the bare prefixes and admin paths do not", () => {
const pending = { present: true, resetRequired: true };
for (const p of ["/r/x", "/i/x", "/c/x"]) {
expect(guardDecision(p, null), p).toBeNull();
expect(guardDecision(p, pending), p).toBeNull();
}
for (const p of ["/events", "/i", "/c/", "/i/", "/c/x/y"]) {
expect(guardDecision(p, null), p).toBe("/login");
expect(guardDecision(p, pending), p).toBe("/reset-password");
}
});
});
describe("landing pages /p/<company>/<slug>", () => {
it("pass signed out; other shapes under /p and /pages do not", () => {
expect(guardDecision("/p/INS/hotel-technology", null)).toBeNull();
expect(isPublicPath("/p/INS/hotel-technology")).toBe(true);
for (const p of ["/p", "/p/INS", "/p/INS/", "/p/INS/a/b", "/pages", "/pages/1"]) {
expect(guardDecision(p, null), p).toBe("/login");
}
});
});