|
| 1 | +import importlib |
| 2 | +import sys |
| 3 | +import textwrap |
| 4 | + |
| 5 | + |
| 6 | +def test_production_media_storage(monkeypatch, tmp_path): |
| 7 | + """Test that MEDIA_STORAGE_BACKEND YAML block overrides default storage + media settings.""" |
| 8 | + |
| 9 | + # Create a temporary YAML file to act as DISCOVERY_CFG |
| 10 | + fake_config = tmp_path / "config.yaml" |
| 11 | + fake_yaml_content = textwrap.dedent(""" |
| 12 | + MEDIA_STORAGE_BACKEND: |
| 13 | + AWS_S3_OBJECT_PARAMETERS: |
| 14 | + CacheControl: max-age=31536 |
| 15 | + AWS_QUERYSTRING_AUTH: false |
| 16 | + AWS_QUERYSTRING_EXPIRE: false |
| 17 | + AWS_S3_CUSTOM_DOMAIN: cdn.org |
| 18 | + AWS_STORAGE_BUCKET_NAME: tests |
| 19 | + DEFAULT_FILE_STORAGE: storages.backends.s3boto3.S3Boto3Storage |
| 20 | + MEDIA_ROOT: media |
| 21 | + MEDIA_URL: https://cdn.org/media/ |
| 22 | + """) |
| 23 | + fake_config.write_text(fake_yaml_content) |
| 24 | + |
| 25 | + # Patch environment variable so production.py sees the config |
| 26 | + monkeypatch.setenv("DISCOVERY_CFG", str(fake_config)) |
| 27 | + |
| 28 | + # Remove production module if already imported |
| 29 | + sys.modules.pop("course_discovery.settings.production", None) |
| 30 | + # Import production settings fresh |
| 31 | + prod = importlib.import_module("course_discovery.settings.production") |
| 32 | + |
| 33 | + # Assert MEDIA_STORAGE_BACKEND unpacked correctly |
| 34 | + assert "default" in prod.STORAGES |
| 35 | + assert prod.STORAGES["default"]["BACKEND"] == "storages.backends.s3boto3.S3Boto3Storage" |
| 36 | + assert prod.MEDIA_URL == "https://cdn.org/media/" |
| 37 | + assert prod.MEDIA_ROOT == "media" |
| 38 | + |
| 39 | + # Assert all AWS keys are present |
| 40 | + assert prod.AWS_STORAGE_BUCKET_NAME == "tests" |
| 41 | + assert prod.AWS_S3_CUSTOM_DOMAIN == "cdn.org" |
| 42 | + assert prod.AWS_QUERYSTRING_AUTH is False |
| 43 | + assert prod.AWS_QUERYSTRING_EXPIRE is False |
| 44 | + assert prod.AWS_S3_OBJECT_PARAMETERS == {"CacheControl": "max-age=31536"} |
0 commit comments