feat: Implement main playlist restrictions; add internal method for sound addition and update tests
Some checks failed
Backend CI / test (push) Has been cancelled
Backend CI / lint (push) Has been cancelled

This commit is contained in:
JSC
2025-08-16 00:51:38 +02:00
parent 4cec3b9d18
commit a109a88eed
4 changed files with 155 additions and 55 deletions

View File

@@ -874,13 +874,13 @@ class TestPlaylistService:
assert stats["total_play_count"] == 10 # From test_sound fixture
@pytest.mark.asyncio
async def test_add_sound_to_main_playlist(
async def test_add_sound_to_main_playlist_forbidden(
self,
playlist_service: PlaylistService,
test_user: User,
test_session: AsyncSession,
) -> None:
"""Test adding sound to main playlist."""
"""Test that adding sound to main playlist is forbidden."""
# Create test sound and main playlist within this test
user_id = test_user.id
sound = Sound(
@@ -911,57 +911,102 @@ class TestPlaylistService:
sound_id = sound.id
main_playlist_id = main_playlist.id
# Add sound to main playlist
await playlist_service.add_sound_to_main_playlist(sound_id, user_id)
# Try to add sound to main playlist (should fail)
with pytest.raises(HTTPException) as exc_info:
await playlist_service.add_sound_to_main_playlist(sound_id, user_id)
assert exc_info.value.status_code == 403
assert "cannot be added to the main playlist" in exc_info.value.detail
@pytest.mark.asyncio
async def test_add_sound_to_main_playlist_blocked(
self,
playlist_service: PlaylistService,
test_user: User,
test_session: AsyncSession,
) -> None:
"""Test that adding sound to main playlist is blocked (should raise 403)."""
# Create test sound and main playlist within this test
user_id = test_user.id
sound = Sound(
name="Test Sound",
filename="test.mp3",
type="SDB",
duration=5000,
size=1024,
hash="test_hash",
play_count=10,
)
test_session.add(sound)
main_playlist = Playlist(
user_id=None,
name="Main Playlist",
description="Main playlist",
is_main=True,
is_current=False,
is_deletable=False,
)
test_session.add(main_playlist)
await test_session.commit()
await test_session.refresh(sound)
await test_session.refresh(main_playlist)
# Extract IDs before async calls
sound_id = sound.id
main_playlist_id = main_playlist.id
# Try to add sound to main playlist (should fail both times)
with pytest.raises(HTTPException) as exc_info:
await playlist_service.add_sound_to_main_playlist(sound_id, user_id)
assert exc_info.value.status_code == 403
with pytest.raises(HTTPException) as exc_info:
await playlist_service.add_sound_to_main_playlist(sound_id, user_id)
assert exc_info.value.status_code == 403
@pytest.mark.asyncio
async def test_internal_add_sound_to_main_playlist_allowed(
self,
playlist_service: PlaylistService,
test_user: User,
test_session: AsyncSession,
) -> None:
"""Test that internal method can add sound to main playlist (bypasses restrictions)."""
# Create test sound and main playlist within this test
user_id = test_user.id
sound = Sound(
name="Test Sound Internal",
filename="test_internal.mp3",
type="EXT",
duration=6000,
size=2048,
hash="test_hash_internal",
play_count=0,
)
test_session.add(sound)
main_playlist = Playlist(
user_id=None,
name="Main Playlist Internal",
description="Main playlist for internal test",
is_main=True,
is_current=False,
is_deletable=False,
)
test_session.add(main_playlist)
await test_session.commit()
await test_session.refresh(sound)
await test_session.refresh(main_playlist)
# Extract IDs before async calls
sound_id = sound.id
main_playlist_id = main_playlist.id
# Use internal method to add sound to main playlist (should work)
await playlist_service._add_sound_to_main_playlist_internal(sound_id, user_id)
# Verify sound was added to main playlist
sounds = await playlist_service.get_playlist_sounds(main_playlist_id)
assert len(sounds) == 1
assert sounds[0].id == sound_id
@pytest.mark.asyncio
async def test_add_sound_to_main_playlist_already_exists(
self,
playlist_service: PlaylistService,
test_user: User,
test_session: AsyncSession,
) -> None:
"""Test adding sound to main playlist when it already exists (should not duplicate)."""
# Create test sound and main playlist within this test
user_id = test_user.id
sound = Sound(
name="Test Sound",
filename="test.mp3",
type="SDB",
duration=5000,
size=1024,
hash="test_hash",
play_count=10,
)
test_session.add(sound)
main_playlist = Playlist(
user_id=None,
name="Main Playlist",
description="Main playlist",
is_main=True,
is_current=False,
is_deletable=False,
)
test_session.add(main_playlist)
await test_session.commit()
await test_session.refresh(sound)
await test_session.refresh(main_playlist)
# Extract IDs before async calls
sound_id = sound.id
main_playlist_id = main_playlist.id
# Add sound to main playlist twice
await playlist_service.add_sound_to_main_playlist(sound_id, user_id)
await playlist_service.add_sound_to_main_playlist(sound_id, user_id)
# Verify sound is only added once
sounds = await playlist_service.get_playlist_sounds(main_playlist_id)
assert len(sounds) == 1
assert sounds[0].id == sound_id