central/tests/test_bootstrap_config.py

143 lines
5.9 KiB
Python
Raw Normal View History

2026-05-16 21:27:30 +00:00
"""Tests for bootstrap configuration."""
import os
from pathlib import Path
from tempfile import NamedTemporaryFile
import pytest
from central.bootstrap_config import Settings, get_settings
class TestSettingsFromEnv:
"""Test loading settings from environment variables."""
def test_reads_from_env(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Settings are read from CENTRAL_* environment variables."""
monkeypatch.setenv("CENTRAL_DB_DSN", "postgresql://test:pass@localhost/testdb")
monkeypatch.setenv("CENTRAL_NATS_URL", "nats://10.0.0.1:4222")
monkeypatch.setenv("CENTRAL_MASTER_KEY_PATH", "/tmp/test.key")
monkeypatch.setenv("CENTRAL_LOG_LEVEL", "DEBUG")
chore: housekeeping - orphan branch + three stale tests (#22) * test(bootstrap): isolate env vars in test_reads_from_env_file The test was failing on CT104 because live CENTRAL_DB_DSN environment variable overrode the test .env file content. Fix: use monkeypatch.delenv to clear all CENTRAL_* env vars before creating the Settings object, ensuring the test env file is the only source of configuration values. Also add CENTRAL_CSRF_SECRET to test env file since it's now a required field. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(models): remove stale test_custom_prefix test The test called subject_for_event(event, prefix="myapp.events") but the prefix parameter was removed from the API. The prefix functionality was intentionally removed - subjects now always use the "central." prefix hardcoded in the function. Delete the test rather than re-add the parameter. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(nws): update fixtures for new adapter signature and region filtering NWSAdapter.__init__ signature changed from (config, cursor_db_path) to (config, config_store, cursor_db_path) with config now being AdapterConfig with a settings dict instead of NWSAdapterConfig. Also adapts tests to region-based bbox filtering: - TestStateFilter now uses region bbox to accept PNW, reject CA - Add geometry to SAMPLE_FEATURE_OR so it passes region filter - Other test fixtures use region=None to skip filtering Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Matt Johnson <mj@k7zvx.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-17 18:14:58 -06:00
monkeypatch.setenv("CENTRAL_CSRF_SECRET", "testsecret12345678901234567890ab")
2026-05-16 21:27:30 +00:00
settings = Settings()
assert settings.db_dsn == "postgresql://test:pass@localhost/testdb"
assert settings.nats_url == "nats://10.0.0.1:4222"
assert settings.master_key_path == Path("/tmp/test.key")
assert settings.log_level == "DEBUG"
def test_defaults_applied(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Default values are used when env vars not set."""
monkeypatch.setenv("CENTRAL_DB_DSN", "postgresql://x:y@localhost/db")
chore: housekeeping - orphan branch + three stale tests (#22) * test(bootstrap): isolate env vars in test_reads_from_env_file The test was failing on CT104 because live CENTRAL_DB_DSN environment variable overrode the test .env file content. Fix: use monkeypatch.delenv to clear all CENTRAL_* env vars before creating the Settings object, ensuring the test env file is the only source of configuration values. Also add CENTRAL_CSRF_SECRET to test env file since it's now a required field. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(models): remove stale test_custom_prefix test The test called subject_for_event(event, prefix="myapp.events") but the prefix parameter was removed from the API. The prefix functionality was intentionally removed - subjects now always use the "central." prefix hardcoded in the function. Delete the test rather than re-add the parameter. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(nws): update fixtures for new adapter signature and region filtering NWSAdapter.__init__ signature changed from (config, cursor_db_path) to (config, config_store, cursor_db_path) with config now being AdapterConfig with a settings dict instead of NWSAdapterConfig. Also adapts tests to region-based bbox filtering: - TestStateFilter now uses region bbox to accept PNW, reject CA - Add geometry to SAMPLE_FEATURE_OR so it passes region filter - Other test fixtures use region=None to skip filtering Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Matt Johnson <mj@k7zvx.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-17 18:14:58 -06:00
monkeypatch.setenv("CENTRAL_CSRF_SECRET", "testsecret12345678901234567890ab")
2026-05-16 21:27:30 +00:00
# Clear any existing env vars that might interfere
monkeypatch.delenv("CENTRAL_NATS_URL", raising=False)
monkeypatch.delenv("CENTRAL_MASTER_KEY_PATH", raising=False)
monkeypatch.delenv("CENTRAL_LOG_LEVEL", raising=False)
settings = Settings()
assert settings.nats_url == "nats://localhost:4222"
assert settings.master_key_path == Path("/etc/central/master.key")
assert settings.log_level == "INFO"
class TestSettingsFromFile:
"""Test loading settings from .env file."""
chore: housekeeping - orphan branch + three stale tests (#22) * test(bootstrap): isolate env vars in test_reads_from_env_file The test was failing on CT104 because live CENTRAL_DB_DSN environment variable overrode the test .env file content. Fix: use monkeypatch.delenv to clear all CENTRAL_* env vars before creating the Settings object, ensuring the test env file is the only source of configuration values. Also add CENTRAL_CSRF_SECRET to test env file since it's now a required field. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(models): remove stale test_custom_prefix test The test called subject_for_event(event, prefix="myapp.events") but the prefix parameter was removed from the API. The prefix functionality was intentionally removed - subjects now always use the "central." prefix hardcoded in the function. Delete the test rather than re-add the parameter. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(nws): update fixtures for new adapter signature and region filtering NWSAdapter.__init__ signature changed from (config, cursor_db_path) to (config, config_store, cursor_db_path) with config now being AdapterConfig with a settings dict instead of NWSAdapterConfig. Also adapts tests to region-based bbox filtering: - TestStateFilter now uses region bbox to accept PNW, reject CA - Add geometry to SAMPLE_FEATURE_OR so it passes region filter - Other test fixtures use region=None to skip filtering Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Matt Johnson <mj@k7zvx.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-17 18:14:58 -06:00
def test_reads_from_env_file(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
2026-05-16 21:27:30 +00:00
"""Settings are read from .env file when env vars not present."""
chore: housekeeping - orphan branch + three stale tests (#22) * test(bootstrap): isolate env vars in test_reads_from_env_file The test was failing on CT104 because live CENTRAL_DB_DSN environment variable overrode the test .env file content. Fix: use monkeypatch.delenv to clear all CENTRAL_* env vars before creating the Settings object, ensuring the test env file is the only source of configuration values. Also add CENTRAL_CSRF_SECRET to test env file since it's now a required field. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(models): remove stale test_custom_prefix test The test called subject_for_event(event, prefix="myapp.events") but the prefix parameter was removed from the API. The prefix functionality was intentionally removed - subjects now always use the "central." prefix hardcoded in the function. Delete the test rather than re-add the parameter. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(nws): update fixtures for new adapter signature and region filtering NWSAdapter.__init__ signature changed from (config, cursor_db_path) to (config, config_store, cursor_db_path) with config now being AdapterConfig with a settings dict instead of NWSAdapterConfig. Also adapts tests to region-based bbox filtering: - TestStateFilter now uses region bbox to accept PNW, reject CA - Add geometry to SAMPLE_FEATURE_OR so it passes region filter - Other test fixtures use region=None to skip filtering Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Matt Johnson <mj@k7zvx.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-17 18:14:58 -06:00
# Remove any live env vars that would override the test .env file
monkeypatch.delenv("CENTRAL_DB_DSN", raising=False)
monkeypatch.delenv("CENTRAL_NATS_URL", raising=False)
monkeypatch.delenv("CENTRAL_LOG_LEVEL", raising=False)
monkeypatch.delenv("CENTRAL_MASTER_KEY_PATH", raising=False)
monkeypatch.delenv("CENTRAL_CSRF_SECRET", raising=False)
2026-05-16 21:27:30 +00:00
env_file = tmp_path / ".env"
env_file.write_text(
"CENTRAL_DB_DSN=postgresql://file:pass@localhost/filedb\n"
"CENTRAL_NATS_URL=nats://file.local:4222\n"
"CENTRAL_LOG_LEVEL=WARNING\n"
chore: housekeeping - orphan branch + three stale tests (#22) * test(bootstrap): isolate env vars in test_reads_from_env_file The test was failing on CT104 because live CENTRAL_DB_DSN environment variable overrode the test .env file content. Fix: use monkeypatch.delenv to clear all CENTRAL_* env vars before creating the Settings object, ensuring the test env file is the only source of configuration values. Also add CENTRAL_CSRF_SECRET to test env file since it's now a required field. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(models): remove stale test_custom_prefix test The test called subject_for_event(event, prefix="myapp.events") but the prefix parameter was removed from the API. The prefix functionality was intentionally removed - subjects now always use the "central." prefix hardcoded in the function. Delete the test rather than re-add the parameter. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(nws): update fixtures for new adapter signature and region filtering NWSAdapter.__init__ signature changed from (config, cursor_db_path) to (config, config_store, cursor_db_path) with config now being AdapterConfig with a settings dict instead of NWSAdapterConfig. Also adapts tests to region-based bbox filtering: - TestStateFilter now uses region bbox to accept PNW, reject CA - Add geometry to SAMPLE_FEATURE_OR so it passes region filter - Other test fixtures use region=None to skip filtering Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Matt Johnson <mj@k7zvx.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-17 18:14:58 -06:00
"CENTRAL_CSRF_SECRET=testsecret12345678901234567890ab\n"
2026-05-16 21:27:30 +00:00
)
# Create settings pointing to the temp .env file
settings = Settings(_env_file=env_file)
assert settings.db_dsn == "postgresql://file:pass@localhost/filedb"
assert settings.nats_url == "nats://file.local:4222"
assert settings.log_level == "WARNING"
def test_env_vars_override_file(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Environment variables take precedence over .env file."""
env_file = tmp_path / ".env"
chore: housekeeping - orphan branch + three stale tests (#22) * test(bootstrap): isolate env vars in test_reads_from_env_file The test was failing on CT104 because live CENTRAL_DB_DSN environment variable overrode the test .env file content. Fix: use monkeypatch.delenv to clear all CENTRAL_* env vars before creating the Settings object, ensuring the test env file is the only source of configuration values. Also add CENTRAL_CSRF_SECRET to test env file since it's now a required field. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(models): remove stale test_custom_prefix test The test called subject_for_event(event, prefix="myapp.events") but the prefix parameter was removed from the API. The prefix functionality was intentionally removed - subjects now always use the "central." prefix hardcoded in the function. Delete the test rather than re-add the parameter. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(nws): update fixtures for new adapter signature and region filtering NWSAdapter.__init__ signature changed from (config, cursor_db_path) to (config, config_store, cursor_db_path) with config now being AdapterConfig with a settings dict instead of NWSAdapterConfig. Also adapts tests to region-based bbox filtering: - TestStateFilter now uses region bbox to accept PNW, reject CA - Add geometry to SAMPLE_FEATURE_OR so it passes region filter - Other test fixtures use region=None to skip filtering Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Matt Johnson <mj@k7zvx.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-17 18:14:58 -06:00
env_file.write_text(
"CENTRAL_DB_DSN=postgresql://file@localhost/filedb\n"
"CENTRAL_CSRF_SECRET=filesecret1234567890123456789012\n"
)
2026-05-16 21:27:30 +00:00
monkeypatch.setenv("CENTRAL_DB_DSN", "postgresql://env@localhost/envdb")
chore: housekeeping - orphan branch + three stale tests (#22) * test(bootstrap): isolate env vars in test_reads_from_env_file The test was failing on CT104 because live CENTRAL_DB_DSN environment variable overrode the test .env file content. Fix: use monkeypatch.delenv to clear all CENTRAL_* env vars before creating the Settings object, ensuring the test env file is the only source of configuration values. Also add CENTRAL_CSRF_SECRET to test env file since it's now a required field. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(models): remove stale test_custom_prefix test The test called subject_for_event(event, prefix="myapp.events") but the prefix parameter was removed from the API. The prefix functionality was intentionally removed - subjects now always use the "central." prefix hardcoded in the function. Delete the test rather than re-add the parameter. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(nws): update fixtures for new adapter signature and region filtering NWSAdapter.__init__ signature changed from (config, cursor_db_path) to (config, config_store, cursor_db_path) with config now being AdapterConfig with a settings dict instead of NWSAdapterConfig. Also adapts tests to region-based bbox filtering: - TestStateFilter now uses region bbox to accept PNW, reject CA - Add geometry to SAMPLE_FEATURE_OR so it passes region filter - Other test fixtures use region=None to skip filtering Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Matt Johnson <mj@k7zvx.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-17 18:14:58 -06:00
monkeypatch.setenv("CENTRAL_CSRF_SECRET", "envsecret12345678901234567890ab")
2026-05-16 21:27:30 +00:00
settings = Settings(_env_file=env_file)
assert settings.db_dsn == "postgresql://env@localhost/envdb"
class TestSettingsValidation:
"""Test settings validation and error handling."""
def test_fails_if_required_var_missing(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Clear error when required CENTRAL_DB_DSN is missing."""
# Ensure no env vars or .env file provides the DSN
monkeypatch.delenv("CENTRAL_DB_DSN", raising=False)
chore: housekeeping - orphan branch + three stale tests (#22) * test(bootstrap): isolate env vars in test_reads_from_env_file The test was failing on CT104 because live CENTRAL_DB_DSN environment variable overrode the test .env file content. Fix: use monkeypatch.delenv to clear all CENTRAL_* env vars before creating the Settings object, ensuring the test env file is the only source of configuration values. Also add CENTRAL_CSRF_SECRET to test env file since it's now a required field. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(models): remove stale test_custom_prefix test The test called subject_for_event(event, prefix="myapp.events") but the prefix parameter was removed from the API. The prefix functionality was intentionally removed - subjects now always use the "central." prefix hardcoded in the function. Delete the test rather than re-add the parameter. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(nws): update fixtures for new adapter signature and region filtering NWSAdapter.__init__ signature changed from (config, cursor_db_path) to (config, config_store, cursor_db_path) with config now being AdapterConfig with a settings dict instead of NWSAdapterConfig. Also adapts tests to region-based bbox filtering: - TestStateFilter now uses region bbox to accept PNW, reject CA - Add geometry to SAMPLE_FEATURE_OR so it passes region filter - Other test fixtures use region=None to skip filtering Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Matt Johnson <mj@k7zvx.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-17 18:14:58 -06:00
monkeypatch.delenv("CENTRAL_CSRF_SECRET", raising=False)
2026-05-16 21:27:30 +00:00
with pytest.raises(Exception) as exc_info:
# Use a non-existent .env file path to ensure no fallback
Settings(_env_file=Path("/nonexistent/.env"))
# pydantic-settings raises ValidationError for missing required fields
assert "db_dsn" in str(exc_info.value).lower() or "validation" in str(exc_info.value).lower()
def test_invalid_log_level_rejected(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Invalid log level values are rejected."""
monkeypatch.setenv("CENTRAL_DB_DSN", "postgresql://x@localhost/db")
chore: housekeeping - orphan branch + three stale tests (#22) * test(bootstrap): isolate env vars in test_reads_from_env_file The test was failing on CT104 because live CENTRAL_DB_DSN environment variable overrode the test .env file content. Fix: use monkeypatch.delenv to clear all CENTRAL_* env vars before creating the Settings object, ensuring the test env file is the only source of configuration values. Also add CENTRAL_CSRF_SECRET to test env file since it's now a required field. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(models): remove stale test_custom_prefix test The test called subject_for_event(event, prefix="myapp.events") but the prefix parameter was removed from the API. The prefix functionality was intentionally removed - subjects now always use the "central." prefix hardcoded in the function. Delete the test rather than re-add the parameter. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(nws): update fixtures for new adapter signature and region filtering NWSAdapter.__init__ signature changed from (config, cursor_db_path) to (config, config_store, cursor_db_path) with config now being AdapterConfig with a settings dict instead of NWSAdapterConfig. Also adapts tests to region-based bbox filtering: - TestStateFilter now uses region bbox to accept PNW, reject CA - Add geometry to SAMPLE_FEATURE_OR so it passes region filter - Other test fixtures use region=None to skip filtering Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Matt Johnson <mj@k7zvx.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-17 18:14:58 -06:00
monkeypatch.setenv("CENTRAL_CSRF_SECRET", "testsecret12345678901234567890ab")
2026-05-16 21:27:30 +00:00
monkeypatch.setenv("CENTRAL_LOG_LEVEL", "INVALID")
with pytest.raises(Exception):
Settings()
class TestGetSettings:
"""Test the cached settings loader."""
def test_caches_result(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""get_settings() returns cached instance."""
monkeypatch.setenv("CENTRAL_DB_DSN", "postgresql://cached@localhost/db")
chore: housekeeping - orphan branch + three stale tests (#22) * test(bootstrap): isolate env vars in test_reads_from_env_file The test was failing on CT104 because live CENTRAL_DB_DSN environment variable overrode the test .env file content. Fix: use monkeypatch.delenv to clear all CENTRAL_* env vars before creating the Settings object, ensuring the test env file is the only source of configuration values. Also add CENTRAL_CSRF_SECRET to test env file since it's now a required field. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(models): remove stale test_custom_prefix test The test called subject_for_event(event, prefix="myapp.events") but the prefix parameter was removed from the API. The prefix functionality was intentionally removed - subjects now always use the "central." prefix hardcoded in the function. Delete the test rather than re-add the parameter. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(nws): update fixtures for new adapter signature and region filtering NWSAdapter.__init__ signature changed from (config, cursor_db_path) to (config, config_store, cursor_db_path) with config now being AdapterConfig with a settings dict instead of NWSAdapterConfig. Also adapts tests to region-based bbox filtering: - TestStateFilter now uses region bbox to accept PNW, reject CA - Add geometry to SAMPLE_FEATURE_OR so it passes region filter - Other test fixtures use region=None to skip filtering Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Matt Johnson <mj@k7zvx.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-17 18:14:58 -06:00
monkeypatch.setenv("CENTRAL_CSRF_SECRET", "testsecret12345678901234567890ab")
2026-05-16 21:27:30 +00:00
get_settings.cache_clear()
s1 = get_settings()
s2 = get_settings()
assert s1 is s2
def test_cache_clear_reloads(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""cache_clear() forces reload on next call."""
monkeypatch.setenv("CENTRAL_DB_DSN", "postgresql://first@localhost/db")
chore: housekeeping - orphan branch + three stale tests (#22) * test(bootstrap): isolate env vars in test_reads_from_env_file The test was failing on CT104 because live CENTRAL_DB_DSN environment variable overrode the test .env file content. Fix: use monkeypatch.delenv to clear all CENTRAL_* env vars before creating the Settings object, ensuring the test env file is the only source of configuration values. Also add CENTRAL_CSRF_SECRET to test env file since it's now a required field. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(models): remove stale test_custom_prefix test The test called subject_for_event(event, prefix="myapp.events") but the prefix parameter was removed from the API. The prefix functionality was intentionally removed - subjects now always use the "central." prefix hardcoded in the function. Delete the test rather than re-add the parameter. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(nws): update fixtures for new adapter signature and region filtering NWSAdapter.__init__ signature changed from (config, cursor_db_path) to (config, config_store, cursor_db_path) with config now being AdapterConfig with a settings dict instead of NWSAdapterConfig. Also adapts tests to region-based bbox filtering: - TestStateFilter now uses region bbox to accept PNW, reject CA - Add geometry to SAMPLE_FEATURE_OR so it passes region filter - Other test fixtures use region=None to skip filtering Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Matt Johnson <mj@k7zvx.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-17 18:14:58 -06:00
monkeypatch.setenv("CENTRAL_CSRF_SECRET", "testsecret12345678901234567890ab")
2026-05-16 21:27:30 +00:00
get_settings.cache_clear()
s1 = get_settings()
monkeypatch.setenv("CENTRAL_DB_DSN", "postgresql://second@localhost/db")
get_settings.cache_clear()
s2 = get_settings()
assert s1.db_dsn != s2.db_dsn