Files

31 lines
1.5 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { NextRequest } from "next/server";
import { middleware } from "./middleware";
import { guardDecision, isPublicPath } from "@/app/lib/routeGuard";
// add-custom-html-block: frame responses carry their own sandbox policy, so the middleware adds no nonce policy.
const get = (path: string) => new NextRequest(new URL(path, "https://portal.test"), { method: "GET" });
describe("middleware: custom HTML frames", () => {
it("lets a signed-out visitor load a public frame, without the nonce policy", async () => {
const res = await middleware(get("/p/INS/hotel-technology/frame/0"));
expect(res.status).toBe(200);
expect(res.headers.get("content-security-policy")).toBeNull();
});
it("sends a signed-out preview frame request to sign in", async () => {
const res = await middleware(get("/pages/5/preview/frame/0"));
expect(res.status).toBeGreaterThanOrEqual(300);
expect(res.headers.get("location")).toContain("/login");
});
it("keeps the nonce policy on the landing page itself and on look-alike paths", async () => {
expect((await middleware(get("/p/INS/hotel-technology"))).headers.get("content-security-policy")).toMatch(/nonce-/);
expect(isPublicPath("/p/INS/hotel-technology/frame/0")).toBe(true);
for (const p of ["/p/INS/hotel/frame/x", "/p/INS/hotel/frame/0/x", "/p/INS/frame/0", "/p/INS/hotel/frame/"]) {
expect(isPublicPath(p), p).toBe(false);
expect(guardDecision(p, null), p).toBe("/login");
}
});
});