@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 329 lines 10.2 KB
Raw
1 /**
2 * Login and session: the CLI login session (created, polled, linked), the
3 * stored login it leaves on disk, `whoami`, `logout`, and the session
4 * artifact directory.
5 */
6
7 import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
8 import {
9 FOREIGN_USER_API_KEY,
10 GLOBAL_TEST_USER_ID,
11 MEMBER,
12 ORG,
13 USER_API_KEY,
14 } from '@e2e-tests/cli-fake-backend';
15 import { startCliRig, type CliRig } from '@e2e-tests/cli-fake-backend/rig';
16 import { z } from 'zod';
17 import {
18 clean,
19 commandRequests,
20 readStoredLogin,
21 RIG_TIMEOUT_MS,
22 routesOf,
23 stdoutJson,
24 TEST_TIMEOUT_MS,
25 writeDebugLog,
26 } from './support';
27
28 const NOT_LOGGED_IN = 'You are not logged in yet. Please run `composio login`.';
29
30 const Whoami = z.looseObject({
31 account_type: z.string(),
32 email: z.string(),
33 current_org_name: z.string(),
34 });
35
36 const PendingLogin = z.looseObject({ key: z.string(), loginUrl: z.string() });
37
38 const PollLogin = z.looseObject({
39 email: z.string(),
40 current_org: z.object({ id: z.string(), name: z.string() }),
41 organizations: z.array(z.looseObject({ id: z.string(), selected: z.boolean() })),
42 });
43
44 describe('login session: browser login completed by polling', () => {
45 let rig: CliRig;
46 const home = () => `${rig.journey.home}/.composio`;
47
48 beforeAll(async () => {
49 rig = await startCliRig({ label: 'login-poll' });
50 }, RIG_TIMEOUT_MS);
51
52 afterAll(async () => {
53 await writeDebugLog(rig, import.meta.dir, 'login-poll');
54 await rig.stop();
55 });
56
57 it(
58 'first run: `whoami` reports no login without calling the backend',
59 async () => {
60 const result = await rig.run('composio whoami');
61 expect(result.exitCode).toBe(1);
62 expect(result.stdout).toBe('');
63 expect(clean(result.stderr)).toBe(NOT_LOGGED_IN);
64 expect(commandRequests(result)).toEqual([]);
65 },
66 TEST_TIMEOUT_MS
67 );
68
69 it(
70 'headless `login` creates a pending session and prints how to finish it',
71 async () => {
72 const result = await rig.run('composio login');
73 expect(result.exitCode).toBe(0);
74 expect(routesOf(result)).toEqual(['_internal.cli.createSession']);
75
76 const session = rig.fake.world.loginSessions.at(-1);
77 expect(session?.status).toBe('pending');
78 const stdout = clean(result.stdout);
79 expect(stdout).toContain(`${rig.fake.docker.https}?cliKey=${session?.id}`);
80 expect(stdout).toContain('composio login --poll');
81
82 const pending = PendingLogin.parse(
83 JSON.parse(await rig.journey.readFile(`${home()}/pending-login-session.json`))
84 );
85 expect(pending.key).toBe(session?.id ?? '');
86 expect((await readStoredLogin(rig)).api_key).toBeNull();
87 },
88 TEST_TIMEOUT_MS
89 );
90
91 it(
92 '`login --poll` keeps polling while nobody approved the session',
93 async () => {
94 const result = await rig.run('composio login --poll --no-skill-install', {
95 timeoutSeconds: 8,
96 });
97 // 124: still waiting when the test's own time limit ended it.
98 expect(result.exitCode).toBe(124);
99 expect(result.stdout).toBe('');
100 const polls = result.requests.filter(entry => entry.route === '_internal.cli.getSession');
101 expect(polls.length).toBeGreaterThanOrEqual(2);
102 expect((await readStoredLogin(rig)).api_key).toBeNull();
103 },
104 TEST_TIMEOUT_MS
105 );
106
107 it(
108 '`login --poll` stores the login once the session is linked',
109 async () => {
110 rig.fake.world.linkLoginSession();
111 const result = await rig.run('composio login --poll --no-skill-install');
112 expect(result.exitCode).toBe(0);
113
114 const summary = stdoutJson(result, PollLogin);
115 expect(summary.email).toBe(MEMBER.email);
116 expect(summary.current_org).toEqual({ id: ORG.id, name: ORG.name });
117 expect(summary.organizations.filter(org => org.selected).map(org => org.id)).toEqual([
118 ORG.id,
119 ]);
120
121 expect(await readStoredLogin(rig)).toMatchObject({
122 api_key: USER_API_KEY,
123 base_url: rig.fake.docker.http,
124 web_url: rig.fake.docker.https,
125 org_id: ORG.id,
126 test_user_id: GLOBAL_TEST_USER_ID,
127 });
128 const leftovers = await rig.run(`ls ${home()}`);
129 expect(leftovers.stdout).not.toContain('pending-login-session.json');
130 },
131 TEST_TIMEOUT_MS
132 );
133
134 it(
135 '`whoami` prints the account as JSON on stdout and nothing on stderr',
136 async () => {
137 const result = await rig.run('composio whoami');
138 expect(result.exitCode).toBe(0);
139 expect(result.stderr).toBe('');
140 expect(stdoutJson(result, Whoami)).toMatchObject({
141 account_type: 'human',
142 email: MEMBER.email,
143 current_org_name: ORG.name,
144 });
145 expect(commandRequests(result).every(entry => entry.keyClass === 'user')).toBe(true);
146 },
147 TEST_TIMEOUT_MS
148 );
149
150 it(
151 '`login` when already logged in changes nothing and creates no session',
152 async () => {
153 const before = await readStoredLogin(rig);
154 const result = await rig.run('composio login');
155 expect(result.exitCode).toBe(0);
156 expect(commandRequests(result)).toEqual([]);
157 expect(await readStoredLogin(rig)).toEqual(before);
158 },
159 TEST_TIMEOUT_MS
160 );
161
162 it(
163 '`logout` forgets the key, and `whoami` then reports no login',
164 async () => {
165 const logout = await rig.run('composio logout');
166 expect(logout.exitCode).toBe(0);
167 expect(logout.stdout).toBe('');
168 expect((await readStoredLogin(rig)).api_key).toBeNull();
169
170 const whoami = await rig.run('composio whoami');
171 expect(whoami.exitCode).toBe(1);
172 expect(clean(whoami.stderr)).toBe(NOT_LOGGED_IN);
173 expect(commandRequests(whoami)).toEqual([]);
174 },
175 TEST_TIMEOUT_MS
176 );
177 });
178
179 describe('login session: completed with an explicit session key', () => {
180 let rig: CliRig;
181
182 beforeAll(async () => {
183 rig = await startCliRig({ label: 'login-key' });
184 }, RIG_TIMEOUT_MS);
185
186 afterAll(async () => {
187 await writeDebugLog(rig, import.meta.dir, 'login-key');
188 await rig.stop();
189 });
190
191 it(
192 '`login --key` on a session nobody approved fails without storing a login',
193 async () => {
194 await rig.run('composio login --no-wait');
195 const session = rig.fake.world.loginSessions.at(-1);
196 const result = await rig.run(`composio login --key ${session?.id} --no-wait`);
197 expect(result.exitCode).toBe(1);
198 expect(result.stdout).toBe('');
199 expect(clean(result.stderr)).toContain('Session not yet linked');
200 expect((await readStoredLogin(rig)).api_key).toBeNull();
201 },
202 TEST_TIMEOUT_MS
203 );
204
205 it(
206 '`login --key` on a linked session stores the login',
207 async () => {
208 const session = rig.fake.world.linkLoginSession();
209 const result = await rig.run(
210 `composio login --key ${session.id} --no-wait --no-skill-install`
211 );
212 expect(result.exitCode).toBe(0);
213 expect(stdoutJson(result, z.looseObject({ email: z.string(), org_id: z.string() }))).toEqual(
214 expect.objectContaining({ email: MEMBER.email, org_id: ORG.id })
215 );
216 expect((await readStoredLogin(rig)).api_key).toBe(USER_API_KEY);
217 },
218 TEST_TIMEOUT_MS
219 );
220
221 it(
222 '`login --key` with an unknown session names the command that gets a new one',
223 async () => {
224 await rig.run('composio logout');
225 const result = await rig.run('composio login --key no-such-session --no-wait');
226 expect(result.exitCode).toBe(1);
227 expect(result.stdout).toBe('');
228 expect(clean(result.stderr)).toBe(
229 'Session not found or expired. Run `composio login --no-wait` to get a new session.'
230 );
231 expect((await readStoredLogin(rig)).api_key).toBeNull();
232 },
233 TEST_TIMEOUT_MS
234 );
235 });
236
237 describe('login session: a user API key given directly', () => {
238 let rig: CliRig;
239
240 beforeAll(async () => {
241 rig = await startCliRig({ label: 'login-direct' });
242 }, RIG_TIMEOUT_MS);
243
244 afterAll(async () => {
245 await writeDebugLog(rig, import.meta.dir, 'login-direct');
246 await rig.stop();
247 });
248
249 it(
250 'a key the backend rejects is not stored',
251 async () => {
252 const result = await rig.run(
253 `composio login --user-api-key ${FOREIGN_USER_API_KEY} --no-skill-install`
254 );
255 expect(result.exitCode).toBe(1);
256 expect(result.stdout).toBe('');
257 expect(clean(result.stderr)).toContain('Invalid API key');
258 const stored = await rig.run('cat ~/.composio/user_data.json');
259 expect(stored.stdout).not.toContain(FOREIGN_USER_API_KEY);
260 },
261 TEST_TIMEOUT_MS
262 );
263
264 it(
265 'a key the backend rejects is reported as an expected failure, without a defect report',
266 async () => {
267 const result = await rig.run(
268 `composio login --user-api-key ${FOREIGN_USER_API_KEY} --no-skill-install`
269 );
270 expect(clean(result.stderr)).not.toContain('💥');
271 },
272 TEST_TIMEOUT_MS
273 );
274
275 it(
276 'an accepted key is stored with the org and the global test user',
277 async () => {
278 const result = await rig.login();
279 expect(result.exitCode).toBe(0);
280 expect(await readStoredLogin(rig)).toMatchObject({
281 api_key: USER_API_KEY,
282 org_id: ORG.id,
283 test_user_id: GLOBAL_TEST_USER_ID,
284 });
285 },
286 TEST_TIMEOUT_MS
287 );
288 });
289
290 describe('session artifacts', () => {
291 let rig: CliRig;
292
293 beforeAll(async () => {
294 rig = await startCliRig({ label: 'artifacts' });
295 }, RIG_TIMEOUT_MS);
296
297 afterAll(async () => {
298 await writeDebugLog(rig, import.meta.dir, 'artifacts');
299 await rig.stop();
300 });
301
302 it(
303 '`artifacts cwd` prints one absolute directory, without a login or a request',
304 async () => {
305 const result = await rig.run('composio artifacts cwd');
306 expect(result.exitCode).toBe(0);
307 expect(result.stderr).toBe('');
308 expect(result.stdout.trim()).toMatch(/^\/\S+$/);
309 expect(commandRequests(result)).toEqual([]);
310 },
311 TEST_TIMEOUT_MS
312 );
313
314 it(
315 '`artifacts cwd` is stable for a directory and follows COMPOSIO_SESSION_DIR',
316 async () => {
317 const first = await rig.run('composio artifacts cwd');
318 const second = await rig.run('composio artifacts cwd');
319 expect(second.stdout).toBe(first.stdout);
320
321 const overridden = await rig.run(
322 'COMPOSIO_SESSION_DIR=/tmp/e2e-sessions composio artifacts cwd'
323 );
324 expect(overridden.exitCode).toBe(0);
325 expect(overridden.stdout.trim().startsWith('/tmp/e2e-sessions')).toBe(true);
326 },
327 TEST_TIMEOUT_MS
328 );
329 });