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
85 lines
2.9 KiB
Python
85 lines
2.9 KiB
Python
"""Predatory data source refuses to overwrite a healthy snapshot when
|
|
the upstream fetch returns suspiciously few rows.
|
|
|
|
This guards against the partial-CDN-outage failure mode: if 2 of the 3
|
|
upstream CSVs return 0 rows, the resulting predatory.json would silently
|
|
disable predatory filtering for everyone. The floor check raises
|
|
instead, leaving the previous good snapshot in place.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from local_deep_research.journal_quality.data_sources import (
|
|
predatory as pred_mod,
|
|
)
|
|
from local_deep_research.journal_quality.data_sources.predatory import (
|
|
PredatorySource,
|
|
)
|
|
|
|
|
|
def _csv_response(text: str) -> MagicMock:
|
|
resp = MagicMock()
|
|
resp.raise_for_status = MagicMock()
|
|
resp.text = text
|
|
return resp
|
|
|
|
|
|
def test_below_floor_raises_and_does_not_overwrite(tmp_path, monkeypatch):
|
|
"""3 CSVs all near-empty → fetch raises before writing the file.
|
|
|
|
A pre-existing predatory.json (simulating last good build) must
|
|
survive on-disk so the filter keeps working with stale-but-valid
|
|
data instead of silently no-op-ing.
|
|
"""
|
|
monkeypatch.setattr(pred_mod, "_MIN_PREDATORY_TOTAL", 100)
|
|
|
|
existing = tmp_path / "predatory.json"
|
|
existing.write_bytes(b'{"sentinel": "previous good build"}')
|
|
|
|
# Only 1 publisher + 1 journal + 0 hijacked = 2 entries (well below 100)
|
|
publishers_csv = "name,url\nBad Publisher,http://example.com\n"
|
|
journals_csv = "name,url\nBad Journal,http://example.com\n"
|
|
hijacked_csv = "hijacked,hijackedurl,authentic,authenticurl\n"
|
|
|
|
with patch(
|
|
"local_deep_research.security.safe_requests.safe_get_with_retries",
|
|
) as mock_get:
|
|
mock_get.side_effect = [
|
|
_csv_response(publishers_csv),
|
|
_csv_response(journals_csv),
|
|
_csv_response(hijacked_csv),
|
|
]
|
|
with pytest.raises(RuntimeError, match="suspiciously few records"):
|
|
PredatorySource().fetch(tmp_path)
|
|
|
|
# Existing snapshot is intact (no partial overwrite).
|
|
assert existing.read_bytes() == b'{"sentinel": "previous good build"}'
|
|
|
|
|
|
def test_above_floor_writes_normally(tmp_path, monkeypatch):
|
|
"""A healthy fetch (well above the floor) writes the snapshot."""
|
|
monkeypatch.setattr(pred_mod, "_MIN_PREDATORY_TOTAL", 5)
|
|
|
|
publishers_csv = "name,url\n" + "\n".join(
|
|
f"Pub {i},http://p{i}.example" for i in range(10)
|
|
)
|
|
journals_csv = "name,url\n"
|
|
hijacked_csv = "hijacked,hijackedurl,authentic,authenticurl\n"
|
|
|
|
with patch(
|
|
"local_deep_research.security.safe_requests.safe_get_with_retries",
|
|
) as mock_get:
|
|
mock_get.side_effect = [
|
|
_csv_response(publishers_csv),
|
|
_csv_response(journals_csv),
|
|
_csv_response(hijacked_csv),
|
|
]
|
|
result = PredatorySource().fetch(tmp_path)
|
|
|
|
assert result == 10
|
|
assert (tmp_path / "predatory.json").exists()
|