from __future__ import annotations import json import pytest from django.test import Client, override_settings from yksa_kit.conf import cache_key, user_agent from yksa_kit.settings import env_bool, env_list, read_secret @pytest.mark.django_db def test_health_reports_the_database(): response = Client().get("/health/") assert response.status_code == 200 assert json.loads(response.content)["database"] == "connected" def test_cache_keys_are_namespaced_per_service(): """Two services share one Redis; an unnamespaced key merges their budgets.""" assert cache_key("host_rl", "example.com") == "kit:host_rl:example.com" def test_user_agent_falls_back_to_the_service_slug(): assert user_agent().startswith("yksa-kit/") @override_settings(YKSA_USER_AGENT="yksa-tdas/0.1 (+https://tdas.tmtc.yksa.space)") def test_user_agent_is_overridable(): assert "tdas" in user_agent() def test_env_bool_accepts_the_spellings_compose_files_actually_use(monkeypatch): for raw in ("1", "true", "TRUE", "True", "yes", "on"): monkeypatch.setenv("YKSA_TEST_FLAG", raw) assert env_bool("YKSA_TEST_FLAG") is True monkeypatch.setenv("YKSA_TEST_FLAG", "0") assert env_bool("YKSA_TEST_FLAG") is False monkeypatch.delenv("YKSA_TEST_FLAG") assert env_bool("YKSA_TEST_FLAG", default=True) is True def test_env_list_splits_and_strips(monkeypatch): monkeypatch.setenv("YKSA_TEST_HOSTS", "a.example, b.example ,") assert env_list("YKSA_TEST_HOSTS") == ["a.example", "b.example"] def test_read_secret_falls_back_to_the_environment(monkeypatch): monkeypatch.setenv("YKSA_TEST_SECRET", "from-env") assert read_secret("yksa_test_secret") == "from-env" assert read_secret("yksa_absent_secret") is None