|
| 1 | +"""Regression: deployment configs must never silently omit CORS_ORIGINS. |
| 2 | +
|
| 3 | +src/config/settings.py defaults CORS_ORIGINS to an empty list (deny all) in |
| 4 | +production when unset, which makes every browser CORS preflight to the |
| 5 | +deployed API fail with 400 before the request ever reaches a route handler. |
| 6 | +
|
| 7 | +GCP/AWS/Azure each implement their own from_dict() (none delegate to |
| 8 | +BaseDeploymentConfig.from_dict() via super()), so the default must be applied |
| 9 | +via the shared apply_cors_origins_default() helper in every provider's |
| 10 | +from_dict(), not just the base class — exercise all three here, plus the |
| 11 | +base class directly since it's also a valid (if currently unused by the |
| 12 | +concrete deployers) construction path. |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +import sys |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +_REPO_ROOT = Path(__file__).resolve().parents[2] |
| 20 | +if str(_REPO_ROOT) not in sys.path: |
| 21 | + sys.path.insert(0, str(_REPO_ROOT)) |
| 22 | + |
| 23 | +import pytest |
| 24 | + |
| 25 | +from deploy.base.config import BaseDeploymentConfig |
| 26 | +from deploy.providers.aws.config import AWSDeploymentConfig |
| 27 | +from deploy.providers.azure.config import AzureDeploymentConfig |
| 28 | +from deploy.providers.gcp.config import GCPDeploymentConfig |
| 29 | + |
| 30 | +_PROVIDER_CONFIG = { |
| 31 | + "gcp": {"project_id": "my-test-project"}, |
| 32 | + "aws": {}, |
| 33 | + "azure": {"resource_group": "test-rg", "subscription_id": "test-sub"}, |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +def _data(provider, **overrides): |
| 38 | + data = { |
| 39 | + "provider": provider, |
| 40 | + "environment": "production", |
| 41 | + "service": { |
| 42 | + "name": "supply-graph-ai", |
| 43 | + "image": "ghcr.io/example/supply-graph-ai:latest", |
| 44 | + "environment_vars": {}, |
| 45 | + }, |
| 46 | + "providers": {provider: _PROVIDER_CONFIG[provider]}, |
| 47 | + } |
| 48 | + data.update(overrides) |
| 49 | + return data |
| 50 | + |
| 51 | + |
| 52 | +CONFIG_CLASSES = [ |
| 53 | + (BaseDeploymentConfig, "gcp"), |
| 54 | + (AzureDeploymentConfig, "azure"), |
| 55 | + (AWSDeploymentConfig, "aws"), |
| 56 | + (GCPDeploymentConfig, "gcp"), |
| 57 | +] |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize("config_cls,provider", CONFIG_CLASSES) |
| 61 | +def test_cors_origins_defaults_to_wildcard_when_omitted(config_cls, provider): |
| 62 | + config = config_cls.from_dict(_data(provider)) |
| 63 | + assert config.service.environment_vars["CORS_ORIGINS"] == "*" |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize("config_cls,provider", CONFIG_CLASSES) |
| 67 | +def test_cors_origins_respects_explicit_value(config_cls, provider): |
| 68 | + data = _data(provider) |
| 69 | + data["service"]["environment_vars"] = {"CORS_ORIGINS": "https://app.example.com"} |
| 70 | + config = config_cls.from_dict(data) |
| 71 | + assert config.service.environment_vars["CORS_ORIGINS"] == "https://app.example.com" |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.parametrize("config_cls,provider", CONFIG_CLASSES) |
| 75 | +def test_cors_origins_default_applies_regardless_of_environment(config_cls, provider): |
| 76 | + data = _data(provider, environment="development") |
| 77 | + config = config_cls.from_dict(data) |
| 78 | + assert config.service.environment_vars["CORS_ORIGINS"] == "*" |
| 79 | + |
| 80 | + |
| 81 | +def test_gcp_with_defaults_defaults_cors_origins(): |
| 82 | + """deploy_gcp.py (the only real, non-test deploy script in this repo) |
| 83 | + constructs config via with_defaults(), not from_dict() — must also default.""" |
| 84 | + config = GCPDeploymentConfig.with_defaults( |
| 85 | + project_id="my-test-project", |
| 86 | + environment_vars={"ENVIRONMENT": "production"}, |
| 87 | + ) |
| 88 | + assert config.service.environment_vars["CORS_ORIGINS"] == "*" |
| 89 | + |
| 90 | + |
| 91 | +def test_azure_with_defaults_defaults_cors_origins(): |
| 92 | + config = AzureDeploymentConfig.with_defaults( |
| 93 | + resource_group="test-rg", |
| 94 | + subscription_id="test-sub", |
| 95 | + ) |
| 96 | + assert config.service.environment_vars["CORS_ORIGINS"] == "*" |
| 97 | + |
| 98 | + |
| 99 | +def test_aws_with_defaults_defaults_cors_origins(): |
| 100 | + config = AWSDeploymentConfig.with_defaults() |
| 101 | + assert config.service.environment_vars["CORS_ORIGINS"] == "*" |
0 commit comments