Files
sdb2-backend/tests/utils/test_token_utils.py
JSC 3dc21337f9 Refactor tests for improved consistency and readability
- Updated test cases in `test_auth_endpoints.py` to ensure consistent formatting and style.
- Enhanced `test_socket_endpoints.py` with consistent parameter formatting and improved readability.
- Cleaned up `conftest.py` by ensuring consistent parameter formatting in fixtures.
- Added comprehensive tests for API token dependencies in `test_api_token_dependencies.py`.
- Refactored `test_auth_service.py` to maintain consistent parameter formatting.
- Cleaned up `test_oauth_service.py` by removing unnecessary imports.
- Improved `test_socket_service.py` with consistent formatting and readability.
- Enhanced `test_cookies.py` by ensuring consistent formatting and readability.
- Introduced new tests for token utilities in `test_token_utils.py` to validate token generation and expiration logic.
2025-07-27 15:11:47 +02:00

81 lines
3.0 KiB
Python

"""Tests for token utilities."""
from datetime import UTC, datetime, timedelta
from app.utils.auth import TokenUtils
class TestTokenUtils:
"""Test token utility functions."""
def test_generate_api_token(self):
"""Test API token generation."""
token = TokenUtils.generate_api_token()
# Should be a string
assert isinstance(token, str)
# Should not be empty
assert len(token) > 0
# Should be URL-safe base64 (43 characters for 32 bytes)
assert len(token) == 43
# Should be unique (generate multiple and check they're different)
tokens = [TokenUtils.generate_api_token() for _ in range(10)]
assert len(set(tokens)) == 10
def test_is_token_expired_none(self):
"""Test token expiration check with None expires_at."""
result = TokenUtils.is_token_expired(None)
assert result is False
def test_is_token_expired_future_naive(self):
"""Test token expiration check with future naive datetime."""
# Use UTC time for naive datetime (as the function assumes)
expires_at = datetime.utcnow() + timedelta(hours=1)
result = TokenUtils.is_token_expired(expires_at)
assert result is False
def test_is_token_expired_past_naive(self):
"""Test token expiration check with past naive datetime."""
# Use UTC time for naive datetime (as the function assumes)
expires_at = datetime.utcnow() - timedelta(hours=1)
result = TokenUtils.is_token_expired(expires_at)
assert result is True
def test_is_token_expired_future_aware(self):
"""Test token expiration check with future timezone-aware datetime."""
expires_at = datetime.now(UTC) + timedelta(hours=1)
result = TokenUtils.is_token_expired(expires_at)
assert result is False
def test_is_token_expired_past_aware(self):
"""Test token expiration check with past timezone-aware datetime."""
expires_at = datetime.now(UTC) - timedelta(hours=1)
result = TokenUtils.is_token_expired(expires_at)
assert result is True
def test_is_token_expired_edge_case_now(self):
"""Test token expiration check with time very close to now."""
# Token expires in 1 second
expires_at = datetime.now(UTC) + timedelta(seconds=1)
result = TokenUtils.is_token_expired(expires_at)
assert result is False
# Token expired 1 second ago
expires_at = datetime.now(UTC) - timedelta(seconds=1)
result = TokenUtils.is_token_expired(expires_at)
assert result is True
def test_is_token_expired_timezone_conversion(self):
"""Test token expiration check with different timezone."""
from zoneinfo import ZoneInfo
# Create a datetime in a different timezone
eastern = ZoneInfo("US/Eastern")
expires_at = datetime.now(eastern) + timedelta(hours=1)
result = TokenUtils.is_token_expired(expires_at)
assert result is False