import pytest import estimator.__main__ as cli from estimator.__main__ import main from estimator.config import get_settings @pytest.fixture def env(monkeypatch, dsn): monkeypatch.setenv("EST_DATABASE__DSN", dsn) monkeypatch.setenv("EST_LLM__ANTHROPIC__API_KEY", "sk-test") get_settings.cache_clear() yield monkeypatch get_settings.cache_clear() def test_wait_db_returns_at_once_when_the_database_is_up(env, capsys): assert main(["wait-db", "--timeout", "5"]) == 0 assert "the database is ready" in capsys.readouterr().out def test_wait_db_gives_up_and_does_not_leak_the_connection_string(env, capsys): env.setenv("EST_DATABASE__DSN", "postgresql://secretuser:secretpass@127.0.0.1:1/nothing") get_settings.cache_clear() assert main(["wait-db", "--timeout", "1"]) == 1 err = capsys.readouterr().err assert "not reachable after 1s" in err and "waiting for the database" in err assert "secretpass" not in err and "secretuser" not in err and "127.0.0.1" not in err def test_serve_passes_the_proxy_settings_to_uvicorn(env): calls = [] env.setattr(cli.uvicorn, "run", lambda *a, **kw: calls.append((a, kw))) main(["serve"]) (args, kwargs), = calls assert args == ("estimator.api.app:create_app",) and kwargs["factory"] is True assert (kwargs["proxy_headers"], kwargs["forwarded_allow_ips"]) == (False, "127.0.0.1") # off by default assert kwargs["host"] == "0.0.0.0" and kwargs["port"] == 8000 # reachable from outside a container def test_proxy_headers_can_be_switched_on_for_a_reverse_proxy(env): calls = [] env.setattr(cli.uvicorn, "run", lambda *a, **kw: calls.append(kw)) env.setenv("EST_API__PROXY_HEADERS", "true") env.setenv("EST_API__FORWARDED_ALLOW_IPS", "10.0.0.0/8") get_settings.cache_clear() main([]) # serving is the default command assert calls[0]["proxy_headers"] is True and calls[0]["forwarded_allow_ips"] == "10.0.0.0/8"