Coverage for app/backend/src/tests/test_config.py: 100%
101 statements
« prev ^ index » next coverage.py v7.17.4, created at 2026-11-23 00:24 +0000
« prev ^ index » next coverage.py v7.17.4, created at 2026-11-23 00:24 +0000
1import typing
3import pytest
5from couchers.config import Config
6from couchers.constants import DB_POOL_SIZE
9def _complete_config(dev: bool) -> Config:
10 """Build a config object attribute populated with a valid, truthy value.
12 This mirrors what Config.load_from_env() produces when every env var is set,
13 so Config.check() should succeed against it.
14 """
15 cfg = Config()
16 for var_name, var_type in Config.__annotations__.items():
17 if var_type is bool:
18 setattr(cfg, var_name, True)
19 elif var_type is int:
20 setattr(cfg, var_name, 1)
21 elif var_type is bytes:
22 setattr(cfg, var_name, b"x")
23 elif typing.get_origin(var_type) is typing.Literal: # type: ignore[comparison-overlap]
24 setattr(cfg, var_name, typing.get_args(var_type)[0]) # type: ignore[unreachable]
25 else:
26 setattr(cfg, var_name, "x")
28 cfg.DEV = dev
29 if dev:
30 # partitioned cookies require Secure, which DEV doesn't set
31 cfg.COOKIE_PARTITIONED = False
32 if not dev:
33 # production invariants that aren't satisfiable by a generic truthy value
34 cfg.BASE_URL = "https://example.com"
35 cfg.ENABLE_EMAIL = True
36 cfg.IN_TEST = False
37 cfg.FEATURE_FLAGS_FILE_OVERRIDE_PATH = ""
38 return cfg
41def test_load_from_env() -> None:
42 cfg = Config()
43 assert not hasattr(cfg, "BASE_URL")
44 cfg.load_from_env({"BASE_URL": "https://example.com"})
45 assert cfg.BASE_URL == "https://example.com"
48def test_load_from_env_types() -> None:
49 cfg = Config()
51 cfg.load_from_env({"IN_TEST": "1"})
52 assert cfg.IN_TEST is True
53 with pytest.raises(ValueError):
54 cfg.load_from_env({"IN_TEST": "not a bool"})
56 cfg.load_from_env({"BACKGROUND_WORKER_PROCESSES": "42"})
57 assert cfg.BACKGROUND_WORKER_PROCESSES == 42
58 with pytest.raises(ValueError):
59 cfg.load_from_env({"BACKGROUND_WORKER_PROCESSES": "not an int"})
61 cfg.load_from_env({"SECRET": bytes.hex(b"abc")})
62 assert cfg.SECRET == b"abc"
63 with pytest.raises(ValueError):
64 cfg.load_from_env({"SECRET": "not hex"})
66 cfg.load_from_env({"ROLE": "worker"})
67 assert cfg.ROLE == "worker"
68 with pytest.raises(ValueError):
69 cfg.load_from_env({"ROLE": "not a valid role"})
72def test_getitem() -> None:
73 cfg = Config()
74 cfg.BASE_URL = "https://example.com"
75 assert cfg.BASE_URL == "https://example.com"
76 assert cfg["BASE_URL"] == "https://example.com"
79def test_setitem() -> None:
80 cfg = Config()
82 cfg["BASE_URL"] = "https://example.com"
83 assert cfg.BASE_URL == "https://example.com"
84 assert cfg["BASE_URL"] == "https://example.com"
86 with pytest.raises(KeyError):
87 cfg["NOT_A_KEY"] = "value"
89 with pytest.raises(TypeError):
90 cfg["BASE_URL"] = 123
92 cfg["ROLE"] = "worker"
93 assert cfg.ROLE == "worker"
94 with pytest.raises(ValueError):
95 cfg["ROLE"] = "not a valid role"
98def test_instances_state_are_independent() -> None:
99 # Default values are declared at the class level, but should be copied to each instance.
100 assert Config.IN_TEST is False
102 cfg1 = Config()
103 cfg2 = Config()
105 assert cfg1.IN_TEST is False
106 assert cfg2.IN_TEST is False
108 cfg1.IN_TEST = True
110 assert cfg1.IN_TEST is True
111 assert cfg2.IN_TEST is False
114def test_copy() -> None:
115 cfg = Config()
117 cfg.BACKGROUND_WORKER_PROCESSES = 1
118 copy1 = cfg.copy()
119 cfg.BACKGROUND_WORKER_PROCESSES = 2
120 copy2 = cfg.copy()
122 assert copy1.BACKGROUND_WORKER_PROCESSES == 1
123 assert copy2.BACKGROUND_WORKER_PROCESSES == 2
126@pytest.mark.parametrize("dev", [True, False])
127def test_check_config_only_references_known_keys(dev):
128 """Config.check() must only access config keys that are declared as attributes.
130 A reference to a key that was removed from attributes (e.g. a toggle migrated to a feature
131 flag) would raise KeyError at app boot but is invisible to the rest of the test suite, since
132 Config.check() only runs in app.py's startup path. Exercising it here catches that.
133 """
134 _complete_config(dev=dev).check()
137def test_check_rejects_too_many_worker_threads() -> None:
138 cfg = _complete_config(dev=True)
139 cfg.BACKGROUND_WORKER_THREADS_PER_PROCESS = DB_POOL_SIZE // 2
140 cfg.check()
142 cfg.BACKGROUND_WORKER_THREADS_PER_PROCESS = DB_POOL_SIZE // 2 + 1
143 with pytest.raises(Exception, match="BACKGROUND_WORKER_THREADS_PER_PROCESS"):
144 cfg.check()
147def test_check_rejects_partitioned_cookies_in_dev() -> None:
148 cfg = _complete_config(dev=False)
149 cfg.COOKIE_PARTITIONED = True
150 cfg.check()
152 cfg = _complete_config(dev=True)
153 cfg.COOKIE_PARTITIONED = True
154 with pytest.raises(Exception, match="COOKIE_PARTITIONED"):
155 cfg.check()