-
Notifications
You must be signed in to change notification settings - Fork 9
feat(api): migrate POST /api/stripe/portal/create #499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arpitgupta1214
wants to merge
2
commits into
test
Choose a base branch
from
migrate/stripe-group4
base: test
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
app/api/stripe/checkout-sessions/__tests__/route.options.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import "./routeTestMocks"; | ||
| import { describe, it, expect } from "vitest"; | ||
| import { getCorsHeaders } from "@/lib/networking/getCorsHeaders"; | ||
|
|
||
| const { OPTIONS } = await import("../route"); | ||
|
|
||
| describe("OPTIONS /api/stripe/checkout-sessions", () => { | ||
| it("returns 200 with CORS headers", async () => { | ||
| const res = await OPTIONS(); | ||
| expect(res.status).toBe(200); | ||
| expect(getCorsHeaders).toHaveBeenCalled(); | ||
| expect(res.headers.get("Access-Control-Allow-Origin")).toBe("*"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import "./routeTestMocks"; | ||
| import { describe, it, expect } from "vitest"; | ||
|
|
||
| const { POST, OPTIONS } = await import("../route"); | ||
|
|
||
| describe("app/api/stripe/checkout-sessions/route", () => { | ||
| it("exports POST and OPTIONS handlers", () => { | ||
| expect(typeof POST).toBe("function"); | ||
| expect(typeof OPTIONS).toBe("function"); | ||
| }); | ||
| }); |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
app/api/stripe/portal-sessions/__tests__/route.post.outcomes.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import "./routeTestMocks"; | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { validateCreatePortalSessionRequest } from "@/lib/stripe/validateCreatePortalSessionRequest"; | ||
| import { createPortalSession } from "@/lib/stripe/createPortalSession"; | ||
| import { getStripeCustomerIdByAccountId } from "@/lib/supabase/billing_customers/getStripeCustomerIdByAccountId"; | ||
|
|
||
| const { POST } = await import("../route"); | ||
|
|
||
| const ACCOUNT = "123e4567-e89b-12d3-a456-426614174001"; | ||
|
|
||
| describe("POST /api/stripe/portal-sessions (handler outcomes)", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.mocked(validateCreatePortalSessionRequest).mockReset(); | ||
| vi.mocked(getStripeCustomerIdByAccountId).mockReset(); | ||
| vi.spyOn(console, "error").mockImplementation(() => undefined); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.mocked(console.error).mockRestore(); | ||
| }); | ||
|
|
||
| it("returns validation response unchanged", async () => { | ||
| const err = NextResponse.json({ error: "bad" }, { status: 400 }); | ||
| vi.mocked(validateCreatePortalSessionRequest).mockResolvedValue(err); | ||
| const req = new NextRequest("http://localhost/api/stripe/portal-sessions", { | ||
| method: "POST", | ||
| body: "{}", | ||
| }); | ||
| expect(await POST(req)).toBe(err); | ||
| expect(getStripeCustomerIdByAccountId).not.toHaveBeenCalled(); | ||
| expect(createPortalSession).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns 404 when account has no Stripe customer", async () => { | ||
| vi.mocked(validateCreatePortalSessionRequest).mockResolvedValue({ | ||
| accountId: ACCOUNT, | ||
| returnUrl: "https://chat.recoupable.com/back", | ||
| }); | ||
| vi.mocked(getStripeCustomerIdByAccountId).mockResolvedValue(null); | ||
|
|
||
| const res = await POST( | ||
| new NextRequest("http://localhost/api/stripe/portal-sessions", { | ||
| method: "POST", | ||
| body: "{}", | ||
| }), | ||
| ); | ||
| expect(res.status).toBe(404); | ||
| await expect(res.json()).resolves.toEqual({ | ||
| error: "No Stripe customer found for this account", | ||
| }); | ||
| expect(createPortalSession).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns 200 with id and url", async () => { | ||
| vi.mocked(validateCreatePortalSessionRequest).mockResolvedValue({ | ||
| accountId: ACCOUNT, | ||
| returnUrl: "https://chat.recoupable.com/back", | ||
| }); | ||
| vi.mocked(getStripeCustomerIdByAccountId).mockResolvedValue("cus_123"); | ||
| vi.mocked(createPortalSession).mockResolvedValue({ | ||
| id: "bps_test_abc", | ||
| url: "https://billing.stripe.com/p/session/abc", | ||
| } as Awaited<ReturnType<typeof createPortalSession>>); | ||
|
|
||
| const res = await POST( | ||
| new NextRequest("http://localhost/api/stripe/portal-sessions", { | ||
| method: "POST", | ||
| body: "{}", | ||
| }), | ||
| ); | ||
| expect(res.status).toBe(200); | ||
| await expect(res.json()).resolves.toEqual({ | ||
| id: "bps_test_abc", | ||
| url: "https://billing.stripe.com/p/session/abc", | ||
| }); | ||
| expect(createPortalSession).toHaveBeenCalledWith("cus_123", "https://chat.recoupable.com/back"); | ||
| }); | ||
|
|
||
| it("returns 400 when session.url is missing", async () => { | ||
| vi.mocked(validateCreatePortalSessionRequest).mockResolvedValue({ | ||
| accountId: ACCOUNT, | ||
| returnUrl: "https://chat.recoupable.com/back", | ||
| }); | ||
| vi.mocked(getStripeCustomerIdByAccountId).mockResolvedValue("cus_123"); | ||
| vi.mocked(createPortalSession).mockResolvedValue({ | ||
| id: "bps_test_abc", | ||
| url: null, | ||
| } as unknown as Awaited<ReturnType<typeof createPortalSession>>); | ||
|
|
||
| const res = await POST( | ||
| new NextRequest("http://localhost/api/stripe/portal-sessions", { | ||
| method: "POST", | ||
| body: "{}", | ||
| }), | ||
| ); | ||
| expect(res.status).toBe(400); | ||
| await expect(res.json()).resolves.toEqual({ error: "Portal session URL missing" }); | ||
| }); | ||
|
|
||
| it("returns 500 when createPortalSession throws", async () => { | ||
| vi.mocked(validateCreatePortalSessionRequest).mockResolvedValue({ | ||
| accountId: ACCOUNT, | ||
| returnUrl: "https://chat.recoupable.com/back", | ||
| }); | ||
| vi.mocked(getStripeCustomerIdByAccountId).mockResolvedValue("cus_123"); | ||
| vi.mocked(createPortalSession).mockRejectedValue(new Error("Stripe down")); | ||
|
|
||
| const res = await POST( | ||
| new NextRequest("http://localhost/api/stripe/portal-sessions", { | ||
| method: "POST", | ||
| body: "{}", | ||
| }), | ||
| ); | ||
| expect(res.status).toBe(500); | ||
| await expect(res.json()).resolves.toEqual({ error: "Internal server error" }); | ||
| }); | ||
| }); | ||
78 changes: 78 additions & 0 deletions
78
app/api/stripe/portal-sessions/__tests__/route.post.validation.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import "./routeTestMocks"; | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { validateCreatePortalSessionRequest } from "@/lib/stripe/validateCreatePortalSessionRequest"; | ||
| import { createPortalSession } from "@/lib/stripe/createPortalSession"; | ||
| import { validateAuthContext } from "@/lib/auth/validateAuthContext"; | ||
|
|
||
| const { POST } = await import("../route"); | ||
|
|
||
| async function loadRealValidate() { | ||
| const mod = await vi.importActual< | ||
| typeof import("@/lib/stripe/validateCreatePortalSessionRequest") | ||
| >("@/lib/stripe/validateCreatePortalSessionRequest"); | ||
| return mod.validateCreatePortalSessionRequest; | ||
| } | ||
|
|
||
| describe("POST /api/stripe/portal-sessions (validation)", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.mocked(validateCreatePortalSessionRequest).mockReset(); | ||
| vi.spyOn(console, "error").mockImplementation(() => undefined); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.mocked(console.error).mockRestore(); | ||
| }); | ||
|
|
||
| it("returns 400 when body is invalid JSON", async () => { | ||
| vi.mocked(validateCreatePortalSessionRequest).mockImplementationOnce(await loadRealValidate()); | ||
| const res = await POST( | ||
| new NextRequest("http://localhost/api/stripe/portal-sessions", { | ||
| method: "POST", | ||
| headers: { "content-type": "application/json" }, | ||
| body: "not-json", | ||
| }), | ||
| ); | ||
| expect(res.status).toBe(400); | ||
| await expect(res.json()).resolves.toEqual({ error: "Invalid JSON body" }); | ||
| expect(createPortalSession).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns 400 when returnUrl is missing", async () => { | ||
| vi.mocked(validateCreatePortalSessionRequest).mockImplementationOnce(await loadRealValidate()); | ||
| const res = await POST( | ||
| new NextRequest("http://localhost/api/stripe/portal-sessions", { | ||
| method: "POST", | ||
| headers: { "content-type": "application/json" }, | ||
| body: JSON.stringify({}), | ||
| }), | ||
| ); | ||
| expect(res.status).toBe(400); | ||
| const body = await res.json(); | ||
| expect(body).toEqual({ error: expect.stringMatching(/returnUrl|Invalid input/i) }); | ||
| expect(createPortalSession).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns 401 when not authenticated", async () => { | ||
| vi.mocked(validateAuthContext).mockResolvedValueOnce( | ||
| NextResponse.json( | ||
| { status: "error", error: "Exactly one of x-api-key or Authorization must be provided" }, | ||
| { status: 401 }, | ||
| ), | ||
| ); | ||
| vi.mocked(validateCreatePortalSessionRequest).mockImplementationOnce(await loadRealValidate()); | ||
| const res = await POST( | ||
| new NextRequest("http://localhost/api/stripe/portal-sessions", { | ||
| method: "POST", | ||
| headers: { "content-type": "application/json" }, | ||
| body: JSON.stringify({ returnUrl: "https://chat.recoupable.com/back" }), | ||
| }), | ||
| ); | ||
| expect(res.status).toBe(401); | ||
| await expect(res.json()).resolves.toEqual({ | ||
| error: "Exactly one of x-api-key or Authorization must be provided", | ||
| }); | ||
| expect(createPortalSession).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
app/api/stripe/portal-sessions/__tests__/routeTestMocks.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { vi } from "vitest"; | ||
|
|
||
| vi.mock("@/lib/auth/validateAuthContext", () => ({ | ||
| validateAuthContext: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/networking/getCorsHeaders", () => ({ | ||
| getCorsHeaders: vi.fn(() => ({ "Access-Control-Allow-Origin": "*" })), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/stripe/validateCreatePortalSessionRequest", () => ({ | ||
| validateCreatePortalSessionRequest: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/stripe/createPortalSession", () => ({ | ||
| createPortalSession: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/billing_customers/getStripeCustomerIdByAccountId", () => ({ | ||
| getStripeCustomerIdByAccountId: vi.fn(), | ||
| })); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { getCorsHeaders } from "@/lib/networking/getCorsHeaders"; | ||
| import { createPortalSessionHandler } from "@/lib/stripe/createPortalSessionHandler"; | ||
|
|
||
| /** | ||
| * OPTIONS handler for CORS preflight requests. | ||
| * | ||
| * @returns A NextResponse with CORS headers. | ||
| */ | ||
| export async function OPTIONS() { | ||
| return new NextResponse(null, { | ||
| status: 200, | ||
| headers: getCorsHeaders(), | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * POST /api/stripe/portal-sessions: creates a Stripe billing portal | ||
| * session for the authenticated account's existing Stripe customer. | ||
| * | ||
| * @param request - The incoming HTTP request. | ||
| * @returns A NextResponse with session `id` and `url`, or an error body. | ||
| */ | ||
| export async function POST(request: NextRequest) { | ||
| return createPortalSessionHandler(request); | ||
| } | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
| export const fetchCache = "force-no-store"; | ||
| export const revalidate = 0; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: Custom agent: Enforce Clear Code Style and Maintainability Practices
File exceeds the repository’s 100-line limit for maintainability.
Prompt for AI agents