Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | import { Toolkits } from '../../src/models/Toolkits'; |
| 3 | import ComposioClient from '@composio/client'; |
| 4 | import { telemetry } from '../../src/telemetry/Telemetry'; |
| 5 | import { ComposioAuthConfigNotFoundError } from '../../src/errors/AuthConfigErrors'; |
| 6 | import { |
| 7 | ComposioToolkitFetchError, |
| 8 | ComposioToolkitNotFoundError, |
| 9 | } from '../../src/errors/ToolkitErrors'; |
| 10 | import { ValidationError } from '../../src/errors/ValidationErrors'; |
| 11 | import { AuthSchemeTypes } from '../../src/types/authConfigs.types'; |
| 12 | import { APIError } from '@composio/client'; |
| 13 | import type { ToolkitListParams } from '../../src/types/toolkit.types'; |
| 14 | |
| 15 | // Mock dependencies |
| 16 | vi.mock('../../src/telemetry/Telemetry', () => ({ |
| 17 | telemetry: { |
| 18 | instrument: vi.fn(), |
| 19 | }, |
| 20 | })); |
| 21 | |
| 22 | // Minimal structural shape for overriding `Toolkits.prototype.getToolkitBySlug` |
| 23 | // (a protected method) with a mock implementation in tests. |
| 24 | type ToolkitsPrototypeOverride = { |
| 25 | getToolkitBySlug: unknown; |
| 26 | }; |
| 27 | |
| 28 | // Create mock client with toolkit-related methods |
| 29 | const createMockClient = () => ({ |
| 30 | baseURL: 'https://api.composio.dev', |
| 31 | apiKey: 'test-api-key', |
| 32 | toolkits: { |
| 33 | list: vi.fn(), |
| 34 | retrieve: vi.fn(), |
| 35 | retrieveCategories: vi.fn(), |
| 36 | retrieveMulti: vi.fn(), |
| 37 | retrieveChangelog: vi.fn(), |
| 38 | listCategories: vi.fn(), |
| 39 | authorize: vi.fn(), |
| 40 | get: vi.fn(), |
| 41 | }, |
| 42 | authConfigs: { |
| 43 | list: vi.fn(), |
| 44 | create: vi.fn(), |
| 45 | }, |
| 46 | connectedAccounts: { |
| 47 | initiate: vi.fn(), |
| 48 | create: vi.fn(), |
| 49 | list: vi.fn(), |
| 50 | }, |
| 51 | }); |
| 52 | |
| 53 | // Mock response data |
| 54 | const mockToolkitListResponse = { |
| 55 | items: [ |
| 56 | { |
| 57 | name: 'GitHub', |
| 58 | slug: 'github', |
| 59 | meta: { |
| 60 | created_at: '2024-01-01', |
| 61 | updated_at: '2024-01-02', |
| 62 | tools_count: 10, |
| 63 | triggers_count: 5, |
| 64 | }, |
| 65 | is_local_toolkit: false, |
| 66 | auth_schemes: ['oauth2'], |
| 67 | composio_managed_auth_schemes: ['oauth2'], |
| 68 | no_auth: false, |
| 69 | }, |
| 70 | ], |
| 71 | }; |
| 72 | |
| 73 | const mockToolkitRetrieveResponse = { |
| 74 | name: 'GitHub', |
| 75 | slug: 'github', |
| 76 | meta: { |
| 77 | created_at: '2024-01-01', |
| 78 | updated_at: '2024-01-02', |
| 79 | tools_count: 10, |
| 80 | triggers_count: 5, |
| 81 | }, |
| 82 | is_local_toolkit: false, |
| 83 | composio_managed_auth_schemes: ['oauth2'], |
| 84 | auth_config_details: [ |
| 85 | { |
| 86 | name: 'oauth2', |
| 87 | mode: 'oauth2', |
| 88 | fields: { |
| 89 | auth_config_creation: { |
| 90 | optional: [], |
| 91 | required: [], |
| 92 | }, |
| 93 | connected_account_initiation: { |
| 94 | optional: [], |
| 95 | required: [], |
| 96 | }, |
| 97 | }, |
| 98 | proxy: { |
| 99 | base_url: 'https://api.github.com', |
| 100 | }, |
| 101 | }, |
| 102 | ], |
| 103 | get_current_user_endpoint: '/user', |
| 104 | get_current_user_endpoint_method: 'GET', |
| 105 | }; |
| 106 | |
| 107 | describe('Toolkits', () => { |
| 108 | let toolkits: Toolkits; |
| 109 | let mockClient: ReturnType<typeof createMockClient>; |
| 110 | |
| 111 | beforeEach(() => { |
| 112 | vi.clearAllMocks(); |
| 113 | mockClient = createMockClient(); |
| 114 | toolkits = new Toolkits(mockClient as unknown as ComposioClient); |
| 115 | }); |
| 116 | |
| 117 | describe('constructor', () => { |
| 118 | it('should create an instance successfully', () => { |
| 119 | expect(toolkits).toBeInstanceOf(Toolkits); |
| 120 | expect(telemetry.instrument).toHaveBeenCalledWith(toolkits, 'Toolkits'); |
| 121 | }); |
| 122 | }); |
| 123 | |
| 124 | describe('get', () => { |
| 125 | it('should get a list of toolkits', async () => { |
| 126 | mockClient.toolkits.list.mockResolvedValue(mockToolkitListResponse); |
| 127 | |
| 128 | const result = await toolkits.get({}); |
| 129 | |
| 130 | expect(result).toEqual([ |
| 131 | { |
| 132 | name: 'GitHub', |
| 133 | slug: 'github', |
| 134 | meta: { |
| 135 | createdAt: '2024-01-01', |
| 136 | updatedAt: '2024-01-02', |
| 137 | toolsCount: 10, |
| 138 | triggersCount: 5, |
| 139 | }, |
| 140 | isLocalToolkit: false, |
| 141 | authSchemes: ['oauth2'], |
| 142 | composioManagedAuthSchemes: ['oauth2'], |
| 143 | noAuth: false, |
| 144 | }, |
| 145 | ]); |
| 146 | expect(mockClient.toolkits.list).toHaveBeenCalledWith({}, undefined); |
| 147 | }); |
| 148 | |
| 149 | it('should get a single toolkit by slug', async () => { |
| 150 | mockClient.toolkits.retrieve.mockResolvedValue(mockToolkitRetrieveResponse); |
| 151 | |
| 152 | const result = await toolkits.get('github'); |
| 153 | |
| 154 | expect(result).toEqual({ |
| 155 | name: 'GitHub', |
| 156 | slug: 'github', |
| 157 | meta: { |
| 158 | createdAt: '2024-01-01', |
| 159 | updatedAt: '2024-01-02', |
| 160 | toolsCount: 10, |
| 161 | triggersCount: 5, |
| 162 | }, |
| 163 | isLocalToolkit: false, |
| 164 | composioManagedAuthSchemes: ['oauth2'], |
| 165 | authConfigDetails: [ |
| 166 | { |
| 167 | name: 'oauth2', |
| 168 | mode: 'oauth2', |
| 169 | fields: { |
| 170 | authConfigCreation: { |
| 171 | optional: [], |
| 172 | required: [], |
| 173 | }, |
| 174 | connectedAccountInitiation: { |
| 175 | optional: [], |
| 176 | required: [], |
| 177 | }, |
| 178 | }, |
| 179 | proxy: { |
| 180 | baseUrl: 'https://api.github.com', |
| 181 | }, |
| 182 | }, |
| 183 | ], |
| 184 | getCurrentUserEndpoint: '/user', |
| 185 | getCurrentUserEndpointMethod: 'GET', |
| 186 | }); |
| 187 | expect(mockClient.toolkits.retrieve).toHaveBeenCalledWith('github', undefined, undefined); |
| 188 | }); |
| 189 | |
| 190 | it('should handle toolkit without getCurrentUserEndpointMethod', async () => { |
| 191 | const responseWithoutMethod = { |
| 192 | ...mockToolkitRetrieveResponse, |
| 193 | get_current_user_endpoint_method: undefined, |
| 194 | }; |
| 195 | mockClient.toolkits.retrieve.mockResolvedValue(responseWithoutMethod); |
| 196 | |
| 197 | const result = await toolkits.get('github'); |
| 198 | |
| 199 | expect(result).toEqual({ |
| 200 | name: 'GitHub', |
| 201 | slug: 'github', |
| 202 | meta: { |
| 203 | createdAt: '2024-01-01', |
| 204 | updatedAt: '2024-01-02', |
| 205 | toolsCount: 10, |
| 206 | triggersCount: 5, |
| 207 | }, |
| 208 | isLocalToolkit: false, |
| 209 | composioManagedAuthSchemes: ['oauth2'], |
| 210 | authConfigDetails: [ |
| 211 | { |
| 212 | name: 'oauth2', |
| 213 | mode: 'oauth2', |
| 214 | fields: { |
| 215 | authConfigCreation: { |
| 216 | optional: [], |
| 217 | required: [], |
| 218 | }, |
| 219 | connectedAccountInitiation: { |
| 220 | optional: [], |
| 221 | required: [], |
| 222 | }, |
| 223 | }, |
| 224 | proxy: { |
| 225 | baseUrl: 'https://api.github.com', |
| 226 | }, |
| 227 | }, |
| 228 | ], |
| 229 | getCurrentUserEndpoint: '/user', |
| 230 | getCurrentUserEndpointMethod: undefined, |
| 231 | }); |
| 232 | }); |
| 233 | |
| 234 | it('should throw ValidationError for invalid list query', async () => { |
| 235 | const promise = toolkits.get({ category: 123 } as unknown as ToolkitListParams); |
| 236 | await expect(promise).rejects.toThrowError('Failed to fetch toolkits'); |
| 237 | }); |
| 238 | |
| 239 | it('should throw ComposioToolkitNotFoundError when the API returns 404', async () => { |
| 240 | const notFound = new ComposioClient.NotFoundError(404, undefined, undefined, new Headers()); |
| 241 | mockClient.toolkits.retrieve.mockRejectedValueOnce(notFound); |
| 242 | |
| 243 | const error = await toolkits.get('non-existent').catch(e => e); |
| 244 | |
| 245 | expect(error).toBeInstanceOf(ComposioToolkitNotFoundError); |
| 246 | expect(error.cause).toBe(notFound); |
| 247 | }); |
| 248 | |
| 249 | it('should not report an invalid API key (401) as toolkit not found', async () => { |
| 250 | const unauthorized = new ComposioClient.AuthenticationError( |
| 251 | 401, |
| 252 | undefined, |
| 253 | undefined, |
| 254 | new Headers() |
| 255 | ); |
| 256 | mockClient.toolkits.retrieve.mockRejectedValueOnce(unauthorized); |
| 257 | |
| 258 | const error = await toolkits.get('github').catch(e => e); |
| 259 | |
| 260 | expect(error).toBeInstanceOf(ComposioToolkitFetchError); |
| 261 | expect(error.cause).toBe(unauthorized); |
| 262 | }); |
| 263 | |
| 264 | it('should throw ComposioToolkitFetchError for non-API failures', async () => { |
| 265 | mockClient.toolkits.retrieve.mockRejectedValueOnce(new Error('socket hang up')); |
| 266 | |
| 267 | const promise = toolkits.get('non-existent'); |
| 268 | await expect(promise).rejects.toThrowError("Couldn't fetch Toolkit with slug: non-existent"); |
| 269 | }); |
| 270 | |
| 271 | it('should throw ComposioToolkitFetchError for other errors', async () => { |
| 272 | mockClient.toolkits.list.mockRejectedValue(new Error('Network error')); |
| 273 | |
| 274 | const promise = toolkits.get({}); |
| 275 | await expect(promise).rejects.toThrowError('Failed to fetch toolkits'); |
| 276 | }); |
| 277 | }); |
| 278 | |
| 279 | describe('listCategories', () => { |
| 280 | it('should list toolkit categories', async () => { |
| 281 | const mockResponse = { |
| 282 | items: [ |
| 283 | { |
| 284 | id: 'cat-1', |
| 285 | name: 'Category 1', |
| 286 | }, |
| 287 | ], |
| 288 | next_cursor: null, |
| 289 | total_pages: 1, |
| 290 | }; |
| 291 | |
| 292 | mockClient.toolkits.retrieveCategories.mockResolvedValue(mockResponse); |
| 293 | |
| 294 | const result = await toolkits.listCategories(); |
| 295 | |
| 296 | expect(result).toEqual({ |
| 297 | items: mockResponse.items, |
| 298 | nextCursor: mockResponse.next_cursor, |
| 299 | totalPages: mockResponse.total_pages, |
| 300 | }); |
| 301 | }); |
| 302 | }); |
| 303 | |
| 304 | describe('getMany', () => { |
| 305 | it('should fetch toolkits by slug and return the list shape', async () => { |
| 306 | mockClient.toolkits.retrieveMulti.mockResolvedValue(mockToolkitListResponse); |
| 307 | |
| 308 | const result = await toolkits.getMany(['github', 'slack']); |
| 309 | |
| 310 | expect(mockClient.toolkits.retrieveMulti).toHaveBeenCalledWith( |
| 311 | { |
| 312 | toolkits: ['github', 'slack'], |
| 313 | category: undefined, |
| 314 | managed_by: undefined, |
| 315 | sort_by: undefined, |
| 316 | cursor: undefined, |
| 317 | limit: undefined, |
| 318 | }, |
| 319 | undefined |
| 320 | ); |
| 321 | expect(result).toEqual([ |
| 322 | { |
| 323 | name: 'GitHub', |
| 324 | slug: 'github', |
| 325 | meta: { |
| 326 | createdAt: '2024-01-01', |
| 327 | updatedAt: '2024-01-02', |
| 328 | toolsCount: 10, |
| 329 | triggersCount: 5, |
| 330 | }, |
| 331 | isLocalToolkit: false, |
| 332 | authSchemes: ['oauth2'], |
| 333 | composioManagedAuthSchemes: ['oauth2'], |
| 334 | noAuth: false, |
| 335 | }, |
| 336 | ]); |
| 337 | }); |
| 338 | |
| 339 | it('should forward filters and request options', async () => { |
| 340 | mockClient.toolkits.retrieveMulti.mockResolvedValue({ items: [] }); |
| 341 | const signal = new AbortController().signal; |
| 342 | |
| 343 | await toolkits.getMany( |
| 344 | ['github'], |
| 345 | { managedBy: 'composio', sortBy: 'usage', limit: 5 }, |
| 346 | { |
| 347 | signal, |
| 348 | } |
| 349 | ); |
| 350 | |
| 351 | expect(mockClient.toolkits.retrieveMulti).toHaveBeenCalledWith( |
| 352 | { |
| 353 | toolkits: ['github'], |
| 354 | category: undefined, |
| 355 | managed_by: 'composio', |
| 356 | sort_by: 'usage', |
| 357 | cursor: undefined, |
| 358 | limit: 5, |
| 359 | }, |
| 360 | { signal } |
| 361 | ); |
| 362 | }); |
| 363 | |
| 364 | it('should throw a ValidationError for an empty slug list', async () => { |
| 365 | await expect(toolkits.getMany([])).rejects.toThrow(ValidationError); |
| 366 | expect(mockClient.toolkits.retrieveMulti).not.toHaveBeenCalled(); |
| 367 | }); |
| 368 | |
| 369 | it('should wrap request failures in ComposioToolkitFetchError', async () => { |
| 370 | mockClient.toolkits.retrieveMulti.mockRejectedValue(new Error('boom')); |
| 371 | |
| 372 | await expect(toolkits.getMany(['github'])).rejects.toThrow(ComposioToolkitFetchError); |
| 373 | }); |
| 374 | }); |
| 375 | |
| 376 | describe('changelog', () => { |
| 377 | it('should return the toolkit changelog in camelCase', async () => { |
| 378 | mockClient.toolkits.retrieveChangelog.mockResolvedValue({ |
| 379 | items: [ |
| 380 | { |
| 381 | slug: 'github', |
| 382 | name: 'github', |
| 383 | display_name: 'GitHub', |
| 384 | versions: [{ version: '20250909_00', changelog: 'Added issues tools' }], |
| 385 | }, |
| 386 | ], |
| 387 | }); |
| 388 | |
| 389 | const result = await toolkits.changelog(); |
| 390 | |
| 391 | expect(mockClient.toolkits.retrieveChangelog).toHaveBeenCalledWith(undefined); |
| 392 | expect(result).toEqual({ |
| 393 | items: [ |
| 394 | { |
| 395 | slug: 'github', |
| 396 | name: 'github', |
| 397 | displayName: 'GitHub', |
| 398 | versions: [{ version: '20250909_00', changelog: 'Added issues tools' }], |
| 399 | }, |
| 400 | ], |
| 401 | }); |
| 402 | }); |
| 403 | }); |
| 404 | |
| 405 | describe('authorize', () => { |
| 406 | const userId = 'user_123'; |
| 407 | const toolkitSlug = 'test-toolkit'; |
| 408 | const mockToolkitResponse = { |
| 409 | slug: toolkitSlug, |
| 410 | name: 'Test Toolkit', |
| 411 | meta: { |
| 412 | created_at: new Date().toISOString(), |
| 413 | updated_at: new Date().toISOString(), |
| 414 | tools_count: 1, |
| 415 | triggers_count: 0, |
| 416 | }, |
| 417 | is_local_toolkit: false, |
| 418 | auth_config_details: [ |
| 419 | { |
| 420 | name: 'OAUTH2', |
| 421 | mode: 'OAUTH2', |
| 422 | fields: { |
| 423 | auth_config_creation: { |
| 424 | optional: [], |
| 425 | required: [], |
| 426 | }, |
| 427 | connected_account_initiation: { |
| 428 | optional: [], |
| 429 | required: [], |
| 430 | }, |
| 431 | }, |
| 432 | }, |
| 433 | ], |
| 434 | }; |
| 435 | |
| 436 | it('should create auth config and initiate connection when no auth config exists', async () => { |
| 437 | // Mock toolkit retrieval |
| 438 | mockClient.toolkits.retrieve.mockResolvedValueOnce(mockToolkitResponse); |
| 439 | |
| 440 | // Mock empty auth config list |
| 441 | mockClient.authConfigs.list.mockResolvedValueOnce({ |
| 442 | items: [], |
| 443 | next_cursor: null, |
| 444 | total_pages: 1, |
| 445 | }); |
| 446 | |
| 447 | // Mock auth config creation |
| 448 | mockClient.authConfigs.create.mockResolvedValueOnce({ |
| 449 | auth_config: { |
| 450 | id: 'auth_config_123', |
| 451 | auth_scheme: 'OAUTH2', |
| 452 | is_composio_managed: true, |
| 453 | is_disabled: false, |
| 454 | name: 'Test Toolkit Auth Config', |
| 455 | tool_access_config: { |
| 456 | tools_for_connected_account_creation: undefined, |
| 457 | }, |
| 458 | no_of_connections: 0, |
| 459 | status: 'ENABLED', |
| 460 | uuid: 'uuid-1', |
| 461 | }, |
| 462 | toolkit: { |
| 463 | logo: 'https://example.com/logo.png', |
| 464 | slug: toolkitSlug, |
| 465 | }, |
| 466 | }); |
| 467 | |
| 468 | // Mock connected accounts list |
| 469 | mockClient.connectedAccounts.list.mockResolvedValueOnce({ |
| 470 | items: [], |
| 471 | next_cursor: null, |
| 472 | total_pages: 1, |
| 473 | }); |
| 474 | |
| 475 | // Mock connection initiation |
| 476 | mockClient.connectedAccounts.create.mockResolvedValueOnce({ |
| 477 | id: 'conn_123', |
| 478 | connectionData: { |
| 479 | type: 'OAUTH2', |
| 480 | val: { |
| 481 | status: 'INITIALIZING', |
| 482 | redirectUrl: 'https://auth.example.com/connect', |
| 483 | authScheme: 'OAUTH2', |
| 484 | }, |
| 485 | }, |
| 486 | }); |
| 487 | |
| 488 | const connectionRequest = await toolkits.authorize(userId, toolkitSlug); |
| 489 | |
| 490 | // Verify the flow |
| 491 | expect(mockClient.toolkits.retrieve).toHaveBeenCalledWith(toolkitSlug, undefined, undefined); |
| 492 | expect(mockClient.authConfigs.list).toHaveBeenCalledWith( |
| 493 | { |
| 494 | toolkit_slug: toolkitSlug, |
| 495 | }, |
| 496 | undefined |
| 497 | ); |
| 498 | expect(mockClient.authConfigs.create).toHaveBeenCalledWith( |
| 499 | { |
| 500 | toolkit: { |
| 501 | slug: toolkitSlug, |
| 502 | }, |
| 503 | auth_config: { |
| 504 | type: 'use_composio_managed_auth', |
| 505 | credentials: undefined, |
| 506 | name: 'Test Toolkit Auth Config', |
| 507 | tool_access_config: undefined, |
| 508 | }, |
| 509 | }, |
| 510 | undefined |
| 511 | ); |
| 512 | expect(mockClient.connectedAccounts.create).toHaveBeenCalledWith( |
| 513 | { |
| 514 | auth_config: { |
| 515 | id: 'auth_config_123', |
| 516 | }, |
| 517 | connection: { |
| 518 | user_id: userId, |
| 519 | }, |
| 520 | }, |
| 521 | undefined |
| 522 | ); |
| 523 | expect(connectionRequest).toHaveProperty('id', 'conn_123'); |
| 524 | expect(connectionRequest).toHaveProperty('waitForConnection'); |
| 525 | expect(typeof connectionRequest.waitForConnection).toBe('function'); |
| 526 | expect(connectionRequest.redirectUrl).toBe('https://auth.example.com/connect'); |
| 527 | }); |
| 528 | |
| 529 | it('should use existing auth config to initiate connection', async () => { |
| 530 | // Mock toolkit retrieval |
| 531 | mockClient.toolkits.retrieve.mockResolvedValueOnce(mockToolkitResponse); |
| 532 | |
| 533 | // Mock existing auth config |
| 534 | mockClient.authConfigs.list.mockResolvedValueOnce({ |
| 535 | items: [ |
| 536 | { |
| 537 | id: 'existing_auth_123', |
| 538 | auth_scheme: 'OAUTH2', |
| 539 | is_composio_managed: true, |
| 540 | is_disabled: false, |
| 541 | name: 'Existing Auth Config', |
| 542 | no_of_connections: 1, |
| 543 | status: 'ENABLED', |
| 544 | uuid: 'uuid-1', |
| 545 | toolkit: { |
| 546 | logo: 'https://example.com/logo.png', |
| 547 | slug: toolkitSlug, |
| 548 | }, |
| 549 | }, |
| 550 | ], |
| 551 | next_cursor: null, |
| 552 | total_pages: 1, |
| 553 | }); |
| 554 | |
| 555 | // Mock connected accounts list |
| 556 | mockClient.connectedAccounts.list.mockResolvedValueOnce({ |
| 557 | items: [], |
| 558 | next_cursor: null, |
| 559 | total_pages: 1, |
| 560 | }); |
| 561 | |
| 562 | // Mock connection initiation |
| 563 | mockClient.connectedAccounts.create.mockResolvedValueOnce({ |
| 564 | id: 'conn_123', |
| 565 | connectionData: { |
| 566 | val: { |
| 567 | status: 'INITIALIZING', |
| 568 | redirectUrl: 'https://auth.example.com/connect', |
| 569 | authScheme: 'OAUTH2', |
| 570 | }, |
| 571 | }, |
| 572 | }); |
| 573 | |
| 574 | const connectionRequest = await toolkits.authorize(userId, toolkitSlug); |
| 575 | |
| 576 | // Verify the flow |
| 577 | expect(mockClient.toolkits.retrieve).toHaveBeenCalledWith(toolkitSlug, undefined, undefined); |
| 578 | expect(mockClient.authConfigs.list).toHaveBeenCalledWith( |
| 579 | { |
| 580 | toolkit_slug: toolkitSlug, |
| 581 | }, |
| 582 | undefined |
| 583 | ); |
| 584 | expect(mockClient.authConfigs.create).not.toHaveBeenCalled(); |
| 585 | expect(mockClient.connectedAccounts.create).toHaveBeenCalledWith( |
| 586 | { |
| 587 | auth_config: { |
| 588 | id: 'existing_auth_123', |
| 589 | }, |
| 590 | connection: { |
| 591 | user_id: userId, |
| 592 | }, |
| 593 | }, |
| 594 | undefined |
| 595 | ); |
| 596 | expect(connectionRequest).toHaveProperty('id', 'conn_123'); |
| 597 | expect(connectionRequest).toHaveProperty('waitForConnection'); |
| 598 | expect(typeof connectionRequest.waitForConnection).toBe('function'); |
| 599 | expect(connectionRequest.redirectUrl).toBe('https://auth.example.com/connect'); |
| 600 | }); |
| 601 | |
| 602 | it('should throw ComposioAuthConfigNotFoundError when toolkit has no auth config details', async () => { |
| 603 | // Mock toolkit retrieval with no auth config details |
| 604 | mockClient.toolkits.retrieve.mockResolvedValueOnce({ |
| 605 | ...mockToolkitResponse, |
| 606 | auth_config_details: [], // Empty auth config details |
| 607 | }); |
| 608 | |
| 609 | // Mock empty auth config list |
| 610 | mockClient.authConfigs.list.mockResolvedValueOnce({ |
| 611 | items: [], |
| 612 | next_cursor: null, |
| 613 | total_pages: 1, |
| 614 | }); |
| 615 | |
| 616 | const promise = toolkits.authorize(userId, toolkitSlug); |
| 617 | |
| 618 | await expect(promise).rejects.toThrow(ComposioAuthConfigNotFoundError); |
| 619 | await expect(promise).rejects.toThrow(`No auth configs found for toolkit ${toolkitSlug}`); |
| 620 | expect(mockClient.authConfigs.create).not.toHaveBeenCalled(); |
| 621 | expect(mockClient.connectedAccounts.create).not.toHaveBeenCalled(); |
| 622 | }); |
| 623 | |
| 624 | it('should throw ComposioAuthConfigNotFoundError when auth config creation fails', async () => { |
| 625 | // Mock toolkit retrieval |
| 626 | mockClient.toolkits.retrieve.mockResolvedValueOnce(mockToolkitResponse); |
| 627 | |
| 628 | // Mock empty auth config list |
| 629 | mockClient.authConfigs.list.mockResolvedValueOnce({ |
| 630 | items: [], |
| 631 | next_cursor: null, |
| 632 | total_pages: 1, |
| 633 | }); |
| 634 | |
| 635 | // Mock auth config creation failure with ComposioClient.APIError |
| 636 | const apiError = new ComposioClient.APIError( |
| 637 | 400, |
| 638 | 'Bad Request', |
| 639 | 'Bad Request', |
| 640 | new Headers() |
| 641 | ); |
| 642 | mockClient.authConfigs.create.mockRejectedValueOnce(apiError); |
| 643 | |
| 644 | const promise = toolkits.authorize(userId, toolkitSlug); |
| 645 | |
| 646 | await expect(promise).rejects.toThrow(ComposioAuthConfigNotFoundError); |
| 647 | await expect(promise).rejects.toThrow( |
| 648 | `No Default auth config found for toolkit ${toolkitSlug}` |
| 649 | ); |
| 650 | expect(mockClient.connectedAccounts.create).not.toHaveBeenCalled(); |
| 651 | }); |
| 652 | |
| 653 | it('should throw ComposioToolkitNotFoundError when toolkit does not exist', async () => { |
| 654 | mockClient.toolkits.retrieve.mockRejectedValue( |
| 655 | new APIError(400, 'Toolkit not found', 'Toolkit not found', new Headers()) |
| 656 | ); |
| 657 | |
| 658 | const promise = toolkits.authorize('user-123', 'non-existent'); |
| 659 | |
| 660 | await expect(promise).rejects.toThrow(ComposioToolkitNotFoundError); |
| 661 | await expect(promise).rejects.toThrow('Toolkit with slug non-existent not found'); |
| 662 | expect(mockClient.authConfigs.list).not.toHaveBeenCalled(); |
| 663 | expect(mockClient.authConfigs.create).not.toHaveBeenCalled(); |
| 664 | expect(mockClient.connectedAccounts.create).not.toHaveBeenCalled(); |
| 665 | }); |
| 666 | }); |
| 667 | |
| 668 | describe('getAuthConfigCreationFields', () => { |
| 669 | const toolkitSlug = 'test-toolkit'; |
| 670 | const mockToolkit = { |
| 671 | authConfigDetails: [ |
| 672 | { |
| 673 | name: 'oauth2', |
| 674 | mode: 'OAUTH2', |
| 675 | fields: { |
| 676 | authConfigCreation: { |
| 677 | required: [{ name: 'client_id' }, { name: 'client_secret' }], |
| 678 | optional: [{ name: 'redirect_uri' }], |
| 679 | }, |
| 680 | connectedAccountInitiation: { |
| 681 | required: [{ name: 'code' }], |
| 682 | optional: [{ name: 'state' }], |
| 683 | }, |
| 684 | }, |
| 685 | }, |
| 686 | ], |
| 687 | }; |
| 688 | |
| 689 | beforeEach(() => { |
| 690 | vi.spyOn(Toolkits.prototype as unknown, 'getToolkitBySlug').mockResolvedValue( |
| 691 | mockToolkit as unknown |
| 692 | ); |
| 693 | }); |
| 694 | |
| 695 | it('returns all fields when requiredOnly is false', async () => { |
| 696 | const result = await toolkits.getAuthConfigCreationFields( |
| 697 | toolkitSlug, |
| 698 | AuthSchemeTypes.OAUTH2, |
| 699 | { |
| 700 | requiredOnly: false, |
| 701 | } |
| 702 | ); |
| 703 | expect(result).toEqual([ |
| 704 | { name: 'client_id', required: true }, |
| 705 | { name: 'client_secret', required: true }, |
| 706 | { name: 'redirect_uri', required: false }, |
| 707 | ]); |
| 708 | }); |
| 709 | |
| 710 | it('returns only required fields when requiredOnly is true', async () => { |
| 711 | const result = await toolkits.getAuthConfigCreationFields( |
| 712 | toolkitSlug, |
| 713 | AuthSchemeTypes.OAUTH2, |
| 714 | { |
| 715 | requiredOnly: true, |
| 716 | } |
| 717 | ); |
| 718 | expect(result).toEqual([ |
| 719 | { name: 'client_id', required: true }, |
| 720 | { name: 'client_secret', required: true }, |
| 721 | ]); |
| 722 | }); |
| 723 | |
| 724 | it('selects the correct auth config by authScheme', async () => { |
| 725 | const multiToolkit = { |
| 726 | authConfigDetails: [ |
| 727 | { |
| 728 | name: 'oauth2', |
| 729 | mode: 'OAUTH2', |
| 730 | fields: { |
| 731 | authConfigCreation: { |
| 732 | required: [{ name: 'client_id' }], |
| 733 | optional: [], |
| 734 | }, |
| 735 | connectedAccountInitiation: { |
| 736 | required: [], |
| 737 | optional: [], |
| 738 | }, |
| 739 | }, |
| 740 | }, |
| 741 | { |
| 742 | name: 'api_key', |
| 743 | mode: 'API_KEY', |
| 744 | fields: { |
| 745 | authConfigCreation: { |
| 746 | required: [{ name: 'api_key' }], |
| 747 | optional: [], |
| 748 | }, |
| 749 | connectedAccountInitiation: { |
| 750 | required: [], |
| 751 | optional: [], |
| 752 | }, |
| 753 | }, |
| 754 | }, |
| 755 | ], |
| 756 | }; |
| 757 | (Toolkits.prototype as unknown as ToolkitsPrototypeOverride).getToolkitBySlug = vi |
| 758 | .fn() |
| 759 | .mockResolvedValueOnce(multiToolkit); |
| 760 | const result = await toolkits.getAuthConfigCreationFields( |
| 761 | toolkitSlug, |
| 762 | AuthSchemeTypes.API_KEY, |
| 763 | { |
| 764 | requiredOnly: true, |
| 765 | } |
| 766 | ); |
| 767 | expect(result).toEqual([{ name: 'api_key', required: true }]); |
| 768 | }); |
| 769 | |
| 770 | it('throws if no authConfigDetails', async () => { |
| 771 | (Toolkits.prototype as unknown as ToolkitsPrototypeOverride).getToolkitBySlug = vi |
| 772 | .fn() |
| 773 | .mockResolvedValueOnce({}); |
| 774 | await expect( |
| 775 | toolkits.getAuthConfigCreationFields(toolkitSlug, AuthSchemeTypes.API_KEY, { |
| 776 | requiredOnly: true, |
| 777 | }) |
| 778 | ).rejects.toThrow(ComposioAuthConfigNotFoundError); |
| 779 | }); |
| 780 | }); |
| 781 | |
| 782 | describe('getConnectedAccountInitiationFields', () => { |
| 783 | const toolkitSlug = 'test-toolkit'; |
| 784 | const mockToolkit = { |
| 785 | authConfigDetails: [ |
| 786 | { |
| 787 | name: 'oauth2', |
| 788 | mode: 'OAUTH2', |
| 789 | fields: { |
| 790 | authConfigCreation: { |
| 791 | required: [], |
| 792 | optional: [], |
| 793 | }, |
| 794 | connectedAccountInitiation: { |
| 795 | required: [{ name: 'code' }], |
| 796 | optional: [{ name: 'state' }], |
| 797 | }, |
| 798 | }, |
| 799 | }, |
| 800 | ], |
| 801 | }; |
| 802 | |
| 803 | beforeEach(() => { |
| 804 | vi.spyOn(Toolkits.prototype as unknown, 'getToolkitBySlug').mockResolvedValue( |
| 805 | mockToolkit as unknown |
| 806 | ); |
| 807 | }); |
| 808 | |
| 809 | it('returns all fields when requiredOnly is false', async () => { |
| 810 | const result = await toolkits.getConnectedAccountInitiationFields(toolkitSlug, 'OAUTH2', { |
| 811 | requiredOnly: false, |
| 812 | }); |
| 813 | expect(result).toEqual([ |
| 814 | { name: 'code', required: true }, |
| 815 | { name: 'state', required: false }, |
| 816 | ]); |
| 817 | }); |
| 818 | |
| 819 | it('returns only required fields when requiredOnly is true', async () => { |
| 820 | const result = await toolkits.getConnectedAccountInitiationFields(toolkitSlug, 'OAUTH2', { |
| 821 | requiredOnly: true, |
| 822 | }); |
| 823 | expect(result).toEqual([{ name: 'code', required: true }]); |
| 824 | }); |
| 825 | |
| 826 | it('selects the correct auth config by authScheme', async () => { |
| 827 | const multiToolkit = { |
| 828 | authConfigDetails: [ |
| 829 | { |
| 830 | name: 'oauth2', |
| 831 | mode: 'OAUTH2', |
| 832 | fields: { |
| 833 | authConfigCreation: { |
| 834 | required: [], |
| 835 | optional: [], |
| 836 | }, |
| 837 | connectedAccountInitiation: { |
| 838 | required: [{ name: 'code' }], |
| 839 | optional: [], |
| 840 | }, |
| 841 | }, |
| 842 | }, |
| 843 | { |
| 844 | name: 'api_key', |
| 845 | mode: 'API_KEY', |
| 846 | fields: { |
| 847 | authConfigCreation: { |
| 848 | required: [], |
| 849 | optional: [], |
| 850 | }, |
| 851 | connectedAccountInitiation: { |
| 852 | required: [{ name: 'api_key_code' }], |
| 853 | optional: [], |
| 854 | }, |
| 855 | }, |
| 856 | }, |
| 857 | ], |
| 858 | }; |
| 859 | (Toolkits.prototype as unknown as ToolkitsPrototypeOverride).getToolkitBySlug = vi |
| 860 | .fn() |
| 861 | .mockResolvedValueOnce(multiToolkit); |
| 862 | const result = await toolkits.getConnectedAccountInitiationFields( |
| 863 | toolkitSlug, |
| 864 | AuthSchemeTypes.API_KEY, |
| 865 | { |
| 866 | requiredOnly: true, |
| 867 | } |
| 868 | ); |
| 869 | expect(result).toEqual([{ name: 'api_key_code', required: true }]); |
| 870 | }); |
| 871 | |
| 872 | it('throws if no authConfigDetails', async () => { |
| 873 | (Toolkits.prototype as unknown as ToolkitsPrototypeOverride).getToolkitBySlug = vi |
| 874 | .fn() |
| 875 | .mockResolvedValueOnce({}); |
| 876 | await expect( |
| 877 | toolkits.getConnectedAccountInitiationFields(toolkitSlug, AuthSchemeTypes.API_KEY, { |
| 878 | requiredOnly: true, |
| 879 | }) |
| 880 | ).rejects.toThrow(ComposioAuthConfigNotFoundError); |
| 881 | }); |
| 882 | }); |
| 883 | }); |