@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 126 lines 3.67 KB
Raw
1 /**
2 * CLI whoami command e2e test
3 *
4 * Verifies, against a mock Composio API, that the compiled composio CLI prints the account
5 * of an accepted user API key as JSON, and fails with how to recover for a rejected one.
6 */
7
8 import {
9 e2e,
10 parseJsonStdout,
11 type E2ETestResult,
12 type E2ETestResultWithFiles,
13 } from '@e2e-tests/utils';
14 import { TIMEOUTS } from '@e2e-tests/utils/const';
15 import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
16 import {
17 ACCEPTED_USER_API_KEY,
18 REJECTED_USER_API_KEY,
19 SESSION_INFO,
20 startMockWhoamiServer,
21 type MockWhoamiServer,
22 } from '../../../packages/cli/scripts/mock-whoami-server';
23
24 e2e(import.meta.url, {
25 versions: {
26 cli: ['current'],
27 },
28 defineTests: ({ runCmd }) => {
29 let server: MockWhoamiServer;
30 let whoamiResult: E2ETestResult;
31 let redirectedResult: E2ETestResultWithFiles<'out.txt'>;
32 let rejectedResult: E2ETestResult;
33
34 const expectedWhoami = {
35 account_type: 'human',
36 email: SESSION_INFO.org_member.email,
37 current_org_name: SESSION_INFO.project.org.name,
38 enhanced_controls_enabled: false,
39 };
40
41 beforeAll(async () => {
42 server = await startMockWhoamiServer();
43
44 const envPrefix = (userApiKey: string) =>
45 [
46 `COMPOSIO_BASE_URL=${server.dockerBaseUrl}`,
47 'COMPOSIO_CACHE_DIR=/tmp/composio-whoami',
48 `COMPOSIO_USER_API_KEY=${userApiKey}`,
49 ].join(' ');
50
51 whoamiResult = await runCmd(`${envPrefix(ACCEPTED_USER_API_KEY)} composio whoami`);
52 redirectedResult = await runCmd({
53 command: `${envPrefix(ACCEPTED_USER_API_KEY)} composio whoami > out.txt`,
54 files: ['out.txt'],
55 });
56 rejectedResult = await runCmd(`${envPrefix(REJECTED_USER_API_KEY)} composio whoami`);
57 }, TIMEOUTS.FIXTURE);
58
59 afterAll(async () => {
60 await server.close();
61 });
62
63 describe('composio whoami (accepted key)', () => {
64 it('exits successfully', () => {
65 expect(whoamiResult.exitCode).toBe(0);
66 });
67
68 it('stdout is the account as JSON', () => {
69 expect(parseJsonStdout(whoamiResult)).toEqual(expectedWhoami);
70 });
71
72 it('stderr is empty', () => {
73 expect(whoamiResult.stderr).toBe('');
74 });
75 });
76
77 describe('stdout redirection to out.txt (accepted key)', () => {
78 it('exits successfully', () => {
79 expect(redirectedResult.exitCode).toBe(0);
80 });
81
82 it('stdout is empty', () => {
83 expect(redirectedResult.stdout).toBe('');
84 });
85
86 it('stderr is empty', () => {
87 expect(redirectedResult.stderr).toBe('');
88 });
89
90 it('out.txt holds the account as JSON', () => {
91 expect(JSON.parse(redirectedResult.files['out.txt'])).toEqual(expectedWhoami);
92 });
93 });
94
95 describe('composio whoami (key rejected with 401)', () => {
96 it('exits with code 1', () => {
97 expect(rejectedResult.exitCode).toBe(1);
98 });
99
100 it('stdout is empty', () => {
101 expect(rejectedResult.stdout).toBe('');
102 });
103
104 it('stderr names the rejection', () => {
105 expect(rejectedResult.stderr).toContain('Invalid or revoked user API key');
106 });
107
108 it('stderr says to replace the key in COMPOSIO_USER_API_KEY', () => {
109 expect(rejectedResult.stderr).toContain(
110 'Replace the key in COMPOSIO_USER_API_KEY, then retry.'
111 );
112 });
113 });
114
115 describe('mock API usage', () => {
116 it('reads the session info with each key', () => {
117 expect(server.requests).toContain(
118 `GET /api/v3.1/auth/session/info ${ACCEPTED_USER_API_KEY}`
119 );
120 expect(server.requests).toContain(
121 `GET /api/v3.1/auth/session/info ${REJECTED_USER_API_KEY}`
122 );
123 });
124 });
125 },
126 });