2024-05-05 22:44:30 +02:00
|
|
|
import os
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
2022-04-04 14:38:50 +02:00
|
|
|
from django.conf import settings
|
|
|
|
from model_bakery import baker
|
|
|
|
from rest_framework.test import APITestCase
|
|
|
|
|
2022-06-22 13:27:10 +02:00
|
|
|
from ...._fixtures import AUDIO_FILENAME
|
2024-05-05 22:44:30 +02:00
|
|
|
from ...models import File
|
2022-04-04 14:38:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestFileViewSet(APITestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.token = settings.CONFIG.general.api_key
|
|
|
|
|
2024-05-05 22:44:30 +02:00
|
|
|
def test_download_invalid(self):
|
2022-04-04 14:38:50 +02:00
|
|
|
self.client.credentials(HTTP_AUTHORIZATION=f"Api-Key {self.token}")
|
2024-05-05 22:44:30 +02:00
|
|
|
file_id = "1"
|
|
|
|
response = self.client.get(f"/api/v2/files/{file_id}/download")
|
|
|
|
self.assertEqual(response.status_code, 404)
|
2022-04-04 14:38:50 +02:00
|
|
|
|
2024-05-05 22:44:30 +02:00
|
|
|
def test_download(self):
|
2022-04-04 14:38:50 +02:00
|
|
|
self.client.credentials(HTTP_AUTHORIZATION=f"Api-Key {self.token}")
|
2024-05-05 22:44:30 +02:00
|
|
|
file: File = baker.make(
|
|
|
|
"storage.File",
|
|
|
|
mime="audio/mp3",
|
|
|
|
filepath=AUDIO_FILENAME,
|
|
|
|
)
|
|
|
|
response = self.client.get(f"/api/v2/files/{file.id}/download")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2022-04-04 14:38:50 +02:00
|
|
|
|
2024-05-05 22:44:30 +02:00
|
|
|
def test_destroy(self):
|
|
|
|
self.client.credentials(HTTP_AUTHORIZATION=f"Api-Key {self.token}")
|
|
|
|
file: File = baker.make(
|
2022-04-04 14:38:50 +02:00
|
|
|
"storage.File",
|
|
|
|
mime="audio/mp3",
|
|
|
|
filepath=AUDIO_FILENAME,
|
|
|
|
)
|
2024-05-05 22:44:30 +02:00
|
|
|
|
|
|
|
with patch("libretime_api.storage.views.file.remove") as remove_mock:
|
|
|
|
response = self.client.delete(f"/api/v2/files/{file.id}")
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 204)
|
|
|
|
remove_mock.assert_called_with(
|
|
|
|
os.path.join(settings.CONFIG.storage.path, file.filepath)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_destroy_no_file(self):
|
2022-04-04 14:38:50 +02:00
|
|
|
self.client.credentials(HTTP_AUTHORIZATION=f"Api-Key {self.token}")
|
2024-05-05 22:44:30 +02:00
|
|
|
file = baker.make(
|
|
|
|
"storage.File",
|
|
|
|
mime="audio/mp3",
|
|
|
|
filepath="invalid.mp3",
|
|
|
|
)
|
|
|
|
response = self.client.delete(f"/api/v2/files/{file.id}")
|
|
|
|
self.assertEqual(response.status_code, 204)
|
|
|
|
|
|
|
|
def test_destroy_invalid(self):
|
|
|
|
self.client.credentials(HTTP_AUTHORIZATION=f"Api-Key {self.token}")
|
|
|
|
file_id = "1"
|
|
|
|
response = self.client.delete(f"/api/v2/files/{file_id}")
|
|
|
|
self.assertEqual(response.status_code, 404)
|
2025-01-08 17:54:53 +01:00
|
|
|
|
|
|
|
def test_filters(self):
|
|
|
|
file = baker.make(
|
|
|
|
"storage.File",
|
|
|
|
mime="audio/mp3",
|
|
|
|
filepath=AUDIO_FILENAME,
|
|
|
|
genre="Soul",
|
|
|
|
md5="5a11ffe0e6c6d70fcdbad1b734be6482",
|
|
|
|
)
|
|
|
|
baker.make(
|
|
|
|
"storage.File",
|
|
|
|
mime="audio/mp3",
|
|
|
|
filepath=AUDIO_FILENAME,
|
|
|
|
genre="R&B",
|
|
|
|
md5="5a11ffe0e6c6d70fcdbad1b734be6483",
|
|
|
|
)
|
|
|
|
self.client.credentials(HTTP_AUTHORIZATION=f"Api-Key {self.token}")
|
|
|
|
|
|
|
|
path = "/api/v2/files"
|
|
|
|
results = self.client.get(path).json()
|
|
|
|
self.assertEqual(len(results), 2)
|
|
|
|
|
|
|
|
path = f"/api/v2/files?md5={file.md5}"
|
|
|
|
results = self.client.get(path).json()
|
|
|
|
self.assertEqual(len(results), 1)
|
|
|
|
|
|
|
|
path = "/api/v2/files?genre=Soul"
|
|
|
|
results = self.client.get(path).json()
|
|
|
|
self.assertEqual(len(results), 1)
|
|
|
|
|
|
|
|
path = "/api/v2/files?genre=R%26B"
|
|
|
|
results = self.client.get(path).json()
|
|
|
|
self.assertEqual(len(results), 1)
|