Skip to main content
Glama
pixxelboy
by pixxelboy
auth.test.ts3.39 kB
/** * Unit tests for auth utility */ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import { getApiKey, hasApiKey, createAuthHeaders, maskApiKey, API_KEY_ENV_VAR, } from '../../../src/utils/auth.js'; describe('auth utility', () => { const originalEnv = process.env; beforeEach(() => { vi.resetModules(); process.env = { ...originalEnv }; }); afterEach(() => { process.env = originalEnv; }); describe('getApiKey', () => { it('should return API key when set', () => { process.env[API_KEY_ENV_VAR] = 'test-api-key-12345'; expect(getApiKey()).toBe('test-api-key-12345'); }); it('should trim whitespace from API key', () => { process.env[API_KEY_ENV_VAR] = ' test-api-key '; expect(getApiKey()).toBe('test-api-key'); }); it('should throw MCPError when API key is not set', () => { delete process.env[API_KEY_ENV_VAR]; expect(() => getApiKey()).toThrow(); }); it('should throw MCPError when API key is empty', () => { process.env[API_KEY_ENV_VAR] = ''; expect(() => getApiKey()).toThrow(); }); it('should throw MCPError when API key is only whitespace', () => { process.env[API_KEY_ENV_VAR] = ' '; expect(() => getApiKey()).toThrow(); }); it('should throw with correct error code', () => { delete process.env[API_KEY_ENV_VAR]; try { getApiKey(); } catch (error) { expect((error as { code: string }).code).toBe('MISSING_API_KEY'); } }); }); describe('hasApiKey', () => { it('should return true when API key is set', () => { process.env[API_KEY_ENV_VAR] = 'test-api-key'; expect(hasApiKey()).toBe(true); }); it('should return false when API key is not set', () => { delete process.env[API_KEY_ENV_VAR]; expect(hasApiKey()).toBe(false); }); it('should return false when API key is empty', () => { process.env[API_KEY_ENV_VAR] = ''; expect(hasApiKey()).toBe(false); }); it('should return false when API key is only whitespace', () => { process.env[API_KEY_ENV_VAR] = ' '; expect(hasApiKey()).toBe(false); }); }); describe('createAuthHeaders', () => { it('should create headers with Bearer token', () => { process.env[API_KEY_ENV_VAR] = 'test-api-key'; const headers = createAuthHeaders(); expect(headers).toHaveProperty('Authorization', 'Bearer test-api-key'); expect(headers).toHaveProperty('Content-Type', 'application/json'); }); it('should throw when API key is not set', () => { delete process.env[API_KEY_ENV_VAR]; expect(() => createAuthHeaders()).toThrow(); }); }); describe('maskApiKey', () => { it('should mask middle of long API key', () => { const masked = maskApiKey('sk-1234567890abcdef'); expect(masked).toBe('sk-1...cdef'); }); it('should show only *** for short API keys', () => { const masked = maskApiKey('short'); expect(masked).toBe('***'); }); it('should handle exactly 8 character keys', () => { const masked = maskApiKey('12345678'); expect(masked).toBe('***'); }); it('should handle 9 character keys (first > 8)', () => { const masked = maskApiKey('123456789'); expect(masked).toBe('1234...6789'); }); }); });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/pixxelboy/amplify-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server