diff --git a/modules/videos/channel/test/test_models.py b/modules/videos/channel/test/test_models.py index e5b8653e..44f8ea63 100644 --- a/modules/videos/channel/test/test_models.py +++ b/modules/videos/channel/test/test_models.py @@ -1,3 +1,11 @@ +import json +from http import HTTPStatus + +import pytest + +from modules.videos.models import Video + + def test_delete_channel_no_url(test_session, test_client, channel_factory): """ A Channel can be deleted even if it has no URL. @@ -7,3 +15,54 @@ def test_delete_channel_no_url(test_session, test_client, channel_factory): test_session.commit() channel.delete_with_videos() + + +@pytest.mark.asyncio +async def test_delete_channel_with_videos(test_session, test_async_client, channel_factory, video_factory): + """Videos are disowned when their Channel is deleted.""" + channel = channel_factory() + video = video_factory(channel_id=channel.id) + video_path, video_id = video.video_path, video.id + test_session.commit() + + # Delete the Channel. + request, response = await test_async_client.delete(f'/api/videos/channels/{channel.id}') + assert response.status == HTTPStatus.NO_CONTENT + + # Video still exists, but has no channel. + video: Video = test_session.query(Video).one() + assert video.video_path == video_path and video.id == video_id, 'Video entry should not be changed' + assert video.video_path.is_file(), 'Video should not be deleted' + assert not video.channel_id and not video.channel, 'Video should not have a Channel' + + +@pytest.mark.asyncio +async def test_nested_channel_directories(test_session, test_async_client, test_directory, channel_factory, + video_factory): + """Channel directories cannot contain another Channel's directory.""" + (test_directory / 'foo').mkdir() + channel1_directory = test_directory / 'foo/one' + channel_factory(directory=channel1_directory) + test_session.commit() + + # Channel 2 cannot be in Channel 1's directory. + channel2_directory = channel1_directory / 'two' + channel2_directory.mkdir() + content = dict( + name='channel 2', + directory=str(channel2_directory), + ) + request, response = await test_async_client.post('/api/videos/channels', content=json.dumps(content)) + assert response.status == HTTPStatus.BAD_REQUEST + assert response.json['cause'] and \ + response.json['cause']['error'] == 'The directory is already used by another channel.' + + # Channel 3 cannot be above Channel 1's directory. + content = dict( + name='channel 3', + directory=str(test_directory), + ) + request, response = await test_async_client.post('/api/videos/channels', content=json.dumps(content)) + assert response.status == HTTPStatus.BAD_REQUEST + assert response.json['cause'] and \ + response.json['cause']['error'] == 'The directory is already used by another channel.' diff --git a/modules/videos/common.py b/modules/videos/common.py index 5cf3f929..d8941509 100644 --- a/modules/videos/common.py +++ b/modules/videos/common.py @@ -80,6 +80,12 @@ def check_for_channel_conflicts(session: Session, id_=None, url=None, name=None, if list(conflicts): raise ChannelSourceIdConflict() + # Assure that this Channel's directory is not in another Channel's directory. + directory_str = str(directory) + for channel in session.query(Channel).filter(Channel.id != id_): + if directory_str.startswith(str(channel.directory)) or str(channel.directory).startswith(directory_str): + raise ChannelDirectoryConflict() + def duration_to_seconds(duration: str) -> int: """Convert ffmpeg duration to seconds""" diff --git a/modules/videos/conftest.py b/modules/videos/conftest.py index 4b258392..cc4739a7 100644 --- a/modules/videos/conftest.py +++ b/modules/videos/conftest.py @@ -36,10 +36,12 @@ def simple_channel(test_session, test_directory) -> Channel: def channel_factory(test_session, test_directory): """Create a random Channel with a directory, but no frequency.""" - def factory(source_id: str = None, download_frequency: DownloadFrequency = None, url: str = None, name: str = None): + def factory(source_id: str = None, download_frequency: DownloadFrequency = None, url: str = None, name: str = None, + directory: pathlib.Path = None): name = name or str(uuid4()) - directory = test_directory / name - directory.mkdir() + directory = directory or test_directory / name + if not directory.is_dir(): + directory.mkdir() channel = Channel( directory=directory, # noqa name=name, diff --git a/modules/videos/models.py b/modules/videos/models.py index 6e5b8535..e7432305 100644 --- a/modules/videos/models.py +++ b/modules/videos/models.py @@ -22,6 +22,8 @@ from wrolpi.vars import PYTEST logger = logger.getChild(__name__) +__all__ = ['Video', 'Channel'] + class Video(ModelHelper, Base): __tablename__ = 'video' @@ -326,8 +328,12 @@ class Channel(ModelHelper, Base): def delete_with_videos(self): """Delete all Video records (but not video files) related to this Channel. Then delete the Channel.""" session = Session.object_session(self) - # Delete the video records, but not the video files! - session.query(Video).filter_by(channel_id=self.id).delete() + + # Disown the videos. + videos = session.query(Video).filter_by(channel_id=self.id) + for video in videos: + video.channel = None + if self.url and (download := self.get_download()): session.delete(download)