Refactor sound and extraction services to include user and timestamp fields
All checks were successful
Backend CI / lint (push) Successful in 18m8s
Backend CI / test (push) Successful in 53m35s

- Updated ExtractionInfo to include user_id, created_at, and updated_at fields.
- Modified ExtractionService to return user and timestamp information in extraction responses.
- Enhanced sound serialization in PlayerState to include extraction URL if available.
- Adjusted PlaylistRepository to load sound extractions when retrieving playlist sounds.
- Added tests for new fields in extraction and sound endpoints, ensuring proper response structure.
- Created new test file endpoints for sound downloads and thumbnail retrievals, including success and error cases.
- Refactored various test cases for consistency and clarity, ensuring proper mocking and assertions.
This commit is contained in:
JSC
2025-08-03 20:54:14 +02:00
parent 77446cb5a8
commit b4f0f54516
20 changed files with 780 additions and 73 deletions

View File

@@ -306,7 +306,8 @@ class TestAdminSoundEndpoints:
@pytest.mark.asyncio
async def test_normalize_all_sounds_unauthenticated(
self, client: AsyncClient,
self,
client: AsyncClient,
) -> None:
"""Test normalizing sounds without authentication."""
response = await client.post("/api/v1/admin/sounds/normalize/all")

View File

@@ -125,7 +125,8 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_generate_api_token_unauthenticated(
self, client: AsyncClient,
self,
client: AsyncClient,
) -> None:
"""Test API token generation without authentication."""
response = await client.post(
@@ -197,7 +198,8 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_get_api_token_status_unauthenticated(
self, client: AsyncClient,
self,
client: AsyncClient,
) -> None:
"""Test getting API token status without authentication."""
response = await client.get("/api/v1/auth/api-token/status")
@@ -277,7 +279,8 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_api_token_authentication_invalid_token(
self, client: AsyncClient,
self,
client: AsyncClient,
) -> None:
"""Test authentication with invalid API token."""
headers = {"API-TOKEN": "invalid_token"}
@@ -312,7 +315,8 @@ class TestApiTokenEndpoints:
@pytest.mark.asyncio
async def test_api_token_authentication_empty_token(
self, client: AsyncClient,
self,
client: AsyncClient,
) -> None:
"""Test authentication with empty API-TOKEN header."""
# Empty token

View File

@@ -1,34 +1,134 @@
"""Tests for extraction API endpoints."""
from unittest.mock import patch
import pytest
import pytest_asyncio
from httpx import AsyncClient
from sqlmodel.ext.asyncio.session import AsyncSession
from app.models.extraction import Extraction
from app.models.user import User
class TestExtractionEndpoints:
"""Test extraction API endpoints."""
@pytest_asyncio.fixture
async def test_extraction(
self,
test_session: AsyncSession,
authenticated_user: User,
) -> Extraction:
"""Create a test extraction."""
extraction = Extraction(
url="https://www.youtube.com/watch?v=test",
user_id=authenticated_user.id,
service="youtube",
service_id="test",
title="Test Video",
status="completed",
)
test_session.add(extraction)
await test_session.commit()
await test_session.refresh(extraction)
return extraction
@pytest.mark.asyncio
async def test_create_extraction_success(
self,
test_client: AsyncClient,
auth_cookies: dict[str, str],
authenticated_client: AsyncClient,
authenticated_user: User,
) -> None:
"""Test successful extraction creation."""
# Set cookies on client instance to avoid deprecation warning
test_client.cookies.update(auth_cookies)
"""Test successful extraction creation with proper response format."""
# Store user ID to avoid session issues
user_id = authenticated_user.id
response = await test_client.post(
"/api/v1/extractions/",
params={"url": "https://www.youtube.com/watch?v=test"},
with patch(
"app.services.extraction_processor.extraction_processor.queue_extraction",
):
response = await authenticated_client.post(
"/api/v1/extractions/",
params={"url": "https://www.youtube.com/watch?v=test"},
)
assert response.status_code == 200
data = response.json()
# Verify response structure
assert "message" in data
assert "extraction" in data
extraction_data = data["extraction"]
# Verify all required fields including timestamps
assert "id" in extraction_data
assert "url" in extraction_data
assert "user_id" in extraction_data
assert "status" in extraction_data
assert "created_at" in extraction_data
assert "updated_at" in extraction_data
assert extraction_data["url"] == "https://www.youtube.com/watch?v=test"
assert extraction_data["user_id"] == user_id
assert extraction_data["status"] == "pending"
@pytest.mark.asyncio
async def test_get_extraction_success(
self,
authenticated_client: AsyncClient,
authenticated_user: User,
test_extraction: Extraction,
) -> None:
"""Test successful extraction retrieval with timestamp fields."""
response = await authenticated_client.get(
f"/api/v1/extractions/{test_extraction.id}",
)
# This will fail because we don't have actual extraction service mocked
# But at least we'll get past authentication
assert response.status_code in [200, 400, 500] # Allow any non-auth error
assert response.status_code == 200
data = response.json()
# Verify all fields including timestamps
assert data["id"] == test_extraction.id
assert data["url"] == test_extraction.url
assert data["user_id"] == test_extraction.user_id
assert data["service"] == test_extraction.service
assert data["service_id"] == test_extraction.service_id
assert data["title"] == test_extraction.title
assert data["status"] == test_extraction.status
assert "created_at" in data
assert "updated_at" in data
@pytest.mark.asyncio
async def test_get_user_extractions_success(
self,
authenticated_client: AsyncClient,
authenticated_user: User,
test_extraction: Extraction,
) -> None:
"""Test successful user extractions retrieval with timestamp fields."""
response = await authenticated_client.get(
"/api/v1/extractions/",
)
assert response.status_code == 200
data = response.json()
# Verify response structure
assert "extractions" in data
assert len(data["extractions"]) >= 1
extraction_data = data["extractions"][0]
# Verify all fields including timestamps
assert "id" in extraction_data
assert "url" in extraction_data
assert "user_id" in extraction_data
assert "status" in extraction_data
assert "created_at" in extraction_data
assert "updated_at" in extraction_data
@pytest.mark.asyncio
async def test_create_extraction_unauthenticated(
self, test_client: AsyncClient,
self,
test_client: AsyncClient,
) -> None:
"""Test extraction creation without authentication."""
response = await test_client.post(
@@ -41,7 +141,8 @@ class TestExtractionEndpoints:
@pytest.mark.asyncio
async def test_get_extraction_unauthenticated(
self, test_client: AsyncClient,
self,
test_client: AsyncClient,
) -> None:
"""Test extraction retrieval without authentication."""
response = await test_client.get("/api/v1/extractions/1")

View File

@@ -0,0 +1,245 @@
"""Tests for file serving API endpoints."""
import tempfile
from pathlib import Path
from unittest.mock import patch
import pytest
import pytest_asyncio
from fastapi import status
from httpx import AsyncClient
from sqlmodel.ext.asyncio.session import AsyncSession
from app.models.sound import Sound
from app.models.user import User
class TestFilesEndpoints:
"""Test file serving endpoints."""
@pytest_asyncio.fixture
async def test_sound(
self,
test_session: AsyncSession,
) -> Sound:
"""Create a test sound."""
sound = Sound(
type="SDB",
name="Test Sound",
filename="test.mp3",
duration=30000,
size=1024,
hash="test_hash",
play_count=0,
is_normalized=False,
is_music=True,
is_deletable=True,
)
test_session.add(sound)
await test_session.commit()
await test_session.refresh(sound)
return sound
@pytest_asyncio.fixture
async def test_sound_with_thumbnail(
self,
test_session: AsyncSession,
) -> Sound:
"""Create a test sound with thumbnail."""
sound = Sound(
type="EXT",
name="Test Extracted Sound",
filename="extracted.mp3",
duration=60000,
size=2048,
hash="extracted_hash",
thumbnail="test_thumb.jpg",
play_count=5,
is_normalized=True,
is_music=True,
is_deletable=True,
)
test_session.add(sound)
await test_session.commit()
await test_session.refresh(sound)
return sound
@pytest.mark.asyncio
async def test_download_sound_success(
self,
authenticated_client: AsyncClient,
authenticated_user: User,
test_sound: Sound,
) -> None:
"""Test successful sound download."""
with (
patch("app.api.v1.files.get_sound_file_path") as mock_get_path,
tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file,
):
# Setup mock file - write to temp file and close it
temp_path = Path(temp_file.name)
temp_file.write(b"fake audio data")
temp_file.close()
mock_get_path.return_value = temp_path
try:
response = await authenticated_client.get(
f"/api/v1/files/sounds/{test_sound.id}/download",
)
assert response.status_code == status.HTTP_200_OK
finally:
# Clean up the temp file
temp_path.unlink(missing_ok=True)
assert response.headers["content-type"] == "audio/mpeg"
assert "attachment" in response.headers.get("content-disposition", "")
assert response.content == b"fake audio data"
@pytest.mark.asyncio
@pytest.mark.asyncio
async def test_download_sound_not_found(
self,
authenticated_client: AsyncClient,
authenticated_user: User,
) -> None:
"""Test download with non-existent sound."""
response = await authenticated_client.get(
"/api/v1/files/sounds/99999/download",
)
assert response.status_code == status.HTTP_404_NOT_FOUND
assert "not found" in response.json()["detail"].lower()
@pytest.mark.asyncio
async def test_download_sound_file_not_found_on_disk(
self,
authenticated_client: AsyncClient,
authenticated_user: User,
test_sound: Sound,
) -> None:
"""Test download when file doesn't exist on disk."""
with patch("app.api.v1.files.get_sound_file_path") as mock_get_path:
# Mock path that doesn't exist
mock_get_path.return_value = Path("/non/existent/file.mp3")
response = await authenticated_client.get(
f"/api/v1/files/sounds/{test_sound.id}/download",
)
assert response.status_code == status.HTTP_404_NOT_FOUND
assert "not found on disk" in response.json()["detail"].lower()
@pytest.mark.asyncio
async def test_download_sound_unauthorized(
self,
client: AsyncClient,
test_sound: Sound,
) -> None:
"""Test download without authentication."""
response = await client.get(f"/api/v1/files/sounds/{test_sound.id}/download")
assert response.status_code == status.HTTP_401_UNAUTHORIZED
@pytest.mark.asyncio
async def test_get_thumbnail_success(
self,
authenticated_client: AsyncClient,
authenticated_user: User,
test_sound_with_thumbnail: Sound,
) -> None:
"""Test successful thumbnail retrieval."""
with tempfile.TemporaryDirectory() as temp_dir:
temp_dir_path = Path(temp_dir)
# Create the actual thumbnail file
thumbnail_path = temp_dir_path / test_sound_with_thumbnail.thumbnail
thumbnail_path.write_bytes(b"fake image data")
with patch("app.api.v1.files.settings") as mock_settings:
mock_settings.EXTRACTION_THUMBNAILS_DIR = temp_dir_path
response = await authenticated_client.get(
f"/api/v1/files/sounds/{test_sound_with_thumbnail.id}/thumbnail",
)
assert response.status_code == status.HTTP_200_OK
assert response.headers["content-type"] == "image/jpeg"
assert "inline" in response.headers.get("content-disposition", "")
assert "Cache-Control" in response.headers
@pytest.mark.asyncio
async def test_get_thumbnail_no_thumbnail_field(
self,
authenticated_client: AsyncClient,
authenticated_user: User,
test_sound: Sound, # Sound without thumbnail
) -> None:
"""Test thumbnail request for sound without thumbnail."""
response = await authenticated_client.get(
f"/api/v1/files/sounds/{test_sound.id}/thumbnail",
)
assert response.status_code == status.HTTP_404_NOT_FOUND
assert "no thumbnail available" in response.json()["detail"].lower()
@pytest.mark.asyncio
async def test_get_thumbnail_file_not_found_on_disk(
self,
authenticated_client: AsyncClient,
authenticated_user: User,
test_sound_with_thumbnail: Sound,
) -> None:
"""Test thumbnail request when file doesn't exist on disk."""
with patch("app.core.config.settings") as mock_settings:
# Mock directory that doesn't contain the thumbnail
mock_settings.EXTRACTION_THUMBNAILS_DIR = "/non/existent/directory"
response = await authenticated_client.get(
f"/api/v1/files/sounds/{test_sound_with_thumbnail.id}/thumbnail",
)
assert response.status_code == status.HTTP_404_NOT_FOUND
assert "not found on disk" in response.json()["detail"].lower()
@pytest.mark.asyncio
async def test_get_thumbnail_unauthorized(
self,
client: AsyncClient,
test_sound_with_thumbnail: Sound,
) -> None:
"""Test thumbnail request without authentication."""
response = await client.get(
f"/api/v1/files/sounds/{test_sound_with_thumbnail.id}/thumbnail",
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED
@pytest.mark.asyncio
async def test_download_normalized_sound(
self,
authenticated_client: AsyncClient,
authenticated_user: User,
test_sound_with_thumbnail: Sound, # This sound is normalized
) -> None:
"""Test downloading a normalized sound returns the normalized file."""
with (
patch("app.api.v1.files.get_sound_file_path") as mock_get_path,
tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file,
):
# Setup mock normalized file
temp_path = Path(temp_file.name)
temp_file.write(b"normalized audio data")
temp_file.close()
mock_get_path.return_value = temp_path
try:
response = await authenticated_client.get(
f"/api/v1/files/sounds/{test_sound_with_thumbnail.id}/download",
)
assert response.status_code == status.HTTP_200_OK
assert response.content == b"normalized audio data"
# Should use normalized filename
mock_get_path.assert_called_once_with(test_sound_with_thumbnail)
finally:
temp_path.unlink(missing_ok=True)

View File

@@ -31,6 +31,9 @@ class TestSoundEndpoints:
"status": "pending",
"error": None,
"sound_id": None,
"user_id": authenticated_user.id,
"created_at": "2025-08-03T12:00:00Z",
"updated_at": "2025-08-03T12:00:00Z",
}
with (
@@ -107,6 +110,9 @@ class TestSoundEndpoints:
"status": "completed",
"error": None,
"sound_id": 42,
"user_id": authenticated_user.id,
"created_at": "2025-08-03T12:00:00Z",
"updated_at": "2025-08-03T12:00:00Z",
}
with patch(
@@ -158,6 +164,9 @@ class TestSoundEndpoints:
"status": "completed",
"error": None,
"sound_id": 42,
"user_id": authenticated_user.id,
"created_at": "2025-08-03T12:00:00Z",
"updated_at": "2025-08-03T12:00:00Z",
},
{
"id": 2,
@@ -168,6 +177,9 @@ class TestSoundEndpoints:
"status": "pending",
"error": None,
"sound_id": None,
"user_id": authenticated_user.id,
"created_at": "2025-08-03T12:00:00Z",
"updated_at": "2025-08-03T12:00:00Z",
},
]