|
| 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