|
| 1 | +import type { NextApiRequest, NextApiResponse } from "next"; |
| 2 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 3 | + |
| 4 | +import sendVerificationRequest from "@calcom/features/auth/lib/sendVerificationRequest"; |
| 5 | +import { HttpError } from "@calcom/lib/http-error"; |
| 6 | +import { VerificationTokenService } from "@calcom/lib/server/service/VerificationTokenService"; |
| 7 | +import { prisma } from "@calcom/prisma"; |
| 8 | + |
| 9 | +import { getCustomerAndCheckoutSession } from "../../lib/getCustomerAndCheckoutSession"; |
| 10 | + |
| 11 | +// Mock dependencies |
| 12 | +vi.mock("@calcom/prisma", () => ({ |
| 13 | + prisma: { |
| 14 | + user: { |
| 15 | + findFirst: vi.fn(), |
| 16 | + update: vi.fn(), |
| 17 | + }, |
| 18 | + }, |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock("../../lib/getCustomerAndCheckoutSession"); |
| 22 | +vi.mock("@calcom/features/auth/lib/sendVerificationRequest"); |
| 23 | +vi.mock("@calcom/lib/server/service/VerificationTokenService"); |
| 24 | + |
| 25 | +const mockGetCustomerAndCheckoutSession = vi.mocked(getCustomerAndCheckoutSession); |
| 26 | +const mockSendVerificationRequest = vi.mocked(sendVerificationRequest); |
| 27 | +const mockVerificationTokenService = vi.mocked(VerificationTokenService); |
| 28 | + |
| 29 | +// Type the mocked prisma properly |
| 30 | +const mockPrisma = prisma as unknown as { |
| 31 | + user: { |
| 32 | + findFirst: ReturnType<typeof vi.fn>; |
| 33 | + update: ReturnType<typeof vi.fn>; |
| 34 | + }; |
| 35 | +}; |
| 36 | + |
| 37 | +describe("paymentCallback", () => { |
| 38 | + let mockReq: Partial<NextApiRequest>; |
| 39 | + let mockRes: Partial<NextApiResponse>; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + vi.clearAllMocks(); |
| 43 | + |
| 44 | + mockReq = { |
| 45 | + query: { |
| 46 | + callbackUrl: "/premium-username-checkout", |
| 47 | + checkoutSessionId: "cs_test_123", |
| 48 | + }, |
| 49 | + url: "/api/payment/callback", |
| 50 | + method: "GET", |
| 51 | + }; |
| 52 | + |
| 53 | + mockRes = { |
| 54 | + redirect: vi.fn().mockReturnThis(), |
| 55 | + end: vi.fn().mockReturnThis(), |
| 56 | + setHeader: vi.fn().mockReturnThis(), |
| 57 | + status: vi.fn().mockReturnThis(), |
| 58 | + json: vi.fn().mockReturnThis(), |
| 59 | + }; |
| 60 | + |
| 61 | + // Default mock implementations |
| 62 | + mockGetCustomerAndCheckoutSession.mockResolvedValue({ |
| 63 | + stripeCustomer: { |
| 64 | + id: "cus_123", |
| 65 | + email: "test@example.com", |
| 66 | + metadata: { |
| 67 | + username: "premium-user", |
| 68 | + }, |
| 69 | + }, |
| 70 | + checkoutSession: { |
| 71 | + payment_status: "paid", |
| 72 | + }, |
| 73 | + } as any); // eslint-disable-line @typescript-eslint/no-explicit-any |
| 74 | + |
| 75 | + mockPrisma.user.findFirst.mockResolvedValue({ |
| 76 | + id: 1, |
| 77 | + email: "test@example.com", |
| 78 | + locale: "en", |
| 79 | + metadata: {}, |
| 80 | + } as any); // eslint-disable-line @typescript-eslint/no-explicit-any |
| 81 | + |
| 82 | + mockPrisma.user.update.mockResolvedValue({ |
| 83 | + id: 1, |
| 84 | + username: "premium-user", |
| 85 | + } as any); // eslint-disable-line @typescript-eslint/no-explicit-any |
| 86 | + |
| 87 | + mockVerificationTokenService.create.mockResolvedValue("test-token-123"); |
| 88 | + mockSendVerificationRequest.mockResolvedValue(undefined); |
| 89 | + }); |
| 90 | + |
| 91 | + describe("VerificationTokenService integration", () => { |
| 92 | + it("should call VerificationTokenService.create with correct parameters", async () => { |
| 93 | + const { default: handler } = await import("../paymentCallback"); |
| 94 | + |
| 95 | + await handler(mockReq as NextApiRequest, mockRes as NextApiResponse); |
| 96 | + |
| 97 | + expect(mockVerificationTokenService.create).toHaveBeenCalledWith({ |
| 98 | + identifier: "test@example.com", |
| 99 | + expires: expect.any(Date), |
| 100 | + }); |
| 101 | + |
| 102 | + const callArgs = mockVerificationTokenService.create.mock.calls[0][0]; |
| 103 | + const expiresDate = callArgs.expires; |
| 104 | + const now = Date.now(); |
| 105 | + const oneDayInMs = 86400 * 1000; |
| 106 | + |
| 107 | + // Verify expires is approximately 1 day from now (within 1 second tolerance) |
| 108 | + expect(expiresDate.getTime()).toBeGreaterThan(now); |
| 109 | + expect(expiresDate.getTime()).toBeLessThanOrEqual(now + oneDayInMs + 1000); |
| 110 | + }); |
| 111 | + |
| 112 | + it("should send verification email with token from service", async () => { |
| 113 | + const { default: handler } = await import("../paymentCallback"); |
| 114 | + |
| 115 | + await handler(mockReq as NextApiRequest, mockRes as NextApiResponse); |
| 116 | + |
| 117 | + expect(mockSendVerificationRequest).toHaveBeenCalledWith({ |
| 118 | + identifier: "test@example.com", |
| 119 | + url: expect.stringContaining("token=test-token-123"), |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + it("should create verification token before sending email", async () => { |
| 124 | + const { default: handler } = await import("../paymentCallback"); |
| 125 | + const callOrder: string[] = []; |
| 126 | + |
| 127 | + mockVerificationTokenService.create.mockImplementation(async () => { |
| 128 | + callOrder.push("create-token"); |
| 129 | + return "test-token"; |
| 130 | + }); |
| 131 | + |
| 132 | + mockSendVerificationRequest.mockImplementation(async () => { |
| 133 | + callOrder.push("send-email"); |
| 134 | + }); |
| 135 | + |
| 136 | + await handler(mockReq as NextApiRequest, mockRes as NextApiResponse); |
| 137 | + |
| 138 | + expect(callOrder).toEqual(["create-token", "send-email"]); |
| 139 | + }); |
| 140 | + |
| 141 | + it("should create verification token only after payment is confirmed", async () => { |
| 142 | + mockGetCustomerAndCheckoutSession.mockResolvedValue({ |
| 143 | + stripeCustomer: { |
| 144 | + id: "cus_123", |
| 145 | + email: "test@example.com", |
| 146 | + metadata: { username: "premium-user" }, |
| 147 | + }, |
| 148 | + checkoutSession: { |
| 149 | + payment_status: "unpaid", |
| 150 | + }, |
| 151 | + } as any); // eslint-disable-line @typescript-eslint/no-explicit-any |
| 152 | + |
| 153 | + const { default: handler } = await import("../paymentCallback"); |
| 154 | + |
| 155 | + await handler(mockReq as NextApiRequest, mockRes as NextApiResponse); |
| 156 | + |
| 157 | + expect(mockVerificationTokenService.create).not.toHaveBeenCalled(); |
| 158 | + expect(mockSendVerificationRequest).not.toHaveBeenCalled(); |
| 159 | + }); |
| 160 | + |
| 161 | + it("should handle user found by stripeCustomerId", async () => { |
| 162 | + mockPrisma.user.findFirst |
| 163 | + .mockResolvedValueOnce(null) // First call by email returns null |
| 164 | + .mockResolvedValueOnce({ |
| 165 | + // Second call by stripeCustomerId succeeds |
| 166 | + id: 2, |
| 167 | + email: "different@example.com", |
| 168 | + locale: "en", |
| 169 | + metadata: { stripeCustomerId: "cus_123" }, |
| 170 | + } as any); // eslint-disable-line @typescript-eslint/no-explicit-any |
| 171 | + |
| 172 | + const { default: handler } = await import("../paymentCallback"); |
| 173 | + |
| 174 | + await handler(mockReq as NextApiRequest, mockRes as NextApiResponse); |
| 175 | + |
| 176 | + expect(mockVerificationTokenService.create).toHaveBeenCalledWith({ |
| 177 | + identifier: "different@example.com", // Should use user.email from found user |
| 178 | + expires: expect.any(Date), |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + it("should update user with premium username before creating token", async () => { |
| 183 | + const { default: handler } = await import("../paymentCallback"); |
| 184 | + const callOrder: string[] = []; |
| 185 | + |
| 186 | + mockPrisma.user.update.mockImplementation(async () => { |
| 187 | + callOrder.push("update-user"); |
| 188 | + return {} as any; // eslint-disable-line @typescript-eslint/no-explicit-any |
| 189 | + }); |
| 190 | + |
| 191 | + mockVerificationTokenService.create.mockImplementation(async () => { |
| 192 | + callOrder.push("create-token"); |
| 193 | + return "token"; |
| 194 | + }); |
| 195 | + |
| 196 | + await handler(mockReq as NextApiRequest, mockRes as NextApiResponse); |
| 197 | + |
| 198 | + expect(callOrder).toEqual(["update-user", "create-token"]); |
| 199 | + }); |
| 200 | + |
| 201 | + it("should redirect with correct parameters after successful payment", async () => { |
| 202 | + const { default: handler } = await import("../paymentCallback"); |
| 203 | + |
| 204 | + await handler(mockReq as NextApiRequest, mockRes as NextApiResponse); |
| 205 | + |
| 206 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 207 | + const redirectUrl = (mockRes.redirect as any).mock.calls[0][0] as string; |
| 208 | + expect(redirectUrl).toContain("email=test%40example.com"); |
| 209 | + expect(redirectUrl).toContain("username=premium-user"); |
| 210 | + expect(redirectUrl).toContain("paymentStatus=paid"); |
| 211 | + }); |
| 212 | + |
| 213 | + it("should not create verification token when stripe customer is not found", async () => { |
| 214 | + mockGetCustomerAndCheckoutSession.mockResolvedValue({ |
| 215 | + stripeCustomer: null, |
| 216 | + checkoutSession: { payment_status: "paid" }, |
| 217 | + } as any); // eslint-disable-line @typescript-eslint/no-explicit-any |
| 218 | + |
| 219 | + const { default: handler } = await import("../paymentCallback"); |
| 220 | + |
| 221 | + try { |
| 222 | + await handler(mockReq as NextApiRequest, mockRes as NextApiResponse); |
| 223 | + } catch (error) { |
| 224 | + expect(error).toBeInstanceOf(HttpError); |
| 225 | + expect((error as HttpError).statusCode).toBe(404); |
| 226 | + } |
| 227 | + |
| 228 | + expect(mockVerificationTokenService.create).not.toHaveBeenCalled(); |
| 229 | + }); |
| 230 | + |
| 231 | + it("should not create verification token when user is not found", async () => { |
| 232 | + mockPrisma.user.findFirst.mockResolvedValue(null); |
| 233 | + |
| 234 | + const { default: handler } = await import("../paymentCallback"); |
| 235 | + |
| 236 | + try { |
| 237 | + await handler(mockReq as NextApiRequest, mockRes as NextApiResponse); |
| 238 | + } catch (error) { |
| 239 | + expect(error).toBeInstanceOf(HttpError); |
| 240 | + expect((error as HttpError).statusCode).toBe(404); |
| 241 | + } |
| 242 | + |
| 243 | + expect(mockVerificationTokenService.create).not.toHaveBeenCalled(); |
| 244 | + }); |
| 245 | + |
| 246 | + it("should use user email if stripe customer email is missing", async () => { |
| 247 | + mockGetCustomerAndCheckoutSession.mockResolvedValue({ |
| 248 | + stripeCustomer: { |
| 249 | + id: "cus_123", |
| 250 | + email: null, |
| 251 | + metadata: { username: "premium-user" }, |
| 252 | + }, |
| 253 | + checkoutSession: { payment_status: "paid" }, |
| 254 | + } as any); // eslint-disable-line @typescript-eslint/no-explicit-any |
| 255 | + |
| 256 | + const { default: handler } = await import("../paymentCallback"); |
| 257 | + |
| 258 | + await handler(mockReq as NextApiRequest, mockRes as NextApiResponse); |
| 259 | + |
| 260 | + expect(mockVerificationTokenService.create).toHaveBeenCalledWith({ |
| 261 | + identifier: "test@example.com", // Should use user.email |
| 262 | + expires: expect.any(Date), |
| 263 | + }); |
| 264 | + }); |
| 265 | + }); |
| 266 | +}); |
0 commit comments