-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathmobile-checkout.test.ts
More file actions
149 lines (130 loc) · 4.36 KB
/
Copy pathmobile-checkout.test.ts
File metadata and controls
149 lines (130 loc) · 4.36 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { beforeEach, describe, expect, it, vi } from "vitest";
import { POST as startGuestCheckout } from "@/app/api/settings/billing/guest-checkout/route";
import { GET } from "@/app/mobile/checkout/complete/route";
import {
getCheckoutRedirectUrls,
getMobileCheckoutDeepLink,
} from "@/lib/mobile-checkout";
const checkoutMocks = vi.hoisted(() => ({
create: vi.fn(),
track: vi.fn(() => Promise.resolve()),
}));
vi.mock("@cap/env", () => ({
buildEnv: {},
serverEnv: () => ({ WEB_URL: "https://cap.so" }),
}));
vi.mock("@cap/utils", () => ({
stripe: () => ({
checkout: {
sessions: { create: checkoutMocks.create },
},
}),
isValidStripePlanPriceId: (id: string) => id === "price_pro",
}));
vi.mock("@/lib/server-analytics", () => ({
trackServerEvent: checkoutMocks.track,
}));
const makeGuestCheckoutRequest = (body: Record<string, unknown>) =>
new Request("https://cap.so/api/settings/billing/guest-checkout", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
}) as unknown as import("next/server").NextRequest;
describe("checkout redirects", () => {
beforeEach(() => {
vi.clearAllMocks();
checkoutMocks.create.mockResolvedValue({
id: "cs_test",
url: "https://pay.cap.so/session",
});
});
it("preserves the existing desktop checkout redirects", () => {
expect(getCheckoutRedirectUrls("desktop", "https://cap.so")).toEqual({
successUrl: "https://cap.so/dashboard/caps?upgrade=true",
cancelUrl: "https://cap.so/pricing",
});
});
it("preserves the existing web guest checkout redirects", () => {
expect(getCheckoutRedirectUrls("web", "https://cap.so")).toEqual({
successUrl:
"https://cap.so/dashboard/caps?upgrade=true&guest=true&session_id={CHECKOUT_SESSION_ID}",
cancelUrl: "https://cap.so/pricing",
});
});
it("keeps existing guest checkout requests on the web flow", async () => {
const response = await startGuestCheckout(
makeGuestCheckoutRequest({ priceId: "price_pro", quantity: 1 }),
);
expect(response.status).toBe(200);
expect(checkoutMocks.create).toHaveBeenCalledWith({
line_items: [{ price: "price_pro", quantity: 1 }],
mode: "subscription",
success_url:
"https://cap.so/dashboard/caps?upgrade=true&guest=true&session_id={CHECKOUT_SESSION_ID}",
cancel_url: "https://cap.so/pricing",
allow_promotion_codes: true,
metadata: {
platform: "web",
guestCheckout: "true",
},
});
});
it("rejects non-allowlisted arbitrary priceId with 400", async () => {
const response = await startGuestCheckout(
makeGuestCheckoutRequest({
priceId: "price_arbitrary_attacker_id",
quantity: 1,
}),
);
expect(response.status).toBe(400);
expect(await response.json()).toEqual({ error: "Invalid priceId" });
expect(checkoutMocks.create).not.toHaveBeenCalled();
});
it("sends mobile checkout results through the HTTPS completion route", () => {
expect(getCheckoutRedirectUrls("mobile", "https://cap.so/")).toEqual({
successUrl: "https://cap.so/mobile/checkout/complete?checkout=success",
cancelUrl: "https://cap.so/mobile/checkout/complete?checkout=cancelled",
});
});
it("uses the app return only when guest checkout is explicitly mobile", async () => {
const response = await startGuestCheckout(
makeGuestCheckoutRequest({
priceId: "price_pro",
quantity: 1,
platform: "mobile",
}),
);
expect(response.status).toBe(200);
expect(checkoutMocks.create).toHaveBeenCalledWith(
expect.objectContaining({
success_url: "https://cap.so/mobile/checkout/complete?checkout=success",
cancel_url:
"https://cap.so/mobile/checkout/complete?checkout=cancelled",
metadata: {
platform: "mobile",
guestCheckout: "true",
},
}),
);
});
it("redirects successful mobile checkout back to the Cap app", () => {
const response = GET(
new Request("https://cap.so/mobile/checkout/complete?checkout=success"),
);
expect(response.status).toBe(302);
expect(response.headers.get("location")).toBe(
getMobileCheckoutDeepLink("success"),
);
});
it("treats missing or unknown results as cancellation", () => {
for (const checkout of ["", "?checkout=unknown"]) {
const response = GET(
new Request(`https://cap.so/mobile/checkout/complete${checkout}`),
);
expect(response.status).toBe(302);
expect(response.headers.get("location")).toBe(
getMobileCheckoutDeepLink("cancelled"),
);
}
});
});