test_debug_start_logs_no_credentials.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """Started with DEBUG, the logs still carry no credentials.
  2. httpx and httpcore log every request URL at INFO/DEBUG, and some carry
  3. credentials (bot tokens, webhook URLs, API keys in the query). The SQL echo and
  4. aiosqlite log every statement's parameters, among them every secret saved. The
  5. runtime log-level toggle already kept these quiet; a start with the DEBUG
  6. environment variable did not.
  7. """
  8. import logging
  9. import os
  10. import subprocess
  11. import sys
  12. from pathlib import Path
  13. REPO = Path(__file__).resolve().parents[3]
  14. def test_a_debug_start_logs_no_request_urls_or_sql_parameters(tmp_path):
  15. env = {
  16. **os.environ,
  17. "DEBUG": "true",
  18. "DATA_DIR": str(tmp_path),
  19. "LOG_DIR": str(tmp_path / "logs"),
  20. "DATABASE_URL": f"sqlite+aiosqlite:///{tmp_path / 'b.db'}",
  21. }
  22. code = (
  23. "import logging\n"
  24. "import backend.app.main # noqa: F401\n"
  25. "from backend.app.core.database import engine\n"
  26. "print(engine.sync_engine.hide_parameters)\n"
  27. "print(logging.getLogger('httpx').getEffectiveLevel(), logging.getLogger('httpcore').getEffectiveLevel(),"
  28. " logging.getLogger('aiosqlite').getEffectiveLevel())\n"
  29. )
  30. result = subprocess.run(
  31. [sys.executable, "-c", code], cwd=REPO, env=env, capture_output=True, text=True, timeout=120, check=True
  32. )
  33. *_, hidden, levels = result.stdout.strip().splitlines()
  34. assert hidden == "True"
  35. assert levels.split() == [str(logging.WARNING)] * 3