@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 281 lines 8.95 KB
Raw
1 import { assert, describe, expect, layer } from '@effect/vitest';
2 import { isUsageFailure } from 'src/commands/errors';
3 import type { AuthConfigCreateParams } from '@composio/client/resources/auth-configs';
4 import { ConfigProvider, Effect } from 'effect';
5 import { cli, TestLive, MockConsole } from 'test/__utils__';
6 import type { TestLiveInput } from 'test/__utils__/services/test-layer';
7 import {
8 DIRECTORY_PROJECT_SCOPE,
9 DEFAULT_PROJECT_SCOPE,
10 defaultDeveloperProjectAccountData,
11 inDeveloperProjectDirectory,
12 loginWithoutDeveloperProject,
13 OTHER_DEVELOPER_PROJECT,
14 OTHER_PROJECT_SCOPE,
15 runRecordingRepositoryScopes,
16 } from 'test/__utils__/layers/developer-project';
17
18 const testConfigProvider = ConfigProvider.fromEnv({
19 env: { COMPOSIO_USER_API_KEY: 'test_api_key' },
20 });
21
22 // Logged in, in a directory set up for a developer project.
23 const inProject = { ...inDeveloperProjectDirectory, baseConfigProvider: testConfigProvider };
24
25 const dangerousDevConfig = {
26 cliUserConfig: {
27 developerDangerousCommandsEnabled: true,
28 },
29 } satisfies TestLiveInput;
30
31 describe('CLI: composio dev auth-configs create', () => {
32 layer(TestLive({ ...inProject, ...dangerousDevConfig }))(
33 '[Given] --toolkit "gmail" [Then] creates with Composio managed auth',
34 it => {
35 it.effect('creates successfully', () =>
36 Effect.gen(function* () {
37 yield* cli(['dev', 'auth-configs', 'create', '--toolkit', 'gmail']);
38 const lines = yield* MockConsole.getLines({ stripAnsi: true });
39 const output = lines.join('\n');
40
41 expect(output).toContain('Auth config created');
42 expect(output).toContain('ac_test');
43 expect(output).toContain('OAUTH2');
44 })
45 );
46 }
47 );
48
49 layer(TestLive({ ...inProject, ...dangerousDevConfig }))(
50 '[Given] named config --toolkit "gmail" [Then] creates with name',
51 it => {
52 it.effect('creates with name successfully', () =>
53 Effect.gen(function* () {
54 yield* cli(['dev', 'auth-configs', 'create', 'my-config', '--toolkit', 'gmail']);
55 const lines = yield* MockConsole.getLines({ stripAnsi: true });
56 const output = lines.join('\n');
57
58 expect(output).toContain('Auth config created');
59 expect(output).toContain('ac_test');
60 })
61 );
62 }
63 );
64
65 layer(TestLive({ ...inProject, ...dangerousDevConfig }))(
66 '[Given] --auth-scheme "OAUTH2" [Then] creates with custom auth',
67 it => {
68 it.effect('creates with custom auth scheme', () =>
69 Effect.gen(function* () {
70 yield* cli([
71 'dev',
72 'auth-configs',
73 'create',
74 '--toolkit',
75 'gmail',
76 '--auth-scheme',
77 'OAUTH2',
78 ]);
79 const lines = yield* MockConsole.getLines({ stripAnsi: true });
80 const output = lines.join('\n');
81
82 expect(output).toContain('Auth config created');
83 })
84 );
85 }
86 );
87
88 layer(
89 TestLive({
90 ...inProject,
91 ...dangerousDevConfig,
92 authConfigsData: {
93 createResponse: {
94 auth_config: { id: 'ac_custom', auth_scheme: 'API_KEY', is_composio_managed: false },
95 toolkit: { slug: 'slack' },
96 },
97 },
98 })
99 )('[Given] custom create response [Then] shows correct details', it => {
100 it.effect('shows custom response data', () =>
101 Effect.gen(function* () {
102 yield* cli(['dev', 'auth-configs', 'create', '--toolkit', 'slack']);
103 const lines = yield* MockConsole.getLines({ stripAnsi: true });
104 const output = lines.join('\n');
105
106 expect(output).toContain('ac_custom');
107 expect(output).toContain('API_KEY');
108 expect(output).toContain('slack');
109 })
110 );
111 });
112
113 layer(TestLive({ ...inProject, ...dangerousDevConfig }))(
114 '[Given] invalid JSON in --custom-credentials [Then] fails with a usage error',
115 it => {
116 it.effect('raises a CommandInputError naming --custom-credentials', () =>
117 Effect.gen(function* () {
118 const failure = yield* cli([
119 'dev',
120 'auth-configs',
121 'create',
122 '--toolkit',
123 'gmail',
124 '--auth-scheme',
125 'OAUTH2',
126 '--custom-credentials',
127 '{invalid json}',
128 ]).pipe(Effect.flip);
129
130 expect(failure).toMatchObject({
131 _tag: 'commands/CommandInputError',
132 option: '--custom-credentials',
133 message: expect.stringContaining('Invalid JSON in --custom-credentials'),
134 });
135 // The invocation prints the message on stderr, and nothing else.
136 assert(isUsageFailure(failure));
137 expect(yield* MockConsole.getLines()).toEqual([failure.message]);
138 })
139 );
140 }
141 );
142
143 layer(TestLive({ ...dangerousDevConfig }))(
144 '[Given] no API key [Then] warns user to login',
145 it => {
146 it.effect('warns user to login', () =>
147 Effect.gen(function* () {
148 const error = yield* cli(['dev', 'auth-configs', 'create', '--toolkit', 'gmail']).pipe(
149 Effect.flip
150 );
151 expect(error).toMatchObject({ _tag: 'commands/NotLoggedInError' });
152 })
153 );
154 }
155 );
156
157 layer(TestLive({ ...inProject, ...dangerousDevConfig }))(
158 '[Given] next step hint [Then] includes auth config ID',
159 it => {
160 it.effect('shows next step hint', () =>
161 Effect.gen(function* () {
162 yield* cli(['dev', 'auth-configs', 'create', '--toolkit', 'gmail']);
163 const lines = yield* MockConsole.getLines({ stripAnsi: true });
164 const output = lines.join('\n');
165
166 expect(output).toContain('composio dev auth-configs info');
167 })
168 );
169 }
170 );
171
172 let capturedCreateAuthConfig: AuthConfigCreateParams | undefined;
173
174 layer(
175 TestLive({
176 ...inProject,
177 ...dangerousDevConfig,
178 authConfigsData: {
179 createResponse: {
180 auth_config: { id: 'ac_oauth', auth_scheme: 'OAUTH2', is_composio_managed: false },
181 toolkit: { slug: 'shopify' },
182 },
183 onCreate: params => {
184 capturedCreateAuthConfig = params;
185 },
186 },
187 })
188 )(
189 '[Given] custom OAuth credentials and scopes [Then] nests them under auth_config.credentials',
190 it => {
191 it.effect('sends custom OAuth settings inside credentials', () =>
192 Effect.gen(function* () {
193 capturedCreateAuthConfig = undefined;
194
195 yield* cli([
196 'dev',
197 'auth-configs',
198 'create',
199 '--toolkit',
200 'shopify',
201 '--auth-scheme',
202 'OAUTH2',
203 '--custom-credentials',
204 '{"client_id":"my_client_id","client_secret":"my_client_secret"}',
205 '--scopes',
206 'read_products, write_products',
207 ]);
208
209 expect(capturedCreateAuthConfig).toStrictEqual({
210 toolkit: { slug: 'shopify' },
211 auth_config: {
212 type: 'use_custom_auth',
213 authScheme: 'OAUTH2',
214 name: undefined,
215 credentials: {
216 client_id: 'my_client_id',
217 client_secret: 'my_client_secret',
218 scopes: ['read_products', 'write_products'],
219 },
220 },
221 });
222 })
223 );
224 }
225 );
226 });
227
228 // The request goes to the developer project the command resolved, never to the one the backend
229 // would pick for the key.
230 describe('CLI: composio dev auth-configs create acts on the developer project', () => {
231 layer(TestLive({ ...inProject, ...dangerousDevConfig }))(it => {
232 it.effect.each([
233 { given: 'a directory set up for a project', args: [], scope: DIRECTORY_PROJECT_SCOPE },
234 {
235 given: '--project-name',
236 args: ['--project-name', OTHER_DEVELOPER_PROJECT.name],
237 scope: OTHER_PROJECT_SCOPE,
238 },
239 ])('[Given] $given [Then] creates the auth config in that project', ({ args, scope }) =>
240 Effect.gen(function* () {
241 const { exitCode, calls } = yield* runRecordingRepositoryScopes([
242 ...['dev', 'auth-configs', 'create', '--toolkit', 'gmail'],
243 ...args,
244 ]);
245
246 expect(exitCode).toBe(0);
247 expect(calls).toEqual([
248 { method: 'createAuthConfig', scope: expect.objectContaining(scope) },
249 ]);
250 })
251 );
252 });
253
254 layer(
255 TestLive({
256 baseConfigProvider: testConfigProvider,
257 ...dangerousDevConfig,
258 accountData: defaultDeveloperProjectAccountData(),
259 })
260 )(it => {
261 it.effect(
262 '[Given] a directory set up for no project [Then] acts on the default developer project',
263 () =>
264 Effect.gen(function* () {
265 yield* loginWithoutDeveloperProject;
266 const { exitCode, calls } = yield* runRecordingRepositoryScopes([
267 'dev',
268 'auth-configs',
269 'create',
270 '--toolkit',
271 'gmail',
272 ]);
273
274 expect(exitCode).toBe(0);
275 expect(calls).toEqual([
276 { method: 'createAuthConfig', scope: expect.objectContaining(DEFAULT_PROJECT_SCOPE) },
277 ]);
278 })
279 );
280 });
281 });