Files
0pt/tests/api/stripe-webhook-config.test.ts
admin 8911709931
masterplan-lock / check (push) Successful in 10s
Security Scan (SIAX Cloud) / security (push) Successful in 14s
CI (SIAX Cloud) / integration (pull_request) Successful in 27s
CI (SIAX Cloud) / quality (push) Successful in 39s
CI (SIAX Cloud) / quality (pull_request) Successful in 32s
CI (SIAX Cloud) / security (push) Successful in 43s
masterplan-lock / check (pull_request) Successful in 41s
CI (SIAX Cloud) / security (pull_request) Successful in 1m12s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 1m14s
CI (SIAX Cloud) / integration (push) Successful in 1m36s
fix: do not acknowledge unverified Stripe webhooks
2026-09-15 18:08:13 +02:00

41 lines
1.5 KiB
TypeScript

import { afterEach, expect, it } from "vitest";
import { NextRequest } from "next/server";
import { POST } from "@/app/api/webhooks/stripe/route";
const savedSecretKey = process.env.STRIPE_SECRET_KEY;
const savedWebhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
afterEach(() => {
if (savedSecretKey === undefined) delete process.env.STRIPE_SECRET_KEY;
else process.env.STRIPE_SECRET_KEY = savedSecretKey;
if (savedWebhookSecret === undefined) delete process.env.STRIPE_WEBHOOK_SECRET;
else process.env.STRIPE_WEBHOOK_SECRET = savedWebhookSecret;
});
it("does not acknowledge an unverified Stripe event when credentials are absent", async () => {
delete process.env.STRIPE_SECRET_KEY;
delete process.env.STRIPE_WEBHOOK_SECRET;
const req = new NextRequest("https://0pt.app/api/webhooks/stripe", {
method: "POST",
headers: { "stripe-signature": "unverified" },
body: "{}",
});
const res = await POST(req);
expect(res.status).toBe(503);
expect(await res.json()).toEqual({ received: false, reason: "not_configured" });
});
it("rejects a delivery with no Stripe signature even when configured", async () => {
process.env.STRIPE_SECRET_KEY = "sk_test_fake_local_regression";
process.env.STRIPE_WEBHOOK_SECRET = "whsec_fake_local_regression";
const req = new NextRequest("https://0pt.app/api/webhooks/stripe", {
method: "POST",
body: "{}",
});
const res = await POST(req);
expect(res.status).toBe(400);
expect(await res.json()).toEqual({ error: "missing_signature" });
});