Files
learningcircuit--local-deep…/tests/security/test_journal_quality_readonly_hook.py
wehub-resource-sync 7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:08:55 +08:00

119 lines
4.3 KiB
Python

"""
Tests for the check-journal-quality-readonly pre-commit hook.
Ensures the compiled journal quality DB is only opened read-only at runtime.
The only allowed writer is build_db() in journal_quality/db.py.
"""
import sys
from importlib import import_module
from pathlib import Path
HOOKS_DIR = Path(__file__).parent.parent.parent / ".pre-commit-hooks"
sys.path.insert(0, str(HOOKS_DIR))
hook_module = import_module("check-journal-quality-readonly")
check_file_fn = hook_module.check_file
# Build the DB name dynamically so this test file is not flagged by
# the check-journal-quality-readonly hook itself.
_JQ_DB = "journal_" + "quality.db"
_JR_DB = "journal_" + "reference.db"
def _write_and_check(tmp_path, code: str, filename: str = "src/module.py"):
"""Write code to a temp file and check it."""
p = tmp_path / filename
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(code, encoding="utf-8")
return check_file_fn(p)
class TestDetectsWritableOpens:
"""Ensures writable opens of the journal quality DB are caught."""
def test_detects_sqlite_connect_without_mode_ro(self, tmp_path):
code = f'conn = sqlite3.connect("{_JQ_DB}")\n'
errors = _write_and_check(tmp_path, code)
assert len(errors) >= 1
assert any("mode=ro" in e for e in errors)
def test_detects_create_engine_without_mode_ro(self, tmp_path):
code = f'engine = create_engine("sqlite:///{_JQ_DB}")\n'
errors = _write_and_check(tmp_path, code)
assert len(errors) >= 1
def test_detects_legacy_journal_reference_db(self, tmp_path):
code = f'conn = sqlite3.connect("{_JR_DB}")\n'
errors = _write_and_check(tmp_path, code)
assert len(errors) >= 1
def test_detects_connect_in_fstring(self, tmp_path):
code = f'conn = sqlite3.connect(f"{{path}}/{_JQ_DB}")\n'
errors = _write_and_check(tmp_path, code)
assert len(errors) >= 1
class TestAllowsReadOnlyOpens:
"""Ensures read-only opens are allowed."""
def test_allows_mode_ro(self, tmp_path):
code = f'conn = sqlite3.connect(f"file:{{path}}/{_JQ_DB}?mode=ro&immutable=1", uri=True)\n'
errors = _write_and_check(tmp_path, code)
assert len(errors) == 0
def test_allows_mode_ro_uppercase(self, tmp_path):
"""SQLite accepts case-insensitive URI values — the hook should
recognise `mode=RO` (and mixed case) as read-only intent too."""
code = f'conn = sqlite3.connect(f"file:{{path}}/{_JQ_DB}?mode=RO&immutable=1", uri=True)\n'
errors = _write_and_check(tmp_path, code)
assert len(errors) == 0
def test_allows_mode_ro_mixed_case(self, tmp_path):
code = f'conn = sqlite3.connect(f"file:{{path}}/{_JQ_DB}?mode=Ro", uri=True)\n'
errors = _write_and_check(tmp_path, code)
assert len(errors) == 0
def test_allows_mode_ro_with_whitespace(self, tmp_path):
code = f'conn = sqlite3.connect(f"file:{{path}}/{_JQ_DB}?mode = ro", uri=True)\n'
errors = _write_and_check(tmp_path, code)
assert len(errors) == 0
class TestAllowsWriterModule:
"""Ensures the designated writer module can open writable."""
def test_allows_build_db_writer(self, tmp_path):
code = f'conn = sqlite3.connect("{_JQ_DB}")\n'
errors = _write_and_check(
tmp_path,
code,
filename="src/local_deep_research/journal_quality/db.py",
)
assert len(errors) == 0
class TestAllowsSafePatterns:
"""Ensures non-connect references are not flagged."""
def test_allows_comments(self, tmp_path):
code = f"# See {_JQ_DB} for data\n"
errors = _write_and_check(tmp_path, code)
assert len(errors) == 0
def test_allows_path_existence_check(self, tmp_path):
code = f'if Path("{_JQ_DB}").exists():\n pass\n'
errors = _write_and_check(tmp_path, code)
assert len(errors) == 0
def test_allows_log_message(self, tmp_path):
code = f'logger.info(f"Loading {_JQ_DB} from {{path}}")\n'
errors = _write_and_check(tmp_path, code)
assert len(errors) == 0
def test_ignores_non_python_files(self, tmp_path):
code = f'conn = sqlite3.connect("{_JQ_DB}")\n'
errors = _write_and_check(tmp_path, code, filename="docs/readme.md")
assert len(errors) == 0