Fix provider API key validation trimming (#810)
This commit is contained in:
committed by
GitHub
Unverified
parent
66b2ddb2dc
commit
49518300dc
42
tests/unit/provider-store-validation.test.ts
Normal file
42
tests/unit/provider-store-validation.test.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const mockFetchProviderSnapshot = vi.fn();
|
||||
const mockHostApiFetch = vi.fn();
|
||||
|
||||
vi.mock('@/lib/provider-accounts', () => ({
|
||||
fetchProviderSnapshot: (...args: unknown[]) => mockFetchProviderSnapshot(...args),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/host-api', () => ({
|
||||
hostApiFetch: (...args: unknown[]) => mockHostApiFetch(...args),
|
||||
}));
|
||||
|
||||
import { useProviderStore } from '@/stores/providers';
|
||||
|
||||
describe('useProviderStore – validateAccountApiKey()', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('trims API keys before sending provider validation requests', async () => {
|
||||
mockHostApiFetch.mockResolvedValueOnce({ valid: true });
|
||||
|
||||
const result = await useProviderStore.getState().validateAccountApiKey('custom', ' sk-lm-test \n', {
|
||||
baseUrl: 'http://127.0.0.1:1234/v1',
|
||||
apiProtocol: 'openai-completions',
|
||||
});
|
||||
|
||||
expect(result).toEqual({ valid: true });
|
||||
expect(mockHostApiFetch).toHaveBeenCalledWith('/api/providers/validate', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
providerId: 'custom',
|
||||
apiKey: 'sk-lm-test',
|
||||
options: {
|
||||
baseUrl: 'http://127.0.0.1:1234/v1',
|
||||
apiProtocol: 'openai-completions',
|
||||
},
|
||||
}),
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
normalizeProviderApiKeyInput,
|
||||
PROVIDER_TYPES,
|
||||
PROVIDER_TYPE_INFO,
|
||||
getProviderDocsUrl,
|
||||
@@ -171,6 +172,7 @@ describe('provider metadata', () => {
|
||||
});
|
||||
|
||||
it('normalizes provider API keys for save flow', () => {
|
||||
expect(normalizeProviderApiKeyInput(' sk-test \n')).toBe('sk-test');
|
||||
expect(resolveProviderApiKeyForSave('ollama', '')).toBe('ollama-local');
|
||||
expect(resolveProviderApiKeyForSave('ollama', ' ')).toBe('ollama-local');
|
||||
expect(resolveProviderApiKeyForSave('ollama', 'real-key')).toBe('real-key');
|
||||
|
||||
Reference in New Issue
Block a user