125 lines
4.9 KiB
Python
125 lines
4.9 KiB
Python
"""Публичная витрина показывает нормативный корпус."""
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
if str(ROOT) not in sys.path:
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
|
|
def _tsn(root: Path, key: str, title: str, fetched: str):
|
|
d = root / "snapshots" / "niac_tsn" / "collections" / key
|
|
d.mkdir(parents=True)
|
|
(d / "extract.json").write_text("{}", encoding="utf-8")
|
|
(d / "meta.json").write_text(json.dumps({"title": title, "fetched_at": fetched,
|
|
"key": key}), encoding="utf-8")
|
|
|
|
|
|
def _bim(root: Path, key: str, fetched: str):
|
|
d = root / "snapshots" / "fgis" / "bim_letters" / key
|
|
d.mkdir(parents=True)
|
|
(d / "letter.pdf").write_bytes(b"%PDF-1.4")
|
|
(d / "meta.json").write_text(json.dumps({"source": "fgis_bim",
|
|
"fetched_at": fetched}), encoding="utf-8")
|
|
|
|
|
|
def test_counts_only_available_public_documents_and_orders_feed(tmp_path, monkeypatch):
|
|
import ext_state
|
|
import public_documents
|
|
|
|
monkeypatch.setattr(ext_state, "ext_dir", lambda: tmp_path)
|
|
public_documents.clear_cache()
|
|
_tsn(tmp_path, "older", "ТСН: старый", "2026-10-01T10:00:00Z")
|
|
_tsn(tmp_path, "newer", "ТСН: новый", "2026-11-18T10:00:00Z")
|
|
_bim(tmp_path, "letter", "2026-11-01T10:00:00Z")
|
|
incomplete = tmp_path / "snapshots" / "niac_tsn" / "collections" / "incomplete"
|
|
incomplete.mkdir()
|
|
(incomplete / "meta.json").write_text('{"title":"не загружен"}', encoding="utf-8")
|
|
|
|
result = public_documents.snapshot()
|
|
assert result["count"] == 3
|
|
assert [d["title"] for d in result["recent"]] == [
|
|
"ТСН: новый", "Письмо-индекс Минстроя", "ТСН: старый"]
|
|
|
|
|
|
def test_public_markup_escapes_titles_and_has_honest_empty_state(tmp_path, monkeypatch):
|
|
import ext_state
|
|
import public_documents
|
|
|
|
monkeypatch.setattr(ext_state, "ext_dir", lambda: tmp_path)
|
|
public_documents.clear_cache()
|
|
html = public_documents.render()
|
|
assert "Документы пока не загружены" in html
|
|
assert 'id="public-doc-count">0<' in html
|
|
|
|
_tsn(tmp_path, "xss", '<img src=x onerror=alert(1)>', "2026-11-18T10:00:00Z")
|
|
public_documents.clear_cache()
|
|
html = public_documents.render()
|
|
assert "<img" in html
|
|
assert "<img" not in html
|
|
assert str(tmp_path) not in html
|
|
assert 'id="public-doc-count">1<' in html
|
|
|
|
|
|
def test_landing_contains_public_documents_section():
|
|
html = (ROOT / "static" / "chat.html").read_text(encoding="utf-8")
|
|
assert 'id="public-documents"' in html
|
|
assert "{{PUBLIC_DOCUMENTS}}" in html
|
|
|
|
|
|
def test_homepage_renders_public_feed_without_login(tmp_path, monkeypatch):
|
|
import ext_state
|
|
import public_documents
|
|
import main
|
|
from fastapi.testclient import TestClient
|
|
|
|
monkeypatch.chdir(ROOT)
|
|
monkeypatch.setattr(ext_state, "ext_dir", lambda: tmp_path)
|
|
public_documents.clear_cache()
|
|
_tsn(tmp_path, "new", "ТСН: опубликованный", "2026-11-18T10:00:00Z")
|
|
response = TestClient(main.app).get("/?v=" + main.APP_VERSION)
|
|
assert response.status_code == 200
|
|
assert 'id="public-doc-count">1<' in response.text
|
|
assert "ТСН: опубликованный" in response.text
|
|
assert "{{PUBLIC_DOCUMENTS}}" not in response.text
|
|
|
|
|
|
def test_isolated_stage_reads_only_exported_public_summary(tmp_path, monkeypatch):
|
|
import ext_state
|
|
import public_documents
|
|
|
|
source = tmp_path / "production"
|
|
stage = tmp_path / "stage"
|
|
_tsn(source, "public", "Общий норматив", "2026-11-18T10:00:00Z")
|
|
_bim(source, "letter", "2026-11-17T10:00:00Z")
|
|
public_documents.export_summary(source, stage / "landing_documents.json")
|
|
|
|
monkeypatch.setattr(ext_state, "ext_dir", lambda: stage)
|
|
public_documents.clear_cache()
|
|
data = public_documents.snapshot()
|
|
assert data["count"] == 2
|
|
assert [doc["title"] for doc in data["recent"]] == [
|
|
"Общий норматив", "Письмо-индекс Минстроя"]
|
|
|
|
|
|
def test_counts_builtin_normative_literature_too(tmp_path, monkeypatch):
|
|
import ext_state
|
|
import public_documents
|
|
|
|
ext = tmp_path / "ext"
|
|
project = tmp_path / "norms"
|
|
literature = project / "literature" / "methods"
|
|
literature.mkdir(parents=True)
|
|
(literature / "method.pdf").write_bytes(b"%PDF-1.4")
|
|
(literature / "appendix.htm").write_text("нормы", encoding="utf-8")
|
|
(literature / "notes.txt").write_text("не документ", encoding="utf-8")
|
|
_tsn(ext, "tsn", "ТСН", "2026-11-18T10:00:00Z")
|
|
monkeypatch.setattr(ext_state, "ext_dir", lambda: ext)
|
|
monkeypatch.setenv("SMETA_PROJECT_DIR", str(project))
|
|
public_documents.clear_cache()
|
|
|
|
result = public_documents.snapshot()
|
|
assert result["count"] == 3
|
|
assert [doc["title"] for doc in result["recent"]] == ["ТСН"]
|