mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
fix: Fix test fixtures and auth headers for backend tests
- Remove mock_auth_service fixture dependency from test_internal_api.py and test_upload_api.py (now using autouse mock_auth_dependency from conftest) - Add upload_auth_headers fixture for file upload tests (no Content-Type header to avoid breaking multipart form data) - Add service._remote_client = None to audio search tests to prevent hitting remote flacfetch service - Add proper skipif decorator for TestRemoteCLIStyleParsing when flacfetch module is not properly installed - Add headers=auth_headers to emulator integration test for annotations endpoint - Fix headers=upload_auth_headers usage in all file upload tests
This commit is contained in:
5 files changed
+57
-21
No files matched your search
@@ -321,6 +321,7 @@ class TestReviewEndpoints:
|
||||
# Annotations endpoint should accept data (even if it just logs it)
|
||||
response = client.post(
|
||||
f"/api/review/{job_id}/v1/annotations",
|
||||
headers=auth_headers,
|
||||
json={"type": "test", "data": "test annotation"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
@@ -234,6 +234,7 @@ class TestAudioSearchServiceSearch:
|
||||
def test_search_no_results_raises_error(self):
|
||||
"""Test search raises NoResultsError when no results."""
|
||||
service = AudioSearchService(redacted_api_key=None, ops_api_key=None)
|
||||
service._remote_client = None # Force local mode to use mocked fetcher
|
||||
service._fetcher = Mock()
|
||||
service._fetcher.search.side_effect = NoResultsError("No results found")
|
||||
|
||||
@@ -258,6 +259,7 @@ class TestAudioSearchServiceSearch:
|
||||
))
|
||||
|
||||
service = AudioSearchService(redacted_api_key=None, ops_api_key=None)
|
||||
service._remote_client = None # Force local mode to use mocked fetcher
|
||||
service._fetcher = Mock()
|
||||
service._fetcher.search.return_value = mock_results
|
||||
|
||||
@@ -354,6 +356,7 @@ class TestAudioSearchServiceDownload:
|
||||
)
|
||||
|
||||
service = AudioSearchService(redacted_api_key=None, ops_api_key=None)
|
||||
service._remote_client = None # Force local mode to use mocked fetcher
|
||||
service._fetcher = Mock()
|
||||
service._fetcher.search.return_value = [mock_result]
|
||||
service._fetcher.download.return_value = mock_fetch_result
|
||||
|
||||
@@ -32,7 +32,7 @@ def _create_mock_job(job_id: str = "test123") -> Job:
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(mock_auth_service):
|
||||
def client():
|
||||
"""Create TestClient with mocked workers and job manager."""
|
||||
mock_creds = MagicMock()
|
||||
mock_creds.universe_domain = 'googleapis.com'
|
||||
|
||||
@@ -16,12 +16,20 @@ from unittest.mock import Mock, MagicMock, patch, AsyncMock
|
||||
from io import BytesIO
|
||||
|
||||
# Test the remote CLI's style parsing
|
||||
# Skip this class if flacfetch is not properly installed (remote_cli depends on it)
|
||||
try:
|
||||
from karaoke_gen.utils.remote_cli import RemoteKaraokeClient, Config
|
||||
_remote_cli_available = True
|
||||
except ImportError:
|
||||
_remote_cli_available = False
|
||||
|
||||
|
||||
@pytest.mark.skipif(not _remote_cli_available, reason="flacfetch not properly installed")
|
||||
class TestRemoteCLIStyleParsing:
|
||||
"""Test that remote CLI correctly parses style_params.json."""
|
||||
|
||||
def test_parse_style_params_extracts_file_paths(self):
|
||||
"""Test that _parse_style_params extracts all file references."""
|
||||
from karaoke_gen.utils.remote_cli import RemoteKaraokeClient, Config
|
||||
|
||||
# Create a mock style_params.json
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
@@ -79,7 +87,6 @@ class TestRemoteCLIStyleParsing:
|
||||
|
||||
def test_parse_style_params_handles_missing_files(self):
|
||||
"""Test that _parse_style_params ignores non-existent files."""
|
||||
from karaoke_gen.utils.remote_cli import RemoteKaraokeClient, Config
|
||||
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
style_params = {
|
||||
|
||||
@@ -10,6 +10,14 @@ from io import BytesIO
|
||||
from backend.models.job import Job, JobStatus
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def upload_auth_headers():
|
||||
"""Auth headers for file upload tests (no Content-Type, let multipart work)."""
|
||||
return {
|
||||
"Authorization": "Bearer test-admin-token"
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_job():
|
||||
"""Create a standard mock job."""
|
||||
@@ -47,7 +55,7 @@ def mock_services(mock_job):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(mock_services, mock_auth_service):
|
||||
def client(mock_services):
|
||||
"""Create TestClient with mocked dependencies."""
|
||||
mock_creds = MagicMock()
|
||||
mock_creds.universe_domain = 'googleapis.com'
|
||||
@@ -65,37 +73,41 @@ def client(mock_services, mock_auth_service):
|
||||
class TestFileUploadEndpoint:
|
||||
"""Tests for POST /api/jobs/upload."""
|
||||
|
||||
def test_upload_flac_returns_200(self, client, mock_services, auth_headers):
|
||||
def test_upload_flac_returns_200(self, client, mock_services, upload_auth_headers):
|
||||
"""Test uploading FLAC file returns 200."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("test.flac", BytesIO(b"fake audio"), "audio/flac")},
|
||||
data={"artist": "Test Artist", "title": "Test Song"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_upload_mp3_returns_200(self, client, mock_services, auth_headers):
|
||||
def test_upload_mp3_returns_200(self, client, mock_services, upload_auth_headers):
|
||||
"""Test uploading MP3 file returns 200."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("test.mp3", BytesIO(b"fake audio"), "audio/mpeg")},
|
||||
data={"artist": "Test Artist", "title": "Test Song"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_upload_wav_returns_200(self, client, mock_services, auth_headers):
|
||||
def test_upload_wav_returns_200(self, client, mock_services, upload_auth_headers):
|
||||
"""Test uploading WAV file returns 200."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("test.wav", BytesIO(b"fake audio"), "audio/wav")},
|
||||
data={"artist": "Test Artist", "title": "Test Song"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_upload_returns_job_id(self, client, mock_services, auth_headers):
|
||||
def test_upload_returns_job_id(self, client, mock_services, upload_auth_headers):
|
||||
"""Test upload response contains job_id."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("test.flac", BytesIO(b"fake audio"), "audio/flac")},
|
||||
data={"artist": "Test", "title": "Song"}
|
||||
)
|
||||
@@ -103,10 +115,11 @@ class TestFileUploadEndpoint:
|
||||
assert "job_id" in data
|
||||
assert data["status"] == "success"
|
||||
|
||||
def test_upload_returns_filename(self, client, mock_services, auth_headers):
|
||||
def test_upload_returns_filename(self, client, mock_services, upload_auth_headers):
|
||||
"""Test upload response contains original filename."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("my_song.flac", BytesIO(b"fake audio"), "audio/flac")},
|
||||
data={"artist": "Test", "title": "Song"}
|
||||
)
|
||||
@@ -114,84 +127,93 @@ class TestFileUploadEndpoint:
|
||||
assert "filename" in data
|
||||
assert data["filename"] == "my_song.flac"
|
||||
|
||||
def test_upload_rejects_txt_file(self, client, mock_services, auth_headers):
|
||||
def test_upload_rejects_txt_file(self, client, mock_services, upload_auth_headers):
|
||||
"""Test upload rejects text files."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("test.txt", BytesIO(b"not audio"), "text/plain")},
|
||||
data={"artist": "Test", "title": "Song"}
|
||||
)
|
||||
assert response.status_code == 400
|
||||
|
||||
def test_upload_rejects_pdf_file(self, client, mock_services, auth_headers):
|
||||
def test_upload_rejects_pdf_file(self, client, mock_services, upload_auth_headers):
|
||||
"""Test upload rejects PDF files."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("doc.pdf", BytesIO(b"pdf content"), "application/pdf")},
|
||||
data={"artist": "Test", "title": "Song"}
|
||||
)
|
||||
assert response.status_code == 400
|
||||
|
||||
def test_upload_rejects_exe_file(self, client, mock_services, auth_headers):
|
||||
def test_upload_rejects_exe_file(self, client, mock_services, upload_auth_headers):
|
||||
"""Test upload rejects executable files."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("app.exe", BytesIO(b"exe content"), "application/octet-stream")},
|
||||
data={"artist": "Test", "title": "Song"}
|
||||
)
|
||||
assert response.status_code == 400
|
||||
|
||||
def test_upload_requires_artist(self, client, mock_services, auth_headers):
|
||||
def test_upload_requires_artist(self, client, mock_services, upload_auth_headers):
|
||||
"""Test upload requires artist field."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("test.flac", BytesIO(b"audio"), "audio/flac")},
|
||||
data={"title": "Song"} # Missing artist
|
||||
)
|
||||
assert response.status_code == 422
|
||||
|
||||
def test_upload_requires_title(self, client, mock_services, auth_headers):
|
||||
def test_upload_requires_title(self, client, mock_services, upload_auth_headers):
|
||||
"""Test upload requires title field."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("test.flac", BytesIO(b"audio"), "audio/flac")},
|
||||
data={"artist": "Artist"} # Missing title
|
||||
)
|
||||
assert response.status_code == 422
|
||||
|
||||
def test_upload_requires_file(self, client, mock_services, auth_headers):
|
||||
def test_upload_requires_file(self, client, mock_services, upload_auth_headers):
|
||||
"""Test upload requires file."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
data={"artist": "Artist", "title": "Song"}
|
||||
# Missing file
|
||||
)
|
||||
assert response.status_code == 422
|
||||
|
||||
def test_upload_triggers_workers(self, client, mock_services, auth_headers):
|
||||
def test_upload_triggers_workers(self, client, mock_services, upload_auth_headers):
|
||||
"""Test upload triggers audio and lyrics workers."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("test.flac", BytesIO(b"audio"), "audio/flac")},
|
||||
data={"artist": "Test", "title": "Song"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
# Workers should be triggered in background
|
||||
|
||||
def test_upload_creates_job(self, client, mock_services, auth_headers):
|
||||
def test_upload_creates_job(self, client, mock_services, upload_auth_headers):
|
||||
"""Test upload creates job in job manager."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("test.flac", BytesIO(b"audio"), "audio/flac")},
|
||||
data={"artist": "Test", "title": "Song"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
mock_services['job_manager'].create_job.assert_called()
|
||||
|
||||
def test_upload_stores_file_to_gcs(self, client, mock_services, auth_headers):
|
||||
def test_upload_stores_file_to_gcs(self, client, mock_services, upload_auth_headers):
|
||||
"""Test upload stores file to GCS."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("test.flac", BytesIO(b"audio"), "audio/flac")},
|
||||
data={"artist": "Test", "title": "Song"}
|
||||
)
|
||||
@@ -202,28 +224,31 @@ class TestFileUploadEndpoint:
|
||||
class TestUploadValidation:
|
||||
"""Tests for upload validation logic."""
|
||||
|
||||
def test_upload_accepts_m4a(self, client, mock_services, auth_headers):
|
||||
def test_upload_accepts_m4a(self, client, mock_services, upload_auth_headers):
|
||||
"""Test upload accepts M4A files."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("test.m4a", BytesIO(b"audio"), "audio/mp4")},
|
||||
data={"artist": "Test", "title": "Song"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_upload_accepts_ogg(self, client, mock_services, auth_headers):
|
||||
def test_upload_accepts_ogg(self, client, mock_services, upload_auth_headers):
|
||||
"""Test upload accepts OGG files."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("test.ogg", BytesIO(b"audio"), "audio/ogg")},
|
||||
data={"artist": "Test", "title": "Song"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_upload_accepts_aac(self, client, mock_services, auth_headers):
|
||||
def test_upload_accepts_aac(self, client, mock_services, upload_auth_headers):
|
||||
"""Test upload accepts AAC files."""
|
||||
response = client.post(
|
||||
"/api/jobs/upload",
|
||||
headers=upload_auth_headers,
|
||||
files={"file": ("test.aac", BytesIO(b"audio"), "audio/aac")},
|
||||
data={"artist": "Test", "title": "Song"}
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user