feat(api): finish Group A stubbed endpoints

Implements previously-stubbed /api/v1 endpoints (hexagonal: ports -> repos
-> services -> routers wired in deps):

- playlists: GET /{id}/cover (serves stored cover, 404 when absent)
- settings: GET/PATCH /settings + GET/PUT /settings/scrobbling — lazy
  per-user row, write-only Fernet-encrypted scrobble session key; adds
  user_settings table + migration (chains off dc126696f5a6)
- storage: GET /duplicates, /broken, /missing-metadata + admin POST
  /cleanup (arq cleanup_storage worker; reconciles local refs only,
  guarded against a storage-outage mass delete)
- admin: GET /services, /sources, /settings + POST /reindex; PATCH
  /settings and /sources/{source} return 501 (config is env-managed)

Adds NotSupportedError (-> HTTP 501). Integration tests for each surface.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Цвылев Александр Вадимович
2026-07-28 14:33:12 +03:00
parent df580578f6
commit a263272935
27 changed files with 1362 additions and 27 deletions
+142
View File
@@ -0,0 +1,142 @@
"""Integration tests for the admin instance-management endpoints.
Covers ``/admin/services`` (dependency health), ``/admin/sources``,
``/admin/reindex`` (enqueue), and ``/admin/settings`` (effective config) plus
their admin gating. Requires a reachable Postgres; skips otherwise.
"""
import asyncio
import os
from collections.abc import AsyncIterator
from pathlib import Path
import pytest
from app.core.config import get_settings
from app.infrastructure.db import Base, dispose_engine, get_engine, session_scope
from app.infrastructure.db.repositories import (
SqlAlchemyRefreshTokenRepository,
SqlAlchemyUserRepository,
)
from asgi_lifespan import LifespanManager
from httpx import ASGITransport, AsyncClient
pytestmark = pytest.mark.asyncio
_db_reachable_cache: bool | None = None
async def _db_reachable() -> bool:
global _db_reachable_cache
if _db_reachable_cache is not None:
return _db_reachable_cache
from sqlalchemy import text
try:
async with asyncio.timeout(3):
async with get_engine().connect() as conn:
await conn.execute(text("SELECT 1"))
_db_reachable_cache = True
except Exception:
_db_reachable_cache = False
return _db_reachable_cache
@pytest.fixture
async def api(tmp_path: Path) -> AsyncIterator[AsyncClient]:
if not await _db_reachable():
pytest.skip("Postgres not reachable — integration test skipped.")
os.environ["MEDIA_PATH"] = str(tmp_path)
get_settings.cache_clear()
try:
async with get_engine().begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
from app.application.user_service import UserService
from app.core.security import Argon2PasswordHasher
async with session_scope() as session:
svc = UserService(
users=SqlAlchemyUserRepository(session),
refresh_tokens=SqlAlchemyRefreshTokenRepository(session),
hasher=Argon2PasswordHasher(),
)
await svc.create_user(username="user", password="testpass1", is_superuser=False)
await svc.create_user(username="admin", password="testpass1", is_superuser=True)
from app.main import create_app
app = create_app()
async with LifespanManager(app):
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
yield client
async with get_engine().begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await dispose_engine()
finally:
os.environ.pop("MEDIA_PATH", None)
get_settings.cache_clear()
async def _auth(api: AsyncClient, username: str) -> dict[str, str]:
resp = await api.post(
"/api/v1/auth/login", json={"username": username, "password": "testpass1"}
)
assert resp.status_code == 200, resp.text
return {"Authorization": f"Bearer {resp.json()['access_token']}"}
async def test_services_reports_dependency_health(api: AsyncClient) -> None:
headers = await _auth(api, "admin")
resp = await api.get("/api/v1/admin/services", headers=headers)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["database"] == "ok"
assert body["redis"] in ("ok", "down") # redis is up in CI/dev, but don't hard-require it
assert body["ml"] == "skipped" # no ML_SERVICE_URL configured
async def test_services_requires_admin(api: AsyncClient) -> None:
headers = await _auth(api, "user")
resp = await api.get("/api/v1/admin/services", headers=headers)
assert resp.status_code == 403
async def test_sources_list_is_admin_only(api: AsyncClient) -> None:
user = await _auth(api, "user")
assert (await api.get("/api/v1/admin/sources", headers=user)).status_code == 403
admin = await _auth(api, "admin")
resp = await api.get("/api/v1/admin/sources", headers=admin)
assert resp.status_code == 200, resp.text
assert isinstance(resp.json(), list)
async def test_reindex_without_indexable_source_is_503(api: AsyncClient) -> None:
# No LOCAL_MEDIA_IMPORT_PATH configured → nothing to index.
headers = await _auth(api, "admin")
resp = await api.post("/api/v1/admin/reindex", headers=headers)
assert resp.status_code == 503, resp.text
async def test_settings_exposes_effective_config_without_secrets(api: AsyncClient) -> None:
headers = await _auth(api, "admin")
resp = await api.get("/api/v1/admin/settings", headers=headers)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["environment"] == "test"
assert body["storage_backend"] == "local"
assert body["allow_registration"] is True
# No secret material should ever appear in the payload.
assert "jwt_secret" not in body
assert "subsonic_secret_key" not in body
async def test_settings_requires_admin(api: AsyncClient) -> None:
headers = await _auth(api, "user")
resp = await api.get("/api/v1/admin/settings", headers=headers)
assert resp.status_code == 403
+153
View File
@@ -0,0 +1,153 @@
"""Integration test for the playlist cover endpoint.
Master already covers playlist reorder in ``test_playlists_api.py``; this file
only exercises ``GET /playlists/{id}/cover`` (served when set, 404 otherwise).
Requires a reachable Postgres; skips otherwise.
"""
import asyncio
import os
import uuid
from collections.abc import AsyncIterator
from pathlib import Path
import pytest
from app.core.config import get_settings
from app.infrastructure.db import Base, dispose_engine, get_engine, session_scope
from app.infrastructure.db.repositories import (
SqlAlchemyPlaylistRepository,
SqlAlchemyRefreshTokenRepository,
SqlAlchemyUserRepository,
)
from app.infrastructure.storage.provider import get_file_storage
from asgi_lifespan import LifespanManager
from httpx import ASGITransport, AsyncClient
pytestmark = pytest.mark.asyncio
# A minimal valid 1x1 PNG.
_PNG_BYTES = bytes.fromhex(
"89504e470d0a1a0a0000000d4948445200000001000000010802000000907753"
"de0000000c4944415408d763f8cfc0f01f0005000155a2b4f60000000049454e44ae426082"
)
_db_reachable_cache: bool | None = None
async def _db_reachable() -> bool:
global _db_reachable_cache
if _db_reachable_cache is not None:
return _db_reachable_cache
from sqlalchemy import text
try:
async with asyncio.timeout(3):
async with get_engine().connect() as conn:
await conn.execute(text("SELECT 1"))
_db_reachable_cache = True
except Exception:
_db_reachable_cache = False
return _db_reachable_cache
async def _seed_playlist(*, owner_id: uuid.UUID, cover: Path | None) -> uuid.UUID:
"""Create a playlist owned by ``owner_id``; if ``cover`` is given, store it
and point the playlist's ``cover_path`` at it."""
from app.infrastructure.db.models.playlist import PlaylistModel
async with session_scope() as session:
playlist = await SqlAlchemyPlaylistRepository(session).add(
name="Mix", description=None, owner_id=owner_id
)
if cover is not None:
key = f"covers/playlists/{playlist.id}.png"
await get_file_storage().save_file(key, cover)
row = await session.get(PlaylistModel, playlist.id)
assert row is not None
row.cover_path = key
return playlist.id
@pytest.fixture
async def ctx(tmp_path: Path) -> AsyncIterator[tuple[AsyncClient, uuid.UUID, Path]]:
if not await _db_reachable():
pytest.skip("Postgres not reachable — integration test skipped.")
os.environ["MEDIA_PATH"] = str(tmp_path)
get_settings.cache_clear()
import app.infrastructure.storage.provider as _storage_provider
_storage_provider._storage = None
try:
async with get_engine().begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
from app.application.user_service import UserService
from app.core.security import Argon2PasswordHasher
async with session_scope() as session:
user = await UserService(
users=SqlAlchemyUserRepository(session),
refresh_tokens=SqlAlchemyRefreshTokenRepository(session),
hasher=Argon2PasswordHasher(),
).create_user(username="pluser", password="testpass1", is_superuser=False)
user_id = user.id
# A real source file for save_file (avoids the Windows NamedTemporaryFile
# re-open quirk).
src = tmp_path / "src_cover.png"
src.write_bytes(_PNG_BYTES)
from app.main import create_app
app = create_app()
async with LifespanManager(app):
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
yield client, user_id, src
async with get_engine().begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await dispose_engine()
finally:
_storage_provider._storage = None
os.environ.pop("MEDIA_PATH", None)
get_settings.cache_clear()
async def _token(api: AsyncClient) -> str:
resp = await api.post(
"/api/v1/auth/login", json={"username": "pluser", "password": "testpass1"}
)
assert resp.status_code == 200, resp.text
return str(resp.json()["access_token"])
async def test_playlist_cover_served(ctx: tuple[AsyncClient, uuid.UUID, Path]) -> None:
api, user_id, src = ctx
token = await _token(api)
playlist_id = await _seed_playlist(owner_id=user_id, cover=src)
resp = await api.get(f"/api/v1/playlists/{playlist_id}/cover?token={token}")
assert resp.status_code == 200, resp.text
assert resp.headers["content-type"] == "image/png"
assert resp.content == _PNG_BYTES
async def test_playlist_without_cover_is_404(ctx: tuple[AsyncClient, uuid.UUID, Path]) -> None:
api, user_id, _ = ctx
token = await _token(api)
playlist_id = await _seed_playlist(owner_id=user_id, cover=None)
resp = await api.get(f"/api/v1/playlists/{playlist_id}/cover?token={token}")
assert resp.status_code == 404
async def test_playlist_cover_requires_auth(ctx: tuple[AsyncClient, uuid.UUID, Path]) -> None:
api, user_id, src = ctx
playlist_id = await _seed_playlist(owner_id=user_id, cover=src)
resp = await api.get(f"/api/v1/playlists/{playlist_id}/cover")
assert resp.status_code == 401
+185
View File
@@ -0,0 +1,185 @@
"""Integration tests for the storage maintenance endpoints.
Covers ``/storage/duplicates`` (shared fingerprint), ``/broken`` (failed
enrichment), ``/missing-metadata`` (pending), and the admin-gated ``/cleanup``
enqueue. Requires a reachable Postgres; skips otherwise.
"""
import asyncio
import os
import uuid
from collections.abc import AsyncIterator
from pathlib import Path
import pytest
from app.core.config import get_settings
from app.infrastructure.db import Base, dispose_engine, get_engine, session_scope
from app.infrastructure.db.repositories import (
SqlAlchemyArtistRepository,
SqlAlchemyRefreshTokenRepository,
SqlAlchemyTrackRepository,
SqlAlchemyUserRepository,
)
from asgi_lifespan import LifespanManager
from httpx import ASGITransport, AsyncClient
pytestmark = pytest.mark.asyncio
_db_reachable_cache: bool | None = None
async def _db_reachable() -> bool:
global _db_reachable_cache
if _db_reachable_cache is not None:
return _db_reachable_cache
from sqlalchemy import text
try:
async with asyncio.timeout(3):
async with get_engine().connect() as conn:
await conn.execute(text("SELECT 1"))
_db_reachable_cache = True
except Exception:
_db_reachable_cache = False
return _db_reachable_cache
async def _seed_track(
*, title: str, source_id: str, metadata_status: str, fingerprint: str | None
) -> uuid.UUID:
async with session_scope() as session:
artist = await SqlAlchemyArtistRepository(session).get_or_create("Storage Artist")
tracks = SqlAlchemyTrackRepository(session)
tid = uuid.uuid4()
await tracks.add(
id=tid,
title=title,
artist_id=artist.id,
storage_uri=f"tracks/zz/{source_id}.mp3",
file_format="mp3",
file_size=10,
source="upload",
source_id=source_id,
metadata_status="pending",
added_by=None,
)
# apply_enrichment lets us set fingerprint + final status precisely.
await tracks.apply_enrichment(
tid,
title=title,
artist_id=artist.id,
album_id=None,
genre=None,
year=None,
track_number=None,
duration_seconds=1,
bitrate=None,
acoustid_fingerprint=fingerprint,
musicbrainz_id=None,
metadata_status=metadata_status,
)
return tid
@pytest.fixture
async def api(tmp_path: Path) -> AsyncIterator[AsyncClient]:
if not await _db_reachable():
pytest.skip("Postgres not reachable — integration test skipped.")
os.environ["MEDIA_PATH"] = str(tmp_path)
get_settings.cache_clear()
try:
async with get_engine().begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
from app.application.user_service import UserService
from app.core.security import Argon2PasswordHasher
async with session_scope() as session:
svc = UserService(
users=SqlAlchemyUserRepository(session),
refresh_tokens=SqlAlchemyRefreshTokenRepository(session),
hasher=Argon2PasswordHasher(),
)
await svc.create_user(username="user", password="testpass1", is_superuser=False)
await svc.create_user(username="admin", password="testpass1", is_superuser=True)
from app.main import create_app
app = create_app()
async with LifespanManager(app):
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
yield client
async with get_engine().begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await dispose_engine()
finally:
os.environ.pop("MEDIA_PATH", None)
get_settings.cache_clear()
async def _auth(api: AsyncClient, username: str) -> dict[str, str]:
resp = await api.post(
"/api/v1/auth/login", json={"username": username, "password": "testpass1"}
)
assert resp.status_code == 200, resp.text
return {"Authorization": f"Bearer {resp.json()['access_token']}"}
async def test_duplicates_grouped_by_fingerprint(api: AsyncClient) -> None:
headers = await _auth(api, "user")
await _seed_track(title="Dup A", source_id="a", metadata_status="enriched", fingerprint="FP1")
await _seed_track(title="Dup B", source_id="b", metadata_status="enriched", fingerprint="FP1")
# A lone fingerprint must NOT show up as a duplicate.
await _seed_track(title="Solo", source_id="c", metadata_status="enriched", fingerprint="FP2")
resp = await api.get("/api/v1/storage/duplicates", headers=headers)
assert resp.status_code == 200, resp.text
groups = resp.json()
assert len(groups) == 1
assert groups[0]["fingerprint"] == "FP1"
assert len(groups[0]["tracks"]) == 2
async def test_broken_lists_failed_tracks(api: AsyncClient) -> None:
headers = await _auth(api, "user")
await _seed_track(title="Bad", source_id="x", metadata_status="failed", fingerprint=None)
await _seed_track(title="Good", source_id="y", metadata_status="enriched", fingerprint=None)
resp = await api.get("/api/v1/storage/broken", headers=headers)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["total"] == 1
assert body["items"][0]["title"] == "Bad"
async def test_missing_metadata_lists_pending(api: AsyncClient) -> None:
headers = await _auth(api, "user")
await _seed_track(title="Pending", source_id="p", metadata_status="pending", fingerprint=None)
await _seed_track(title="Done", source_id="d", metadata_status="enriched", fingerprint=None)
resp = await api.get("/api/v1/storage/missing-metadata", headers=headers)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["total"] == 1
assert body["items"][0]["title"] == "Pending"
async def test_cleanup_requires_admin(api: AsyncClient) -> None:
user = await _auth(api, "user")
resp = await api.post("/api/v1/storage/cleanup", headers=user)
assert resp.status_code == 403
async def test_cleanup_enqueues_for_admin(api: AsyncClient) -> None:
admin = await _auth(api, "admin")
resp = await api.post("/api/v1/storage/cleanup", headers=admin)
# 202 with a job id when the queue is reachable; 503 if Redis is down.
assert resp.status_code in (202, 503), resp.text
if resp.status_code == 202:
assert resp.json()["status"] == "enqueued"
assert resp.json()["job_id"]
+174
View File
@@ -0,0 +1,174 @@
"""Integration tests for the user-settings + scrobbling endpoints.
Drives ``/api/v1/settings`` end to end: lazy defaults, partial update,
enum validation, and scrobbling config (write-only session key). Requires a
reachable Postgres; skips otherwise.
"""
import asyncio
import os
from collections.abc import AsyncIterator
from pathlib import Path
import pytest
from app.core.config import get_settings
from app.infrastructure.db import Base, dispose_engine, get_engine, session_scope
from app.infrastructure.db.repositories import (
SqlAlchemyRefreshTokenRepository,
SqlAlchemyUserRepository,
)
from asgi_lifespan import LifespanManager
from httpx import ASGITransport, AsyncClient
pytestmark = pytest.mark.asyncio
_db_reachable_cache: bool | None = None
async def _db_reachable() -> bool:
global _db_reachable_cache
if _db_reachable_cache is not None:
return _db_reachable_cache
from sqlalchemy import text
try:
async with asyncio.timeout(3):
async with get_engine().connect() as conn:
await conn.execute(text("SELECT 1"))
_db_reachable_cache = True
except Exception:
_db_reachable_cache = False
return _db_reachable_cache
@pytest.fixture
async def api(tmp_path: Path) -> AsyncIterator[AsyncClient]:
if not await _db_reachable():
pytest.skip("Postgres not reachable — integration test skipped.")
os.environ["MEDIA_PATH"] = str(tmp_path)
get_settings.cache_clear()
try:
async with get_engine().begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
from app.application.user_service import UserService
from app.core.security import Argon2PasswordHasher
async with session_scope() as session:
await UserService(
users=SqlAlchemyUserRepository(session),
refresh_tokens=SqlAlchemyRefreshTokenRepository(session),
hasher=Argon2PasswordHasher(),
).create_user(username="setuser", password="testpass1", is_superuser=False)
from app.main import create_app
app = create_app()
async with LifespanManager(app):
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
yield client
async with get_engine().begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await dispose_engine()
finally:
os.environ.pop("MEDIA_PATH", None)
get_settings.cache_clear()
async def _auth(api: AsyncClient) -> dict[str, str]:
resp = await api.post(
"/api/v1/auth/login", json={"username": "setuser", "password": "testpass1"}
)
assert resp.status_code == 200, resp.text
return {"Authorization": f"Bearer {resp.json()['access_token']}"}
async def test_get_settings_returns_defaults(api: AsyncClient) -> None:
headers = await _auth(api)
resp = await api.get("/api/v1/settings", headers=headers)
assert resp.status_code == 200, resp.text
assert resp.json() == {"theme": "system", "stream_quality": "original"}
async def test_patch_settings_persists(api: AsyncClient) -> None:
headers = await _auth(api)
resp = await api.patch("/api/v1/settings", json={"theme": "dark"}, headers=headers)
assert resp.status_code == 200, resp.text
assert resp.json() == {"theme": "dark", "stream_quality": "original"}
# Persisted + partial update leaves the untouched field alone.
again = await api.patch("/api/v1/settings", json={"stream_quality": "low"}, headers=headers)
assert again.json() == {"theme": "dark", "stream_quality": "low"}
async def test_invalid_theme_is_422(api: AsyncClient) -> None:
headers = await _auth(api)
resp = await api.patch("/api/v1/settings", json={"theme": "neon"}, headers=headers)
assert resp.status_code == 422
async def test_scrobbling_defaults_and_enable(api: AsyncClient) -> None:
headers = await _auth(api)
resp = await api.get("/api/v1/settings/scrobbling", headers=headers)
assert resp.status_code == 200, resp.text
assert resp.json() == {
"enabled": False,
"provider": None,
"username": None,
"configured": False,
}
# Enabling without a provider is rejected.
bad = await api.put("/api/v1/settings/scrobbling", json={"enabled": True}, headers=headers)
assert bad.status_code == 422
# Enable with a provider + secret token; the token is never echoed back.
ok = await api.put(
"/api/v1/settings/scrobbling",
json={
"enabled": True,
"provider": "listenbrainz",
"username": "me",
"session_key": "super-secret-token",
},
headers=headers,
)
assert ok.status_code == 200, ok.text
body = ok.json()
assert body == {
"enabled": True,
"provider": "listenbrainz",
"username": "me",
"configured": True,
}
assert "session_key" not in body
assert "super-secret-token" not in ok.text
async def test_scrobbling_keeps_stored_key_when_omitted(api: AsyncClient) -> None:
headers = await _auth(api)
await api.put(
"/api/v1/settings/scrobbling",
json={"enabled": True, "provider": "lastfm", "session_key": "k"},
headers=headers,
)
# A later update without a session_key must keep the stored one.
resp = await api.put(
"/api/v1/settings/scrobbling",
json={"enabled": True, "provider": "lastfm", "username": "changed"},
headers=headers,
)
assert resp.status_code == 200, resp.text
assert resp.json()["configured"] is True
assert resp.json()["username"] == "changed"
async def test_settings_require_auth(api: AsyncClient) -> None:
resp = await api.get("/api/v1/settings")
assert resp.status_code == 401