Add comprehensive tests for playlist service and refactor socket service tests

- Introduced a new test suite for the PlaylistService covering various functionalities including creation, retrieval, updating, and deletion of playlists.
- Added tests for handling sounds within playlists, ensuring correct behavior when adding/removing sounds and managing current playlists.
- Refactored socket service tests for improved readability by adjusting function signatures.
- Cleaned up unnecessary whitespace in sound normalizer and sound scanner tests for consistency.
- Enhanced audio utility tests to ensure accurate hash and size calculations, including edge cases for nonexistent files.
- Removed redundant blank lines in cookie utility tests for cleaner code.
This commit is contained in:
JSC
2025-07-29 19:25:46 +02:00
parent 301b5dd794
commit 5ed19c8f0f
31 changed files with 4248 additions and 194 deletions

View File

@@ -14,7 +14,9 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_generate_api_token_success(
self, authenticated_client: AsyncClient, authenticated_user: User,
self,
authenticated_client: AsyncClient,
authenticated_user: User,
):
"""Test successful API token generation."""
request_data = {"expires_days": 30}
@@ -33,6 +35,7 @@ class TestApiTokenEndpoints:
# Verify token format (should be URL-safe base64)
import base64
try:
base64.urlsafe_b64decode(data["api_token"] + "===") # Add padding
except Exception:
@@ -40,7 +43,8 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_generate_api_token_default_expiry(
self, authenticated_client: AsyncClient,
self,
authenticated_client: AsyncClient,
):
"""Test API token generation with default expiry."""
response = await authenticated_client.post("/api/v1/auth/api-token", json={})
@@ -65,7 +69,8 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_generate_api_token_custom_expiry(
self, authenticated_client: AsyncClient,
self,
authenticated_client: AsyncClient,
):
"""Test API token generation with custom expiry."""
expires_days = 90
@@ -96,7 +101,8 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_generate_api_token_validation_errors(
self, authenticated_client: AsyncClient,
self,
authenticated_client: AsyncClient,
):
"""Test API token generation with validation errors."""
# Test minimum validation
@@ -124,7 +130,8 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_get_api_token_status_no_token(
self, authenticated_client: AsyncClient,
self,
authenticated_client: AsyncClient,
):
"""Test getting API token status when user has no token."""
response = await authenticated_client.get("/api/v1/auth/api-token/status")
@@ -138,7 +145,8 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_get_api_token_status_with_token(
self, authenticated_client: AsyncClient,
self,
authenticated_client: AsyncClient,
):
"""Test getting API token status when user has a token."""
# First generate a token
@@ -159,14 +167,18 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_get_api_token_status_expired_token(
self, authenticated_client: AsyncClient, authenticated_user: User,
self,
authenticated_client: AsyncClient,
authenticated_user: User,
):
"""Test getting API token status with expired token."""
# Mock expired token
with patch("app.utils.auth.TokenUtils.is_token_expired", return_value=True):
# Set a token on the user
authenticated_user.api_token = "expired_token"
authenticated_user.api_token_expires_at = datetime.now(UTC) - timedelta(days=1)
authenticated_user.api_token_expires_at = datetime.now(UTC) - timedelta(
days=1
)
response = await authenticated_client.get("/api/v1/auth/api-token/status")
@@ -185,7 +197,8 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_revoke_api_token_success(
self, authenticated_client: AsyncClient,
self,
authenticated_client: AsyncClient,
):
"""Test successful API token revocation."""
# First generate a token
@@ -195,7 +208,9 @@ class TestApiTokenEndpoints:
)
# Verify token exists
status_response = await authenticated_client.get("/api/v1/auth/api-token/status")
status_response = await authenticated_client.get(
"/api/v1/auth/api-token/status"
)
assert status_response.json()["has_token"] is True
# Revoke the token
@@ -206,12 +221,15 @@ class TestApiTokenEndpoints:
assert data["message"] == "API token revoked successfully"
# Verify token is gone
status_response = await authenticated_client.get("/api/v1/auth/api-token/status")
status_response = await authenticated_client.get(
"/api/v1/auth/api-token/status"
)
assert status_response.json()["has_token"] is False
@pytest.mark.asyncio
async def test_revoke_api_token_no_token(
self, authenticated_client: AsyncClient,
self,
authenticated_client: AsyncClient,
):
"""Test revoking API token when user has no token."""
response = await authenticated_client.delete("/api/v1/auth/api-token")
@@ -228,7 +246,9 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_api_token_authentication_success(
self, client: AsyncClient, authenticated_client: AsyncClient,
self,
client: AsyncClient,
authenticated_client: AsyncClient,
):
"""Test successful authentication using API token."""
# Generate API token
@@ -259,7 +279,9 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_api_token_authentication_expired_token(
self, client: AsyncClient, authenticated_client: AsyncClient,
self,
client: AsyncClient,
authenticated_client: AsyncClient,
):
"""Test authentication with expired API token."""
# Generate API token
@@ -299,7 +321,10 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_api_token_authentication_inactive_user(
self, client: AsyncClient, authenticated_client: AsyncClient, authenticated_user: User,
self,
client: AsyncClient,
authenticated_client: AsyncClient,
authenticated_user: User,
):
"""Test authentication with API token for inactive user."""
# Generate API token
@@ -322,7 +347,10 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_flexible_authentication_prefers_api_token(
self, client: AsyncClient, authenticated_client: AsyncClient, auth_cookies: dict[str, str],
self,
client: AsyncClient,
authenticated_client: AsyncClient,
auth_cookies: dict[str, str],
):
"""Test that flexible authentication prefers API token over cookie."""
# Generate API token