Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | import { AuthConfigs } from '../../src/models/AuthConfigs'; |
| 3 | import ComposioClient from '@composio/client'; |
| 4 | import { ValidationError } from '../../src/errors/ValidationErrors'; |
| 5 | import { |
| 6 | AuthConfigRetrieveResponse as ComposioAuthConfigRetrieveResponse, |
| 7 | AuthConfigDeleteResponse, |
| 8 | AuthConfigUpdateResponse, |
| 9 | AuthConfigUpdateStatusResponse, |
| 10 | } from '@composio/client/resources/auth-configs'; |
| 11 | import { transformAuthConfigRetrieveResponse } from '../../src/utils/transformers/authConfigs'; |
| 12 | |
| 13 | // Mock data for testing |
| 14 | const mockComposioAuthConfigResponse: ComposioAuthConfigRetrieveResponse = { |
| 15 | id: 'auth_12345', |
| 16 | name: 'Test Auth Config', |
| 17 | no_of_connections: 5, |
| 18 | status: 'ENABLED', |
| 19 | type: 'custom', |
| 20 | toolkit: { |
| 21 | logo: 'https://example.com/logo.png', |
| 22 | slug: 'github', |
| 23 | }, |
| 24 | auth_scheme: 'OAUTH2', |
| 25 | credentials: { |
| 26 | client_id: 'test_client_id', |
| 27 | client_secret: 'test_client_secret', |
| 28 | }, |
| 29 | expected_input_fields: [ |
| 30 | { name: 'client_id', type: 'string' }, |
| 31 | { name: 'client_secret', type: 'string' }, |
| 32 | ], |
| 33 | is_composio_managed: true, |
| 34 | managed_app_id: 'ma_default0001', |
| 35 | created_by: 'user_123', |
| 36 | created_at: '2023-01-01T00:00:00Z', |
| 37 | last_updated_at: '2023-01-01T00:00:00Z', |
| 38 | tool_access_config: { |
| 39 | tools_for_connected_account_creation: undefined, |
| 40 | }, |
| 41 | }; |
| 42 | |
| 43 | const mockTransformedAuthConfigResponse = { |
| 44 | id: 'auth_12345', |
| 45 | name: 'Test Auth Config', |
| 46 | noOfConnections: 5, |
| 47 | status: 'ENABLED', |
| 48 | toolkit: { |
| 49 | logo: 'https://example.com/logo.png', |
| 50 | slug: 'github', |
| 51 | }, |
| 52 | authScheme: 'OAUTH2', |
| 53 | credentials: { |
| 54 | client_id: 'test_client_id', |
| 55 | client_secret: 'test_client_secret', |
| 56 | }, |
| 57 | expectedInputFields: [ |
| 58 | { name: 'client_id', type: 'string' }, |
| 59 | { name: 'client_secret', type: 'string' }, |
| 60 | ], |
| 61 | isComposioManaged: true, |
| 62 | createdBy: 'user_123', |
| 63 | createdAt: '2023-01-01T00:00:00Z', |
| 64 | lastUpdatedAt: '2023-01-01T00:00:00Z', |
| 65 | restrictToFollowingTools: undefined, |
| 66 | toolAccessConfig: { |
| 67 | toolsAvailableForExecution: undefined, |
| 68 | toolsForConnectedAccountCreation: undefined, |
| 69 | }, |
| 70 | managedAppId: 'ma_default0001', |
| 71 | }; |
| 72 | |
| 73 | // Extended mock client with all AuthConfigs methods |
| 74 | const mockClient = { |
| 75 | authConfigs: { |
| 76 | list: vi.fn(), |
| 77 | create: vi.fn(), |
| 78 | retrieve: vi.fn(), |
| 79 | update: vi.fn(), |
| 80 | delete: vi.fn(), |
| 81 | updateStatus: vi.fn(), |
| 82 | }, |
| 83 | }; |
| 84 | |
| 85 | describe('AuthConfigs', () => { |
| 86 | let authConfigs: AuthConfigs; |
| 87 | |
| 88 | beforeEach(() => { |
| 89 | vi.clearAllMocks(); |
| 90 | authConfigs = new AuthConfigs(mockClient as unknown as ComposioClient); |
| 91 | }); |
| 92 | |
| 93 | describe('parseAuthConfigRetrieveResponse', () => { |
| 94 | it('should transform API response to SDK format correctly', () => { |
| 95 | const result = transformAuthConfigRetrieveResponse(mockComposioAuthConfigResponse); |
| 96 | expect(result).toEqual(mockTransformedAuthConfigResponse); |
| 97 | }); |
| 98 | |
| 99 | // We have relaxed the validation for the response data, so we don't expect a ValidationError to be thrown |
| 100 | // it('should throw ValidationError for invalid response data', () => { |
| 101 | // const invalidResponse = { |
| 102 | // ...mockComposioAuthConfigResponse, |
| 103 | // id: null, // Invalid: id should be string |
| 104 | // } as unknown as ComposioAuthConfigRetrieveResponse; |
| 105 | |
| 106 | // expect(() => { |
| 107 | // // @ts-expect-error - Accessing private method for testing |
| 108 | // authConfigs.parseAuthConfigRetrieveResponse(invalidResponse); |
| 109 | // }).toThrow(ValidationError); |
| 110 | // }); |
| 111 | |
| 112 | it('keeps a null managed_app_id for auth configs not on a managed app', () => { |
| 113 | expect( |
| 114 | transformAuthConfigRetrieveResponse({ |
| 115 | ...mockComposioAuthConfigResponse, |
| 116 | is_composio_managed: false, |
| 117 | managed_app_id: null, |
| 118 | }).managedAppId |
| 119 | ).toBeNull(); |
| 120 | }); |
| 121 | |
| 122 | it('should handle optional fields correctly', () => { |
| 123 | const responseWithOptionalFields = { |
| 124 | id: 'auth_12345', |
| 125 | name: 'Test Auth Config', |
| 126 | no_of_connections: 5, |
| 127 | status: 'ENABLED', |
| 128 | type: 'default', |
| 129 | toolkit: { |
| 130 | logo: 'https://example.com/logo.png', |
| 131 | slug: 'github', |
| 132 | }, |
| 133 | // Optional fields omitted |
| 134 | } as ComposioAuthConfigRetrieveResponse; |
| 135 | |
| 136 | const result = transformAuthConfigRetrieveResponse(responseWithOptionalFields); |
| 137 | expect(result).toMatchObject({ |
| 138 | id: 'auth_12345', |
| 139 | name: 'Test Auth Config', |
| 140 | noOfConnections: 5, |
| 141 | status: 'ENABLED', |
| 142 | toolkit: { |
| 143 | logo: 'https://example.com/logo.png', |
| 144 | slug: 'github', |
| 145 | }, |
| 146 | }); |
| 147 | }); |
| 148 | }); |
| 149 | |
| 150 | describe('list', () => { |
| 151 | const mockListResponse = { |
| 152 | items: [mockComposioAuthConfigResponse], |
| 153 | next_cursor: 'next_cursor_123', |
| 154 | total_pages: 1, |
| 155 | }; |
| 156 | |
| 157 | it('should list auth configs without query parameters', async () => { |
| 158 | mockClient.authConfigs.list.mockResolvedValueOnce(mockListResponse); |
| 159 | |
| 160 | const result = await authConfigs.list(); |
| 161 | |
| 162 | expect(mockClient.authConfigs.list).toHaveBeenCalledWith( |
| 163 | { |
| 164 | cursor: undefined, |
| 165 | is_composio_managed: undefined, |
| 166 | limit: undefined, |
| 167 | search: undefined, |
| 168 | show_disabled: undefined, |
| 169 | toolkit_slug: undefined, |
| 170 | }, |
| 171 | undefined |
| 172 | ); |
| 173 | |
| 174 | expect(result).toEqual({ |
| 175 | items: [mockTransformedAuthConfigResponse], |
| 176 | nextCursor: 'next_cursor_123', |
| 177 | totalPages: 1, |
| 178 | }); |
| 179 | }); |
| 180 | |
| 181 | it('should list auth configs with query parameters', async () => { |
| 182 | mockClient.authConfigs.list.mockResolvedValueOnce(mockListResponse); |
| 183 | |
| 184 | const query = { |
| 185 | cursor: 'cursor_123', |
| 186 | isComposioManaged: true, |
| 187 | limit: 10, |
| 188 | search: 'github config', |
| 189 | showDisabled: true, |
| 190 | toolkit: 'github', |
| 191 | }; |
| 192 | |
| 193 | const result = await authConfigs.list(query); |
| 194 | |
| 195 | expect(mockClient.authConfigs.list).toHaveBeenCalledWith( |
| 196 | { |
| 197 | cursor: 'cursor_123', |
| 198 | is_composio_managed: true, |
| 199 | limit: 10, |
| 200 | search: 'github config', |
| 201 | show_disabled: true, |
| 202 | toolkit_slug: 'github', |
| 203 | }, |
| 204 | undefined |
| 205 | ); |
| 206 | |
| 207 | expect(result).toEqual({ |
| 208 | items: [mockTransformedAuthConfigResponse], |
| 209 | nextCursor: 'next_cursor_123', |
| 210 | totalPages: 1, |
| 211 | }); |
| 212 | }); |
| 213 | |
| 214 | it('should throw ValidationError for invalid query parameters', async () => { |
| 215 | const invalidQuery = { |
| 216 | limit: 'invalid_limit', // Should be number |
| 217 | }; |
| 218 | |
| 219 | await expect( |
| 220 | // @ts-expect-error - Testing invalid input |
| 221 | authConfigs.list(invalidQuery) |
| 222 | ).rejects.toThrow(); // Just expect any error since Zod throws ZodError directly |
| 223 | }); |
| 224 | |
| 225 | it('should handle empty list response', async () => { |
| 226 | const emptyResponse = { |
| 227 | items: [], |
| 228 | next_cursor: null, // Changed back to null as schema expects nullable string |
| 229 | total_pages: 0, |
| 230 | }; |
| 231 | |
| 232 | mockClient.authConfigs.list.mockResolvedValueOnce(emptyResponse); |
| 233 | |
| 234 | const result = await authConfigs.list(); |
| 235 | |
| 236 | expect(result).toEqual({ |
| 237 | items: [], |
| 238 | nextCursor: null, |
| 239 | totalPages: 0, |
| 240 | }); |
| 241 | }); |
| 242 | }); |
| 243 | |
| 244 | describe('create', () => { |
| 245 | const mockCreateResponse = { |
| 246 | auth_config: { |
| 247 | id: 'auth_12345', |
| 248 | auth_scheme: 'OAUTH2', |
| 249 | is_composio_managed: true, |
| 250 | managed_app_id: 'ma_default0001', |
| 251 | }, |
| 252 | toolkit: { |
| 253 | slug: 'github', |
| 254 | }, |
| 255 | }; |
| 256 | |
| 257 | it('should create auth config with default Composio managed type', async () => { |
| 258 | mockClient.authConfigs.create.mockResolvedValueOnce(mockCreateResponse); |
| 259 | |
| 260 | const result = await authConfigs.create('github'); |
| 261 | |
| 262 | expect(mockClient.authConfigs.create).toHaveBeenCalledWith( |
| 263 | { |
| 264 | toolkit: { |
| 265 | slug: 'github', |
| 266 | }, |
| 267 | auth_config: { |
| 268 | type: 'use_composio_managed_auth', |
| 269 | credentials: undefined, |
| 270 | name: undefined, |
| 271 | tool_access_config: undefined, |
| 272 | }, |
| 273 | }, |
| 274 | undefined |
| 275 | ); |
| 276 | |
| 277 | expect(result).toEqual({ |
| 278 | id: 'auth_12345', |
| 279 | authScheme: 'OAUTH2', |
| 280 | isComposioManaged: true, |
| 281 | toolkit: 'github', |
| 282 | managedAppId: 'ma_default0001', |
| 283 | }); |
| 284 | }); |
| 285 | |
| 286 | it('should create custom auth config with credentials', async () => { |
| 287 | mockClient.authConfigs.create.mockResolvedValueOnce({ |
| 288 | ...mockCreateResponse, |
| 289 | auth_config: { |
| 290 | ...mockCreateResponse.auth_config, |
| 291 | is_composio_managed: false, |
| 292 | managed_app_id: null, |
| 293 | }, |
| 294 | }); |
| 295 | |
| 296 | const options = { |
| 297 | type: 'use_custom_auth' as const, |
| 298 | name: 'Custom GitHub Auth', |
| 299 | tool_access_config: { |
| 300 | tools_for_connected_account_creation: undefined, |
| 301 | }, |
| 302 | authScheme: 'OAUTH2' as const, |
| 303 | credentials: { |
| 304 | client_id: 'test_client_id', |
| 305 | client_secret: 'test_client_secret', |
| 306 | }, |
| 307 | }; |
| 308 | |
| 309 | const result = await authConfigs.create('github', options); |
| 310 | |
| 311 | expect(mockClient.authConfigs.create).toHaveBeenCalledWith( |
| 312 | { |
| 313 | toolkit: { |
| 314 | slug: 'github', |
| 315 | }, |
| 316 | auth_config: { |
| 317 | type: 'use_custom_auth', |
| 318 | name: 'Custom GitHub Auth', |
| 319 | authScheme: 'OAUTH2', |
| 320 | credentials: { |
| 321 | client_id: 'test_client_id', |
| 322 | client_secret: 'test_client_secret', |
| 323 | }, |
| 324 | proxy_config: undefined, |
| 325 | tool_access_config: undefined, |
| 326 | }, |
| 327 | }, |
| 328 | undefined |
| 329 | ); |
| 330 | |
| 331 | expect(result).toEqual({ |
| 332 | id: 'auth_12345', |
| 333 | authScheme: 'OAUTH2', |
| 334 | isComposioManaged: false, |
| 335 | toolkit: 'github', |
| 336 | managedAppId: null, |
| 337 | }); |
| 338 | }); |
| 339 | |
| 340 | it('should create Composio managed auth config with optional name', async () => { |
| 341 | mockClient.authConfigs.create.mockResolvedValueOnce(mockCreateResponse); |
| 342 | |
| 343 | const options = { |
| 344 | type: 'use_composio_managed_auth' as const, |
| 345 | name: 'My GitHub Config', |
| 346 | tool_access_config: { |
| 347 | tools_for_connected_account_creation: undefined, |
| 348 | }, |
| 349 | credentials: { |
| 350 | custom_field: 'value', |
| 351 | }, |
| 352 | }; |
| 353 | |
| 354 | const result = await authConfigs.create('github', options); |
| 355 | |
| 356 | expect(mockClient.authConfigs.create).toHaveBeenCalledWith( |
| 357 | { |
| 358 | toolkit: { |
| 359 | slug: 'github', |
| 360 | }, |
| 361 | auth_config: { |
| 362 | type: 'use_composio_managed_auth', |
| 363 | credentials: { |
| 364 | custom_field: 'value', |
| 365 | }, |
| 366 | name: 'My GitHub Config', |
| 367 | tool_access_config: undefined, |
| 368 | }, |
| 369 | }, |
| 370 | undefined |
| 371 | ); |
| 372 | |
| 373 | expect(result).toEqual({ |
| 374 | id: 'auth_12345', |
| 375 | authScheme: 'OAUTH2', |
| 376 | isComposioManaged: true, |
| 377 | toolkit: 'github', |
| 378 | managedAppId: 'ma_default0001', |
| 379 | }); |
| 380 | }); |
| 381 | |
| 382 | it('sends managedAppId as managed_app_id and returns the app the config uses', async () => { |
| 383 | mockClient.authConfigs.create.mockResolvedValueOnce({ |
| 384 | ...mockCreateResponse, |
| 385 | auth_config: { ...mockCreateResponse.auth_config, managed_app_id: 'ma_second00001' }, |
| 386 | }); |
| 387 | |
| 388 | const result = await authConfigs.create('slack', { |
| 389 | type: 'use_composio_managed_auth', |
| 390 | managedAppId: 'ma_second00001', |
| 391 | }); |
| 392 | |
| 393 | expect(mockClient.authConfigs.create.mock.calls[0][0].auth_config.managed_app_id).toBe( |
| 394 | 'ma_second00001' |
| 395 | ); |
| 396 | expect(result.managedAppId).toBe('ma_second00001'); |
| 397 | }); |
| 398 | |
| 399 | it('omits managed_app_id when managedAppId is not given', async () => { |
| 400 | mockClient.authConfigs.create.mockResolvedValueOnce(mockCreateResponse); |
| 401 | |
| 402 | await authConfigs.create('slack', { type: 'use_composio_managed_auth' }); |
| 403 | |
| 404 | expect(mockClient.authConfigs.create.mock.calls[0][0].auth_config.managed_app_id).toBe( |
| 405 | undefined |
| 406 | ); |
| 407 | }); |
| 408 | |
| 409 | it('should throw ValidationError for invalid create options', async () => { |
| 410 | const invalidOptions = { |
| 411 | type: 'use_custom_auth' as const, |
| 412 | // Missing required authScheme for custom auth |
| 413 | credentials: { |
| 414 | client_id: 'test', |
| 415 | }, |
| 416 | }; |
| 417 | |
| 418 | await expect( |
| 419 | // @ts-expect-error - Testing invalid input |
| 420 | authConfigs.create('github', invalidOptions) |
| 421 | ).rejects.toThrow(ValidationError); |
| 422 | }); |
| 423 | }); |
| 424 | |
| 425 | describe('get', () => { |
| 426 | it('should retrieve auth config by nanoid', async () => { |
| 427 | mockClient.authConfigs.retrieve.mockResolvedValueOnce(mockComposioAuthConfigResponse); |
| 428 | |
| 429 | const result = await authConfigs.get('auth_12345'); |
| 430 | |
| 431 | expect(mockClient.authConfigs.retrieve).toHaveBeenCalledWith('auth_12345', undefined); |
| 432 | expect(result).toEqual(mockTransformedAuthConfigResponse); |
| 433 | }); |
| 434 | |
| 435 | it('should handle API errors when retrieving auth config', async () => { |
| 436 | const apiError = new Error('Auth config not found'); |
| 437 | mockClient.authConfigs.retrieve.mockRejectedValueOnce(apiError); |
| 438 | |
| 439 | await expect(authConfigs.get('nonexistent_auth')).rejects.toThrow('Auth config not found'); |
| 440 | }); |
| 441 | |
| 442 | it('should retrieve auth config with isEnabledForToolRouter set to true', async () => { |
| 443 | const responseWithToolRouter = { |
| 444 | ...mockComposioAuthConfigResponse, |
| 445 | is_enabled_for_tool_router: true, |
| 446 | }; |
| 447 | mockClient.authConfigs.retrieve.mockResolvedValueOnce(responseWithToolRouter); |
| 448 | |
| 449 | const result = await authConfigs.get('auth_12345'); |
| 450 | |
| 451 | expect(result.isEnabledForToolRouter).toBe(true); |
| 452 | }); |
| 453 | |
| 454 | it('should retrieve auth config with isEnabledForToolRouter set to false', async () => { |
| 455 | const responseWithoutToolRouter = { |
| 456 | ...mockComposioAuthConfigResponse, |
| 457 | is_enabled_for_tool_router: false, |
| 458 | }; |
| 459 | mockClient.authConfigs.retrieve.mockResolvedValueOnce(responseWithoutToolRouter); |
| 460 | |
| 461 | const result = await authConfigs.get('auth_12345'); |
| 462 | |
| 463 | expect(result.isEnabledForToolRouter).toBe(false); |
| 464 | }); |
| 465 | |
| 466 | it('should retrieve auth config with isEnabledForToolRouter undefined', async () => { |
| 467 | const responseWithoutField = { |
| 468 | ...mockComposioAuthConfigResponse, |
| 469 | is_enabled_for_tool_router: undefined, |
| 470 | }; |
| 471 | mockClient.authConfigs.retrieve.mockResolvedValueOnce(responseWithoutField); |
| 472 | |
| 473 | const result = await authConfigs.get('auth_12345'); |
| 474 | |
| 475 | expect(result.isEnabledForToolRouter).toBeUndefined(); |
| 476 | }); |
| 477 | }); |
| 478 | |
| 479 | describe('update', () => { |
| 480 | const mockUpdateResponse: AuthConfigUpdateResponse = { |
| 481 | id: 'auth_12345', |
| 482 | status: 'success', |
| 483 | }; |
| 484 | |
| 485 | it('should update custom auth config with credentials', async () => { |
| 486 | mockClient.authConfigs.update.mockResolvedValueOnce(mockUpdateResponse); |
| 487 | |
| 488 | const updateData = { |
| 489 | type: 'custom' as const, |
| 490 | credentials: { |
| 491 | client_id: 'new_client_id', |
| 492 | client_secret: 'new_client_secret', |
| 493 | }, |
| 494 | }; |
| 495 | |
| 496 | const result = await authConfigs.update('auth_12345', updateData); |
| 497 | |
| 498 | expect(mockClient.authConfigs.update).toHaveBeenCalledWith( |
| 499 | 'auth_12345', |
| 500 | { |
| 501 | type: 'custom', |
| 502 | credentials: { |
| 503 | client_id: 'new_client_id', |
| 504 | client_secret: 'new_client_secret', |
| 505 | }, |
| 506 | tool_access_config: { |
| 507 | tools_for_connected_account_creation: undefined, |
| 508 | }, |
| 509 | }, |
| 510 | undefined |
| 511 | ); |
| 512 | |
| 513 | expect(result).toEqual(mockUpdateResponse); |
| 514 | }); |
| 515 | |
| 516 | it('should update default auth config with scopes', async () => { |
| 517 | mockClient.authConfigs.update.mockResolvedValueOnce(mockUpdateResponse); |
| 518 | |
| 519 | const updateData = { |
| 520 | type: 'default' as const, |
| 521 | scopes: 'read:user,repo', |
| 522 | }; |
| 523 | |
| 524 | const result = await authConfigs.update('auth_12345', updateData); |
| 525 | |
| 526 | expect(mockClient.authConfigs.update).toHaveBeenCalledWith( |
| 527 | 'auth_12345', |
| 528 | { |
| 529 | type: 'default', |
| 530 | scopes: 'read:user,repo', |
| 531 | tool_access_config: { |
| 532 | tools_for_connected_account_creation: undefined, |
| 533 | }, |
| 534 | }, |
| 535 | undefined |
| 536 | ); |
| 537 | |
| 538 | expect(result).toEqual(mockUpdateResponse); |
| 539 | }); |
| 540 | |
| 541 | it('should handle optional restrictToFollowingTools parameter', async () => { |
| 542 | mockClient.authConfigs.update.mockResolvedValueOnce(mockUpdateResponse); |
| 543 | |
| 544 | const updateData = { |
| 545 | type: 'custom' as const, |
| 546 | credentials: { |
| 547 | api_key: 'new_api_key', |
| 548 | }, |
| 549 | }; |
| 550 | |
| 551 | const result = await authConfigs.update('auth_12345', updateData); |
| 552 | |
| 553 | expect(mockClient.authConfigs.update).toHaveBeenCalledWith( |
| 554 | 'auth_12345', |
| 555 | { |
| 556 | type: 'custom', |
| 557 | credentials: { |
| 558 | api_key: 'new_api_key', |
| 559 | }, |
| 560 | tool_access_config: { |
| 561 | tools_for_connected_account_creation: undefined, |
| 562 | }, |
| 563 | }, |
| 564 | undefined |
| 565 | ); |
| 566 | |
| 567 | expect(result).toEqual(mockUpdateResponse); |
| 568 | }); |
| 569 | |
| 570 | it('should throw ValidationError for invalid update data', async () => { |
| 571 | const invalidUpdateData = { |
| 572 | type: 'invalid_type' as const, |
| 573 | credentials: {}, |
| 574 | }; |
| 575 | |
| 576 | await expect( |
| 577 | // @ts-expect-error - Testing invalid input |
| 578 | authConfigs.update('auth_12345', invalidUpdateData) |
| 579 | ).rejects.toThrow(ValidationError); |
| 580 | }); |
| 581 | |
| 582 | it('should handle API errors during update', async () => { |
| 583 | const apiError = new Error('Update failed'); |
| 584 | mockClient.authConfigs.update.mockRejectedValueOnce(apiError); |
| 585 | |
| 586 | const updateData = { |
| 587 | type: 'custom' as const, |
| 588 | credentials: { |
| 589 | api_key: 'key', |
| 590 | }, |
| 591 | }; |
| 592 | |
| 593 | await expect(authConfigs.update('auth_12345', updateData)).rejects.toThrow('Update failed'); |
| 594 | }); |
| 595 | }); |
| 596 | |
| 597 | describe('delete', () => { |
| 598 | const mockDeleteResponse: AuthConfigDeleteResponse = { |
| 599 | id: 'auth_12345', |
| 600 | status: 'deleted', |
| 601 | }; |
| 602 | |
| 603 | it('should delete auth config by nanoid', async () => { |
| 604 | mockClient.authConfigs.delete.mockResolvedValueOnce(mockDeleteResponse); |
| 605 | |
| 606 | const result = await authConfigs.delete('auth_12345'); |
| 607 | |
| 608 | expect(mockClient.authConfigs.delete).toHaveBeenCalledWith( |
| 609 | 'auth_12345', |
| 610 | undefined, |
| 611 | undefined |
| 612 | ); |
| 613 | expect(result).toEqual(mockDeleteResponse); |
| 614 | }); |
| 615 | |
| 616 | it('should handle API errors during deletion', async () => { |
| 617 | const apiError = new Error('Delete failed'); |
| 618 | mockClient.authConfigs.delete.mockRejectedValueOnce(apiError); |
| 619 | |
| 620 | await expect(authConfigs.delete('auth_12345')).rejects.toThrow('Delete failed'); |
| 621 | }); |
| 622 | }); |
| 623 | |
| 624 | describe('updateStatus', () => { |
| 625 | const mockStatusUpdateResponse: AuthConfigUpdateStatusResponse = { |
| 626 | id: 'auth_12345', |
| 627 | status: 'ENABLED', |
| 628 | }; |
| 629 | |
| 630 | it('should update status to ENABLED', async () => { |
| 631 | mockClient.authConfigs.updateStatus.mockResolvedValueOnce(mockStatusUpdateResponse); |
| 632 | |
| 633 | const result = await authConfigs.updateStatus('ENABLED', 'auth_12345'); |
| 634 | |
| 635 | expect(mockClient.authConfigs.updateStatus).toHaveBeenCalledWith( |
| 636 | 'ENABLED', |
| 637 | { |
| 638 | nanoid: 'auth_12345', |
| 639 | }, |
| 640 | undefined |
| 641 | ); |
| 642 | expect(result).toEqual(mockStatusUpdateResponse); |
| 643 | }); |
| 644 | |
| 645 | it('should update status to DISABLED', async () => { |
| 646 | const disabledResponse = { |
| 647 | id: 'auth_12345', |
| 648 | status: 'DISABLED', |
| 649 | }; |
| 650 | mockClient.authConfigs.updateStatus.mockResolvedValueOnce(disabledResponse); |
| 651 | |
| 652 | const result = await authConfigs.updateStatus('DISABLED', 'auth_12345'); |
| 653 | |
| 654 | expect(mockClient.authConfigs.updateStatus).toHaveBeenCalledWith( |
| 655 | 'DISABLED', |
| 656 | { |
| 657 | nanoid: 'auth_12345', |
| 658 | }, |
| 659 | undefined |
| 660 | ); |
| 661 | expect(result).toEqual(disabledResponse); |
| 662 | }); |
| 663 | |
| 664 | it('should handle API errors during status update', async () => { |
| 665 | const apiError = new Error('Status update failed'); |
| 666 | mockClient.authConfigs.updateStatus.mockRejectedValueOnce(apiError); |
| 667 | |
| 668 | await expect(authConfigs.updateStatus('ENABLED', 'auth_12345')).rejects.toThrow( |
| 669 | 'Status update failed' |
| 670 | ); |
| 671 | }); |
| 672 | }); |
| 673 | |
| 674 | describe('enable', () => { |
| 675 | const mockStatusUpdateResponse: AuthConfigUpdateStatusResponse = { |
| 676 | id: 'auth_12345', |
| 677 | status: 'ENABLED', |
| 678 | }; |
| 679 | |
| 680 | it('should enable auth config', async () => { |
| 681 | mockClient.authConfigs.updateStatus.mockResolvedValueOnce(mockStatusUpdateResponse); |
| 682 | |
| 683 | const result = await authConfigs.enable('auth_12345'); |
| 684 | |
| 685 | expect(mockClient.authConfigs.updateStatus).toHaveBeenCalledWith( |
| 686 | 'ENABLED', |
| 687 | { |
| 688 | nanoid: 'auth_12345', |
| 689 | }, |
| 690 | undefined |
| 691 | ); |
| 692 | expect(result).toEqual(mockStatusUpdateResponse); |
| 693 | }); |
| 694 | |
| 695 | it('should handle API errors during enable', async () => { |
| 696 | const apiError = new Error('Enable failed'); |
| 697 | mockClient.authConfigs.updateStatus.mockRejectedValueOnce(apiError); |
| 698 | |
| 699 | await expect(authConfigs.enable('auth_12345')).rejects.toThrow('Enable failed'); |
| 700 | }); |
| 701 | }); |
| 702 | |
| 703 | describe('disable', () => { |
| 704 | const mockStatusUpdateResponse: AuthConfigUpdateStatusResponse = { |
| 705 | id: 'auth_12345', |
| 706 | status: 'DISABLED', |
| 707 | }; |
| 708 | |
| 709 | it('should disable auth config', async () => { |
| 710 | mockClient.authConfigs.updateStatus.mockResolvedValueOnce(mockStatusUpdateResponse); |
| 711 | |
| 712 | const result = await authConfigs.disable('auth_12345'); |
| 713 | |
| 714 | expect(mockClient.authConfigs.updateStatus).toHaveBeenCalledWith( |
| 715 | 'DISABLED', |
| 716 | { |
| 717 | nanoid: 'auth_12345', |
| 718 | }, |
| 719 | undefined |
| 720 | ); |
| 721 | expect(result).toEqual(mockStatusUpdateResponse); |
| 722 | }); |
| 723 | |
| 724 | it('should handle API errors during disable', async () => { |
| 725 | const apiError = new Error('Disable failed'); |
| 726 | mockClient.authConfigs.updateStatus.mockRejectedValueOnce(apiError); |
| 727 | |
| 728 | await expect(authConfigs.disable('auth_12345')).rejects.toThrow('Disable failed'); |
| 729 | }); |
| 730 | }); |
| 731 | |
| 732 | describe('error handling', () => { |
| 733 | // We have relaxed the validation for auth config response, so we don't throw an error when parsing fails |
| 734 | // it('should preserve ValidationError details when parsing fails', async () => { |
| 735 | // const invalidResponse = { |
| 736 | // id: null, // Invalid: id should be string |
| 737 | // name: 'Test Config', |
| 738 | // no_of_connections: 5, |
| 739 | // status: 'ENABLED', |
| 740 | // deprecated_params: { |
| 741 | // default_connector_id: null, |
| 742 | // }, |
| 743 | // type: 'custom', |
| 744 | // toolkit: { |
| 745 | // logo: 'valid_logo', |
| 746 | // slug: 'valid_slug', |
| 747 | // }, |
| 748 | // uuid: 'uuid-123', |
| 749 | // } as unknown as ComposioAuthConfigRetrieveResponse; |
| 750 | |
| 751 | // expect(() => { |
| 752 | // // @ts-expect-error - Accessing private method for testing |
| 753 | // authConfigs.parseAuthConfigRetrieveResponse(invalidResponse); |
| 754 | // }).toThrow(ValidationError); |
| 755 | |
| 756 | // try { |
| 757 | // // @ts-expect-error - Accessing private method for testing |
| 758 | // authConfigs.parseAuthConfigRetrieveResponse(invalidResponse); |
| 759 | // } catch (error) { |
| 760 | // expect(error).toBeInstanceOf(ValidationError); |
| 761 | // expect((error as ValidationError).message).toContain( |
| 762 | // 'Failed to parse auth config response' |
| 763 | // ); |
| 764 | // expect((error as ValidationError).cause).toBeInstanceOf(ZodError); |
| 765 | // } |
| 766 | // }); |
| 767 | |
| 768 | it('should handle network errors gracefully', async () => { |
| 769 | const networkError = new Error('Network error'); |
| 770 | mockClient.authConfigs.list.mockRejectedValueOnce(networkError); |
| 771 | |
| 772 | await expect(authConfigs.list()).rejects.toThrow('Network error'); |
| 773 | }); |
| 774 | }); |
| 775 | |
| 776 | describe('edge cases', () => { |
| 777 | it('should handle auth config with minimal fields', () => { |
| 778 | const minimalResponse = { |
| 779 | id: 'auth_minimal', |
| 780 | name: 'Minimal Config', |
| 781 | no_of_connections: 0, |
| 782 | status: 'DISABLED', |
| 783 | type: 'default', |
| 784 | toolkit: { |
| 785 | logo: '', |
| 786 | slug: 'minimal-toolkit', |
| 787 | }, |
| 788 | } as ComposioAuthConfigRetrieveResponse; |
| 789 | |
| 790 | const result = transformAuthConfigRetrieveResponse(minimalResponse); |
| 791 | |
| 792 | expect(result).toMatchObject({ |
| 793 | id: 'auth_minimal', |
| 794 | name: 'Minimal Config', |
| 795 | noOfConnections: 0, |
| 796 | status: 'DISABLED', |
| 797 | toolkit: { |
| 798 | logo: '', |
| 799 | slug: 'minimal-toolkit', |
| 800 | }, |
| 801 | }); |
| 802 | }); |
| 803 | |
| 804 | it('should handle large credential objects', async () => { |
| 805 | const largeCredentials = { |
| 806 | field1: 'value1', |
| 807 | field2: 'value2', |
| 808 | field3: { nested: 'object' }, |
| 809 | field4: ['array', 'values'], |
| 810 | field5: 12345, |
| 811 | field6: true, |
| 812 | }; |
| 813 | |
| 814 | const updateData = { |
| 815 | type: 'custom' as const, |
| 816 | credentials: largeCredentials, |
| 817 | }; |
| 818 | |
| 819 | const mockUpdateResponse: AuthConfigUpdateResponse = { |
| 820 | id: 'auth_12345', |
| 821 | status: 'success', |
| 822 | }; |
| 823 | |
| 824 | mockClient.authConfigs.update.mockResolvedValueOnce(mockUpdateResponse); |
| 825 | |
| 826 | const result = await authConfigs.update('auth_12345', updateData); |
| 827 | |
| 828 | expect(mockClient.authConfigs.update).toHaveBeenCalledWith( |
| 829 | 'auth_12345', |
| 830 | { |
| 831 | type: 'custom', |
| 832 | credentials: largeCredentials, |
| 833 | tool_access_config: { |
| 834 | tools_for_connected_account_creation: undefined, |
| 835 | }, |
| 836 | }, |
| 837 | undefined |
| 838 | ); |
| 839 | |
| 840 | expect(result).toEqual(mockUpdateResponse); |
| 841 | }); |
| 842 | |
| 843 | it('should handle very long scopes string', async () => { |
| 844 | const longScopes = Array(100).fill('scope').join(','); |
| 845 | |
| 846 | const updateData = { |
| 847 | type: 'default' as const, |
| 848 | scopes: longScopes, |
| 849 | }; |
| 850 | |
| 851 | const mockUpdateResponse: AuthConfigUpdateResponse = { |
| 852 | id: 'auth_12345', |
| 853 | status: 'success', |
| 854 | }; |
| 855 | |
| 856 | mockClient.authConfigs.update.mockResolvedValueOnce(mockUpdateResponse); |
| 857 | |
| 858 | const result = await authConfigs.update('auth_12345', updateData); |
| 859 | |
| 860 | expect(mockClient.authConfigs.update).toHaveBeenCalledWith( |
| 861 | 'auth_12345', |
| 862 | { |
| 863 | type: 'default', |
| 864 | scopes: longScopes, |
| 865 | tool_access_config: { |
| 866 | tools_for_connected_account_creation: undefined, |
| 867 | }, |
| 868 | }, |
| 869 | undefined |
| 870 | ); |
| 871 | |
| 872 | expect(result).toEqual(mockUpdateResponse); |
| 873 | }); |
| 874 | }); |
| 875 | }); |