57 lines
2.1 KiB
Python
57 lines
2.1 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from app.models import Video
|
|
from app.worker import cleanup_uploaded_video_artifacts, settings
|
|
|
|
|
|
class CleanupUploadedVideoArtifactsTests(unittest.TestCase):
|
|
def test_removes_video_directory_and_clears_paths(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temporary_dir:
|
|
storage = Path(temporary_dir)
|
|
video_dir = storage / "vk" / "7"
|
|
video_dir.mkdir(parents=True)
|
|
video_path = video_dir / "video.mp4"
|
|
cover_path = video_dir / "video.jpg"
|
|
info_path = video_dir / "video.info.json"
|
|
for path in (video_path, cover_path, info_path):
|
|
path.write_text("test", encoding="utf-8")
|
|
|
|
video = Video(
|
|
id=7,
|
|
vk_video_url="https://vk.com/video-1_7",
|
|
local_video_path=str(video_path),
|
|
local_cover_path=str(cover_path),
|
|
)
|
|
|
|
with patch.object(settings, "storage_dir", str(storage)):
|
|
cleanup_uploaded_video_artifacts(video)
|
|
|
|
self.assertFalse(video_dir.exists())
|
|
self.assertIsNone(video.local_video_path)
|
|
self.assertIsNone(video.local_cover_path)
|
|
|
|
def test_refuses_path_outside_video_storage(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temporary_dir:
|
|
storage = Path(temporary_dir) / "storage"
|
|
outside = Path(temporary_dir) / "outside"
|
|
outside.mkdir()
|
|
video_path = outside / "video.mp4"
|
|
video_path.write_text("test", encoding="utf-8")
|
|
video = Video(
|
|
id=8,
|
|
vk_video_url="https://vk.com/video-1_8",
|
|
local_video_path=str(video_path),
|
|
)
|
|
|
|
with patch.object(settings, "storage_dir", str(storage)):
|
|
with self.assertRaises(ValueError):
|
|
cleanup_uploaded_video_artifacts(video)
|
|
|
|
self.assertTrue(video_path.exists())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|