web: add tests for lib/ + revalidation api
This commit is contained in:
12 files changed
+688
-23
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import type { NextRequest } from "next/server";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("next/cache", () => ({ revalidateTag: vi.fn() }));
|
||||
|
||||
import { revalidateTag } from "next/cache";
|
||||
import { CMS_CACHE_TAG } from "@/lib/revalidation";
|
||||
import { POST } from "./route.ts";
|
||||
|
||||
const request = (headers: Record<string, string> = {}) =>
|
||||
new Request("http://localhost/api/revalidate", {
|
||||
method: "POST",
|
||||
headers,
|
||||
}) as unknown as NextRequest;
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("POST /api/revalidate", () => {
|
||||
it("returns 503 when no secret is configured", async () => {
|
||||
vi.stubEnv("REVALIDATE_WEBHOOK_SECRET", undefined);
|
||||
|
||||
const res = await POST(request({ "x-revalidate-secret": "hemmelig" }));
|
||||
|
||||
expect(res.status).toBe(503);
|
||||
expect(revalidateTag).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns 401 on missing or wrong secret", async () => {
|
||||
vi.stubEnv("REVALIDATE_WEBHOOK_SECRET", "hemmelig");
|
||||
|
||||
expect((await POST(request())).status).toBe(401);
|
||||
expect(
|
||||
(await POST(request({ "x-revalidate-secret": "feil" }))).status
|
||||
).toBe(401);
|
||||
expect(revalidateTag).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("purges the cms tag on the correct secret", async () => {
|
||||
vi.stubEnv("REVALIDATE_WEBHOOK_SECRET", "hemmelig");
|
||||
|
||||
const res = await POST(request({ "x-revalidate-secret": "hemmelig" }));
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(await res.json()).toEqual({ revalidated: true, tag: CMS_CACHE_TAG });
|
||||
expect(revalidateTag).toHaveBeenCalledWith(CMS_CACHE_TAG, { expire: 0 });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user