Skip to content

Open-source status page, incident response and uptime monitoring

25.1 KB
Raw Blame
import { db, eq } from "@openstatus/db";
import {
  page,
  user,
  usersToWorkspaces,
  workspace,
} from "@openstatus/db/src/schema";
import {
  addUserToWorkspace,
  createPage,
  createTestWorkspace,
  createUser,
} from "@openstatus/db/src/test/factories";
import { delivery, resend } from "@openstatus/emails/src/send";
import type Stripe from "stripe";
import {
  afterEach,
  beforeEach,
  describe,
  expect,
  test,
  vi,
  type MockInstance,
} from "vitest";

import { createInnerTRPCContext } from "../../trpc";
import { stripe } from "./shared";
import { PLANS } from "./utils";
import { webhookRouter } from "./webhook";

const TEAM_PRICE = PLANS.find((p) => p.plan === "team")?.price.monthly.priceIds
  .test;
const DAY = 86_400;
const now = () => Math.floor(Date.now() / 1000);

// biome-ignore lint/suspicious/noExplicitAny: stubs over the Stripe and Resend clients
type AnyStub = MockInstance;

// `current_period_end` lives on the items since API version 2025-03-31.
function subscription(
  customer: string,
  {
    current_period_end = now() + 10 * DAY,
    ...overrides
  }: Partial<Stripe.Subscription> & { current_period_end?: number } = {},
) {
  return {
    id: "sub_test_1",
    customer,
    status: "active",
    created: now() - 30 * DAY,
    cancel_at_period_end: false,
    metadata: {},
    items: {
      data: [{ price: { id: TEAM_PRICE }, quantity: 1, current_period_end }],
    },
    ...overrides,
  } as unknown as Stripe.Subscription;
}

function event(
  type: string,
  object: Stripe.Subscription,
  previous_attributes?: Record<string, unknown>,
  id = `evt_${crypto.randomUUID()}`,
) {
  return {
    event: {
      id,
      created: now(),
      type,
      // biome-ignore lint/suspicious/noExplicitAny: zod record input
      data: { object: object as any, previous_attributes },
    },
  };
}

const caller = () =>
  webhookRouter.createCaller(createInnerTRPCContext({ session: null }));

describe("stripe webhook emails", () => {
  let live: Stripe.Subscription[];
  let stubs: AnyStub[];
  let send: AnyStub;
  let batch: AnyStub;
  let cancelEmail: AnyStub;
  let updateSubscription: AnyStub;

  beforeEach(() => {
    live = [];
    // biome-ignore lint/suspicious/noExplicitAny: Resend result double
    const ok = { data: { id: "email_reminder_1" }, error: null } as any;
    send = vi
      .spyOn(resend.emails, "send")
      .mockImplementation(() => Promise.resolve(ok));
    batch = vi
      .spyOn(resend.batch, "send")
      .mockImplementation(() => Promise.resolve(ok));
    cancelEmail = vi
      .spyOn(resend.emails, "cancel")
      .mockImplementation(() => Promise.resolve(ok));
    updateSubscription = vi
      .spyOn(stripe.subscriptions, "update")
      .mockImplementation(() =>
        // biome-ignore lint/suspicious/noExplicitAny: Stripe response double
        Promise.resolve({ cancel_at_period_end: true } as any),
      );
    stubs = [
      send,
      batch,
      cancelEmail,
      updateSubscription,
      vi.spyOn(delivery, "enabled").mockImplementation(() => true),
      vi.spyOn(stripe.subscriptions, "list").mockImplementation(
        // biome-ignore lint/suspicious/noExplicitAny: auto-paging list double
        () => ({ autoPagingToArray: () => Promise.resolve(live) }) as any,
      ),
      vi.spyOn(stripe.customers, "retrieve").mockImplementation(() =>
        Promise.resolve({
          deleted: false,
          email: "[email protected]",
          // biome-ignore lint/suspicious/noExplicitAny: Stripe customer double
        } as any),
      ),
    ];
  });

  afterEach(() => {
    for (const s of stubs) s.mockRestore();
  });

  function failSends() {
    send.mockRestore();
    batch.mockRestore();
    stubs = stubs.filter((s) => s !== send && s !== batch);
    send = vi
      .spyOn(resend.emails, "send")
      .mockImplementation(() => Promise.reject(new Error("resend is down")));
    batch = vi
      .spyOn(resend.batch, "send")
      .mockImplementation(() => Promise.reject(new Error("resend is down")));
    stubs.push(send, batch);
  }

  async function seed() {
    const fixture = await createTestWorkspace();
    const ownerEmail = `owner-${fixture.user.id}@example.test`;
    await db
      .update(user)
      .set({ email: ownerEmail })
      .where(eq(user.id, fixture.user.id));
    return {
      ...fixture,
      ownerEmail,
      stripeId: fixture.workspace.stripeId ?? "",
    };
  }

  async function addMember(workspaceId: number) {
    const member = await createUser();
    const email = `member-${member.id}@example.test`;
    await db.update(user).set({ email }).where(eq(user.id, member.id));
    await addUserToWorkspace(member.id, workspaceId, "member");
    return email;
  }

  describe("customer.subscription.deleted", () => {
    test("live subscriptions left → no downgrade, no email", async () => {
      const s = await seed();
      live = [subscription(s.stripeId)];

      await caller().customerSubscriptionDeleted(
        event("customer.subscription.deleted", subscription(s.stripeId)),
      );

      const ws = await db
        .select()
        .from(workspace)
        .where(eq(workspace.id, s.workspace.id))
        .get();
      expect(ws?.plan).toBe("team");
      expect(send).toHaveBeenCalledTimes(0);
      expect(batch).toHaveBeenCalledTimes(0);
    });

    test("none left → plan-downgraded to owners + customer, member-removed per trimmed member", async () => {
      const s = await seed();
      const member = await createUser();
      const memberEmail = `member-${member.id}@example.test`;
      await db
        .update(user)
        .set({ email: memberEmail })
        .where(eq(user.id, member.id));
      await addUserToWorkspace(member.id, s.workspace.id, "member");
      // no email on file: cannot be notified, but still counts as removed
      const silent = await createUser({ email: null });
      await addUserToWorkspace(silent.id, s.workspace.id, "member");
      await createPage(s.workspace.id, {
        title: "Kept",
        createdAt: new Date("2020-01-01T00:00:00Z"),
      });
      await createPage(s.workspace.id, {
        title: "Gone",
        createdAt: new Date("2021-01-01T00:00:00Z"),
      });

      const evt = event(
        "customer.subscription.deleted",
        subscription(s.stripeId, { status: "canceled" }),
      );
      await caller().customerSubscriptionDeleted(evt);

      expect(send).toHaveBeenCalledTimes(1);
      const [payload, options] = send.mock.calls[0];
      expect(payload.to).toEqual([
        s.ownerEmail,
        "[email protected]",
      ]);
      expect(payload.subject).toBe(
        "Your workspace is on the free plan — 1 status page deleted",
      );
      expect(options).toEqual({
        idempotencyKey: `stripe:${evt.event.id}:plan-downgraded`,
      });
      expect(payload.react.props.loss.membersRemoved).toBe(2);

      expect(batch).toHaveBeenCalledTimes(1);
      const [emails, batchOptions] = batch.mock.calls[0];
      expect(emails.map((e: { to: string }) => e.to)).toEqual([memberEmail]);
      expect(emails[0].html).toContain(s.ownerEmail);
      expect(batchOptions).toEqual({
        idempotencyKey: `stripe:${evt.event.id}:member-removed:0`,
      });

      const pages = await db
        .select()
        .from(page)
        .where(eq(page.workspaceId, s.workspace.id))
        .all();
      expect(pages.length).toBe(1);
    });

    test("a mail failure never fails the handler; the downgrade is committed", async () => {
      const s = await seed();
      const member = await createUser();
      await db
        .update(user)
        .set({ email: `member-${member.id}@example.test` })
        .where(eq(user.id, member.id));
      await addUserToWorkspace(member.id, s.workspace.id, "member");
      failSends();

      await caller().customerSubscriptionDeleted(
        event(
          "customer.subscription.deleted",
          subscription(s.stripeId, { status: "canceled" }),
        ),
      );

      const ws = await db
        .select()
        .from(workspace)
        .where(eq(workspace.id, s.workspace.id))
        .get();
      expect(ws?.plan).toBe("free");
      expect(send).toHaveBeenCalledTimes(1);
      expect(batch).toHaveBeenCalledTimes(1);
      const members = await db
        .select()
        .from(usersToWorkspaces)
        .where(eq(usersToWorkspaces.workspaceId, s.workspace.id))
        .all();
      expect(members.length).toBe(1);
    });
  });

  describe("customer.subscription.updated", () => {
    test("cancel_at_period_end false → true: confirmation sent, reminder scheduled, id stored", async () => {
      const s = await seed();
      const sub = subscription(s.stripeId, { cancel_at_period_end: true });
      live = [sub];
      // The raw payload's period end is not trustworthy; only live state is.
      const raw = subscription(s.stripeId, {
        cancel_at_period_end: true,
        current_period_end: now() + 25 * DAY,
      });

      const evt = event("customer.subscription.updated", raw, {
        cancel_at_period_end: false,
      });
      await caller().customerSubscriptionUpdated(evt);

      expect(send).toHaveBeenCalledTimes(2);
      const [confirmation, confirmationOptions] = send.mock.calls[0];
      expect(confirmation.subject).toBe("Your openstatus cancellation");
      expect(confirmation.to).toEqual([
        s.ownerEmail,
        "[email protected]",
      ]);
      expect(confirmationOptions).toEqual({
        idempotencyKey: `stripe:${evt.event.id}:cancellation-scheduled`,
      });

      const [reminder, reminderOptions] = send.mock.calls[1];
      expect(new Date(reminder.scheduledAt).getTime()).toBe(
        (sub.items.data[0].current_period_end - 3 * DAY) * 1000,
      );
      expect(reminderOptions).toEqual({
        idempotencyKey: `stripe:${evt.event.id}:plan-ending-soon`,
      });

      expect(updateSubscription).toHaveBeenCalledTimes(1);
      expect(updateSubscription.mock.calls[0]).toEqual([
        sub.id,
        { metadata: { reminder_email_id: "email_reminder_1" } },
      ]);
    });

    test("cancellation scheduled → members get a reminder 3 days before, id stored", async () => {
      const s = await seed();
      const memberEmail = await addMember(s.workspace.id);
      const sub = subscription(s.stripeId, { cancel_at_period_end: true });
      live = [sub];
      send.mockRestore();
      stubs = stubs.filter((x) => x !== send);
      let n = 0;
      send = vi.spyOn(resend.emails, "send").mockImplementation(() =>
        // biome-ignore lint/suspicious/noExplicitAny: Resend result double
        Promise.resolve({ data: { id: `email_${n++}` }, error: null } as any),
      );
      stubs.push(send);

      const evt = event("customer.subscription.updated", sub, {
        cancel_at_period_end: false,
      });
      await caller().customerSubscriptionUpdated(evt);

      expect(send).toHaveBeenCalledTimes(3);
      const [warning, options] = send.mock.calls[2];
      expect(warning.to).toEqual([memberEmail]);
      expect(warning.subject).toContain("ends on");
      expect(warning.react.props.owners).toEqual([s.ownerEmail]);
      expect(new Date(warning.scheduledAt).getTime()).toBe(
        (sub.items.data[0].current_period_end - 3 * DAY) * 1000,
      );
      expect(options).toEqual({
        idempotencyKey: `stripe:${evt.event.id}:member-access-ending:0`,
      });
      expect(batch).toHaveBeenCalledTimes(0);
      expect(updateSubscription.mock.calls[0]).toEqual([
        sub.id,
        {
          metadata: {
            reminder_email_id: "email_1",
            member_reminder_email_ids: "email_2",
          },
        },
      ]);
    });

    test("true → false: member reminders are cancelled with the owner one", async () => {
      const s = await seed();
      const sub = subscription(s.stripeId, {
        metadata: {
          reminder_email_id: "email_owner",
          member_reminder_email_ids: "email_m1,email_m2",
        },
      });
      live = [sub];

      await caller().customerSubscriptionUpdated(
        event("customer.subscription.updated", sub, {
          cancel_at_period_end: true,
        }),
      );

      expect(cancelEmail.mock.calls.map((c) => c[0])).toEqual([
        "email_owner",
        "email_m1",
        "email_m2",
      ]);
      expect(updateSubscription.mock.calls[0]).toEqual([
        sub.id,
        { metadata: { reminder_email_id: "", member_reminder_email_ids: "" } },
      ]);
    });

    test("period end beyond 30 days: confirmation only, nothing stored", async () => {
      const s = await seed();
      const sub = subscription(s.stripeId, {
        cancel_at_period_end: true,
        current_period_end: now() + 200 * DAY,
      });
      live = [sub];

      await caller().customerSubscriptionUpdated(
        event("customer.subscription.updated", sub, {
          cancel_at_period_end: false,
        }),
      );

      expect(send).toHaveBeenCalledTimes(1);
      expect(send.mock.calls[0][0].scheduledAt).toBeUndefined();
      expect(updateSubscription).toHaveBeenCalledTimes(0);
    });

    test("true → false: the scheduled reminder is cancelled and the id cleared", async () => {
      const s = await seed();
      const sub = subscription(s.stripeId, {
        metadata: { reminder_email_id: "email_reminder_1" },
      });
      live = [sub];

      await caller().customerSubscriptionUpdated(
        event("customer.subscription.updated", sub, {
          cancel_at_period_end: true,
        }),
      );

      expect(send).toHaveBeenCalledTimes(0);
      expect(cancelEmail).toHaveBeenCalledTimes(1);
      expect(cancelEmail.mock.calls[0][0]).toBe("email_reminder_1");
      expect(updateSubscription.mock.calls[0]).toEqual([
        sub.id,
        { metadata: { reminder_email_id: "" } },
      ]);
    });

    test("a stale cancel event after a resume sends nothing", async () => {
      const s = await seed();
      live = [subscription(s.stripeId, { cancel_at_period_end: false })];

      await caller().customerSubscriptionUpdated(
        event(
          "customer.subscription.updated",
          subscription(s.stripeId, { cancel_at_period_end: true }),
          { cancel_at_period_end: false },
        ),
      );

      expect(send).toHaveBeenCalledTimes(0);
      expect(updateSubscription).toHaveBeenCalledTimes(0);
    });

    test("a resume racing the metadata write cancels the fresh reminder", async () => {
      const s = await seed();
      const sub = subscription(s.stripeId, { cancel_at_period_end: true });
      live = [sub];
      updateSubscription.mockRestore();
      stubs = stubs.filter((x) => x !== updateSubscription);
      updateSubscription = vi
        .spyOn(stripe.subscriptions, "update")
        .mockImplementation(() =>
          // biome-ignore lint/suspicious/noExplicitAny: Stripe response double
          Promise.resolve({ cancel_at_period_end: false } as any),
        );
      stubs.push(updateSubscription);

      await caller().customerSubscriptionUpdated(
        event("customer.subscription.updated", sub, {
          cancel_at_period_end: false,
        }),
      );

      expect(cancelEmail.mock.calls[0][0]).toBe("email_reminder_1");
      expect(updateSubscription.mock.calls.map((c) => c[1])).toEqual([
        { metadata: { reminder_email_id: "email_reminder_1" } },
        { metadata: { reminder_email_id: "" } },
      ]);
    });

    test("a failed reminder cancel keeps the id for a retry", async () => {
      const s = await seed();
      const sub = subscription(s.stripeId, {
        metadata: { reminder_email_id: "email_reminder_1" },
      });
      live = [sub];
      cancelEmail.mockRestore();
      stubs = stubs.filter((x) => x !== cancelEmail);
      cancelEmail = vi.spyOn(resend.emails, "cancel").mockImplementation(() =>
        Promise.resolve({
          data: null,
          error: { name: "application_error" },
          // biome-ignore lint/suspicious/noExplicitAny: Resend error double
        } as any),
      );
      stubs.push(cancelEmail);

      await caller().customerSubscriptionUpdated(
        event("customer.subscription.updated", sub, {
          cancel_at_period_end: true,
        }),
      );

      expect(cancelEmail).toHaveBeenCalledTimes(1);
      expect(updateSubscription).toHaveBeenCalledTimes(0);
    });

    test("a failed confirmation does not suppress the reminder", async () => {
      const s = await seed();
      const sub = subscription(s.stripeId, { cancel_at_period_end: true });
      live = [sub];
      send.mockRestore();
      stubs = stubs.filter((x) => x !== send);
      let calls = 0;
      send = vi.spyOn(resend.emails, "send").mockImplementation(() =>
        calls++ === 0
          ? Promise.reject(new Error("resend is down"))
          : // biome-ignore lint/suspicious/noExplicitAny: Resend result double
            Promise.resolve({
              data: { id: "email_reminder_1" },
              error: null,
            } as any),
      );
      stubs.push(send);

      await caller().customerSubscriptionUpdated(
        event("customer.subscription.updated", sub, {
          cancel_at_period_end: false,
        }),
      );

      expect(send).toHaveBeenCalledTimes(2);
      expect(send.mock.calls[1][0].scheduledAt).toBeDefined();
      expect(updateSubscription).toHaveBeenCalledTimes(1);
    });

    test("metadata-only update and unrelated updates send nothing", async () => {
      const s = await seed();
      const sub = subscription(s.stripeId, { cancel_at_period_end: true });
      live = [sub];

      await caller().customerSubscriptionUpdated(
        event("customer.subscription.updated", sub, { metadata: {} }),
      );
      await caller().customerSubscriptionUpdated(
        event("customer.subscription.updated", sub),
      );

      expect(send).toHaveBeenCalledTimes(0);
      expect(cancelEmail).toHaveBeenCalledTimes(0);
      expect(updateSubscription).toHaveBeenCalledTimes(0);
    });

    test("a replayed event reuses the same idempotency keys", async () => {
      const s = await seed();
      const sub = subscription(s.stripeId, { cancel_at_period_end: true });
      live = [sub];
      const evt = event("customer.subscription.updated", sub, {
        cancel_at_period_end: false,
      });

      await caller().customerSubscriptionUpdated(evt);
      await caller().customerSubscriptionUpdated(evt);

      const keys = send.mock.calls.map((c) => c[1].idempotencyKey);
      expect(keys.slice(0, 2)).toEqual(keys.slice(2, 4));
    });

    test("a custom-deal price syncs the plan but keeps hand-set limits", async () => {
      const s = await seed();
      const handSet = JSON.stringify({ monitors: 999 });
      await db
        .update(workspace)
        .set({ limits: handSet })
        .where(eq(workspace.id, s.workspace.id));
      const sub = subscription(s.stripeId, {
        id: "sub_custom_deal",
        items: {
          data: [
            {
              price: { id: TEAM_PRICE },
              quantity: 1,
              current_period_end: now() + 10 * DAY,
            },
            {
              price: { id: "price_custom_deal" },
              quantity: 1,
              current_period_end: now() + 10 * DAY,
            },
          ],
        },
      } as Partial<Stripe.Subscription>);
      live = [sub];

      await caller().customerSubscriptionUpdated(
        event("customer.subscription.updated", sub, { metadata: {} }),
      );

      const ws = await db
        .select()
        .from(workspace)
        .where(eq(workspace.id, s.workspace.id))
        .get();
      expect(ws?.subscriptionId).toBe("sub_custom_deal");
      expect(ws?.plan).toBe("team");
      expect(ws?.limits).toBe(handSet);
    });

    test("a mail failure does not fail the plan sync", async () => {
      const s = await seed();
      const sub = subscription(s.stripeId, { cancel_at_period_end: true });
      live = [sub];
      failSends();

      await caller().customerSubscriptionUpdated(
        event("customer.subscription.updated", sub, {
          cancel_at_period_end: false,
        }),
      );

      const ws = await db
        .select()
        .from(workspace)
        .where(eq(workspace.id, s.workspace.id))
        .get();
      expect(ws?.subscriptionId).toBe(sub.id);
    });
  });

  describe("customer.subscription.trial_will_end", () => {
    const trialEnd = now() + 3 * DAY;

    test("no card → lists what the free plan trims, once per event", async () => {
      const s = await seed();
      await createPage(s.workspace.id, {
        title: "Kept",
        createdAt: new Date("2020-01-01T00:00:00Z"),
      });
      await createPage(s.workspace.id, {
        title: "Gone",
        createdAt: new Date("2021-01-01T00:00:00Z"),
      });
      const evt = event(
        "customer.subscription.trial_will_end",
        subscription(s.stripeId, { status: "trialing", trial_end: trialEnd }),
      );

      await caller().customerSubscriptionTrialWillEnd(evt);

      expect(send).toHaveBeenCalledTimes(1);
      const [payload, options] = send.mock.calls[0];
      expect(payload.subject).toContain("add a payment method to keep");
      expect(payload.to).toContain(s.ownerEmail);
      expect(payload.react.props).toMatchObject({
        workspaceSlug: s.workspace.slug,
        plan: s.workspace.plan,
        hasPaymentMethod: false,
        loss: { pagesDeleted: ["Gone"], keptPageTitle: "Kept" },
      });
      expect(options).toEqual({
        idempotencyKey: `stripe:${evt.event.id}:trial-ending`,
      });
    });

    test("no card → non-owner members are warned now; card on file → not", async () => {
      const s = await seed();
      const memberEmail = await addMember(s.workspace.id);

      const evt = event(
        "customer.subscription.trial_will_end",
        subscription(s.stripeId, { status: "trialing", trial_end: trialEnd }),
      );
      await caller().customerSubscriptionTrialWillEnd(evt);

      expect(send).toHaveBeenCalledTimes(2);
      const [warning, options] = send.mock.calls[1];
      expect(warning.to).toEqual([memberEmail]);
      expect(warning.react.props.reason).toBe("trial");
      expect(warning.scheduledAt).toBeUndefined();
      expect(options).toEqual({
        idempotencyKey: `stripe:${evt.event.id}:member-access-ending:0`,
      });

      await caller().customerSubscriptionTrialWillEnd(
        event(
          "customer.subscription.trial_will_end",
          subscription(s.stripeId, {
            status: "trialing",
            trial_end: trialEnd,
            default_payment_method: "pm_test_1",
          }),
        ),
      );
      expect(send).toHaveBeenCalledTimes(3);
    });

    test("card on file → plan continues, no loss lines", async () => {
      const s = await seed();
      await createPage(s.workspace.id, {
        title: "Kept",
        createdAt: new Date("2020-01-01T00:00:00Z"),
      });
      await createPage(s.workspace.id, {
        title: "Gone",
        createdAt: new Date("2021-01-01T00:00:00Z"),
      });

      await caller().customerSubscriptionTrialWillEnd(
        event(
          "customer.subscription.trial_will_end",
          subscription(s.stripeId, {
            status: "trialing",
            trial_end: trialEnd,
            default_payment_method: "pm_test_1",
          }),
        ),
      );

      expect(send).toHaveBeenCalledTimes(1);
      const [payload] = send.mock.calls[0];
      expect(payload.subject).toContain("plan continues");
      expect(payload.react.props).toMatchObject({ hasPaymentMethod: true });
      expect(payload.react.props.loss).toBeUndefined();
    });

    test("trial ended early (trial_end: now) → nothing sent", async () => {
      const s = await seed();
      const evt = event(
        "customer.subscription.trial_will_end",
        subscription(s.stripeId, { status: "active", trial_end: now() - 1 }),
      );
      await caller().customerSubscriptionTrialWillEnd(evt);
      expect(send).toHaveBeenCalledTimes(0);
    });

    test("a mail failure fails the webhook so Stripe redelivers", async () => {
      const s = await seed();
      failSends();
      const evt = event(
        "customer.subscription.trial_will_end",
        subscription(s.stripeId, { status: "trialing", trial_end: trialEnd }),
      );

      await expect(
        caller().customerSubscriptionTrialWillEnd(evt),
      ).rejects.toThrow();
    });

    test("a Resend error response fails the webhook too", async () => {
      const s = await seed();
      send.mockRestore();
      stubs = stubs.filter((x) => x !== send);
      send = vi.spyOn(resend.emails, "send").mockImplementation(() =>
        Promise.resolve({
          data: null,
          error: { name: "application_error", message: "boom" },
          // biome-ignore lint/suspicious/noExplicitAny: Resend result double
        } as any),
      );
      stubs.push(send);

      await expect(
        caller().customerSubscriptionTrialWillEnd(
          event(
            "customer.subscription.trial_will_end",
            subscription(s.stripeId, {
              status: "trialing",
              trial_end: trialEnd,
            }),
          ),
        ),
      ).rejects.toThrow();
    });

    test("no trial_end → nothing sent", async () => {
      const s = await seed();
      await caller().customerSubscriptionTrialWillEnd(
        event(
          "customer.subscription.trial_will_end",
          subscription(s.stripeId, { trial_end: null }),
        ),
      );
      expect(send).toHaveBeenCalledTimes(0);
    });
  });
});