Skip to content

Commit f1d8aba

Browse files
authored
Set MEDIA_URL based on whether S3 storage with a custom domain is configured (#370)
1 parent 3913817 commit f1d8aba

File tree

5 files changed

+76
-4
lines changed

5 files changed

+76
-4
lines changed

.github/workflows/deploy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: deploy
22

33
on:
44
push:
5-
branches: [main, develop, CU-868em67g4_Upgrade-to-Django-52]
5+
branches: [main, develop, 366-broken-resource-links]
66

77
jobs:
88
deploy:

pytest.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[pytest]
2-
testpaths = nc tsdata
2+
testpaths = nc tsdata traffic_stops
33
python_files = tests.py test_*.py *_tests.py
4-
addopts = --ds=traffic_stops.settings.test -p no:warnings --cov-config=.coveragerc --cov-fail-under=44 --cov=nc --cov=tsdata --cov-report=html --cov-report=term-missing:skip-covered -vvv
4+
addopts = --ds=traffic_stops.settings.test -p no:warnings --cov-config=.coveragerc --cov-fail-under=44 --cov=nc --cov=tsdata --cov=traffic_stops --cov-report=html --cov-report=term-missing:skip-covered -vvv

traffic_stops/settings/base.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,20 @@ def __init__(self, tz_name=None):
112112
STATIC_URL = "/static/"
113113

114114
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
115-
MEDIA_URL = "/media/"
116115
MEDIA_STORAGE_BUCKET_NAME = os.getenv("MEDIA_STORAGE_BUCKET_NAME", "")
117116
MEDIA_LOCATION = os.getenv("MEDIA_LOCATION", "")
118117
MEDIA_S3_CUSTOM_DOMAIN = os.getenv("MEDIA_S3_CUSTOM_DOMAIN", "")
118+
119+
# Set MEDIA_URL based on whether we're using S3 storage
120+
if MEDIA_S3_CUSTOM_DOMAIN:
121+
# When using S3 with custom domain, construct the full URL
122+
MEDIA_URL = f"https://{MEDIA_S3_CUSTOM_DOMAIN}/"
123+
if MEDIA_LOCATION:
124+
MEDIA_URL += f"{MEDIA_LOCATION}/"
125+
else:
126+
# Fall back to local media serving
127+
MEDIA_URL = "/media/"
128+
119129
STORAGES = {
120130
"default": {
121131
"BACKEND": os.getenv("DEFAULT_FILE_STORAGE", "django.core.files.storage.FileSystemStorage")

traffic_stops/tests/__init__.py

Whitespace-only changes.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""Tests for settings configuration."""
2+
3+
import os
4+
5+
from unittest import mock
6+
7+
import pytest
8+
9+
10+
@pytest.mark.parametrize(
11+
"custom_domain,media_location,expected_url",
12+
[
13+
# With custom domain and no location
14+
("files.nccopwatch.org", "", "https://files.nccopwatch.org/"),
15+
# With custom domain and location
16+
("files.nccopwatch.org", "media", "https://files.nccopwatch.org/media/"),
17+
# With custom domain and nested location
18+
("files.example.com", "uploads/files", "https://files.example.com/uploads/files/"),
19+
# Without custom domain (local development)
20+
("", "", "/media/"),
21+
(None, "", "/media/"),
22+
],
23+
)
24+
def test_media_url_configuration(custom_domain, media_location, expected_url):
25+
"""Test that MEDIA_URL is correctly set based on S3 configuration."""
26+
# Mock environment variables
27+
env_vars = {
28+
"MEDIA_S3_CUSTOM_DOMAIN": custom_domain or "",
29+
"MEDIA_LOCATION": media_location,
30+
"DEFAULT_FILE_STORAGE": "django.core.files.storage.FileSystemStorage",
31+
}
32+
33+
with mock.patch.dict(os.environ, env_vars, clear=False):
34+
# Import settings module to trigger configuration
35+
# We need to reload to pick up the mocked environment
36+
import importlib
37+
38+
from traffic_stops.settings import base
39+
40+
importlib.reload(base)
41+
42+
assert base.MEDIA_URL == expected_url
43+
44+
45+
def test_media_url_with_s3_storage():
46+
"""Test MEDIA_URL when using S3 storage backend."""
47+
env_vars = {
48+
"MEDIA_S3_CUSTOM_DOMAIN": "files.nccopwatch.org",
49+
"MEDIA_LOCATION": "",
50+
"DEFAULT_FILE_STORAGE": "traffic_stops.storages.MediaBoto3Storage",
51+
}
52+
53+
with mock.patch.dict(os.environ, env_vars, clear=False):
54+
import importlib
55+
56+
from traffic_stops.settings import base
57+
58+
importlib.reload(base)
59+
60+
# When S3 storage is configured with custom domain, MEDIA_URL should be absolute
61+
assert base.MEDIA_URL.startswith("https://")
62+
assert "files.nccopwatch.org" in base.MEDIA_URL

0 commit comments

Comments
 (0)