@devries-cameron20 / composio Mirror · read-only
Fork 0

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

next
ts 343 lines 9.22 KB
Raw
1 import { describe, it, expect } from 'vitest';
2 import { AuthScheme } from '../../src/models/AuthScheme';
3 import { AuthSchemeTypes } from '../../src/types/authConfigs.types';
4 import {
5 ConnectionStatuses,
6 ConnectionDataSchema,
7 } from '../../src/types/connectedAccountAuthStates.types';
8
9 describe('AuthScheme', () => {
10 describe('OAuth2', () => {
11 it('should set ACTIVE status when access_token is provided', () => {
12 const params = {
13 access_token: 'test_token',
14 token_type: 'Bearer',
15 };
16
17 const result = AuthScheme.OAuth2(params);
18
19 expect(result).toEqual({
20 authScheme: AuthSchemeTypes.OAUTH2,
21 val: {
22 status: ConnectionStatuses.ACTIVE,
23 access_token: 'test_token',
24 token_type: 'Bearer',
25 },
26 });
27
28 // Verify Zod schema validation
29 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
30 });
31
32 it('should set ACTIVE status with all optional fields when access_token is provided', () => {
33 const params = {
34 access_token: 'test_token',
35 token_type: 'Bearer',
36 id_token: 'id_token',
37 refresh_token: 'refresh_token',
38 expires_in: 3600,
39 scope: 'read write',
40 webhook_signature: 'signature',
41 authed_user: {
42 access_token: 'user_token',
43 scope: 'user_scope',
44 },
45 };
46
47 const result = AuthScheme.OAuth2(params);
48
49 expect(result).toEqual({
50 authScheme: AuthSchemeTypes.OAUTH2,
51 val: {
52 status: ConnectionStatuses.ACTIVE,
53 ...params,
54 },
55 });
56
57 // Verify Zod schema validation
58 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
59 });
60
61 it('should set INITIALIZING status when no access_token is provided', () => {
62 const result = AuthScheme.OAuth2({});
63
64 expect(result).toEqual({
65 authScheme: AuthSchemeTypes.OAUTH2,
66 val: {
67 status: ConnectionStatuses.INITIALIZING,
68 },
69 });
70
71 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
72 });
73
74 it('should set INITIALIZING status when access_token is empty string', () => {
75 const result = AuthScheme.OAuth2({ access_token: '' });
76
77 expect(result).toEqual({
78 authScheme: AuthSchemeTypes.OAUTH2,
79 val: {
80 status: ConnectionStatuses.INITIALIZING,
81 access_token: '',
82 },
83 });
84
85 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
86 });
87
88 it('should honor explicit status override from user', () => {
89 const result = AuthScheme.OAuth2({
90 access_token: 'test_token',
91 status: ConnectionStatuses.INITIALIZING,
92 });
93
94 expect(result.val.status).toBe(ConnectionStatuses.INITIALIZING);
95 });
96 });
97
98 describe('OAuth1', () => {
99 it('should set INITIALIZING status when only oauth_token is provided (no secret)', () => {
100 const params = {
101 oauth_token: 'test_token',
102 };
103
104 const result = AuthScheme.OAuth1(params);
105
106 expect(result).toEqual({
107 authScheme: AuthSchemeTypes.OAUTH1,
108 val: {
109 status: ConnectionStatuses.INITIALIZING,
110 oauth_token: 'test_token',
111 },
112 });
113
114 // Verify Zod schema validation
115 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
116 });
117
118 it('should set INITIALIZING status with optional fields but no oauth_token_secret', () => {
119 const params = {
120 oauth_token: 'test_token',
121 consumer_key: 'consumer_key',
122 redirectUrl: 'http://example.com/callback',
123 callback_url: 'http://example.com/callback',
124 };
125
126 const result = AuthScheme.OAuth1(params);
127
128 expect(result).toEqual({
129 authScheme: AuthSchemeTypes.OAUTH1,
130 val: {
131 status: ConnectionStatuses.INITIALIZING,
132 ...params,
133 },
134 });
135
136 // Verify Zod schema validation
137 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
138 });
139
140 it('should set ACTIVE status when both oauth_token and oauth_token_secret are provided', () => {
141 const params = {
142 oauth_token: 'test_token',
143 oauth_token_secret: 'test_secret',
144 };
145
146 const result = AuthScheme.OAuth1(params);
147
148 expect(result).toEqual({
149 authScheme: AuthSchemeTypes.OAUTH1,
150 val: {
151 status: ConnectionStatuses.ACTIVE,
152 oauth_token: 'test_token',
153 oauth_token_secret: 'test_secret',
154 },
155 });
156
157 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
158 });
159
160 it('should set INITIALIZING status when oauth_token is empty', () => {
161 const result = AuthScheme.OAuth1({
162 oauth_token: '',
163 oauth_token_secret: 'test_secret',
164 });
165
166 expect(result.val.status).toBe(ConnectionStatuses.INITIALIZING);
167 });
168
169 it('should honor explicit status override from user', () => {
170 const result = AuthScheme.OAuth1({
171 oauth_token: 'test_token',
172 oauth_token_secret: 'test_secret',
173 status: ConnectionStatuses.INITIALIZING,
174 });
175
176 expect(result.val.status).toBe(ConnectionStatuses.INITIALIZING);
177 });
178 });
179
180 describe('APIKey', () => {
181 it('should create APIKey connection data', () => {
182 const params = {
183 api_key: 'test_api_key',
184 };
185
186 const result = AuthScheme.APIKey(params);
187
188 expect(result).toEqual({
189 authScheme: AuthSchemeTypes.API_KEY,
190 val: {
191 status: ConnectionStatuses.ACTIVE,
192 api_key: 'test_api_key',
193 },
194 });
195
196 // Verify Zod schema validation
197 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
198 });
199 });
200
201 describe('Basic', () => {
202 it('should create Basic connection data', () => {
203 const params = {
204 username: 'test_user',
205 password: 'test_pass',
206 };
207
208 const result = AuthScheme.Basic(params);
209
210 expect(result).toEqual({
211 authScheme: AuthSchemeTypes.BASIC,
212 val: {
213 status: ConnectionStatuses.ACTIVE,
214 username: 'test_user',
215 password: 'test_pass',
216 },
217 });
218
219 // Verify Zod schema validation
220 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
221 });
222 });
223
224 describe('BearerToken', () => {
225 it('should create BearerToken connection data', () => {
226 const params = {
227 token: 'test_token',
228 };
229
230 const result = AuthScheme.BearerToken(params);
231
232 expect(result).toEqual({
233 authScheme: AuthSchemeTypes.BEARER_TOKEN,
234 val: {
235 status: ConnectionStatuses.ACTIVE,
236 token: 'test_token',
237 },
238 });
239
240 // Verify Zod schema validation
241 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
242 });
243 });
244
245 describe('GoogleServiceAccount', () => {
246 it('should create GoogleServiceAccount connection data', () => {
247 const params = {
248 credentials_json: '{"key": "value"}',
249 };
250
251 const result = AuthScheme.GoogleServiceAccount(params);
252
253 expect(result).toEqual({
254 authScheme: AuthSchemeTypes.GOOGLE_SERVICE_ACCOUNT,
255 val: {
256 status: ConnectionStatuses.ACTIVE,
257 credentials_json: '{"key": "value"}',
258 },
259 });
260
261 // Verify Zod schema validation
262 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
263 });
264 });
265
266 describe('NoAuth', () => {
267 it('should create NoAuth connection data', () => {
268 const result = AuthScheme.NoAuth({});
269
270 expect(result).toEqual({
271 authScheme: AuthSchemeTypes.NO_AUTH,
272 val: {
273 status: ConnectionStatuses.ACTIVE,
274 },
275 });
276
277 // Verify Zod schema validation
278 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
279 });
280 });
281
282 describe('BasicWithJWT', () => {
283 it('should create BasicWithJWT connection data', () => {
284 const params = {
285 username: 'test_user',
286 password: 'test_pass',
287 };
288
289 const result = AuthScheme.BasicWithJWT(params);
290
291 expect(result).toEqual({
292 authScheme: AuthSchemeTypes.BASIC_WITH_JWT,
293 val: {
294 status: ConnectionStatuses.ACTIVE,
295 username: 'test_user',
296 password: 'test_pass',
297 },
298 });
299
300 // Verify Zod schema validation
301 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
302 });
303 });
304
305 describe('CalcomAuth', () => {
306 it('should create CalcomAuth connection data', () => {
307 const result = AuthScheme.CalcomAuth({});
308
309 expect(result).toEqual({
310 authScheme: AuthSchemeTypes.CALCOM_AUTH,
311 val: {
312 status: ConnectionStatuses.ACTIVE,
313 },
314 });
315
316 // Verify Zod schema validation
317 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
318 });
319 });
320
321 describe('BillcomAuth', () => {
322 it('should create BillcomAuth connection data', () => {
323 const params = {
324 sessionId: 'test_session',
325 devKey: 'test_key',
326 };
327
328 const result = AuthScheme.BillcomAuth(params);
329
330 expect(result).toEqual({
331 authScheme: AuthSchemeTypes.BILLCOM_AUTH,
332 val: {
333 status: ConnectionStatuses.ACTIVE,
334 sessionId: 'test_session',
335 devKey: 'test_key',
336 },
337 });
338
339 // Verify Zod schema validation
340 expect(() => ConnectionDataSchema.parse(result)).not.toThrow();
341 });
342 });
343 });