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 } from 'vitest'; |
| 2 | import { |
| 3 | ConnectionDataSchema, |
| 4 | ConnectionStatuses, |
| 5 | Oauth2RevokedConnectionDataSchema, |
| 6 | } from '../../src/types/connectedAccountAuthStates.types'; |
| 7 | import { transformConnectedAccountResponse } from '../../src/utils/transformers/connectedAccounts'; |
| 8 | import { AuthSchemeTypes } from '../../src/types/authConfigs.types'; |
| 9 | |
| 10 | describe('connectedAccountAuthStates — REVOKED arms', () => { |
| 11 | describe('Oauth2RevokedConnectionDataSchema', () => { |
| 12 | it('parses a payload with only the status discriminator', () => { |
| 13 | const parsed = Oauth2RevokedConnectionDataSchema.parse({ |
| 14 | status: ConnectionStatuses.REVOKED, |
| 15 | }); |
| 16 | |
| 17 | expect(parsed.status).toBe(ConnectionStatuses.REVOKED); |
| 18 | expect(parsed.revoked_at).toBeUndefined(); |
| 19 | }); |
| 20 | |
| 21 | it('parses a payload with optional revoked_at', () => { |
| 22 | const parsed = Oauth2RevokedConnectionDataSchema.parse({ |
| 23 | status: ConnectionStatuses.REVOKED, |
| 24 | revoked_at: '2026-05-01T12:34:56Z', |
| 25 | }); |
| 26 | |
| 27 | expect(parsed.revoked_at).toBe('2026-05-01T12:34:56Z'); |
| 28 | }); |
| 29 | |
| 30 | it('rejects when status is not REVOKED', () => { |
| 31 | expect(() => |
| 32 | Oauth2RevokedConnectionDataSchema.parse({ |
| 33 | status: ConnectionStatuses.ACTIVE, |
| 34 | }) |
| 35 | ).toThrow(); |
| 36 | }); |
| 37 | }); |
| 38 | |
| 39 | describe('ConnectionDataSchema discriminated union', () => { |
| 40 | it('accepts REVOKED for OAUTH2 and preserves auth-scheme metadata', () => { |
| 41 | const parsed = ConnectionDataSchema.parse({ |
| 42 | authScheme: AuthSchemeTypes.OAUTH2, |
| 43 | val: { |
| 44 | status: ConnectionStatuses.REVOKED, |
| 45 | revoked_at: '2026-05-01T00:00:00Z', |
| 46 | }, |
| 47 | }); |
| 48 | |
| 49 | expect(parsed.authScheme).toBe(AuthSchemeTypes.OAUTH2); |
| 50 | expect(parsed.val.status).toBe(ConnectionStatuses.REVOKED); |
| 51 | }); |
| 52 | |
| 53 | it('accepts REVOKED for S2S_OAUTH2 (preempts the queued Apollo rollout)', () => { |
| 54 | const parsed = ConnectionDataSchema.parse({ |
| 55 | authScheme: AuthSchemeTypes.S2S_OAUTH2, |
| 56 | val: { |
| 57 | status: ConnectionStatuses.REVOKED, |
| 58 | revoked_at: '2026-05-01T00:00:00Z', |
| 59 | }, |
| 60 | }); |
| 61 | |
| 62 | expect(parsed.authScheme).toBe(AuthSchemeTypes.S2S_OAUTH2); |
| 63 | expect(parsed.val.status).toBe(ConnectionStatuses.REVOKED); |
| 64 | }); |
| 65 | }); |
| 66 | |
| 67 | describe('transformConnectedAccountResponse — REVOKED preserves state', () => { |
| 68 | const baseResponse = { |
| 69 | id: 'conn_123', |
| 70 | auth_config: { |
| 71 | id: 'auth_123', |
| 72 | auth_scheme: 'OAUTH2', |
| 73 | is_composio_managed: true, |
| 74 | is_disabled: false, |
| 75 | }, |
| 76 | user_id: 'user_123', |
| 77 | data: {}, |
| 78 | params: {}, |
| 79 | is_disabled: false, |
| 80 | created_at: '2023-01-01T00:00:00Z', |
| 81 | updated_at: '2023-01-01T00:00:00Z', |
| 82 | status_reason: 'token revoked at provider', |
| 83 | toolkit: { slug: 'gmail' }, |
| 84 | test_request_endpoint: '', |
| 85 | } as const; |
| 86 | |
| 87 | it('does not drop state for an OAUTH2 REVOKED payload', () => { |
| 88 | const transformed = transformConnectedAccountResponse({ |
| 89 | ...baseResponse, |
| 90 | status: ConnectionStatuses.REVOKED, |
| 91 | state: { |
| 92 | authScheme: AuthSchemeTypes.OAUTH2, |
| 93 | val: { |
| 94 | status: ConnectionStatuses.REVOKED, |
| 95 | revoked_at: '2026-05-01T00:00:00Z', |
| 96 | }, |
| 97 | }, |
| 98 | } as unknown as Parameters<typeof transformConnectedAccountResponse>[0]); |
| 99 | |
| 100 | expect(transformed.status).toBe(ConnectionStatuses.REVOKED); |
| 101 | expect(transformed.state).toBeDefined(); |
| 102 | expect(transformed.state?.authScheme).toBe(AuthSchemeTypes.OAUTH2); |
| 103 | expect(transformed.state?.val.status).toBe(ConnectionStatuses.REVOKED); |
| 104 | }); |
| 105 | |
| 106 | it('does not drop state for an S2S_OAUTH2 REVOKED payload', () => { |
| 107 | const transformed = transformConnectedAccountResponse({ |
| 108 | ...baseResponse, |
| 109 | auth_config: { ...baseResponse.auth_config, auth_scheme: 'S2S_OAUTH2' }, |
| 110 | status: ConnectionStatuses.REVOKED, |
| 111 | state: { |
| 112 | authScheme: AuthSchemeTypes.S2S_OAUTH2, |
| 113 | val: { |
| 114 | status: ConnectionStatuses.REVOKED, |
| 115 | revoked_at: '2026-05-01T00:00:00Z', |
| 116 | }, |
| 117 | }, |
| 118 | } as unknown as Parameters<typeof transformConnectedAccountResponse>[0]); |
| 119 | |
| 120 | expect(transformed.state).toBeDefined(); |
| 121 | expect(transformed.state?.authScheme).toBe(AuthSchemeTypes.S2S_OAUTH2); |
| 122 | }); |
| 123 | }); |
| 124 | |
| 125 | describe('ConnectionStatuses enum closure', () => { |
| 126 | // Forces deliberate updates when Apollo adds a status. |
| 127 | it('enumerates exactly the values this PR contracts for', () => { |
| 128 | expect(new Set(Object.values(ConnectionStatuses))).toEqual( |
| 129 | new Set(['INITIALIZING', 'INITIATED', 'ACTIVE', 'FAILED', 'EXPIRED', 'INACTIVE', 'REVOKED']) |
| 130 | ); |
| 131 | }); |
| 132 | }); |
| 133 | }); |