Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

5.6 KB
Raw Blame
"""
Tests for the check-env-vars pre-commit hook.

Covers regressions for the substring-based filename exemption:
- `"test_" in filepath` matched `protest_handler.py` (contains 'test_')
- `"settings/" in filepath` matched `foo_settings_override.py`
- Segment / basename / prefix / suffix matching now used instead.
"""

import subprocess
import sys
import tempfile
from pathlib import Path


HOOK_SCRIPT = (
    Path(__file__).parent.parent.parent
    / ".pre-commit-hooks"
    / "check-env-vars.py"
)


def _run_hook(
    content: str, filename: str = "service.py"
) -> subprocess.CompletedProcess:
    with tempfile.TemporaryDirectory() as d:
        path = Path(d) / filename
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text(content)
        return subprocess.run(
            [sys.executable, str(HOOK_SCRIPT), str(path)],
            capture_output=True,
            text=True,
        )


VIOLATION = 'import os\nx = os.environ.get("LDR_FOO")\n'


class TestSubstringExemptionBugFixed:
    """Production files whose paths merely contain an allowlist substring
    must no longer be silently exempted."""

    def test_protest_handler_scanned(self):
        """'protest_handler.py' contains substring 'test_'."""
        result = _run_hook(VIOLATION, "protest_handler.py")
        assert result.returncode == 1
        assert "LDR_FOO" in result.stdout

    def test_foo_settings_override_scanned(self):
        """'foo_settings_override.py' contains substring 'settings/' is
        false, but 'foo_settings.py' would have matched 'settings' as a
        bare string if we weren't segment-anchored. Use a path where
        'settings' is part of the basename, not a directory segment."""
        result = _run_hook(VIOLATION, "foo_settings.py")
        assert result.returncode == 1

    def test_scriptlike_basename_scanned(self):
        """A basename containing 'scripts' is not a scripts/ directory."""
        result = _run_hook(VIOLATION, "typescripts_loader.py")
        assert result.returncode == 1


class TestRealExemptionsStillApply:
    """All the genuine exemption cases must still work."""

    def test_settings_directory_exempt(self):
        result = _run_hook(VIOLATION, "src/settings/foo.py")
        assert result.returncode == 0

    def test_tests_directory_exempt(self):
        result = _run_hook(VIOLATION, "tests/helpers.py")
        assert result.returncode == 0

    def test_test_prefix_exempt(self):
        result = _run_hook(VIOLATION, "test_foo.py")
        assert result.returncode == 0

    def test_underscore_test_suffix_exempt(self):
        """Go-style *_test.py — previously this was matched as a bare
        substring which happened to work, but now is handled explicitly."""
        result = _run_hook(VIOLATION, "foo_test.py")
        assert result.returncode == 0

    def test_migrations_directory_exempt(self):
        result = _run_hook(VIOLATION, "migrations/0001_init.py")
        assert result.returncode == 0

    def test_scripts_directory_exempt(self):
        result = _run_hook(VIOLATION, "scripts/bootstrap.py")
        assert result.returncode == 0

    def test_examples_directory_exempt(self):
        result = _run_hook(VIOLATION, "examples/demo.py")
        assert result.returncode == 0

    def test_log_utils_exempt(self):
        result = _run_hook(VIOLATION, "log_utils.py")
        assert result.returncode == 0

    def test_sqlcipher_utils_exempt(self):
        result = _run_hook(VIOLATION, "sqlcipher_utils.py")
        assert result.returncode == 0

    def test_server_config_exempt(self):
        result = _run_hook(VIOLATION, "server_config.py")
        assert result.returncode == 0

    def test_routers_rag_exempt(self):
        """Path-anchored entry: 'web/routers/rag.py' (LDR_LOCAL_INDEX_ROOTS).

        Replaces the previous `security/rate_limiter.py` test — that
        module was removed in the FastAPI migration along with its
        ALLOWED_PATH_ENDINGS entry.
        """
        result = _run_hook(
            VIOLATION, "src/local_deep_research/web/routers/rag.py"
        )
        assert result.returncode == 0


class TestBareRagPyOutsideRoutersFlagged:
    """A file literally named rag.py at a different path must NOT be
    exempt — only the routers/ one is."""

    def test_rag_py_outside_routers_flagged(self):
        result = _run_hook(VIOLATION, "other/rag.py")
        assert result.returncode == 1


class TestWindowsBackslashPathsExempt:
    """Path-anchored exemptions must match backslash paths too, so the
    forward-slash-anchored ALLOWED_PATH_ENDINGS survive a Windows
    checkout (where Path.parts and str(Path(...)) use backslashes)."""

    def _checker(self, filename: str):
        import importlib.util

        spec = importlib.util.spec_from_file_location(
            "check_env_vars", HOOK_SCRIPT
        )
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        return module.EnvVarChecker(filename)

    def test_backslash_rate_limiter_path_exempt(self):
        checker = self._checker(
            "src\\local_deep_research\\security\\rate_limiter.py"
        )
        assert checker._is_file_allowed() is True

    def test_backslash_secure_logging_path_exempt(self):
        checker = self._checker(
            "src\\local_deep_research\\security\\secure_logging.py"
        )
        assert checker._is_file_allowed() is True


class TestRealViolation:
    """Sanity check — normal production files are still flagged."""

    def test_production_module_flagged(self):
        result = _run_hook(VIOLATION, "service.py")
        assert result.returncode == 1