"""Regression tests for the _debug_enabled helper. `app.cm_api` defines a private _debug_enabled() function that parses the CM_DEBUG environment variable. This test runs a parametrized matrix against it. Historically `app.cm_web_view` had its own copy that this suite kept in lockstep — that module retired in the B4 cutover, leaving only cm_api as the consumer. The matrix-style harness stays so the next Flask entrypoint can be wired in by appending one line to HELPER_MODULES. """ import os import unittest from unittest import mock # Import the module at top-level (before any mock.patch.dict with # clear=True), so module-load-time os.getenv() reads see the real # environment. The patches inside individual tests then only affect the # helper's runtime read of CM_DEBUG. import app.cm_api # Modules expected to expose a private _debug_enabled() helper. # Add new entries here if more Flask entrypoints adopt the same toggle. HELPER_MODULES = ( app.cm_api, ) # (env_value, expected_result). env_value=None means CM_DEBUG is unset. CASES = ( (None, False), ("", False), ("false", False), ("False", False), ("FALSE", False), ("0", False), ("no", False), ("anything-else", False), ("true", True), ("True", True), ("TRUE", True), ("1", True), ("yes", True), ("YES", True), (" true ", True), ) class DebugEnabledTests(unittest.TestCase): def _resolve(self, module): return getattr(module, "_debug_enabled", None) def test_helper_exists_on_every_module(self): for module in HELPER_MODULES: with self.subTest(module=module.__name__): helper = self._resolve(module) self.assertTrue( callable(helper), f"{module.__name__}._debug_enabled must be callable", ) def test_parses_cm_debug_consistently(self): for module in HELPER_MODULES: helper = self._resolve(module) if helper is None: self.fail( f"{module.__name__}._debug_enabled is missing — " "make test_helper_exists_on_every_module pass first" ) for env_value, expected in CASES: with self.subTest(module=module.__name__, env=env_value): env = {} if env_value is None else {"CM_DEBUG": env_value} with mock.patch.dict(os.environ, env, clear=True): self.assertEqual( helper(), expected, f"{module.__name__}._debug_enabled() should be " f"{expected!r} for CM_DEBUG={env_value!r}", ) if __name__ == "__main__": unittest.main()