Files
rehearshalhub/api/tests/unit/test_services.py
Steffen Schuhmann f7be1b994d Initial commit: RehearsalHub POC
Full-stack self-hosted band rehearsal platform:

Backend (FastAPI + SQLAlchemy 2.0 async):
- Auth with JWT (register, login, /me, settings)
- Band management with Nextcloud folder integration
- Song management with audio version tracking
- Nextcloud scan to auto-import audio files
- Band membership with link-based invite system
- Song comments
- Audio analysis worker (BPM, key, loudness, waveform)
- Nextcloud activity watcher for auto-import
- WebSocket support for real-time annotation updates
- Alembic migrations (0001–0003)
- Repository pattern, Ruff + mypy configured

Frontend (React 18 + Vite + TypeScript strict):
- Login/register page with post-login redirect
- Home page with band list and creation form
- Band page with member panel, invite link, song list, NC scan
- Song page with waveform player, annotations, comment thread
- Settings page for per-user Nextcloud credentials
- Invite acceptance page (/invite/:token)
- ESLint v9 flat config + TypeScript strict mode

Infrastructure:
- Docker Compose: PostgreSQL, Redis, API, worker, watcher, nginx
- nginx reverse proxy for static files + /api/ proxy
- make check runs all linters before docker compose build

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-28 21:53:03 +01:00

152 lines
4.5 KiB
Python

"""Unit tests for service layer — band and annotation services."""
import uuid
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from rehearsalhub.services.annotation import AnnotationService
from rehearsalhub.services.band import BandService
@pytest.mark.asyncio
async def test_create_band_raises_on_duplicate_slug(mock_session):
from rehearsalhub.db.models import Band
from rehearsalhub.schemas.band import BandCreate
existing_band = MagicMock(spec=Band)
existing_band.slug = "taken"
with patch(
"rehearsalhub.repositories.band.BandRepository.get_by_slug",
new_callable=AsyncMock,
return_value=existing_band,
):
svc = BandService(mock_session)
with pytest.raises(ValueError, match="Slug already taken"):
await svc.create_band(
BandCreate(name="Test", slug="taken"),
creator_id=uuid.uuid4(),
)
@pytest.mark.asyncio
async def test_assert_membership_raises_when_not_member(mock_session):
band_id = uuid.uuid4()
member_id = uuid.uuid4()
with patch(
"rehearsalhub.repositories.band.BandRepository.get_member_role",
new_callable=AsyncMock,
return_value=None,
):
svc = BandService(mock_session)
with pytest.raises(PermissionError, match="Not a member"):
await svc.assert_membership(band_id, member_id)
@pytest.mark.asyncio
async def test_assert_admin_raises_when_member_role(mock_session):
band_id = uuid.uuid4()
member_id = uuid.uuid4()
with patch(
"rehearsalhub.repositories.band.BandRepository.get_member_role",
new_callable=AsyncMock,
return_value="member",
):
svc = BandService(mock_session)
with pytest.raises(PermissionError, match="Admin role required"):
await svc.assert_admin(band_id, member_id)
@pytest.mark.asyncio
async def test_create_range_annotation_enqueues_job(mock_session):
from rehearsalhub.db.models import Annotation
from rehearsalhub.schemas.annotation import AnnotationCreate
annotation = MagicMock(spec=Annotation)
annotation.id = uuid.uuid4()
mock_queue = AsyncMock()
mock_queue.enqueue = AsyncMock(return_value=uuid.uuid4())
with patch(
"rehearsalhub.repositories.annotation.AnnotationRepository.create",
new_callable=AsyncMock,
return_value=annotation,
):
svc = AnnotationService(mock_session, job_queue=mock_queue)
await svc.create_annotation(
version_id=uuid.uuid4(),
author_id=uuid.uuid4(),
data=AnnotationCreate(
type="range",
timestamp_ms=1000,
range_end_ms=5000,
tags=["hook"],
),
)
mock_queue.enqueue.assert_called_once_with(
"analyse_range",
{
"annotation_id": str(annotation.id),
"version_id": unittest_any_str(),
"start_ms": 1000,
"end_ms": 5000,
},
)
@pytest.mark.asyncio
async def test_create_point_annotation_does_not_enqueue(mock_session):
from rehearsalhub.db.models import Annotation
from rehearsalhub.schemas.annotation import AnnotationCreate
annotation = MagicMock(spec=Annotation)
annotation.id = uuid.uuid4()
mock_queue = AsyncMock()
mock_queue.enqueue = AsyncMock()
with patch(
"rehearsalhub.repositories.annotation.AnnotationRepository.create",
new_callable=AsyncMock,
return_value=annotation,
):
svc = AnnotationService(mock_session, job_queue=mock_queue)
await svc.create_annotation(
version_id=uuid.uuid4(),
author_id=uuid.uuid4(),
data=AnnotationCreate(type="point", timestamp_ms=2000),
)
mock_queue.enqueue.assert_not_called()
@pytest.mark.asyncio
async def test_delete_annotation_by_non_author_raises(mock_session):
from rehearsalhub.db.models import Annotation
author_id = uuid.uuid4()
other_id = uuid.uuid4()
annotation = MagicMock(spec=Annotation)
annotation.id = uuid.uuid4()
annotation.author_id = author_id
svc = AnnotationService(mock_session)
with pytest.raises(PermissionError, match="Only the author"):
await svc.delete_annotation(annotation, other_id)
def unittest_any_str():
"""Helper that matches any string in assert_called_with."""
class AnyStr:
def __eq__(self, other):
return isinstance(other, str)
return AnyStr()