Kuma/tests/api/cron/marketing.test.ts
2026-10-02 17:31:08 +02:00

60 lines
1.9 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { NextRequest } from "next/server";
vi.mock("@/src/modules/marketing/lib/run-marketing", () => ({ runMarketing: vi.fn() }));
import { GET } from "@/src/app/api/cron/marketing/route";
import { runMarketing } from "@/src/modules/marketing/lib/run-marketing";
function request(query = "", auth: string | null = "Bearer geheim") {
return new NextRequest(`http://localhost/api/cron/marketing${query}`, {
headers: auth ? { authorization: auth } : {},
});
}
beforeEach(() => {
vi.clearAllMocks();
process.env.CRON_SECRET = "geheim";
delete process.env.VERCEL_ENV;
vi.mocked(runMarketing).mockResolvedValue({
today: "2026-10-05",
dryRun: false,
incomplete: false,
businesses: [],
});
});
afterEach(() => {
delete process.env.VERCEL_ENV;
});
describe("GET /api/cron/marketing", () => {
it("lässt nur Aufrufe mit dem Secret durch", async () => {
expect((await GET(request("", null))).status).toBe(401);
expect((await GET(request("", "Bearer falsch"))).status).toBe(401);
expect(runMarketing).not.toHaveBeenCalled();
});
it("startet den Lauf mit den Test-Parametern", async () => {
const res = await GET(request("?dryRun=1&date=2026-05-12&businessId=biz-1"));
expect(res.status).toBe(200);
expect(runMarketing).toHaveBeenCalledWith(
expect.objectContaining({ dryRun: true, today: "2026-05-12", businessId: "biz-1" })
);
});
it("erlaubt kein Datum in Production", async () => {
process.env.VERCEL_ENV = "production";
expect((await GET(request("?date=2026-05-12"))).status).toBe(400);
expect(runMarketing).not.toHaveBeenCalled();
});
it("lehnt ein kaputtes Datum ab", async () => {
expect((await GET(request("?date=12.05.2026"))).status).toBe(400);
});
it("verweigert ohne CRON_SECRET", async () => {
delete process.env.CRON_SECRET;
expect((await GET(request())).status).toBe(500);
});
});