Skip to content

Commit 2cf9e44

Browse files
authored
Merge pull request #8782 from Couchers-org/backend/bugfix/check-config-stale-keys
Backend: add check_config coverage for stale config key references
2 parents 96de3e2 + c93387c commit 2cf9e44

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

app/backend/src/couchers/config.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,6 @@ def check_config(cfg: dict[str, Any]) -> None:
168168
if not cfg["RECAPTHCA_PROJECT_ID"] or not cfg["RECAPTHCA_API_KEY"] or not cfg["RECAPTHCA_SITE_KEY"]:
169169
raise Exception("reCAPTCHA credentials must be configured in production")
170170

171-
if cfg["ENABLE_DONATIONS"]:
172-
if not cfg["STRIPE_API_KEY"] or not cfg["STRIPE_WEBHOOK_SECRET"] or not cfg["STRIPE_RECURRING_PRODUCT_ID"]:
173-
raise Exception("No Stripe API key/recurring donation ID but donations enabled")
174-
175171
if cfg["EXPERIMENTATION_ENABLED"]:
176172
if not cfg["GROWTHBOOK_CLIENT_KEY"]:
177173
raise Exception("No GrowthBook client key but experimentation enabled")
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typing import Any
2+
3+
import pytest
4+
5+
from couchers.config import CONFIG_OPTIONS, check_config
6+
7+
8+
def _complete_config(dev: bool) -> dict[str, Any]:
9+
"""Build a config dict with every CONFIG_OPTIONS key populated with a valid, truthy value.
10+
11+
This mirrors what make_config() produces when every env var is set, so check_config() must be
12+
able to run against it without touching any key outside CONFIG_OPTIONS.
13+
"""
14+
cfg: dict[str, Any] = {}
15+
for name, type_, *_ in CONFIG_OPTIONS:
16+
if type_ is bool:
17+
cfg[name] = True
18+
elif type_ is int:
19+
cfg[name] = 1
20+
elif type_ is bytes:
21+
cfg[name] = b"x"
22+
elif isinstance(type_, list):
23+
cfg[name] = type_[0]
24+
else:
25+
cfg[name] = "x"
26+
27+
cfg["DEV"] = dev
28+
if not dev:
29+
# production invariants that aren't satisfiable by a generic truthy value
30+
cfg["BASE_URL"] = "https://example.com"
31+
cfg["ENABLE_EMAIL"] = True
32+
cfg["IN_TEST"] = False
33+
return cfg
34+
35+
36+
@pytest.mark.parametrize("dev", [True, False])
37+
def test_check_config_only_references_known_keys(dev):
38+
"""check_config() must only access config keys that are declared in CONFIG_OPTIONS.
39+
40+
A reference to a key that was removed from CONFIG_OPTIONS (e.g. a toggle migrated to a feature
41+
flag) would raise KeyError at app boot but is invisible to the rest of the test suite, since
42+
check_config() only runs in app.py's startup path. Exercising it here catches that.
43+
"""
44+
check_config(_complete_config(dev=dev))

0 commit comments

Comments
 (0)