|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | + |
| 3 | +import { getServerSession } from "@calcom/features/auth/lib/getServerSession"; |
| 4 | + |
| 5 | +import { POST } from "../route"; |
| 6 | + |
| 7 | +vi.mock("@calcom/features/auth/lib/getServerSession", () => ({ |
| 8 | + getServerSession: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("@calcom/lib/constants", async () => { |
| 12 | + const actual = (await vi.importActual("@calcom/lib/constants")) as typeof import("@calcom/lib/constants"); |
| 13 | + return { |
| 14 | + ...actual, |
| 15 | + IS_PRODUCTION: true, |
| 16 | + IS_PLAIN_CHAT_ENABLED: true, |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +vi.mock("@lib/buildLegacyCtx", () => ({ |
| 21 | + buildLegacyRequest: vi.fn(() => ({ headers: {}, cookies: {} })), |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock("next/headers", () => ({ |
| 25 | + headers: vi.fn(() => new Map()), |
| 26 | + cookies: vi.fn(() => ({ getAll: () => [] })), |
| 27 | +})); |
| 28 | + |
| 29 | +vi.mock("next/server", () => ({ |
| 30 | + NextResponse: { |
| 31 | + json: vi.fn((data, options) => ({ |
| 32 | + json: () => Promise.resolve(data), |
| 33 | + status: options?.status || 200, |
| 34 | + })), |
| 35 | + }, |
| 36 | +})); |
| 37 | + |
| 38 | +vi.mock("@lib/plain/plain", () => { |
| 39 | + const mockGetCustomerByEmail = vi.fn(); |
| 40 | + const mockCreateThread = vi.fn(); |
| 41 | + const mockUpsertPlainCustomer = vi.fn(); |
| 42 | + |
| 43 | + return { |
| 44 | + plain: { |
| 45 | + getCustomerByEmail: mockGetCustomerByEmail, |
| 46 | + createThread: mockCreateThread, |
| 47 | + }, |
| 48 | + upsertPlainCustomer: mockUpsertPlainCustomer, |
| 49 | + }; |
| 50 | +}); |
| 51 | + |
| 52 | +const mockGetServerSession = vi.mocked(getServerSession); |
| 53 | + |
| 54 | +describe("/api/support", () => { |
| 55 | + beforeEach(() => { |
| 56 | + vi.clearAllMocks(); |
| 57 | + process.env.PLAIN_API_KEY = "test-api-key"; |
| 58 | + }); |
| 59 | + |
| 60 | + it("should return 404 when Plain Chat is disabled", async () => { |
| 61 | + expect(true).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should return 401 when user is not authenticated", async () => { |
| 65 | + mockGetServerSession.mockResolvedValue(null); |
| 66 | + |
| 67 | + const request = new Request("http://localhost:3000/api/support", { |
| 68 | + method: "POST", |
| 69 | + body: JSON.stringify({ |
| 70 | + message: "Test message", |
| 71 | + attachmentIds: [], |
| 72 | + }), |
| 73 | + }); |
| 74 | + |
| 75 | + const response = await POST(request); |
| 76 | + expect(response.status).toBe(401); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should return 500 for invalid form data", async () => { |
| 80 | + mockGetServerSession.mockResolvedValue({ |
| 81 | + hasValidLicense: true, |
| 82 | + upId: "test-up-id", |
| 83 | + expires: "2025-12-31T23:59:59.999Z", |
| 84 | + user: { id: 123, email: "test@example.com" }, |
| 85 | + }); |
| 86 | + |
| 87 | + const request = new Request("http://localhost:3000/api/support", { |
| 88 | + method: "POST", |
| 89 | + body: JSON.stringify({ |
| 90 | + message: "", |
| 91 | + attachmentIds: [], |
| 92 | + }), |
| 93 | + }); |
| 94 | + |
| 95 | + const response = await POST(request); |
| 96 | + expect(response.status).toBe(500); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should return 500 when Plain API key is not configured", async () => { |
| 100 | + delete process.env.PLAIN_API_KEY; |
| 101 | + |
| 102 | + mockGetServerSession.mockResolvedValue({ |
| 103 | + hasValidLicense: true, |
| 104 | + upId: "test-up-id", |
| 105 | + expires: "2025-12-31T23:59:59.999Z", |
| 106 | + user: { id: 123, email: "test@example.com" }, |
| 107 | + }); |
| 108 | + |
| 109 | + const request = new Request("http://localhost:3000/api/support", { |
| 110 | + method: "POST", |
| 111 | + body: JSON.stringify({ |
| 112 | + message: "Test message", |
| 113 | + attachmentIds: [], |
| 114 | + }), |
| 115 | + }); |
| 116 | + |
| 117 | + const response = await POST(request); |
| 118 | + expect(response.status).toBe(500); |
| 119 | + }); |
| 120 | + |
| 121 | + it("should successfully create customer and thread", async () => { |
| 122 | + mockGetServerSession.mockResolvedValue({ |
| 123 | + hasValidLicense: true, |
| 124 | + upId: "test-up-id", |
| 125 | + expires: "2025-12-31T23:59:59.999Z", |
| 126 | + user: { id: 123, email: "test@example.com" }, |
| 127 | + }); |
| 128 | + |
| 129 | + const { plain } = vi.mocked(await import("@lib/plain/plain")); |
| 130 | + vi.mocked(plain.getCustomerByEmail).mockResolvedValue({ |
| 131 | + data: { |
| 132 | + id: "customer-123", |
| 133 | + __typename: "Customer", |
| 134 | + fullName: "Test User", |
| 135 | + shortName: "Test", |
| 136 | + externalId: "123", |
| 137 | + email: { |
| 138 | + email: "test@example.com", |
| 139 | + isVerified: true, |
| 140 | + verifiedAt: { |
| 141 | + __typename: "DateTime", |
| 142 | + iso8601: "2025-01-01T00:00:00Z", |
| 143 | + unixTimestamp: "1735689600", |
| 144 | + }, |
| 145 | + }, |
| 146 | + company: null, |
| 147 | + createdBy: { __typename: "SystemActor" as const, systemId: "system" }, |
| 148 | + updatedAt: { __typename: "DateTime", iso8601: "2025-01-01T00:00:00Z", unixTimestamp: "1735689600" }, |
| 149 | + createdAt: { __typename: "DateTime", iso8601: "2025-01-01T00:00:00Z", unixTimestamp: "1735689600" }, |
| 150 | + markedAsSpamAt: null, |
| 151 | + }, |
| 152 | + }); |
| 153 | + |
| 154 | + vi.mocked(plain.createThread).mockResolvedValue({ |
| 155 | + data: { |
| 156 | + id: "thread-123", |
| 157 | + __typename: "Thread", |
| 158 | + externalId: null, |
| 159 | + title: "Test message", |
| 160 | + description: null, |
| 161 | + status: "Todo" as any, |
| 162 | + statusChangedAt: { |
| 163 | + __typename: "DateTime", |
| 164 | + iso8601: "2025-01-01T00:00:00Z", |
| 165 | + unixTimestamp: "1735689600", |
| 166 | + }, |
| 167 | + statusDetail: null, |
| 168 | + customer: { id: "customer-123" }, |
| 169 | + priority: 0, |
| 170 | + previewText: "Test message", |
| 171 | + updatedAt: { __typename: "DateTime", iso8601: "2025-01-01T00:00:00Z", unixTimestamp: "1735689600" }, |
| 172 | + createdAt: { __typename: "DateTime", iso8601: "2025-01-01T00:00:00Z", unixTimestamp: "1735689600" }, |
| 173 | + } as any, |
| 174 | + }); |
| 175 | + |
| 176 | + const request = new Request("http://localhost:3000/api/support", { |
| 177 | + method: "POST", |
| 178 | + body: JSON.stringify({ |
| 179 | + message: "Test message", |
| 180 | + attachmentIds: [], |
| 181 | + }), |
| 182 | + }); |
| 183 | + |
| 184 | + const response = await POST(request); |
| 185 | + expect(response.status).toBe(200); |
| 186 | + |
| 187 | + const responseData = await response.json(); |
| 188 | + expect(responseData).toBeDefined(); |
| 189 | + |
| 190 | + expect(vi.mocked(plain.getCustomerByEmail)).toHaveBeenCalledWith({ email: "test@example.com" }); |
| 191 | + expect(vi.mocked(plain.createThread)).toHaveBeenCalled(); |
| 192 | + }); |
| 193 | + |
| 194 | + it("should handle form submission with file attachments", async () => { |
| 195 | + mockGetServerSession.mockResolvedValue({ |
| 196 | + hasValidLicense: true, |
| 197 | + upId: "test-up-id", |
| 198 | + expires: "2025-12-31T23:59:59.999Z", |
| 199 | + user: { id: 123, email: "test@example.com" }, |
| 200 | + }); |
| 201 | + |
| 202 | + const { plain, upsertPlainCustomer } = vi.mocked(await import("@lib/plain/plain")); |
| 203 | + vi.mocked(plain.getCustomerByEmail).mockResolvedValue({ |
| 204 | + data: null, |
| 205 | + }); |
| 206 | + |
| 207 | + vi.mocked(upsertPlainCustomer).mockResolvedValue({ |
| 208 | + data: { |
| 209 | + result: "Created" as any, |
| 210 | + customer: { |
| 211 | + id: "customer-456", |
| 212 | + __typename: "Customer", |
| 213 | + fullName: "Test User", |
| 214 | + shortName: "Test", |
| 215 | + externalId: "123", |
| 216 | + email: { |
| 217 | + email: "test@example.com", |
| 218 | + isVerified: true, |
| 219 | + verifiedAt: { |
| 220 | + __typename: "DateTime", |
| 221 | + iso8601: "2025-01-01T00:00:00Z", |
| 222 | + unixTimestamp: "1735689600", |
| 223 | + }, |
| 224 | + }, |
| 225 | + company: null, |
| 226 | + createdBy: { __typename: "SystemActor" as const, systemId: "system" }, |
| 227 | + updatedAt: { __typename: "DateTime", iso8601: "2025-01-01T00:00:00Z", unixTimestamp: "1735689600" }, |
| 228 | + createdAt: { __typename: "DateTime", iso8601: "2025-01-01T00:00:00Z", unixTimestamp: "1735689600" }, |
| 229 | + markedAsSpamAt: null, |
| 230 | + }, |
| 231 | + }, |
| 232 | + }); |
| 233 | + |
| 234 | + vi.mocked(plain.createThread).mockResolvedValue({ |
| 235 | + data: { |
| 236 | + id: "thread-456", |
| 237 | + __typename: "Thread", |
| 238 | + externalId: null, |
| 239 | + title: "Test message", |
| 240 | + description: null, |
| 241 | + status: "Todo" as any, |
| 242 | + statusChangedAt: { |
| 243 | + __typename: "DateTime", |
| 244 | + iso8601: "2025-01-01T00:00:00Z", |
| 245 | + unixTimestamp: "1735689600", |
| 246 | + }, |
| 247 | + statusDetail: null, |
| 248 | + customer: { id: "customer-456" }, |
| 249 | + priority: 0, |
| 250 | + previewText: "Test message", |
| 251 | + updatedAt: { __typename: "DateTime", iso8601: "2025-01-01T00:00:00Z", unixTimestamp: "1735689600" }, |
| 252 | + createdAt: { __typename: "DateTime", iso8601: "2025-01-01T00:00:00Z", unixTimestamp: "1735689600" }, |
| 253 | + } as any, |
| 254 | + }); |
| 255 | + |
| 256 | + const request = new Request("http://localhost:3000/api/support", { |
| 257 | + method: "POST", |
| 258 | + body: JSON.stringify({ |
| 259 | + message: "Test message", |
| 260 | + attachmentIds: ["attachment-123"], |
| 261 | + }), |
| 262 | + }); |
| 263 | + |
| 264 | + const response = await POST(request); |
| 265 | + expect(response.status).toBe(200); |
| 266 | + |
| 267 | + const responseData = await response.json(); |
| 268 | + expect(responseData).toBeDefined(); |
| 269 | + |
| 270 | + expect(vi.mocked(plain.getCustomerByEmail)).toHaveBeenCalledWith({ email: "test@example.com" }); |
| 271 | + expect(vi.mocked(upsertPlainCustomer)).toHaveBeenCalled(); |
| 272 | + expect(vi.mocked(plain.createThread)).toHaveBeenCalled(); |
| 273 | + }); |
| 274 | + |
| 275 | + it("should handle Plain customer creation error", async () => { |
| 276 | + mockGetServerSession.mockResolvedValue({ |
| 277 | + hasValidLicense: true, |
| 278 | + upId: "test-up-id", |
| 279 | + expires: "2025-12-31T23:59:59.999Z", |
| 280 | + user: { id: 123, email: "test@example.com" }, |
| 281 | + }); |
| 282 | + |
| 283 | + const { plain, upsertPlainCustomer } = vi.mocked(await import("@lib/plain/plain")); |
| 284 | + vi.mocked(plain.getCustomerByEmail).mockResolvedValue({ |
| 285 | + data: null, |
| 286 | + }); |
| 287 | + |
| 288 | + vi.mocked(upsertPlainCustomer).mockResolvedValue({ |
| 289 | + error: { |
| 290 | + type: "unknown", |
| 291 | + message: "Customer creation failed", |
| 292 | + }, |
| 293 | + }); |
| 294 | + |
| 295 | + const request = new Request("http://localhost:3000/api/support", { |
| 296 | + method: "POST", |
| 297 | + body: JSON.stringify({ |
| 298 | + message: "Test message", |
| 299 | + attachmentIds: [], |
| 300 | + }), |
| 301 | + }); |
| 302 | + |
| 303 | + const response = await POST(request); |
| 304 | + expect(response.status).toBe(500); |
| 305 | + }); |
| 306 | + |
| 307 | + it("should handle Plain thread creation error", async () => { |
| 308 | + mockGetServerSession.mockResolvedValue({ |
| 309 | + hasValidLicense: true, |
| 310 | + upId: "test-up-id", |
| 311 | + expires: "2025-12-31T23:59:59.999Z", |
| 312 | + user: { id: 123, email: "test@example.com" }, |
| 313 | + }); |
| 314 | + |
| 315 | + const { plain } = vi.mocked(await import("@lib/plain/plain")); |
| 316 | + vi.mocked(plain.getCustomerByEmail).mockResolvedValue({ |
| 317 | + data: { |
| 318 | + id: "customer-123", |
| 319 | + __typename: "Customer", |
| 320 | + fullName: "Test User", |
| 321 | + shortName: "Test", |
| 322 | + externalId: "123", |
| 323 | + email: { |
| 324 | + email: "test@example.com", |
| 325 | + isVerified: true, |
| 326 | + verifiedAt: { |
| 327 | + __typename: "DateTime", |
| 328 | + iso8601: "2025-01-01T00:00:00Z", |
| 329 | + unixTimestamp: "1735689600", |
| 330 | + }, |
| 331 | + }, |
| 332 | + company: null, |
| 333 | + createdBy: { __typename: "SystemActor" as const, systemId: "system" }, |
| 334 | + updatedAt: { __typename: "DateTime", iso8601: "2025-01-01T00:00:00Z", unixTimestamp: "1735689600" }, |
| 335 | + createdAt: { __typename: "DateTime", iso8601: "2025-01-01T00:00:00Z", unixTimestamp: "1735689600" }, |
| 336 | + markedAsSpamAt: null, |
| 337 | + }, |
| 338 | + }); |
| 339 | + |
| 340 | + vi.mocked(plain.createThread).mockResolvedValue({ |
| 341 | + error: { |
| 342 | + type: "unknown", |
| 343 | + message: "Thread creation failed", |
| 344 | + }, |
| 345 | + }); |
| 346 | + |
| 347 | + const request = new Request("http://localhost:3000/api/support", { |
| 348 | + method: "POST", |
| 349 | + body: JSON.stringify({ |
| 350 | + message: "Test message", |
| 351 | + attachmentIds: [], |
| 352 | + }), |
| 353 | + }); |
| 354 | + |
| 355 | + const response = await POST(request); |
| 356 | + expect(response.status).toBe(500); |
| 357 | + }); |
| 358 | +}); |
0 commit comments