|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + ConnectorOAuth2Schema, |
| 4 | + ConnectorAPIKeySchema, |
| 5 | + ConnectorBasicAuthSchema, |
| 6 | + ConnectorBearerAuthSchema, |
| 7 | + ConnectorNoAuthSchema, |
| 8 | + ConnectorAuthConfigSchema, |
| 9 | +} from './connector-auth.zod'; |
| 10 | + |
| 11 | +describe('ConnectorOAuth2Schema', () => { |
| 12 | + const validOAuth2 = { |
| 13 | + type: 'oauth2' as const, |
| 14 | + authorizationUrl: 'https://auth.example.com/authorize', |
| 15 | + tokenUrl: 'https://auth.example.com/token', |
| 16 | + clientId: 'my-client-id', |
| 17 | + clientSecret: 'my-client-secret', |
| 18 | + }; |
| 19 | + |
| 20 | + it('should accept valid minimal oauth2 config', () => { |
| 21 | + const result = ConnectorOAuth2Schema.parse(validOAuth2); |
| 22 | + expect(result.type).toBe('oauth2'); |
| 23 | + expect(result.authorizationUrl).toBe('https://auth.example.com/authorize'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should accept oauth2 with all optional fields', () => { |
| 27 | + const result = ConnectorOAuth2Schema.parse({ |
| 28 | + ...validOAuth2, |
| 29 | + scopes: ['read', 'write'], |
| 30 | + redirectUri: 'https://app.example.com/callback', |
| 31 | + refreshToken: 'refresh-token-value', |
| 32 | + tokenExpiry: 1700000000, |
| 33 | + }); |
| 34 | + expect(result.scopes).toEqual(['read', 'write']); |
| 35 | + expect(result.redirectUri).toBe('https://app.example.com/callback'); |
| 36 | + expect(result.refreshToken).toBe('refresh-token-value'); |
| 37 | + expect(result.tokenExpiry).toBe(1700000000); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should have optional fields as undefined when not provided', () => { |
| 41 | + const result = ConnectorOAuth2Schema.parse(validOAuth2); |
| 42 | + expect(result.scopes).toBeUndefined(); |
| 43 | + expect(result.redirectUri).toBeUndefined(); |
| 44 | + expect(result.refreshToken).toBeUndefined(); |
| 45 | + expect(result.tokenExpiry).toBeUndefined(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should reject invalid authorizationUrl', () => { |
| 49 | + expect(() => |
| 50 | + ConnectorOAuth2Schema.parse({ ...validOAuth2, authorizationUrl: 'not-a-url' }), |
| 51 | + ).toThrow(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should reject invalid tokenUrl', () => { |
| 55 | + expect(() => |
| 56 | + ConnectorOAuth2Schema.parse({ ...validOAuth2, tokenUrl: 'not-a-url' }), |
| 57 | + ).toThrow(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should reject missing required fields', () => { |
| 61 | + expect(() => ConnectorOAuth2Schema.parse({ type: 'oauth2' })).toThrow(); |
| 62 | + expect(() => |
| 63 | + ConnectorOAuth2Schema.parse({ type: 'oauth2', authorizationUrl: 'https://a.com' }), |
| 64 | + ).toThrow(); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe('ConnectorAPIKeySchema', () => { |
| 69 | + it('should accept valid api-key config with default headerName', () => { |
| 70 | + const result = ConnectorAPIKeySchema.parse({ |
| 71 | + type: 'api-key', |
| 72 | + key: 'my-api-key-123', |
| 73 | + }); |
| 74 | + expect(result.type).toBe('api-key'); |
| 75 | + expect(result.key).toBe('my-api-key-123'); |
| 76 | + expect(result.headerName).toBe('X-API-Key'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should accept custom headerName', () => { |
| 80 | + const result = ConnectorAPIKeySchema.parse({ |
| 81 | + type: 'api-key', |
| 82 | + key: 'key123', |
| 83 | + headerName: 'Authorization', |
| 84 | + }); |
| 85 | + expect(result.headerName).toBe('Authorization'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should accept optional paramName', () => { |
| 89 | + const result = ConnectorAPIKeySchema.parse({ |
| 90 | + type: 'api-key', |
| 91 | + key: 'key123', |
| 92 | + paramName: 'api_key', |
| 93 | + }); |
| 94 | + expect(result.paramName).toBe('api_key'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should reject missing key', () => { |
| 98 | + expect(() => ConnectorAPIKeySchema.parse({ type: 'api-key' })).toThrow(); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +describe('ConnectorBasicAuthSchema', () => { |
| 103 | + it('should accept valid basic auth', () => { |
| 104 | + const result = ConnectorBasicAuthSchema.parse({ |
| 105 | + type: 'basic', |
| 106 | + username: 'admin', |
| 107 | + password: 'secret', |
| 108 | + }); |
| 109 | + expect(result.type).toBe('basic'); |
| 110 | + expect(result.username).toBe('admin'); |
| 111 | + expect(result.password).toBe('secret'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should reject missing username', () => { |
| 115 | + expect(() => |
| 116 | + ConnectorBasicAuthSchema.parse({ type: 'basic', password: 'secret' }), |
| 117 | + ).toThrow(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should reject missing password', () => { |
| 121 | + expect(() => |
| 122 | + ConnectorBasicAuthSchema.parse({ type: 'basic', username: 'admin' }), |
| 123 | + ).toThrow(); |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
| 127 | +describe('ConnectorBearerAuthSchema', () => { |
| 128 | + it('should accept valid bearer auth', () => { |
| 129 | + const result = ConnectorBearerAuthSchema.parse({ |
| 130 | + type: 'bearer', |
| 131 | + token: 'my-bearer-token', |
| 132 | + }); |
| 133 | + expect(result.type).toBe('bearer'); |
| 134 | + expect(result.token).toBe('my-bearer-token'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should reject missing token', () => { |
| 138 | + expect(() => ConnectorBearerAuthSchema.parse({ type: 'bearer' })).toThrow(); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +describe('ConnectorNoAuthSchema', () => { |
| 143 | + it('should accept valid no-auth config', () => { |
| 144 | + const result = ConnectorNoAuthSchema.parse({ type: 'none' }); |
| 145 | + expect(result.type).toBe('none'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should reject wrong type', () => { |
| 149 | + expect(() => ConnectorNoAuthSchema.parse({ type: 'other' })).toThrow(); |
| 150 | + }); |
| 151 | +}); |
| 152 | + |
| 153 | +describe('ConnectorAuthConfigSchema', () => { |
| 154 | + it('should accept oauth2 via discriminated union', () => { |
| 155 | + const result = ConnectorAuthConfigSchema.parse({ |
| 156 | + type: 'oauth2', |
| 157 | + authorizationUrl: 'https://auth.example.com/authorize', |
| 158 | + tokenUrl: 'https://auth.example.com/token', |
| 159 | + clientId: 'id', |
| 160 | + clientSecret: 'secret', |
| 161 | + }); |
| 162 | + expect(result.type).toBe('oauth2'); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should accept api-key via discriminated union', () => { |
| 166 | + const result = ConnectorAuthConfigSchema.parse({ |
| 167 | + type: 'api-key', |
| 168 | + key: 'key123', |
| 169 | + }); |
| 170 | + expect(result.type).toBe('api-key'); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should accept basic via discriminated union', () => { |
| 174 | + const result = ConnectorAuthConfigSchema.parse({ |
| 175 | + type: 'basic', |
| 176 | + username: 'user', |
| 177 | + password: 'pass', |
| 178 | + }); |
| 179 | + expect(result.type).toBe('basic'); |
| 180 | + }); |
| 181 | + |
| 182 | + it('should accept bearer via discriminated union', () => { |
| 183 | + const result = ConnectorAuthConfigSchema.parse({ |
| 184 | + type: 'bearer', |
| 185 | + token: 'tok', |
| 186 | + }); |
| 187 | + expect(result.type).toBe('bearer'); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should accept none via discriminated union', () => { |
| 191 | + const result = ConnectorAuthConfigSchema.parse({ type: 'none' }); |
| 192 | + expect(result.type).toBe('none'); |
| 193 | + }); |
| 194 | + |
| 195 | + it('should reject unknown auth type', () => { |
| 196 | + expect(() => ConnectorAuthConfigSchema.parse({ type: 'custom' })).toThrow(); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should reject missing type', () => { |
| 200 | + expect(() => ConnectorAuthConfigSchema.parse({ key: 'value' })).toThrow(); |
| 201 | + }); |
| 202 | +}); |
0 commit comments