forked from fahrPool/fahrPool
feat: POST /api/notifications/dispatch -- Outbox-Dispatch-Route mit Shared-Secret
This commit is contained in:
parent
018ad1fbfe
commit
0d5b3ef10b
|
|
@ -0,0 +1,77 @@
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import { timingSafeEqual } from "crypto";
|
||||||
|
import { createAdminClient } from "@/lib/supabase/admin";
|
||||||
|
import { getWhatsAppConfig } from "@/lib/notifications/config";
|
||||||
|
import { verarbeiteOutbox } from "@/lib/notifications/dispatch";
|
||||||
|
|
||||||
|
export const runtime = "nodejs";
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
function sichererVergleich(a: string, b: string): boolean {
|
||||||
|
const bufA = Buffer.from(a);
|
||||||
|
const bufB = Buffer.from(b);
|
||||||
|
if (bufA.length !== bufB.length) return false;
|
||||||
|
return timingSafeEqual(bufA, bufB);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function POST(request: Request): Promise<Response> {
|
||||||
|
const secret = process.env.NOTIFICATION_DISPATCH_SECRET;
|
||||||
|
if (!secret) {
|
||||||
|
console.error("[notifications] NOTIFICATION_DISPATCH_SECRET nicht gesetzt");
|
||||||
|
return new NextResponse("Fehlkonfiguration", { status: 500 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const auth = request.headers.get("authorization") ?? "";
|
||||||
|
const token = auth.startsWith("Bearer ") ? auth.slice(7) : "";
|
||||||
|
if (!token || !sichererVergleich(token, secret)) {
|
||||||
|
return new NextResponse("Unauthorized", { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const cfg = getWhatsAppConfig();
|
||||||
|
if (!cfg) {
|
||||||
|
console.warn("[notifications] WhatsApp nicht konfiguriert – Outbox wird nicht verarbeitet");
|
||||||
|
return NextResponse.json({ verarbeitet: 0, uebersprungen: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
const ergebnis = await verarbeiteOutbox(createAdminClient(), cfg);
|
||||||
|
return NextResponse.json(ergebnis);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue