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