Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

11.7 KB
Raw Blame
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";

const mocks = vi.hoisted(() => ({
  customersCreate: vi.fn(),
  checkoutSessionsCreate: vi.fn(),
  subscriptionsList: vi.fn(),
}));

vi.mock("../../lib/stripe.js", () => ({
  stripe: {
    customers: { create: mocks.customersCreate },
    checkout: { sessions: { create: mocks.checkoutSessionsCreate } },
    subscriptions: { list: mocks.subscriptionsList },
  },
}));

// Real Drizzle queries against PGlite so the inline membership check β€” the SOLE
// authorization gate on this route β€” is exercised for real.
vi.mock("../../db/postgres/postgres.js", async () => {
  const { PGlite } = await import("@electric-sql/pglite");
  const { drizzle } = await import("drizzle-orm/pglite");
  const schema = await import("../../db/postgres/schema.js");
  const client = new PGlite();
  const db = drizzle(client, { schema });
  return { db, sql: client };
});

import { sql } from "../../db/postgres/postgres.js";
import { invalidateStripeSubscriptionCache } from "../../lib/subscriptionUtils.js";
import { createCheckoutSession } from "./createCheckoutSession.js";

const DDL = `
CREATE TABLE "organization" ("id" text PRIMARY KEY, "name" text NOT NULL, "slug" text, "createdAt" timestamp DEFAULT now(), "stripeCustomerId" text);
CREATE TABLE "user" ("id" text PRIMARY KEY, "name" text, "email" text, "createdAt" timestamp DEFAULT now());
CREATE TABLE "member" ("id" text PRIMARY KEY, "organizationId" text NOT NULL, "userId" text NOT NULL, "role" text NOT NULL, "createdAt" timestamp DEFAULT now(), "has_restricted_site_access" boolean NOT NULL DEFAULT false);
`;

function replyStub() {
  const reply: any = { statusCode: 200 };
  reply.status = (code: number) => {
    reply.statusCode = code;
    return reply;
  };
  reply.send = (body: unknown) => {
    reply.body = body;
    return reply;
  };
  return reply;
}

function requestStub(userId: string | undefined, body: Record<string, unknown>) {
  return { user: userId ? { id: userId } : undefined, body } as any;
}

async function orgCustomerId(orgId: string): Promise<string | null> {
  const result = await (sql as any).query(`SELECT "stripeCustomerId" FROM "organization" WHERE "id" = $1`, [orgId]);
  return result.rows[0]?.stripeCustomerId ?? null;
}

const validBody = { priceId: "price_pro", returnUrl: "https://app.rybbit.io/billing", organizationId: "org_1" };

beforeAll(async () => {
  await (sql as any).exec(DDL);
});

beforeEach(async () => {
  vi.clearAllMocks();
  await (sql as any).exec(`TRUNCATE "organization", "user", "member"`);
  await (sql as any).exec(`
    INSERT INTO "organization" ("id","name","slug","stripeCustomerId") VALUES
      ('org_1','Acme','acme','cus_1'),
      ('org_2','Rival','rival','cus_2');
    INSERT INTO "user" ("id","name","email") VALUES
      ('u_owner','Owner','[email protected]'),
      ('u_member','Member','[email protected]'),
      ('u_outsider','Outsider','[email protected]');
    INSERT INTO "member" ("id","organizationId","userId","role") VALUES
      ('m_owner','org_1','u_owner','owner'),
      ('m_member','org_1','u_member','member'),
      ('m_outsider','org_2','u_outsider','owner');
  `);
  mocks.customersCreate.mockResolvedValue({ id: "cus_new" });
  mocks.checkoutSessionsCreate.mockResolvedValue({ client_secret: "cs_secret_123" });
  mocks.subscriptionsList.mockResolvedValue({ data: [] });
  invalidateStripeSubscriptionCache("cus_1");
});

describe("createCheckoutSession β€” one trial per organization", () => {
  it("gives an organization with no subscription history the 7-day trial", async () => {
    await createCheckoutSession(requestStub("u_owner", validBody), replyStub());

    expect(mocks.checkoutSessionsCreate).toHaveBeenCalledWith(
      expect.objectContaining({ subscription_data: { trial_period_days: 7 } })
    );
  });

  it("charges a returning customer straight away instead of starting another trial", async () => {
    mocks.subscriptionsList.mockResolvedValue({ data: [{ id: "sub_old", status: "canceled" }] });

    await createCheckoutSession(requestStub("u_owner", validBody), replyStub());

    expect(mocks.subscriptionsList).toHaveBeenCalledWith(expect.objectContaining({ customer: "cus_1" }));
    expect(mocks.checkoutSessionsCreate.mock.calls[0][0]).not.toHaveProperty("subscription_data");
  });

  it("fails the checkout instead of granting a trial when the customer's history can't be read", async () => {
    mocks.subscriptionsList.mockRejectedValue(new Error("stripe unavailable"));
    const reply = replyStub();

    await createCheckoutSession({ ...requestStub("u_owner", validBody), log: { error: vi.fn() } }, reply);

    expect(reply.statusCode).toBe(500);
    expect(mocks.checkoutSessionsCreate).not.toHaveBeenCalled();
  });

  it("still offers the trial after an abandoned checkout", async () => {
    mocks.subscriptionsList.mockResolvedValue({ data: [{ id: "sub_abandoned", status: "incomplete_expired" }] });

    await createCheckoutSession(requestStub("u_owner", validBody), replyStub());

    expect(mocks.checkoutSessionsCreate).toHaveBeenCalledWith(
      expect.objectContaining({ subscription_data: { trial_period_days: 7 } })
    );
  });
});

describe("createCheckoutSession β€” existing subscribers", () => {
  it.each(["active", "trialing"])("refuses a second checkout when the org has a %s subscription", async status => {
    mocks.subscriptionsList.mockResolvedValue({ data: [{ id: "sub_live", status }] });
    const reply = replyStub();

    await createCheckoutSession(requestStub("u_owner", validBody), reply);

    expect(reply.statusCode).toBe(409);
    expect(reply.body.error).toContain("already has an active subscription");
    expect(mocks.checkoutSessionsCreate).not.toHaveBeenCalled();
  });

  it("finds an active subscription behind a full page of newer canceled ones", async () => {
    const canceled = Array.from({ length: 100 }, (_, i) => ({ id: `sub_old_${i}`, status: "canceled" }));
    mocks.subscriptionsList.mockImplementation(async ({ status }: { status: string }) => ({
      data: status === "all" ? canceled : status === "active" ? [{ id: "sub_live", status: "active" }] : [],
    }));
    const reply = replyStub();

    await createCheckoutSession(requestStub("u_owner", validBody), reply);

    expect(reply.statusCode).toBe(409);
    expect(mocks.subscriptionsList).toHaveBeenCalledWith(
      expect.objectContaining({ customer: "cus_1", status: "active" })
    );
    expect(mocks.subscriptionsList).toHaveBeenCalledWith(
      expect.objectContaining({ customer: "cus_1", status: "trialing" })
    );
    expect(mocks.checkoutSessionsCreate).not.toHaveBeenCalled();
  });

  it("lets an org whose subscription lapsed subscribe again", async () => {
    mocks.subscriptionsList.mockResolvedValue({ data: [{ id: "sub_old", status: "canceled" }] });
    const reply = replyStub();

    await createCheckoutSession(requestStub("u_owner", validBody), reply);

    expect(reply.statusCode).toBe(200);
    expect(mocks.checkoutSessionsCreate).toHaveBeenCalledOnce();
  });
});

describe("createCheckoutSession β€” authorization", () => {
  it("creates a checkout session for the org owner with metadata.organizationId from the authorized org", async () => {
    const reply = replyStub();

    await createCheckoutSession(requestStub("u_owner", validBody), reply);

    expect(reply.statusCode).toBe(200);
    expect(reply.body).toEqual({ clientSecret: "cs_secret_123" });
    // metadata.organizationId is what the webhook later trusts β€” it must be the org
    // the membership check authorized, not anything attacker-controlled.
    expect(mocks.checkoutSessionsCreate).toHaveBeenCalledWith(
      expect.objectContaining({
        mode: "subscription",
        ui_mode: "embedded",
        customer: "cus_1",
        line_items: [{ price: "price_pro", quantity: 1 }],
        return_url: "https://app.rybbit.io/billing",
        metadata: { organizationId: "org_1" },
        subscription_data: { trial_period_days: 7 },
      })
    );
    // Org already had a Stripe customer β€” no new customer, no DB write.
    expect(mocks.customersCreate).not.toHaveBeenCalled();
    expect(await orgCustomerId("org_1")).toBe("cus_1");
  });

  it("rejects a non-owner member with 403 and creates nothing", async () => {
    const reply = replyStub();

    await createCheckoutSession(requestStub("u_member", validBody), reply);

    expect(reply.statusCode).toBe(403);
    expect(reply.body.error).toBe("Only organization owners can manage billing");
    expect(mocks.customersCreate).not.toHaveBeenCalled();
    expect(mocks.checkoutSessionsCreate).not.toHaveBeenCalled();
  });

  it("rejects a user with no membership in the target org (cross-org attempt)", async () => {
    const reply = replyStub();

    // u_outsider owns org_2 but must not be able to start checkout for org_1.
    await createCheckoutSession(requestStub("u_outsider", validBody), reply);

    expect(reply.statusCode).toBe(403);
    expect(mocks.customersCreate).not.toHaveBeenCalled();
    expect(mocks.checkoutSessionsCreate).not.toHaveBeenCalled();
    expect(await orgCustomerId("org_1")).toBe("cus_1");
  });

  it("returns 401 when there is no authenticated user", async () => {
    const reply = replyStub();

    await createCheckoutSession(requestStub(undefined, validBody), reply);

    expect(reply.statusCode).toBe(401);
    expect(mocks.checkoutSessionsCreate).not.toHaveBeenCalled();
  });

  it("returns 400 when priceId, returnUrl or organizationId is missing", async () => {
    for (const omit of ["priceId", "returnUrl", "organizationId"] as const) {
      const body: Record<string, unknown> = { ...validBody };
      delete body[omit];
      const reply = replyStub();
      await createCheckoutSession(requestStub("u_owner", body), reply);
      expect(reply.statusCode).toBe(400);
      expect(reply.body.error).toContain("Missing required parameters");
    }
    expect(mocks.checkoutSessionsCreate).not.toHaveBeenCalled();
  });
});

describe("createCheckoutSession β€” customer creation and referral wiring", () => {
  it("creates a Stripe customer and persists it when the org has none", async () => {
    await (sql as any).exec(`UPDATE "organization" SET "stripeCustomerId" = NULL WHERE "id" = 'org_1'`);
    const reply = replyStub();

    await createCheckoutSession(requestStub("u_owner", validBody), reply);

    expect(reply.statusCode).toBe(200);
    expect(mocks.customersCreate).toHaveBeenCalledWith({
      email: "[email protected]",
      name: "Acme",
      metadata: { organizationId: "org_1", createdByUserId: "u_owner" },
    });
    expect(await orgCustomerId("org_1")).toBe("cus_new");
    expect(mocks.checkoutSessionsCreate).toHaveBeenCalledWith(expect.objectContaining({ customer: "cus_new" }));
  });

  it("threads the referral through customer metadata and client_reference_id", async () => {
    await (sql as any).exec(`UPDATE "organization" SET "stripeCustomerId" = NULL WHERE "id" = 'org_1'`);
    const reply = replyStub();

    await createCheckoutSession(requestStub("u_owner", { ...validBody, referral: "ref_abc" }), reply);

    expect(mocks.customersCreate).toHaveBeenCalledWith(
      expect.objectContaining({ metadata: expect.objectContaining({ referral: "ref_abc" }) })
    );
    expect(mocks.checkoutSessionsCreate).toHaveBeenCalledWith(
      expect.objectContaining({ client_reference_id: "ref_abc" })
    );
  });

  it("returns 404 when the organization does not exist", async () => {
    // Membership row without a matching organization row.
    await (sql as any).exec(
      `INSERT INTO "member" ("id","organizationId","userId","role") VALUES ('m_ghost','org_ghost','u_owner','owner')`
    );
    const reply = replyStub();

    await createCheckoutSession(requestStub("u_owner", { ...validBody, organizationId: "org_ghost" }), reply);

    expect(reply.statusCode).toBe(404);
    expect(mocks.checkoutSessionsCreate).not.toHaveBeenCalled();
  });
});