Skip to content

Commit 34a8864

Browse files
authored
Avoid double slashes in paths (#2652)
1 parent a304bcf commit 34a8864

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

video_shorts/factories.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ class VideoShortFactory(DjangoModelFactory):
2020
datetime.now(tz=UTC),
2121
)
2222
thumbnail_url = factory.LazyAttribute(
23-
lambda obj: f"https://example.com/thumbnails/{obj.youtube_id}.jpg"
23+
lambda obj: f"shorts/{obj.youtube_id}/{obj.youtube_id}.jpg"
2424
)
2525
thumbnail_height = FuzzyInteger(360, 720)
2626
thumbnail_width = FuzzyInteger(480, 1280)
2727
video_url = factory.LazyAttribute(
28-
lambda obj: f"https://example.com/videos/{obj.youtube_id}.mp4"
28+
lambda obj: f"shorts/{obj.youtube_id}/{obj.youtube_id}.mp4"
2929
)
3030

3131
class Meta:

video_shorts/serializers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Serializers for video shorts"""
22

3+
from urllib.parse import urljoin
4+
35
from django.conf import settings
46
from rest_framework import serializers
57

@@ -46,7 +48,7 @@ def to_internal_value(self, data):
4648
)
4749

4850
# Use relative URLs so they work regardless of domain
49-
base_path = f"/{settings.VIDEO_SHORTS_S3_PREFIX}/{youtube_id}/"
51+
base_path = urljoin(f"/{settings.VIDEO_SHORTS_S3_PREFIX}/", f"{youtube_id}/")
5052
transformed_data = {
5153
"youtube_id": youtube_id,
5254
"title": snippet.get("title"),

video_shorts/serializers_test.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ def test_video_short_serializer_read():
2222
title="Test Video",
2323
description="Test description",
2424
published_at=datetime(2024, 1, 15, 12, 0, 0, tzinfo=UTC),
25-
thumbnail_url="https://example.com/thumb.jpg",
25+
thumbnail_url="/shorts/test_123/test_123.jpg",
2626
thumbnail_height=360,
2727
thumbnail_width=480,
28-
video_url="https://example.com/video.mp4",
28+
video_url="/shorts/test_123/test_123.mp4",
2929
)
3030

3131
serializer = VideoShortSerializer(video_short)
@@ -41,10 +41,10 @@ def test_video_short_serializer_read():
4141
"title": "Test Video",
4242
"description": "Test description",
4343
"published_at": "2024-01-15T12:00:00Z",
44-
"thumbnail_url": "https://example.com/thumb.jpg",
44+
"thumbnail_url": "/shorts/test_123/test_123.jpg",
4545
"thumbnail_height": 360,
4646
"thumbnail_width": 480,
47-
"video_url": "https://example.com/video.mp4",
47+
"video_url": "/shorts/test_123/test_123.mp4",
4848
},
4949
)
5050

@@ -56,10 +56,10 @@ def test_video_short_serializer_create():
5656
"title": "Created Video",
5757
"description": "Created description",
5858
"published_at": "2024-01-15T12:00:00Z",
59-
"thumbnail_url": "https://example.com/created_thumb.jpg",
59+
"thumbnail_url": "/shorts/create_test/create_test.jpg",
6060
"thumbnail_height": 720,
6161
"thumbnail_width": 1280,
62-
"video_url": "https://example.com/created_video.mp4",
62+
"video_url": "/shorts/create_test/create_test.mp4",
6363
}
6464

6565
serializer = VideoShortSerializer(data=data)
@@ -120,9 +120,12 @@ def test_video_short_serializer_update():
120120
assert_json_equal(result_data, data)
121121

122122

123-
def test_youtube_metadata_serializer(settings, sample_youtube_metadata):
123+
@pytest.mark.parametrize(
124+
"prefix", ["youtube_shorts/", "youtube_shorts", "youtube_shorts//"]
125+
)
126+
def test_youtube_metadata_serializer(settings, sample_youtube_metadata, prefix):
124127
"""Test YouTubeMetadataSerializer transforms YouTube API data correctly"""
125-
settings.VIDEO_SHORTS_S3_PREFIX = "youtube_shorts"
128+
settings.VIDEO_SHORTS_S3_PREFIX = prefix
126129

127130
serializer = YouTubeMetadataSerializer(data=sample_youtube_metadata)
128131
assert serializer.is_valid()

0 commit comments

Comments
 (0)