Skip to content

Commit ca1c81b

Browse files
fix: raise an error when duration is non-zero for video assets (#2399)
1 parent f2db850 commit ca1c81b

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

api/errors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class AssetCreationError(Exception):
2+
pass

api/serializers/mixins.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from rest_framework.serializers import CharField, Serializer
66

7+
from api.errors import AssetCreationError
78
from lib.utils import (
89
download_video_from_youtube,
910
get_video_duration,
@@ -74,6 +75,10 @@ def prepare_asset(self, data, asset_id=None, version='v2'):
7475
asset['duration'] = (
7576
duration if version == 'v2' else int(duration)
7677
)
78+
else:
79+
raise AssetCreationError(
80+
'Duration must be zero for video assets.'
81+
)
7782
else:
7883
# Crashes if it's not an int. We want that.
7984
duration = data.get(
@@ -99,7 +104,9 @@ def prepare_asset(self, data, asset_id=None, version='v2'):
99104
asset['end_date'] = data.get('end_date').replace(tzinfo=None)
100105

101106
if not asset['skip_asset_check'] and url_fails(asset['uri']):
102-
raise Exception("Could not retrieve file. Check the asset URL.")
107+
raise AssetCreationError(
108+
'Could not retrieve file. Check the asset URL.'
109+
)
103110

104111
return asset
105112

api/tests/test_assets.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Tests for asset-related API endpoints.
33
"""
4+
import mock
45
from django.test import TestCase
56
from django.urls import reverse
67
from rest_framework import status
@@ -79,6 +80,48 @@ def test_create_asset_should_return_201(self, version):
7980
self.assertEqual(response.data['play_order'], 0)
8081
self.assertEqual(response.data['skip_asset_check'], 0)
8182

83+
@mock.patch('api.serializers.mixins.rename')
84+
@mock.patch('api.serializers.mixins.validate_uri')
85+
def test_create_video_asset_v2_with_non_zero_duration_should_fail(
86+
self,
87+
mock_validate_uri,
88+
mock_rename
89+
):
90+
"""Test that v2 rejects video assets with non-zero duration."""
91+
mock_validate_uri.return_value = True
92+
asset_list_url = reverse('api:asset_list_v2')
93+
94+
test_data = {
95+
'name': 'Test Video',
96+
'uri': '/data/screenly_assets/video.mp4',
97+
'start_date': '2019-08-24T14:15:22Z',
98+
'end_date': '2029-08-24T14:15:22Z',
99+
'duration': 30,
100+
'mimetype': 'video',
101+
'is_enabled': True,
102+
'nocache': False,
103+
'play_order': 0,
104+
'skip_asset_check': False
105+
}
106+
107+
response = self.client.post(
108+
asset_list_url,
109+
data=test_data,
110+
format='json'
111+
)
112+
113+
self.assertEqual(
114+
response.status_code,
115+
status.HTTP_500_INTERNAL_SERVER_ERROR
116+
)
117+
self.assertIn(
118+
'Duration must be zero for video assets',
119+
str(response.data)
120+
)
121+
122+
self.assertEqual(mock_rename.call_count, 1)
123+
self.assertEqual(mock_validate_uri.call_count, 1)
124+
82125
@parametrize_version
83126
def test_get_assets_after_create_should_return_1_asset(self, version):
84127
self.create_asset(ASSET_CREATION_DATA, version)

0 commit comments

Comments
 (0)