"""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