|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { isValidStripePlanPriceId, STRIPE_PLAN_IDS } from "@cap/utils"; |
| 3 | +import { POST as subscribe } from "@/app/api/settings/billing/subscribe/route"; |
| 4 | + |
| 5 | +const checkoutMocks = vi.hoisted(() => ({ |
| 6 | + create: vi.fn(), |
| 7 | + track: vi.fn(() => Promise.resolve()), |
| 8 | + getCurrentUser: vi.fn(), |
| 9 | + dbUpdate: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("@cap/env", () => ({ |
| 13 | + buildEnv: { NEXT_PUBLIC_IS_CAP: "true" }, |
| 14 | + serverEnv: () => ({ WEB_URL: "https://cap.so" }), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock("@cap/database/auth/session", () => ({ |
| 18 | + getCurrentUser: checkoutMocks.getCurrentUser, |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock("@cap/database", () => ({ |
| 22 | + db: () => ({ |
| 23 | + update: () => ({ |
| 24 | + set: () => ({ |
| 25 | + where: checkoutMocks.dbUpdate, |
| 26 | + }), |
| 27 | + }), |
| 28 | + }), |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock("@cap/database/schema", () => ({ |
| 32 | + users: { id: "id" }, |
| 33 | +})); |
| 34 | + |
| 35 | +vi.mock("@cap/utils", async (importOriginal) => { |
| 36 | + const actual = await importOriginal<typeof import("@cap/utils")>(); |
| 37 | + return { |
| 38 | + ...actual, |
| 39 | + stripe: () => ({ |
| 40 | + customers: { |
| 41 | + list: vi.fn().mockResolvedValue({ data: [] }), |
| 42 | + create: vi.fn().mockResolvedValue({ id: "cus_new" }), |
| 43 | + update: vi.fn().mockResolvedValue({ id: "cus_new" }), |
| 44 | + }, |
| 45 | + checkout: { |
| 46 | + sessions: { create: checkoutMocks.create }, |
| 47 | + }, |
| 48 | + }), |
| 49 | + userIsPro: vi.fn().mockReturnValue(false), |
| 50 | + }; |
| 51 | +}); |
| 52 | + |
| 53 | +vi.mock("@/lib/server-analytics", () => ({ |
| 54 | + trackServerEvent: checkoutMocks.track, |
| 55 | +})); |
| 56 | + |
| 57 | +const makeSubscribeRequest = (body: Record<string, unknown>) => |
| 58 | + new Request("https://cap.so/api/settings/billing/subscribe", { |
| 59 | + method: "POST", |
| 60 | + headers: { "Content-Type": "application/json" }, |
| 61 | + body: JSON.stringify(body), |
| 62 | + }) as unknown as import("next/server").NextRequest; |
| 63 | + |
| 64 | +describe("Stripe plan allowlist", () => { |
| 65 | + it("accepts only legitimate Pro plan price IDs", () => { |
| 66 | + expect(isValidStripePlanPriceId(STRIPE_PLAN_IDS.development.yearly)).toBe( |
| 67 | + true, |
| 68 | + ); |
| 69 | + expect(isValidStripePlanPriceId(STRIPE_PLAN_IDS.development.monthly)).toBe( |
| 70 | + true, |
| 71 | + ); |
| 72 | + expect(isValidStripePlanPriceId(STRIPE_PLAN_IDS.production.yearly)).toBe( |
| 73 | + true, |
| 74 | + ); |
| 75 | + expect(isValidStripePlanPriceId(STRIPE_PLAN_IDS.production.monthly)).toBe( |
| 76 | + true, |
| 77 | + ); |
| 78 | + |
| 79 | + expect(isValidStripePlanPriceId("price_arbitrary_attacker_id")).toBe(false); |
| 80 | + expect(isValidStripePlanPriceId("price_free_tier")).toBe(false); |
| 81 | + expect(isValidStripePlanPriceId("")).toBe(false); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +describe("POST /api/settings/billing/subscribe", () => { |
| 86 | + beforeEach(() => { |
| 87 | + vi.clearAllMocks(); |
| 88 | + checkoutMocks.getCurrentUser.mockResolvedValue({ |
| 89 | + id: "user_123", |
| 90 | + email: "user@example.test", |
| 91 | + stripeCustomerId: "cus_123", |
| 92 | + }); |
| 93 | + checkoutMocks.create.mockResolvedValue({ |
| 94 | + id: "cs_test", |
| 95 | + url: "https://pay.cap.so/session", |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it("rejects arbitrary price IDs with 400", async () => { |
| 100 | + const response = await subscribe( |
| 101 | + makeSubscribeRequest({ |
| 102 | + priceId: "price_arbitrary_malicious", |
| 103 | + quantity: 1, |
| 104 | + }), |
| 105 | + ); |
| 106 | + |
| 107 | + expect(response.status).toBe(400); |
| 108 | + expect(await response.json()).toEqual({ |
| 109 | + error: true, |
| 110 | + message: "Invalid priceId", |
| 111 | + }); |
| 112 | + expect(checkoutMocks.create).not.toHaveBeenCalled(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("accepts valid Pro plan price ID and creates checkout session", async () => { |
| 116 | + const response = await subscribe( |
| 117 | + makeSubscribeRequest({ |
| 118 | + priceId: STRIPE_PLAN_IDS.development.monthly, |
| 119 | + quantity: 2, |
| 120 | + }), |
| 121 | + ); |
| 122 | + |
| 123 | + expect(response.status).toBe(200); |
| 124 | + expect(checkoutMocks.create).toHaveBeenCalledWith( |
| 125 | + expect.objectContaining({ |
| 126 | + customer: "cus_123", |
| 127 | + line_items: [ |
| 128 | + { price: STRIPE_PLAN_IDS.development.monthly, quantity: 2 }, |
| 129 | + ], |
| 130 | + mode: "subscription", |
| 131 | + }), |
| 132 | + ); |
| 133 | + }); |
| 134 | +}); |
0 commit comments