Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: deploy

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

jobs:
deploy:
Expand Down
4 changes: 2 additions & 2 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[pytest]
testpaths = nc tsdata
testpaths = nc tsdata traffic_stops
python_files = tests.py test_*.py *_tests.py
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
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
12 changes: 11 additions & 1 deletion traffic_stops/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,20 @@ def __init__(self, tz_name=None):
STATIC_URL = "/static/"

MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
MEDIA_STORAGE_BUCKET_NAME = os.getenv("MEDIA_STORAGE_BUCKET_NAME", "")
MEDIA_LOCATION = os.getenv("MEDIA_LOCATION", "")
MEDIA_S3_CUSTOM_DOMAIN = os.getenv("MEDIA_S3_CUSTOM_DOMAIN", "")

# Set MEDIA_URL based on whether we're using S3 storage
if MEDIA_S3_CUSTOM_DOMAIN:
# When using S3 with custom domain, construct the full URL
MEDIA_URL = f"https://{MEDIA_S3_CUSTOM_DOMAIN}/"
if MEDIA_LOCATION:
MEDIA_URL += f"{MEDIA_LOCATION}/"
else:
# Fall back to local media serving
MEDIA_URL = "/media/"

STORAGES = {
"default": {
"BACKEND": os.getenv("DEFAULT_FILE_STORAGE", "django.core.files.storage.FileSystemStorage")
Expand Down
Empty file added traffic_stops/tests/__init__.py
Empty file.
62 changes: 62 additions & 0 deletions traffic_stops/tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Tests for settings configuration."""

import os

from unittest import mock

import pytest


@pytest.mark.parametrize(
"custom_domain,media_location,expected_url",
[
# With custom domain and no location
("files.nccopwatch.org", "", "https://files.nccopwatch.org/"),
# With custom domain and location
("files.nccopwatch.org", "media", "https://files.nccopwatch.org/media/"),
# With custom domain and nested location
("files.example.com", "uploads/files", "https://files.example.com/uploads/files/"),
# Without custom domain (local development)
("", "", "/media/"),
(None, "", "/media/"),
],
)
def test_media_url_configuration(custom_domain, media_location, expected_url):
"""Test that MEDIA_URL is correctly set based on S3 configuration."""
# Mock environment variables
env_vars = {
"MEDIA_S3_CUSTOM_DOMAIN": custom_domain or "",
"MEDIA_LOCATION": media_location,
"DEFAULT_FILE_STORAGE": "django.core.files.storage.FileSystemStorage",
}

with mock.patch.dict(os.environ, env_vars, clear=False):
# Import settings module to trigger configuration
# We need to reload to pick up the mocked environment
import importlib

from traffic_stops.settings import base

importlib.reload(base)

assert base.MEDIA_URL == expected_url


def test_media_url_with_s3_storage():
"""Test MEDIA_URL when using S3 storage backend."""
env_vars = {
"MEDIA_S3_CUSTOM_DOMAIN": "files.nccopwatch.org",
"MEDIA_LOCATION": "",
"DEFAULT_FILE_STORAGE": "traffic_stops.storages.MediaBoto3Storage",
}

with mock.patch.dict(os.environ, env_vars, clear=False):
import importlib

from traffic_stops.settings import base

importlib.reload(base)

# When S3 storage is configured with custom domain, MEDIA_URL should be absolute
assert base.MEDIA_URL.startswith("https://")
assert "files.nccopwatch.org" in base.MEDIA_URL