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
| 1 | import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | import { Entry } from '../src/core/entry'; |
| 3 | import { KeyringError } from '../src/core/errors'; |
| 4 | import { |
| 5 | type CredentialStore, |
| 6 | setDefaultStore, |
| 7 | unsetDefaultStore, |
| 8 | hasDefaultStore, |
| 9 | getDefaultStore, |
| 10 | } from '../src/core/store'; |
| 11 | import { CredentialPersistence } from '../src/core/persistence'; |
| 12 | |
| 13 | /** |
| 14 | * In-memory store used by every unit test. Lets us exercise Entry's |
| 15 | * routing and validation logic without shelling out to a real keystore. |
| 16 | */ |
| 17 | class MemoryStore implements CredentialStore { |
| 18 | readonly id = 'memory'; |
| 19 | readonly vendor = 'test in-memory'; |
| 20 | private readonly items = new Map<string, Uint8Array>(); |
| 21 | |
| 22 | persistence() { |
| 23 | return CredentialPersistence.ProcessOnly; |
| 24 | } |
| 25 | |
| 26 | private key(service: string, user: string) { |
| 27 | return `${service}\0${user}`; |
| 28 | } |
| 29 | |
| 30 | async setSecret(service: string, user: string, secret: Uint8Array) { |
| 31 | this.items.set(this.key(service, user), new Uint8Array(secret)); |
| 32 | } |
| 33 | |
| 34 | async getSecret(service: string, user: string) { |
| 35 | const v = this.items.get(this.key(service, user)); |
| 36 | if (v === undefined) throw new KeyringError({ kind: 'NoEntry' }); |
| 37 | return v; |
| 38 | } |
| 39 | |
| 40 | async deleteCredential(service: string, user: string) { |
| 41 | if (!this.items.delete(this.key(service, user))) { |
| 42 | throw new KeyringError({ kind: 'NoEntry' }); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | describe('Entry', () => { |
| 48 | let store: MemoryStore; |
| 49 | |
| 50 | beforeEach(() => { |
| 51 | store = new MemoryStore(); |
| 52 | setDefaultStore(store); |
| 53 | }); |
| 54 | |
| 55 | afterEach(() => { |
| 56 | unsetDefaultStore(); |
| 57 | }); |
| 58 | |
| 59 | it('rejects empty service', () => { |
| 60 | expect(() => new Entry('', 'user')).toThrowError(KeyringError); |
| 61 | try { |
| 62 | new Entry('', 'user'); |
| 63 | } catch (err) { |
| 64 | expect(err).toBeInstanceOf(KeyringError); |
| 65 | expect((err as KeyringError).kind).toBe('Invalid'); |
| 66 | } |
| 67 | }); |
| 68 | |
| 69 | it('rejects empty user', () => { |
| 70 | expect(() => new Entry('svc', '')).toThrowError(KeyringError); |
| 71 | }); |
| 72 | |
| 73 | it('roundtrips a UTF-8 password', async () => { |
| 74 | const entry = new Entry('com.composio.cli', 'default'); |
| 75 | await entry.setPassword('hunter2-😀'); |
| 76 | expect(await entry.getPassword()).toBe('hunter2-😀'); |
| 77 | }); |
| 78 | |
| 79 | it('throws NoEntry when reading a missing credential', async () => { |
| 80 | const entry = new Entry('com.composio.cli', 'nobody'); |
| 81 | await expect(entry.getPassword()).rejects.toSatisfy( |
| 82 | (err: unknown) => err instanceof KeyringError && err.kind === 'NoEntry' |
| 83 | ); |
| 84 | }); |
| 85 | |
| 86 | it('throws BadEncoding when stored bytes are not valid UTF-8', async () => { |
| 87 | const entry = new Entry('com.composio.cli', 'binaryuser'); |
| 88 | await entry.setSecret(new Uint8Array([0xff, 0xfe, 0xfd])); |
| 89 | await expect(entry.getPassword()).rejects.toSatisfy( |
| 90 | (err: unknown) => err instanceof KeyringError && err.kind === 'BadEncoding' |
| 91 | ); |
| 92 | }); |
| 93 | |
| 94 | it('roundtrips raw binary bytes', async () => { |
| 95 | const entry = new Entry('com.composio.cli', 'binaryuser'); |
| 96 | const bytes = new Uint8Array([0, 1, 2, 3, 4, 255]); |
| 97 | await entry.setSecret(bytes); |
| 98 | const got = await entry.getSecret(); |
| 99 | expect(Array.from(got)).toEqual(Array.from(bytes)); |
| 100 | }); |
| 101 | |
| 102 | it('deleteCredential removes the entry and future reads throw NoEntry', async () => { |
| 103 | const entry = new Entry('com.composio.cli', 'default'); |
| 104 | await entry.setPassword('secret'); |
| 105 | await entry.deleteCredential(); |
| 106 | await expect(entry.getPassword()).rejects.toSatisfy( |
| 107 | (err: unknown) => err instanceof KeyringError && err.kind === 'NoEntry' |
| 108 | ); |
| 109 | }); |
| 110 | |
| 111 | it('overrides the default store via constructor argument', async () => { |
| 112 | const other = new MemoryStore(); |
| 113 | const entry = new Entry('svc', 'user', {}, other); |
| 114 | await entry.setPassword('x'); |
| 115 | // Default store should still be empty. |
| 116 | await expect(new Entry('svc', 'user').getPassword()).rejects.toSatisfy( |
| 117 | (err: unknown) => err instanceof KeyringError && err.kind === 'NoEntry' |
| 118 | ); |
| 119 | expect(await entry.getPassword()).toBe('x'); |
| 120 | }); |
| 121 | |
| 122 | it('throws NoDefaultStore when no store is registered', async () => { |
| 123 | unsetDefaultStore(); |
| 124 | expect(hasDefaultStore()).toBe(false); |
| 125 | expect(() => getDefaultStore()).toThrowError(KeyringError); |
| 126 | const entry = new Entry('svc', 'user'); |
| 127 | await expect(entry.getPassword()).rejects.toSatisfy( |
| 128 | (err: unknown) => err instanceof KeyringError && err.kind === 'NoDefaultStore' |
| 129 | ); |
| 130 | }); |
| 131 | }); |