fix: Lint fixes of core and repositories tests
All checks were successful
Backend CI / lint (push) Successful in 9m26s
Backend CI / test (push) Successful in 4m24s

This commit is contained in:
JSC
2025-08-01 09:17:20 +02:00
parent 389cfe2d6a
commit dc29915fbc
8 changed files with 135 additions and 88 deletions

1
tests/core/__init__.py Normal file
View File

@@ -0,0 +1 @@
"""Tests for core module."""

View File

@@ -1,4 +1,5 @@
"""Tests for API token authentication dependencies."""
# ruff: noqa: S106
from datetime import UTC, datetime, timedelta
from unittest.mock import AsyncMock
@@ -10,17 +11,20 @@ from app.core.dependencies import get_current_user_api_token, get_current_user_f
from app.models.user import User
from app.services.auth import AuthService
# Constants
HTTP_401_UNAUTHORIZED = 401
class TestApiTokenDependencies:
"""Test API token authentication dependencies."""
@pytest.fixture
def mock_auth_service(self):
def mock_auth_service(self) -> AsyncMock:
"""Create a mock auth service."""
return AsyncMock(spec=AuthService)
@pytest.fixture
def test_user(self):
def test_user(self) -> User:
"""Create a test user."""
return User(
id=1,
@@ -37,9 +41,9 @@ class TestApiTokenDependencies:
@pytest.mark.asyncio
async def test_get_current_user_api_token_success(
self,
mock_auth_service,
test_user,
):
mock_auth_service: AsyncMock,
test_user: User,
) -> None:
"""Test successful API token authentication."""
mock_auth_service.get_user_by_api_token.return_value = test_user
@@ -53,38 +57,46 @@ class TestApiTokenDependencies:
)
@pytest.mark.asyncio
async def test_get_current_user_api_token_no_header(self, mock_auth_service):
async def test_get_current_user_api_token_no_header(
self, mock_auth_service: AsyncMock,
) -> None:
"""Test API token authentication without API-TOKEN header."""
with pytest.raises(HTTPException) as exc_info:
await get_current_user_api_token(mock_auth_service, None)
assert exc_info.value.status_code == 401
assert exc_info.value.status_code == HTTP_401_UNAUTHORIZED
assert "API-TOKEN header required" in exc_info.value.detail
@pytest.mark.asyncio
async def test_get_current_user_api_token_empty_token(self, mock_auth_service):
async def test_get_current_user_api_token_empty_token(
self, mock_auth_service: AsyncMock,
) -> None:
"""Test API token authentication with empty token."""
api_token_header = " "
with pytest.raises(HTTPException) as exc_info:
await get_current_user_api_token(mock_auth_service, api_token_header)
assert exc_info.value.status_code == 401
assert exc_info.value.status_code == HTTP_401_UNAUTHORIZED
assert "API token required" in exc_info.value.detail
@pytest.mark.asyncio
async def test_get_current_user_api_token_whitespace_token(self, mock_auth_service):
async def test_get_current_user_api_token_whitespace_token(
self, mock_auth_service: AsyncMock,
) -> None:
"""Test API token authentication with whitespace-only token."""
api_token_header = " "
with pytest.raises(HTTPException) as exc_info:
await get_current_user_api_token(mock_auth_service, api_token_header)
assert exc_info.value.status_code == 401
assert exc_info.value.status_code == HTTP_401_UNAUTHORIZED
assert "API token required" in exc_info.value.detail
@pytest.mark.asyncio
async def test_get_current_user_api_token_invalid_token(self, mock_auth_service):
async def test_get_current_user_api_token_invalid_token(
self, mock_auth_service: AsyncMock,
) -> None:
"""Test API token authentication with invalid token."""
mock_auth_service.get_user_by_api_token.return_value = None
@@ -93,15 +105,15 @@ class TestApiTokenDependencies:
with pytest.raises(HTTPException) as exc_info:
await get_current_user_api_token(mock_auth_service, api_token_header)
assert exc_info.value.status_code == 401
assert exc_info.value.status_code == HTTP_401_UNAUTHORIZED
assert "Invalid API token" in exc_info.value.detail
@pytest.mark.asyncio
async def test_get_current_user_api_token_expired_token(
self,
mock_auth_service,
test_user,
):
mock_auth_service: AsyncMock,
test_user: User,
) -> None:
"""Test API token authentication with expired token."""
# Set expired token
test_user.api_token_expires_at = datetime.now(UTC) - timedelta(days=1)
@@ -112,15 +124,15 @@ class TestApiTokenDependencies:
with pytest.raises(HTTPException) as exc_info:
await get_current_user_api_token(mock_auth_service, api_token_header)
assert exc_info.value.status_code == 401
assert exc_info.value.status_code == HTTP_401_UNAUTHORIZED
assert "API token has expired" in exc_info.value.detail
@pytest.mark.asyncio
async def test_get_current_user_api_token_inactive_user(
self,
mock_auth_service,
test_user,
):
mock_auth_service: AsyncMock,
test_user: User,
) -> None:
"""Test API token authentication with inactive user."""
test_user.is_active = False
mock_auth_service.get_user_by_api_token.return_value = test_user
@@ -130,13 +142,13 @@ class TestApiTokenDependencies:
with pytest.raises(HTTPException) as exc_info:
await get_current_user_api_token(mock_auth_service, api_token_header)
assert exc_info.value.status_code == 401
assert exc_info.value.status_code == HTTP_401_UNAUTHORIZED
assert "Account is deactivated" in exc_info.value.detail
@pytest.mark.asyncio
async def test_get_current_user_api_token_service_exception(
self, mock_auth_service,
):
self, mock_auth_service: AsyncMock,
) -> None:
"""Test API token authentication with service exception."""
mock_auth_service.get_user_by_api_token.side_effect = Exception(
"Database error",
@@ -147,15 +159,15 @@ class TestApiTokenDependencies:
with pytest.raises(HTTPException) as exc_info:
await get_current_user_api_token(mock_auth_service, api_token_header)
assert exc_info.value.status_code == 401
assert exc_info.value.status_code == HTTP_401_UNAUTHORIZED
assert "Could not validate API token" in exc_info.value.detail
@pytest.mark.asyncio
async def test_get_current_user_flexible_uses_api_token(
self,
mock_auth_service,
test_user,
):
mock_auth_service: AsyncMock,
test_user: User,
) -> None:
"""Test flexible authentication uses API token when available."""
mock_auth_service.get_user_by_api_token.return_value = test_user
@@ -174,18 +186,20 @@ class TestApiTokenDependencies:
)
@pytest.mark.asyncio
async def test_get_current_user_flexible_falls_back_to_jwt(self, mock_auth_service):
async def test_get_current_user_flexible_falls_back_to_jwt(
self, mock_auth_service: AsyncMock,
) -> None:
"""Test flexible authentication falls back to JWT when no API token."""
# Mock the get_current_user function (normally imported)
with pytest.raises(Exception):
with pytest.raises(Exception, match="Database error|Could not validate"):
# This will fail because we can't easily mock the get_current_user import
# In a real test, you'd mock the import or use dependency injection
await get_current_user_flexible(mock_auth_service, "jwt_token", None)
@pytest.mark.asyncio
async def test_api_token_no_expiry_never_expires(
self, mock_auth_service, test_user,
):
self, mock_auth_service: AsyncMock, test_user: User,
) -> None:
"""Test API token with no expiry date never expires."""
test_user.api_token_expires_at = None
mock_auth_service.get_user_by_api_token.return_value = test_user
@@ -197,7 +211,9 @@ class TestApiTokenDependencies:
assert result == test_user
@pytest.mark.asyncio
async def test_api_token_with_whitespace(self, mock_auth_service, test_user):
async def test_api_token_with_whitespace(
self, mock_auth_service: AsyncMock, test_user: User,
) -> None:
"""Test API token with leading/trailing whitespace is handled correctly."""
mock_auth_service.get_user_by_api_token.return_value = test_user