Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | /** |
| 2 | * Auth faults: no key, a key the backend rejects, a key sent to a backend that |
| 3 | * did not issue it, and a key of the wrong class. |
| 4 | */ |
| 5 | |
| 6 | import { afterAll, afterEach, beforeAll, describe, expect, it } from 'bun:test'; |
| 7 | import { |
| 8 | DEVELOPER_PROJECT, |
| 9 | FOREIGN_USER_API_KEY, |
| 10 | startFakeComposio, |
| 11 | USER_API_KEY, |
| 12 | type FakeComposio, |
| 13 | } from '@e2e-tests/cli-fake-backend'; |
| 14 | import { startCliRig, type CliRig } from '@e2e-tests/cli-fake-backend/rig'; |
| 15 | import { |
| 16 | clean, |
| 17 | commandRequests, |
| 18 | EXECUTE_SLACK, |
| 19 | PLAYGROUND_EXECUTE_SLACK, |
| 20 | readStoredLogin, |
| 21 | RIG_TIMEOUT_MS, |
| 22 | SEARCH_SLACK, |
| 23 | TEST_TIMEOUT_MS, |
| 24 | writeDebugLog, |
| 25 | } from './support'; |
| 26 | |
| 27 | const NOT_LOGGED_IN = 'You are not logged in yet. Please run `composio login`.'; |
| 28 | |
| 29 | describe('auth faults: no key', () => { |
| 30 | let rig: CliRig; |
| 31 | |
| 32 | beforeAll(async () => { |
| 33 | rig = await startCliRig({ label: 'auth-none' }); |
| 34 | }, RIG_TIMEOUT_MS); |
| 35 | |
| 36 | afterAll(async () => { |
| 37 | await writeDebugLog(rig, import.meta.dir, 'auth-none'); |
| 38 | await rig.stop(); |
| 39 | }); |
| 40 | |
| 41 | for (const command of [ |
| 42 | SEARCH_SLACK, |
| 43 | EXECUTE_SLACK, |
| 44 | 'composio link slack', |
| 45 | 'composio connections list', |
| 46 | 'composio dev auth-configs list', |
| 47 | PLAYGROUND_EXECUTE_SLACK, |
| 48 | 'composio dev connected-accounts link slack', |
| 49 | ]) { |
| 50 | it( |
| 51 | `\`${command.split(' -d ')[0]}\` names \`composio login\` and sends nothing`, |
| 52 | async () => { |
| 53 | const result = await rig.run(command); |
| 54 | expect(result.exitCode).toBe(1); |
| 55 | expect(result.stdout).toBe(''); |
| 56 | expect(clean(result.stderr)).toBe(NOT_LOGGED_IN); |
| 57 | expect(commandRequests(result)).toEqual([]); |
| 58 | }, |
| 59 | TEST_TIMEOUT_MS |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | it( |
| 64 | 'a key from COMPOSIO_USER_API_KEY alone has no org: the command says how to get one', |
| 65 | async () => { |
| 66 | const result = await rig.run(`COMPOSIO_USER_API_KEY=${USER_API_KEY} ${SEARCH_SLACK}`); |
| 67 | expect(result.exitCode).toBe(1); |
| 68 | expect(result.stdout).toBe(''); |
| 69 | expect(clean(result.stderr)).toBe( |
| 70 | 'No current org configured. Run `composio login` or `composio orgs switch`.' |
| 71 | ); |
| 72 | expect(commandRequests(result)).toEqual([]); |
| 73 | }, |
| 74 | TEST_TIMEOUT_MS |
| 75 | ); |
| 76 | }); |
| 77 | |
| 78 | describe('auth faults: a stored key the backend stops accepting', () => { |
| 79 | let rig: CliRig; |
| 80 | |
| 81 | beforeAll(async () => { |
| 82 | rig = await startCliRig({ label: 'auth-rejected' }); |
| 83 | await rig.login(); |
| 84 | }, RIG_TIMEOUT_MS); |
| 85 | |
| 86 | afterEach(() => rig.fake.faults.clear()); |
| 87 | |
| 88 | afterAll(async () => { |
| 89 | await writeDebugLog(rig, import.meta.dir, 'auth-rejected'); |
| 90 | await rig.stop(); |
| 91 | }); |
| 92 | |
| 93 | const reject = () => |
| 94 | rig.fake.faults.inject({ |
| 95 | route: /^(GET|POST) \/api\//, |
| 96 | fault: { |
| 97 | kind: 'status', |
| 98 | status: 401, |
| 99 | slug: 'Auth_InvalidApiKey', |
| 100 | message: 'Invalid API key', |
| 101 | }, |
| 102 | }); |
| 103 | |
| 104 | for (const command of [SEARCH_SLACK, EXECUTE_SLACK, 'composio connections list']) { |
| 105 | it( |
| 106 | `\`${command.split(' -d ')[0]}\`: exit 1, the rejection on stderr, the login left in place`, |
| 107 | async () => { |
| 108 | reject(); |
| 109 | const result = await rig.run(command); |
| 110 | expect(result.exitCode).toBe(1); |
| 111 | expect(`${clean(result.stdout)}\n${clean(result.stderr)}`).toContain('Invalid API key'); |
| 112 | expect(commandRequests(result).length).toBeGreaterThan(0); |
| 113 | expect(commandRequests(result).every(entry => entry.status === 401)).toBe(true); |
| 114 | expect((await readStoredLogin(rig)).api_key).toBe(USER_API_KEY); |
| 115 | }, |
| 116 | TEST_TIMEOUT_MS |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | it( |
| 121 | 'a rejected stored key names `composio login` as the way out', |
| 122 | async () => { |
| 123 | reject(); |
| 124 | const result = await rig.run(SEARCH_SLACK); |
| 125 | expect(clean(result.stderr)).toContain('composio login'); |
| 126 | }, |
| 127 | TEST_TIMEOUT_MS |
| 128 | ); |
| 129 | |
| 130 | it( |
| 131 | '`whoami` fails when the backend rejects the stored key', |
| 132 | async () => { |
| 133 | reject(); |
| 134 | const result = await rig.run('composio whoami'); |
| 135 | expect(result.exitCode).toBe(1); |
| 136 | expect(result.stdout).toBe(''); |
| 137 | }, |
| 138 | TEST_TIMEOUT_MS |
| 139 | ); |
| 140 | }); |
| 141 | |
| 142 | describe('auth faults: a key sent to another backend', () => { |
| 143 | let rig: CliRig; |
| 144 | let elsewhere: FakeComposio; |
| 145 | |
| 146 | beforeAll(async () => { |
| 147 | rig = await startCliRig({ label: 'auth-mismatch' }); |
| 148 | // A deployment that issued its own key and has never seen this rig's. |
| 149 | elsewhere = await startFakeComposio({ userApiKey: FOREIGN_USER_API_KEY }); |
| 150 | await rig.login(); |
| 151 | }, RIG_TIMEOUT_MS); |
| 152 | |
| 153 | afterAll(async () => { |
| 154 | await writeDebugLog(rig, import.meta.dir, 'auth-mismatch'); |
| 155 | await elsewhere.close(); |
| 156 | await rig.stop(); |
| 157 | }); |
| 158 | |
| 159 | it( |
| 160 | 'COMPOSIO_BASE_URL pointing away from the issuer warns on stderr before sending the key', |
| 161 | async () => { |
| 162 | const result = await rig.run(`COMPOSIO_BASE_URL=${elsewhere.docker.http} composio whoami`); |
| 163 | const stderr = clean(result.stderr); |
| 164 | expect(stderr).toContain( |
| 165 | `COMPOSIO_BASE_URL sends your stored API key to ${elsewhere.docker.http}, but it was issued by ${rig.fake.docker.http}.` |
| 166 | ); |
| 167 | expect(stderr).toContain('composio logout && composio login'); |
| 168 | expect(result.stdout).not.toContain('COMPOSIO_BASE_URL'); |
| 169 | // The stored key did go to the other backend, which rejected it; nothing went to the issuer. |
| 170 | const received = elsewhere.journal.entries().filter(entry => entry.role === 'backend'); |
| 171 | expect(received.length).toBeGreaterThan(0); |
| 172 | expect(received.every(entry => entry.keyClass === 'user' && !entry.keyKnown)).toBe(true); |
| 173 | expect(received.every(entry => entry.status === 401)).toBe(true); |
| 174 | expect(commandRequests(result)).toEqual([]); |
| 175 | expect((await readStoredLogin(rig)).api_key).toBe(USER_API_KEY); |
| 176 | }, |
| 177 | TEST_TIMEOUT_MS |
| 178 | ); |
| 179 | |
| 180 | it( |
| 181 | 'a command that needs the backend fails there with the rejection on stderr', |
| 182 | async () => { |
| 183 | const result = await rig.run(`COMPOSIO_BASE_URL=${elsewhere.docker.http} ${SEARCH_SLACK}`); |
| 184 | expect(result.exitCode).toBe(1); |
| 185 | expect(result.stdout).toBe(''); |
| 186 | const stderr = clean(result.stderr); |
| 187 | expect(stderr).toContain('COMPOSIO_BASE_URL sends your stored API key'); |
| 188 | expect(stderr).toContain('Invalid API key'); |
| 189 | expect(commandRequests(result)).toEqual([]); |
| 190 | }, |
| 191 | TEST_TIMEOUT_MS |
| 192 | ); |
| 193 | }); |
| 194 | |
| 195 | describe('auth faults: a key of the wrong class', () => { |
| 196 | let rig: CliRig; |
| 197 | |
| 198 | beforeAll(async () => { |
| 199 | rig = await startCliRig({ label: 'auth-class' }); |
| 200 | await rig.login(); |
| 201 | }, RIG_TIMEOUT_MS); |
| 202 | |
| 203 | afterAll(async () => { |
| 204 | await writeDebugLog(rig, import.meta.dir, 'auth-class'); |
| 205 | await rig.stop(); |
| 206 | }); |
| 207 | |
| 208 | it( |
| 209 | 'a project API key in the user-key slot is sent as a user key and rejected', |
| 210 | async () => { |
| 211 | const result = await rig.run( |
| 212 | `COMPOSIO_USER_API_KEY=${DEVELOPER_PROJECT.apiKey} composio whoami` |
| 213 | ); |
| 214 | const sent = commandRequests(result); |
| 215 | expect(sent.length).toBeGreaterThan(0); |
| 216 | // The journal classifies by prefix: an `ak_` key arrived where a `uk_` key belongs. |
| 217 | expect(sent.every(entry => entry.keyClass === 'project')).toBe(true); |
| 218 | expect(sent.every(entry => entry.status === 401)).toBe(true); |
| 219 | }, |
| 220 | TEST_TIMEOUT_MS |
| 221 | ); |
| 222 | }); |