Skip to content

Commit b2db9db

Browse files
committed
Ensure that file_format and preset match during file_upload requests.
1 parent 97c593c commit b2db9db

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

contentcuration/contentcuration/tests/viewsets/test_file.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ def test_duration_invalid(self):
438438
def test_duration_missing(self):
439439
del self.file["duration"]
440440
self.file["file_format"] = file_formats.EPUB
441+
self.file["preset"] = format_presets.EPUB
441442

442443
self.client.force_authenticate(user=self.user)
443444
response = self.client.post(
@@ -461,9 +462,23 @@ def test_duration_missing_but_required(self):
461462

462463
self.assertEqual(response.status_code, 400)
463464

465+
def test_duration_present_but_not_allowed(self):
466+
self.file["file_format"] = file_formats.EPUB
467+
self.file["preset"] = format_presets.DOCUMENT
468+
469+
self.client.force_authenticate(user=self.user)
470+
response = self.client.post(
471+
reverse("file-upload-url"),
472+
self.file,
473+
format="json",
474+
)
475+
476+
self.assertEqual(response.status_code, 400)
477+
464478
def test_duration_null(self):
465479
self.file["duration"] = None
466480
self.file["file_format"] = file_formats.EPUB
481+
self.file["preset"] = format_presets.EPUB
467482

468483
self.client.force_authenticate(user=self.user)
469484
response = self.client.post(
@@ -517,6 +532,18 @@ def test_invalid_preset_upload(self):
517532
response = self.client.post(reverse("file-upload-url"), file, format="json")
518533
self.assertEqual(response.status_code, 400)
519534

535+
def test_mismatched_preset_upload(self):
536+
self.file["file_format"] = file_formats.EPUB
537+
538+
self.client.force_authenticate(user=self.user)
539+
response = self.client.post(
540+
reverse("file-upload-url"),
541+
self.file,
542+
format="json",
543+
)
544+
545+
self.assertEqual(response.status_code, 400)
546+
520547
def test_insufficient_storage(self):
521548
self.file["size"] = 100000000000000
522549

contentcuration/contentcuration/viewsets/file.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
from contentcuration.viewsets.sync.utils import generate_update_event
3333

3434

35+
PRESET_LOOKUP = {p.id: p for p in format_presets.PRESETLIST}
36+
37+
3538
class StrictFloatField(serializers.FloatField):
3639
def to_internal_value(self, data):
3740
# If data is a string, reject it even if it represents a number.
@@ -81,6 +84,11 @@ def validate(self, attrs):
8184
raise serializers.ValidationError(
8285
"Duration is required for audio/video files"
8386
)
87+
preset_obj = PRESET_LOOKUP[attrs["preset"]]
88+
if attrs["file_format"] not in preset_obj.allowed_formats:
89+
raise serializers.ValidationError(
90+
f"File format {attrs['file_format']} is not an allowed format for this preset {attrs['preset']}"
91+
)
8492
return attrs
8593

8694

0 commit comments

Comments
 (0)