|
1 | 1 | import { http, HttpResponse } from 'msw'; |
2 | 2 | import { describe, expect, expectTypeOf, it } from 'vitest'; |
3 | 3 |
|
| 4 | +import type { EnterpriseConnectionTestRun as PublicEnterpriseConnectionTestRun } from '../../index'; |
4 | 5 | import { server, validateHeaders } from '../../mock-server'; |
5 | 6 | import type { |
6 | 7 | CreateEnterpriseConnectionParams, |
7 | 8 | EnterpriseConnectionSamlLoginHintParams, |
8 | 9 | } from '../endpoints/EnterpriseConnectionApi'; |
9 | 10 | import { createBackendApiClient } from '../factory'; |
| 11 | +import { EnterpriseConnectionTestRun } from '../resources'; |
10 | 12 |
|
11 | 13 | describe('EnterpriseConnectionAPI', () => { |
12 | 14 | const apiClient = createBackendApiClient({ |
@@ -410,4 +412,135 @@ describe('EnterpriseConnectionAPI', () => { |
410 | 412 | expect(response.name).toBe('Clerk'); |
411 | 413 | }); |
412 | 414 | }); |
| 415 | + |
| 416 | + describe('createEnterpriseConnectionTestRun', () => { |
| 417 | + it('starts a test run and returns its URL', async () => { |
| 418 | + server.use( |
| 419 | + http.post( |
| 420 | + 'https://api.clerk.test/v1/enterprise_connections/entconn_123/test_runs', |
| 421 | + validateHeaders(() => HttpResponse.json({ url: 'https://clerk.example.com/v1/test_runs/start?token=abc' })), |
| 422 | + ), |
| 423 | + ); |
| 424 | + |
| 425 | + const response = await apiClient.enterpriseConnections.createEnterpriseConnectionTestRun('entconn_123'); |
| 426 | + |
| 427 | + expect(response).toEqual({ url: 'https://clerk.example.com/v1/test_runs/start?token=abc' }); |
| 428 | + }); |
| 429 | + |
| 430 | + it('requires an enterprise connection ID', async () => { |
| 431 | + await expect(apiClient.enterpriseConnections.createEnterpriseConnectionTestRun('')).rejects.toThrow(); |
| 432 | + }); |
| 433 | + }); |
| 434 | + |
| 435 | + describe('getEnterpriseConnectionTestRunList', () => { |
| 436 | + const mockTestRunListResponse = { |
| 437 | + data: [ |
| 438 | + { |
| 439 | + id: 'ectr_1', |
| 440 | + status: 'success', |
| 441 | + connection_type: 'saml', |
| 442 | + parsed_user_info: { |
| 443 | + email_address: 'jane@clerk.dev', |
| 444 | + first_name: 'Jane', |
| 445 | + last_name: 'Doe', |
| 446 | + user_id: 'jane', |
| 447 | + }, |
| 448 | + logs: [ |
| 449 | + { level: 'info', code: 'response_received', short_message: 'Received', message: 'SAML response received' }, |
| 450 | + ], |
| 451 | + saml: { |
| 452 | + saml_request: 'PHNhbWxwOkF1dGhuUmVxdWVzdA==', |
| 453 | + saml_response: 'PHNhbWxwOlJlc3BvbnNl', |
| 454 | + relay_state: 'rs', |
| 455 | + }, |
| 456 | + oauth: null, |
| 457 | + created_at: 1672531200000, |
| 458 | + }, |
| 459 | + { |
| 460 | + id: 'ectr_2', |
| 461 | + status: 'failed', |
| 462 | + connection_type: 'oauth', |
| 463 | + parsed_user_info: null, |
| 464 | + oauth: { id_token: 'id', access_token: 'access', user_info: '{}' }, |
| 465 | + created_at: 1672531100000, |
| 466 | + }, |
| 467 | + ], |
| 468 | + total_count: 2, |
| 469 | + }; |
| 470 | + |
| 471 | + it('fetches test runs as resources, with status filters and pagination in the query', async () => { |
| 472 | + let capturedRequestUrl: string | null = null; |
| 473 | + server.use( |
| 474 | + http.get( |
| 475 | + 'https://api.clerk.test/v1/enterprise_connections/entconn_123/test_runs', |
| 476 | + validateHeaders(({ request }) => { |
| 477 | + capturedRequestUrl = request.url; |
| 478 | + return HttpResponse.json(mockTestRunListResponse); |
| 479 | + }), |
| 480 | + ), |
| 481 | + ); |
| 482 | + |
| 483 | + const response = await apiClient.enterpriseConnections.getEnterpriseConnectionTestRunList('entconn_123', { |
| 484 | + status: ['success', 'failed'], |
| 485 | + limit: 10, |
| 486 | + offset: 5, |
| 487 | + }); |
| 488 | + |
| 489 | + expect(capturedRequestUrl).toBeTruthy(); |
| 490 | + const url = new URL(capturedRequestUrl!); |
| 491 | + expect(url.searchParams.getAll('status')).toEqual(['success', 'failed']); |
| 492 | + expect(url.searchParams.get('limit')).toBe('10'); |
| 493 | + expect(url.searchParams.get('offset')).toBe('5'); |
| 494 | + |
| 495 | + expect(response.totalCount).toBe(2); |
| 496 | + expect(response.data).toHaveLength(2); |
| 497 | + expectTypeOf(response.data).toEqualTypeOf<PublicEnterpriseConnectionTestRun[]>(); |
| 498 | + |
| 499 | + const [saml, oauth] = response.data; |
| 500 | + expect(saml).toBeInstanceOf(EnterpriseConnectionTestRun); |
| 501 | + expect(saml.status).toBe('success'); |
| 502 | + expect(saml.connectionType).toBe('saml'); |
| 503 | + expect(saml.createdAt).toBe(1672531200000); |
| 504 | + expect(saml.parsedUserInfo).toEqual({ |
| 505 | + emailAddress: 'jane@clerk.dev', |
| 506 | + firstName: 'Jane', |
| 507 | + lastName: 'Doe', |
| 508 | + userId: 'jane', |
| 509 | + }); |
| 510 | + expect(saml.logs).toEqual([ |
| 511 | + { level: 'info', code: 'response_received', shortMessage: 'Received', message: 'SAML response received' }, |
| 512 | + ]); |
| 513 | + expect(saml.saml?.samlResponse).toBe('PHNhbWxwOlJlc3BvbnNl'); |
| 514 | + expect(saml.oauth).toBeNull(); |
| 515 | + |
| 516 | + expect(oauth.status).toBe('failed'); |
| 517 | + expect(oauth.parsedUserInfo).toBeNull(); |
| 518 | + expect(oauth.logs).toEqual([]); |
| 519 | + expect(oauth.saml).toBeNull(); |
| 520 | + expect(oauth.oauth?.idToken).toBe('id'); |
| 521 | + }); |
| 522 | + |
| 523 | + it('sends no query params when called without filters', async () => { |
| 524 | + let capturedRequestUrl: string | null = null; |
| 525 | + server.use( |
| 526 | + http.get( |
| 527 | + 'https://api.clerk.test/v1/enterprise_connections/entconn_123/test_runs', |
| 528 | + validateHeaders(({ request }) => { |
| 529 | + capturedRequestUrl = request.url; |
| 530 | + return HttpResponse.json({ data: [], total_count: 0 }); |
| 531 | + }), |
| 532 | + ), |
| 533 | + ); |
| 534 | + |
| 535 | + const response = await apiClient.enterpriseConnections.getEnterpriseConnectionTestRunList('entconn_123'); |
| 536 | + |
| 537 | + expect(capturedRequestUrl).toBeTruthy(); |
| 538 | + expect(new URL(capturedRequestUrl!).search).toBe(''); |
| 539 | + expect(response).toEqual({ data: [], totalCount: 0 }); |
| 540 | + }); |
| 541 | + |
| 542 | + it('requires an enterprise connection ID', async () => { |
| 543 | + await expect(apiClient.enterpriseConnections.getEnterpriseConnectionTestRunList('')).rejects.toThrow(); |
| 544 | + }); |
| 545 | + }); |
413 | 546 | }); |
0 commit comments