"""Tests for configuration validation and core.config helpers.""" import pytest from core.config import ( OWS_REFERENCE_CACHE_TTL_MAX_SECONDS, _int_env_in_range, _max_content_length_bytes, ) # Pure-unit tests: no Flask app or DB, so opt out of the DB-vendor gate. pytestmark = pytest.mark.no_db @pytest.fixture(autouse=True) def test_app_in_context(): """No-op override: these tests don't need a DB-backed app context.""" yield @pytest.fixture(autouse=True) def test_app_request(): """No-op override: these tests don't need a request context.""" yield def test_accepts_limits_within_the_supported_range(): """The min (1), the default (2), and the max (10) MB are all accepted and returned in bytes.""" assert _max_content_length_bytes('1') == 1 * 1024 * 1024 assert _max_content_length_bytes('2') == 2 * 1024 * 1024 assert _max_content_length_bytes('10') == 10 * 1024 * 1024 def test_rejects_a_limit_below_one_mb(): """A sub-1MB ceiling would shrink the abuse backstop to near nothing, so it fails fast.""" with pytest.raises(ValueError, match='between 1 and 10'): _max_content_length_bytes('0') def test_rejects_a_limit_above_ten_mb(): """A ceiling over 10MB balloons per-thread memory under 15 threads, so it fails fast.""" with pytest.raises(ValueError, match='between 1 and 10'): _max_content_length_bytes('11') def test_returns_default_when_unset(): """The default is parsed when the env var is absent.""" assert _int_env_in_range('OWS_UNSET_TEST_VAR', '300', 0, 86400) == 300 def test_returns_env_value_in_range(monkeypatch): """An in-range env value is returned as an int.""" monkeypatch.setenv('OWS_RANGE_TEST_VAR', '600') assert _int_env_in_range('OWS_RANGE_TEST_VAR', '300', 0, 86400) == 600 def test_boundaries_are_inclusive(monkeypatch): """Both the minimum and maximum are accepted.""" monkeypatch.setenv('OWS_RANGE_TEST_VAR', '0') assert _int_env_in_range('OWS_RANGE_TEST_VAR', '300', 0, 86400) == 0 monkeypatch.setenv('OWS_RANGE_TEST_VAR', '86400') assert _int_env_in_range('OWS_RANGE_TEST_VAR', '300', 0, 86400) == 86400 def test_raises_above_maximum(monkeypatch): """A value above the maximum fails loudly.""" monkeypatch.setenv('OWS_RANGE_TEST_VAR', '999999') with pytest.raises(ValueError, match='between 0 and 86400'): _int_env_in_range('OWS_RANGE_TEST_VAR', '300', 0, 86400) def test_raises_below_minimum(monkeypatch): """A negative value fails loudly.""" monkeypatch.setenv('OWS_RANGE_TEST_VAR', '-1') with pytest.raises(ValueError, match='between 0 and 86400'): _int_env_in_range('OWS_RANGE_TEST_VAR', '300', 0, 86400) def test_raises_on_non_integer(monkeypatch): """A non-integer value fails loudly.""" monkeypatch.setenv('OWS_RANGE_TEST_VAR', 'not-a-number') with pytest.raises(ValueError, match='must be an integer'): _int_env_in_range('OWS_RANGE_TEST_VAR', '300', 0, 86400) def test_reference_cache_ttl_max_constant(): """The reference-cache TTL ceiling is 24 hours.""" assert OWS_REFERENCE_CACHE_TTL_MAX_SECONDS == 86400