-
Notifications
You must be signed in to change notification settings - Fork 0
π§ͺ [testing improvement] Add tests for parseApiKeys #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| */ | ||
|
|
||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import { validateMessage, validateApiKey, sanitizeAgentId, checkRateLimit, sanitizeMessage } from '../src/protocol/validation.js'; | ||
| import { validateMessage, validateApiKey, sanitizeAgentId, checkRateLimit, sanitizeMessage, parseApiKeys, ApiKeyScope } from '../src/protocol/validation.js'; | ||
| import { DAPMessageSchema } from '../src/protocol/types.js'; | ||
|
|
||
| describe('validateMessage', () => { | ||
|
|
@@ -73,6 +73,75 @@ describe('validateMessage', () => { | |
| }); | ||
| }); | ||
|
|
||
| describe('parseApiKeys', () => { | ||
| it('should return empty array for undefined or empty input', () => { | ||
| expect(parseApiKeys(undefined)).toEqual([]); | ||
| expect(parseApiKeys('')).toEqual([]); | ||
| expect(parseApiKeys(' ')).toEqual([]); | ||
| }); | ||
|
|
||
| it('should parse single key without scope (defaults to read)', () => { | ||
| const result = parseApiKeys('my-key'); | ||
| expect(result).toHaveLength(1); | ||
| expect(result[0].key).toBe('my-key'); | ||
| expect(result[0].scope).toBe(ApiKeyScope.READ); | ||
| expect(result[0].description).toBe('Key ending in ...-key'); | ||
| expect(result[0].createdAt).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should parse single key with valid scope', () => { | ||
| const result = parseApiKeys('admin-key:admin'); | ||
| expect(result).toHaveLength(1); | ||
| expect(result[0].key).toBe('admin-key'); | ||
| expect(result[0].scope).toBe(ApiKeyScope.ADMIN); | ||
| }); | ||
|
|
||
| it('should fallback to read scope for invalid scope', () => { | ||
| const result = parseApiKeys('some-key:super-admin'); | ||
| expect(result).toHaveLength(1); | ||
| expect(result[0].key).toBe('some-key'); | ||
| expect(result[0].scope).toBe(ApiKeyScope.READ); | ||
| }); | ||
|
|
||
| it('should parse multiple keys with varying scopes', () => { | ||
| const result = parseApiKeys('key1,key2:write,key3:admin,key4:invalid'); | ||
| expect(result).toHaveLength(4); | ||
|
|
||
| expect(result[0].key).toBe('key1'); | ||
| expect(result[0].scope).toBe(ApiKeyScope.READ); | ||
|
|
||
| expect(result[1].key).toBe('key2'); | ||
| expect(result[1].scope).toBe(ApiKeyScope.WRITE); | ||
|
|
||
| expect(result[2].key).toBe('key3'); | ||
| expect(result[2].scope).toBe(ApiKeyScope.ADMIN); | ||
|
|
||
| expect(result[3].key).toBe('key4'); | ||
| expect(result[3].scope).toBe(ApiKeyScope.READ); | ||
| }); | ||
|
|
||
| it('should handle spaces in the input', () => { | ||
| const result = parseApiKeys(' key1 , key2:write '); | ||
| expect(result).toHaveLength(2); | ||
| expect(result[0].key).toBe('key1'); | ||
| expect(result[1].key).toBe('key2'); | ||
| expect(result[1].scope).toBe(ApiKeyScope.WRITE); | ||
| }); | ||
|
|
||
| it('should handle malformed lists (empty entries)', () => { | ||
| const result = parseApiKeys('key1,,key2,,,key3:write'); | ||
| expect(result).toHaveLength(3); | ||
| expect(result.map(r => r.key)).toEqual(['key1', 'key2', 'key3']); | ||
| }); | ||
|
Comment on lines
+131
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test currently only asserts the parsed keys but does not verify that their scopes are correctly parsed or defaulted. Asserting the full object structure (including the scopes) makes the test more robust and ensures that the scope parsing logic works as expected when handling malformed lists with empty entries. it('should handle malformed lists (empty entries)', () => {
const result = parseApiKeys('key1,,key2,,,key3:write');
expect(result).toHaveLength(3);
expect(result.map(r => ({ key: r.key, scope: r.scope }))).toEqual([
{ key: 'key1', scope: ApiKeyScope.READ },
{ key: 'key2', scope: ApiKeyScope.READ },
{ key: 'key3', scope: ApiKeyScope.WRITE }
]);
}); |
||
|
|
||
| it('should handle keys with multiple colons correctly', () => { | ||
| const result = parseApiKeys('complex:key:name:write'); | ||
| expect(result).toHaveLength(1); | ||
| expect(result[0].key).toBe('complex'); | ||
| expect(result[0].scope).toBe(ApiKeyScope.READ); // 'key:name:write' is invalid scope | ||
| }); | ||
| }); | ||
|
|
||
| describe('validateApiKey', () => { | ||
| it('should validate matching key', () => { | ||
| const validKeys = ['key1', 'key2', 'key3']; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is recommended to add a test case to verify how
parseApiKeyshandles short keys (less than 4 characters) when generating the key description. This ensures that theslice(-4)logic does not cause any unexpected behavior or crashes for short keys.