364 lines · 15.1 KB
import { beforeEach, describe, expect, test } from "bun:test";
const {
  state,
  accountSql,
  fakeEnv,
  disabled,
  adminNotices,
  handleStripeWebhook,
  processStripeEvent,
  topupPacks,
  creditBalance,
  event,
  post,
  plan,
  subscription,
  resetWebhookHarness,
} = await import("./webhook.test-helper");

beforeEach(() => resetWebhookHarness());

describe("stripe webhook", () => {
  test("rejects missing and wrong signatures", async () => {
    const payload = event("customer.subscription.created", subscription("active"));
    const unsigned = await handleStripeWebhook(
      new Request("https://forge.test/stripe/webhook", { method: "POST", body: JSON.stringify(payload) }),
    );
    expect(unsigned.status).toBe(400);
    expect((await post(payload, "whsec_other")).status).toBe(400);
    expect(plan("o1")).toBeNull();
    expect((await post(payload)).status).toBe(200);
    expect(plan("o1")).toBe("pro");
  });

  test("the custom domain add-on is tracked on its own and never changes the plan", async () => {
    fakeEnv.STRIPE_PRICE_CUSTOM_DOMAIN = "price_domain";
    const addon = subscription("active", {
      id: "sub_domains",
      metadata: { account_id: "o1", kind: "custom_domains" },
      items: { data: [{ id: "si_d", quantity: 2, price: { id: "price_domain" }, current_period_end: 1_800_000_000 }] },
    });