Files

174 lines
7.0 KiB
Python

"""报告备案对照,以及首页报告查询、报告审核固定打开站内页。"""
from uuid import uuid4
import pytest
from fastapi.testclient import TestClient
from app.core.config import get_settings
from app.core.security import create_access_token
FOUND = "以出具机构数据为准,协会结果仅供参考。"
MISSING = "协会备案库没有该编号,请向出具机构核实。"
@pytest.fixture
def client(monkeypatch):
monkeypatch.setenv("JWT_SECRET", "test-secret-must-be-32-characters!")
monkeypatch.setenv("API_PAYLOAD_ENCRYPT", "false")
get_settings.cache_clear()
if not get_settings().database_url.strip():
pytest.skip("DATABASE_URL 为空")
from app.main import create_app
with TestClient(create_app()) as api:
yield api
get_settings.cache_clear()
def _headers(token_type: str) -> dict[str, str]:
return {"Authorization": f"Bearer {create_access_token('1', token_type)}"}
def _filing(number: str, *, published: bool, institution: str = "兰州示例估价") -> dict:
return {
"institutionName": institution,
"reportNumber": number,
"issuedOn": "2026-03-18",
"published": published,
}
def test_public_query_reads_only_published_filings(client: TestClient):
staff = _headers("staff")
member = _headers("member")
number = f"GS-{uuid4().hex[:10]}"
other = f"GS-{uuid4().hex[:10]}"
filing_id = None
other_id = None
try:
body = _filing(f" {number} ", published=False, institution=" 兰州示例估价 ")
assert client.post("/api/admin/reports", json=body).status_code == 401
assert client.post("/api/admin/reports", headers=member, json=body).status_code == 403
created = client.post("/api/admin/reports", headers=staff, json=body)
assert created.status_code == 200, created.text
filing_id = created.json()["id"]
hidden = client.get("/api/site/reports/query", params={"number": number})
assert hidden.status_code == 200, hidden.text
assert hidden.json() == {"found": False, "notice": MISSING}
assert "兰州示例估价" not in hidden.text
assert "真" not in hidden.text
assert "伪" not in hidden.text
published = client.put(
f"/api/admin/reports/{filing_id}",
headers=staff,
json=_filing(number, published=True),
)
assert published.status_code == 200, published.text
found = client.get("/api/site/reports/query", params={"number": number})
assert found.json() == {
"found": True,
"institutionName": "兰州示例估价",
"reportNumber": number,
"issuedOn": "2026-03-18",
"notice": FOUND,
}
assert "真" not in found.text
assert "伪" not in found.text
duplicate = client.post("/api/admin/reports", headers=staff, json=_filing(number, published=True))
assert duplicate.status_code == 400
assert duplicate.json()["detail"] == "报告编号已存在"
second = client.post("/api/admin/reports", headers=staff, json=_filing(other, published=True))
assert second.status_code == 200, second.text
other_id = second.json()["id"]
clash = client.put(
f"/api/admin/reports/{filing_id}",
headers=staff,
json=_filing(other, published=True),
)
assert clash.status_code == 400
assert clash.json()["detail"] == "报告编号已存在"
blank = client.get("/api/site/reports/query", params={"number": " "})
assert blank.status_code == 400
assert blank.json()["detail"] == "请填写报告编号"
listed = client.get("/api/admin/reports", headers=staff)
assert listed.status_code == 200
assert any(item["id"] == filing_id and item["published"] is True for item in listed.json()["items"])
assert client.get("/api/admin/reports", headers=member).status_code == 403
finally:
for item_id in (filing_id, other_id):
if item_id is not None:
client.delete(f"/api/admin/reports/{item_id}", headers=staff)
gone = client.get("/api/site/reports/query", params={"number": number})
assert gone.json()["found"] is False
def test_hot_links_open_report_pages_on_site(client: TestClient):
staff = _headers("staff")
before = client.get("/api/admin/home", headers=staff).json()["hotLinks"]["links"]
hidden = [
{"key": "reportQuery", "label": "报告查询", "url": "/reports/query", "visible": False, "sortOrder": 0},
{"key": "reportReview", "label": "报告审核", "url": "/reports/review", "visible": False, "sortOrder": 1},
{"key": "complaint", "label": "投诉举报", "url": "", "visible": False, "sortOrder": 2},
]
try:
saved = client.put(
"/api/admin/home/hot-links",
headers=staff,
json={
"links": [
{
"key": "reportReview",
"label": "辅助审核",
"url": "https://example.com/review",
"visible": True,
"sortOrder": 0,
},
{
"key": "complaint",
"label": "投诉举报",
"url": "https://example.com/complaint",
"visible": True,
"sortOrder": 1,
},
{
"key": "reportQuery",
"label": "查备案",
"url": "https://example.com/report-check",
"visible": True,
"sortOrder": 2,
},
]
},
)
assert saved.status_code == 200, saved.text
home = client.get("/api/site/home")
assert home.json()["hotLinks"]["links"] == [
{"label": "辅助审核", "url": "/reports/review", "navigation": "sameWindow"},
{"label": "投诉举报", "url": "https://example.com/complaint", "navigation": "newWindow"},
{"label": "查备案", "url": "/reports/query", "navigation": "sameWindow"},
]
assert "example.com/report-check" not in home.text
assert "example.com/review" not in home.text
rejected = client.put(
"/api/admin/home/hot-links",
headers=staff,
json={"links": [{"key": "reportQuery", "label": "报告查询", "url": "/reports/query", "visible": True, "sortOrder": 0}]},
)
assert rejected.status_code == 400
cleared = client.put("/api/admin/home/hot-links", headers=staff, json={"links": hidden})
assert cleared.status_code == 200, cleared.text
assert client.get("/api/site/home").json()["hotLinks"]["placeholder"] == "报告查询、报告审核与投诉举报入口即将开放"
finally:
restore = before or hidden
client.put("/api/admin/home/hot-links", headers=staff, json={"links": restore})