The audit logger is opened at startup (exit 2 on failure) and obtained before every call, which is refused with audit_unavailable otherwise. Audit records only take validated path and sudo values and use the request id as correlation id. The rate limiter evicts idle keys, caps tracked keys, audits the first excess per key and window and uses the settings passed to the app. FORWARDED_ALLOW_IPS is now a setting, and startup warns about a missing host allow-list and unavailable OAuth.
48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
"""Pytest configuration and fixtures.
|
|
|
|
Every test runs with a scrubbed environment, an empty working directory (so a
|
|
developer's own ``.env`` is never read) and state paths inside ``tmp_path``.
|
|
Nothing may write to ``/var/log``, ``/var/lib`` or the real home directory.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Generator
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from aegis_gitea_mcp.audit import reset_audit_logger
|
|
from aegis_gitea_mcp.config import Settings, reset_settings
|
|
from aegis_gitea_mcp.request_context import clear_gitea_auth_context
|
|
|
|
|
|
def _reset_singletons() -> None:
|
|
"""Reset every module-level singleton."""
|
|
reset_settings()
|
|
reset_audit_logger()
|
|
clear_gitea_auth_context()
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def reset_globals(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]:
|
|
"""Isolate each test: clean env, tmp cwd, tmp state paths, fresh singletons."""
|
|
for field_name in Settings.model_fields:
|
|
monkeypatch.delenv(field_name.upper(), raising=False)
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
data_dir = tmp_path / "data"
|
|
monkeypatch.setenv("DATA_DIR", str(data_dir))
|
|
monkeypatch.setenv("AUDIT_LOG_PATH", str(data_dir / "audit.log"))
|
|
|
|
_reset_singletons()
|
|
yield
|
|
_reset_singletons()
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_env(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""Minimal valid environment: a Gitea URL and a token."""
|
|
monkeypatch.setenv("GITEA_URL", "https://gitea.example.com")
|
|
monkeypatch.setenv("GITEA_TOKEN", "test-token-12345")
|