- Add tests for all backend modules: config, database, models, services, routes, middleware, and providers - Separate dev dependencies using dependency-groups - Update CLAUDE.md with test commands and project structure - Add .coverage and .pytest_cache to gitignore 88 tests covering: - Authentication and authorization - Model routing and key selection - Async logging with batch processing - All API endpoints (chat, openai, anthropic, health) - LiteLLM wrapper (mocked external calls) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
48 lines
2.0 KiB
Python
48 lines
2.0 KiB
Python
"""Tests for authentication service."""
|
|
import pytest
|
|
from unittest.mock import patch, AsyncMock
|
|
from app.services.auth import AuthService
|
|
from app.models import ClientKey
|
|
|
|
|
|
class TestAuthService:
|
|
"""Test AuthService."""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_validate_key_success(self, mock_db):
|
|
"""Test validating a valid API key."""
|
|
with patch("app.services.auth.db_connection", mock_db):
|
|
result = await AuthService.validate_key("test-api-key")
|
|
assert result is not None
|
|
assert result.key == "test-api-key"
|
|
assert result.name == "test-client"
|
|
assert result.is_active in (True, 1) # SQLite returns 1 for boolean true
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_validate_key_invalid(self, mock_db):
|
|
"""Test validating an invalid API key."""
|
|
with patch("app.services.auth.db_connection", mock_db):
|
|
result = await AuthService.validate_key("invalid-key")
|
|
assert result is None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_validate_key_disabled(self, mock_db):
|
|
"""Test validating a disabled API key."""
|
|
with patch("app.services.auth.db_connection", mock_db):
|
|
result = await AuthService.validate_key("disabled-key")
|
|
assert result is None # Disabled keys should not validate
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_validate_key_returns_client_key_instance(self, mock_db):
|
|
"""Test validate_key returns ClientKey instance."""
|
|
with patch("app.services.auth.db_connection", mock_db):
|
|
result = await AuthService.validate_key("test-api-key")
|
|
assert isinstance(result, ClientKey)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_validate_key_case_sensitive(self, mock_db):
|
|
"""Test key validation is case sensitive."""
|
|
with patch("app.services.auth.db_connection", mock_db):
|
|
result = await AuthService.validate_key("TEST-API-KEY")
|
|
assert result is None # Should not match due to case
|