Add more tests
This commit is contained in:
@@ -3,7 +3,7 @@ Tests for Admin Authentication.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from fastapi import HTTPException
|
||||
@@ -132,6 +132,47 @@ class TestTokenRepository:
|
||||
with patch.object(repo, "_now", return_value=datetime.utcnow()):
|
||||
assert repo.is_valid("test-token") is False
|
||||
|
||||
def test_is_valid_expired_token_timezone_aware(self):
|
||||
"""Test expired token with timezone-aware datetime.
|
||||
|
||||
This verifies the fix for comparing timezone-aware and naive datetimes.
|
||||
The auth API now creates tokens with timezone-aware expiration dates.
|
||||
"""
|
||||
with patch("inference.data.repositories.token_repository.BaseRepository._session") as mock_ctx:
|
||||
mock_session = MagicMock()
|
||||
mock_ctx.return_value.__enter__.return_value = mock_session
|
||||
|
||||
# Create token with timezone-aware expiration (as auth API now does)
|
||||
mock_token = AdminToken(
|
||||
token="test-token",
|
||||
name="Test",
|
||||
is_active=True,
|
||||
expires_at=datetime.now(timezone.utc) - timedelta(days=1),
|
||||
)
|
||||
mock_session.get.return_value = mock_token
|
||||
|
||||
repo = TokenRepository()
|
||||
# _now() returns timezone-aware datetime, should compare correctly
|
||||
assert repo.is_valid("test-token") is False
|
||||
|
||||
def test_is_valid_not_expired_token_timezone_aware(self):
|
||||
"""Test non-expired token with timezone-aware datetime."""
|
||||
with patch("inference.data.repositories.token_repository.BaseRepository._session") as mock_ctx:
|
||||
mock_session = MagicMock()
|
||||
mock_ctx.return_value.__enter__.return_value = mock_session
|
||||
|
||||
# Create token with timezone-aware expiration in the future
|
||||
mock_token = AdminToken(
|
||||
token="test-token",
|
||||
name="Test",
|
||||
is_active=True,
|
||||
expires_at=datetime.now(timezone.utc) + timedelta(days=1),
|
||||
)
|
||||
mock_session.get.return_value = mock_token
|
||||
|
||||
repo = TokenRepository()
|
||||
assert repo.is_valid("test-token") is True
|
||||
|
||||
def test_is_valid_token_not_found(self):
|
||||
"""Test token not found."""
|
||||
with patch("inference.data.repositories.token_repository.BaseRepository._session") as mock_ctx:
|
||||
|
||||
Reference in New Issue
Block a user