forked from fahrPool/fahrPool
78 lines
2.8 KiB
TypeScript
78 lines
2.8 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
vi.mock("@/lib/supabase/admin", () => ({ createAdminClient: vi.fn(() => ({})) }));
|
|
vi.mock("@/lib/notifications/config", () => ({ getWhatsAppConfig: vi.fn() }));
|
|
vi.mock("@/lib/notifications/dispatch", () => ({ verarbeiteOutbox: vi.fn() }));
|
|
|
|
import { getWhatsAppConfig } from "@/lib/notifications/config";
|
|
import { verarbeiteOutbox } from "@/lib/notifications/dispatch";
|
|
import { POST } from "./route";
|
|
|
|
const CFG = {
|
|
accessToken: "tok",
|
|
phoneNumberId: "PN1",
|
|
recipient: "4915100000000",
|
|
templateName: "neuer_auftrag",
|
|
templateLang: "de",
|
|
graphVersion: "v21.0",
|
|
};
|
|
|
|
function req(headers: Record<string, string> = {}) {
|
|
return new Request("http://localhost/api/notifications/dispatch", { method: "POST", headers });
|
|
}
|
|
|
|
let altSecret: string | undefined;
|
|
|
|
beforeEach(() => {
|
|
altSecret = process.env.NOTIFICATION_DISPATCH_SECRET;
|
|
process.env.NOTIFICATION_DISPATCH_SECRET = "geheim";
|
|
vi.mocked(getWhatsAppConfig).mockReset();
|
|
vi.mocked(verarbeiteOutbox).mockReset();
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (altSecret === undefined) delete process.env.NOTIFICATION_DISPATCH_SECRET;
|
|
else process.env.NOTIFICATION_DISPATCH_SECRET = altSecret;
|
|
});
|
|
|
|
describe("POST /api/notifications/dispatch", () => {
|
|
it("antwortet 401 ohne Bearer-Token", async () => {
|
|
const res = await POST(req());
|
|
expect(res.status).toBe(401);
|
|
expect(verarbeiteOutbox).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("antwortet 401 bei falschem Token", async () => {
|
|
const res = await POST(req({ Authorization: "Bearer falsch" }));
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it("antwortet 500, wenn NOTIFICATION_DISPATCH_SECRET nicht gesetzt ist", async () => {
|
|
delete process.env.NOTIFICATION_DISPATCH_SECRET;
|
|
const res = await POST(req({ Authorization: "Bearer geheim" }));
|
|
expect(res.status).toBe(500);
|
|
});
|
|
|
|
it("antwortet 200 uebersprungen, wenn keine WhatsApp-Config vorhanden ist", async () => {
|
|
vi.mocked(getWhatsAppConfig).mockReturnValue(null);
|
|
const res = await POST(req({ Authorization: "Bearer geheim" }));
|
|
expect(res.status).toBe(200);
|
|
expect(await res.json()).toEqual({ verarbeitet: 0, uebersprungen: true });
|
|
expect(verarbeiteOutbox).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("ruft verarbeiteOutbox und gibt dessen Ergebnis als 200 zurueck", async () => {
|
|
vi.mocked(getWhatsAppConfig).mockReturnValue(CFG);
|
|
vi.mocked(verarbeiteOutbox).mockResolvedValue({
|
|
verarbeitet: 2,
|
|
gesendet: 1,
|
|
fehlgeschlagen: 1,
|
|
tot: 0,
|
|
});
|
|
const res = await POST(req({ Authorization: "Bearer geheim" }));
|
|
expect(res.status).toBe(200);
|
|
expect(await res.json()).toEqual({ verarbeitet: 2, gesendet: 1, fehlgeschlagen: 1, tot: 0 });
|
|
expect(verarbeiteOutbox).toHaveBeenCalledOnce();
|
|
});
|
|
});
|