feat: add official artifact download endpoints and UI for KS-2/KS-3
This commit is contained in:
@@ -0,0 +1,378 @@
|
||||
"""Tests for DOC-B6: artifact download UI and endpoints."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import create_engine, select
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from app.config import ENV_DOCUMENT_STORAGE_ROOT
|
||||
from app.database.database import Base
|
||||
from app.main import app
|
||||
from app.models import ( # noqa: F401
|
||||
agreement, boq, daily_report, document_artifact,
|
||||
ks2 as ks2_models, ks3 as ks3_models, payment, project, work_journal,
|
||||
)
|
||||
from app.models.boq import BoqDocument, WorkItem
|
||||
from app.models.daily_report import DailyReport, DailyReportItem
|
||||
from app.models.document_artifact import DocumentArtifact
|
||||
from app.models.ks2 import KS2
|
||||
from app.models.ks3 import KS3
|
||||
from app.models.project import Project
|
||||
from app.routers import ks2 as ks2_router, ks3 as ks3_router
|
||||
from app.services.document_artifact_service import (
|
||||
compute_content_hash,
|
||||
persist_artifact,
|
||||
resolve_storage_path,
|
||||
)
|
||||
from app.services.ks2_service import cancel_ks2, issue_ks2, save_draft as save_ks2_draft
|
||||
from app.services.ks3_service import cancel_ks3, issue_ks3, save_draft as save_ks3_draft
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def storage_root(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv(ENV_DOCUMENT_STORAGE_ROOT, str(tmp_path / "b6-storage"))
|
||||
return tmp_path / "b6-storage"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def db_session(storage_root):
|
||||
engine = create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool)
|
||||
Base.metadata.create_all(engine)
|
||||
Session = sessionmaker(bind=engine, expire_on_commit=False)
|
||||
session = Session()
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
session.close()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ctx(db_session):
|
||||
"""Project + work item + DR so KS-2 can be issued."""
|
||||
p = Project(name="B6 project")
|
||||
db_session.add(p)
|
||||
db_session.flush()
|
||||
doc = BoqDocument(project_id=p.id, name="BOQ")
|
||||
db_session.add(doc)
|
||||
db_session.flush()
|
||||
wi = WorkItem(project_id=p.id, boq_document_id=doc.id, name="Item", unit="шт",
|
||||
quantity=Decimal("100"), unit_price=Decimal("10"), amount=Decimal("1000"))
|
||||
db_session.add(wi)
|
||||
db_session.flush()
|
||||
dr = DailyReport(project_id=p.id, report_date=date(2026, 2, 1))
|
||||
db_session.add(dr)
|
||||
db_session.flush()
|
||||
db_session.add(DailyReportItem(daily_report_id=dr.id, work_item_id=wi.id, completed_quantity=Decimal("50")))
|
||||
db_session.commit()
|
||||
return {"project_id": p.id, "work_item_id": wi.id}
|
||||
|
||||
|
||||
def _client(Session):
|
||||
def override():
|
||||
s = Session()
|
||||
try:
|
||||
yield s
|
||||
finally:
|
||||
s.close()
|
||||
app.dependency_overrides[ks2_router.get_db] = override
|
||||
app.dependency_overrides[ks3_router.get_db] = override
|
||||
from app.routers import projects as project_router
|
||||
app.dependency_overrides[project_router.get_db] = override
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
from fastapi.testclient import TestClient # noqa: E402
|
||||
|
||||
|
||||
def _clear():
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
|
||||
def _make_issued_ks2(db, ctx, number="K2-B6"):
|
||||
draft = save_ks2_draft(db, ctx["project_id"], number, date(2026, 3, 1), None,
|
||||
[(ctx["work_item_id"], Decimal("10"))],
|
||||
reporting_period_from=date(2026, 2, 1),
|
||||
reporting_period_to=date(2026, 2, 28))
|
||||
return issue_ks2(db, ctx["project_id"], draft.id)
|
||||
|
||||
|
||||
def _make_issued_ks3(db, ctx, ks2, number="K3-B6"):
|
||||
draft = save_ks3_draft(db, ctx["project_id"], number, date(2026, 4, 1), None,
|
||||
[ks2.id],
|
||||
reporting_period_from=date(2026, 2, 1),
|
||||
reporting_period_to=date(2026, 2, 28))
|
||||
return issue_ks3(db, ctx["project_id"], draft.id)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# KS-2
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_ks2_issued_download_xlsx(storage_root, db_session, ctx):
|
||||
ks2 = _make_issued_ks2(db_session, ctx)
|
||||
art = db_session.execute(select(DocumentArtifact).where(
|
||||
DocumentArtifact.document_type == "KS2",
|
||||
DocumentArtifact.document_id == ks2.id,
|
||||
DocumentArtifact.is_current.is_(True),
|
||||
)).scalar_one()
|
||||
stored = resolve_storage_path(art.storage_key).read_bytes()
|
||||
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
resp = client.get(f"/projects/{ctx['project_id']}/ks2/{ks2.id}/artifact")
|
||||
assert resp.status_code == 200
|
||||
assert resp.content == stored
|
||||
assert "spreadsheetml" in resp.headers["content-type"]
|
||||
assert "attachment" in resp.headers["content-disposition"]
|
||||
assert ".xlsx" in resp.headers["content-disposition"]
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
|
||||
def test_ks2_download_does_not_mutate_artifact(storage_root, db_session, ctx):
|
||||
ks2 = _make_issued_ks2(db_session, ctx)
|
||||
art = db_session.execute(select(DocumentArtifact).where(
|
||||
DocumentArtifact.document_type == "KS2",
|
||||
DocumentArtifact.is_current.is_(True),
|
||||
)).scalar_one()
|
||||
art_id, is_current, ch, ih = art.id, art.is_current, art.content_hash, art.input_hash
|
||||
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
client.get(f"/projects/{ctx['project_id']}/ks2/{ks2.id}/artifact")
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
art = db_session.get(DocumentArtifact, art_id)
|
||||
assert art.is_current == is_current
|
||||
assert art.content_hash == ch
|
||||
assert art.input_hash == ih
|
||||
count = db_session.execute(select(DocumentArtifact).where(
|
||||
DocumentArtifact.document_type == "KS2")).all()
|
||||
assert len(count) == 1
|
||||
|
||||
|
||||
def test_ks2_cancelled_download_same_artifact(storage_root, db_session, ctx):
|
||||
ks2 = _make_issued_ks2(db_session, ctx)
|
||||
art = db_session.execute(select(DocumentArtifact).where(
|
||||
DocumentArtifact.document_type == "KS2",
|
||||
DocumentArtifact.is_current.is_(True),
|
||||
)).scalar_one()
|
||||
stored = resolve_storage_path(art.storage_key).read_bytes()
|
||||
cancel_ks2(db_session, ctx["project_id"], ks2.id)
|
||||
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
resp = client.get(f"/projects/{ctx['project_id']}/ks2/{ks2.id}/artifact")
|
||||
assert resp.status_code == 200
|
||||
assert resp.content == stored
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
|
||||
def test_ks2_draft_artifact_rejected(storage_root, db_session, ctx):
|
||||
draft = save_ks2_draft(db_session, ctx["project_id"], "K2-D", date(2026, 3, 1), None,
|
||||
[(ctx["work_item_id"], Decimal("10"))],
|
||||
reporting_period_from=date(2026, 2, 1),
|
||||
reporting_period_to=date(2026, 2, 28))
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
resp = client.get(f"/projects/{ctx['project_id']}/ks2/{draft.id}/artifact")
|
||||
assert resp.status_code == 409
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
|
||||
def test_ks2_missing_artifact_404(storage_root, db_session, ctx):
|
||||
ks2 = _make_issued_ks2(db_session, ctx)
|
||||
# Delete the physical file
|
||||
art = db_session.execute(select(DocumentArtifact).where(
|
||||
DocumentArtifact.document_type == "KS2", DocumentArtifact.is_current.is_(True))).scalar_one()
|
||||
resolve_storage_path(art.storage_key).unlink()
|
||||
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
resp = client.get(f"/projects/{ctx['project_id']}/ks2/{ks2.id}/artifact")
|
||||
assert resp.status_code == 404
|
||||
assert "недоступна" in resp.text
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
|
||||
def test_ks2_hash_mismatch_not_served(storage_root, db_session, ctx):
|
||||
ks2 = _make_issued_ks2(db_session, ctx)
|
||||
art = db_session.execute(select(DocumentArtifact).where(
|
||||
DocumentArtifact.document_type == "KS2", DocumentArtifact.is_current.is_(True))).scalar_one()
|
||||
path = resolve_storage_path(art.storage_key)
|
||||
path.write_bytes(b"tampered")
|
||||
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
resp = client.get(f"/projects/{ctx['project_id']}/ks2/{ks2.id}/artifact")
|
||||
assert resp.status_code == 500
|
||||
assert b"tampered" not in resp.content
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
|
||||
def test_ks2_cross_project_rejected(storage_root, db_session, ctx):
|
||||
ks2 = _make_issued_ks2(db_session, ctx)
|
||||
other = Project(name="Other")
|
||||
db_session.add(other)
|
||||
db_session.commit()
|
||||
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
# Wrong project id
|
||||
resp = client.get(f"/projects/{other.id}/ks2/{ks2.id}/artifact")
|
||||
assert resp.status_code == 404
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# KS-3
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_ks3_issued_download_docx(storage_root, db_session, ctx):
|
||||
ks2 = _make_issued_ks2(db_session, ctx)
|
||||
ks3 = _make_issued_ks3(db_session, ctx, ks2)
|
||||
art = db_session.execute(select(DocumentArtifact).where(
|
||||
DocumentArtifact.document_type == "KS3",
|
||||
DocumentArtifact.document_id == ks3.id,
|
||||
DocumentArtifact.is_current.is_(True),
|
||||
)).scalar_one()
|
||||
stored = resolve_storage_path(art.storage_key).read_bytes()
|
||||
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
resp = client.get(f"/projects/{ctx['project_id']}/ks3/{ks3.id}/artifact")
|
||||
assert resp.status_code == 200
|
||||
assert resp.content == stored
|
||||
assert "wordprocessingml" in resp.headers["content-type"]
|
||||
assert ".docx" in resp.headers["content-disposition"]
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
|
||||
def test_ks3_cancelled_download_same_artifact(storage_root, db_session, ctx):
|
||||
ks2 = _make_issued_ks2(db_session, ctx)
|
||||
ks3 = _make_issued_ks3(db_session, ctx, ks2)
|
||||
art = db_session.execute(select(DocumentArtifact).where(
|
||||
DocumentArtifact.document_type == "KS3",
|
||||
DocumentArtifact.is_current.is_(True),
|
||||
)).scalar_one()
|
||||
stored = resolve_storage_path(art.storage_key).read_bytes()
|
||||
cancel_ks3(db_session, ctx["project_id"], ks3.id)
|
||||
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
resp = client.get(f"/projects/{ctx['project_id']}/ks3/{ks3.id}/artifact")
|
||||
assert resp.status_code == 200
|
||||
assert resp.content == stored
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
|
||||
def test_ks3_draft_artifact_rejected(storage_root, db_session, ctx):
|
||||
ks2 = _make_issued_ks2(db_session, ctx)
|
||||
draft = save_ks3_draft(db_session, ctx["project_id"], "K3-D", date(2026, 4, 1), None,
|
||||
[ks2.id],
|
||||
reporting_period_from=date(2026, 2, 1),
|
||||
reporting_period_to=date(2026, 2, 28))
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
resp = client.get(f"/projects/{ctx['project_id']}/ks3/{draft.id}/artifact")
|
||||
assert resp.status_code == 409
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
|
||||
def test_ks3_missing_artifact_404(storage_root, db_session, ctx):
|
||||
ks2 = _make_issued_ks2(db_session, ctx)
|
||||
ks3 = _make_issued_ks3(db_session, ctx, ks2)
|
||||
art = db_session.execute(select(DocumentArtifact).where(
|
||||
DocumentArtifact.document_type == "KS3", DocumentArtifact.is_current.is_(True))).scalar_one()
|
||||
resolve_storage_path(art.storage_key).unlink()
|
||||
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
resp = client.get(f"/projects/{ctx['project_id']}/ks3/{ks3.id}/artifact")
|
||||
assert resp.status_code == 404
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
|
||||
def test_ks3_cross_project_rejected(storage_root, db_session, ctx):
|
||||
ks2 = _make_issued_ks2(db_session, ctx)
|
||||
ks3 = _make_issued_ks3(db_session, ctx, ks2)
|
||||
other = Project(name="Other2")
|
||||
db_session.add(other)
|
||||
db_session.commit()
|
||||
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
resp = client.get(f"/projects/{other.id}/ks3/{ks3.id}/artifact")
|
||||
assert resp.status_code == 404
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# UI visibility
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_ks2_ui_shows_official_button_only_non_draft(storage_root, db_session, ctx):
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
# Draft: no official button
|
||||
draft = save_ks2_draft(db_session, ctx["project_id"], "K2-UI", date(2026, 3, 1), None,
|
||||
[(ctx["work_item_id"], Decimal("5"))],
|
||||
reporting_period_from=date(2026, 2, 1),
|
||||
reporting_period_to=date(2026, 2, 28))
|
||||
resp = client.get(f"/projects/{ctx['project_id']}/ks2/{draft.id}")
|
||||
assert "Официальная форма" not in resp.text
|
||||
|
||||
# Issued: button present
|
||||
ks2 = issue_ks2(db_session, ctx["project_id"], draft.id)
|
||||
resp = client.get(f"/projects/{ctx['project_id']}/ks2/{ks2.id}")
|
||||
assert "Официальная форма" in resp.text
|
||||
assert "/artifact" in resp.text
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
|
||||
def test_ks2_ui_cancelled_shows_storno_badge(storage_root, db_session, ctx):
|
||||
ks2 = _make_issued_ks2(db_session, ctx)
|
||||
cancel_ks2(db_session, ctx["project_id"], ks2.id)
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
resp = client.get(f"/projects/{ctx['project_id']}/ks2/{ks2.id}")
|
||||
assert "Официальная форма" in resp.text
|
||||
assert "СТОРНИРОВАН" in resp.text
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
|
||||
def test_ks3_ui_shows_official_button(storage_root, db_session, ctx):
|
||||
ks2 = _make_issued_ks2(db_session, ctx)
|
||||
ks3 = _make_issued_ks3(db_session, ctx, ks2)
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
resp = client.get(f"/projects/{ctx['project_id']}/ks3/{ks3.id}")
|
||||
assert "Официальная форма" in resp.text
|
||||
assert "/artifact" in resp.text
|
||||
finally:
|
||||
_clear()
|
||||
|
||||
|
||||
def test_existing_export_endpoints_still_work(storage_root, db_session, ctx):
|
||||
ks2 = _make_issued_ks2(db_session, ctx)
|
||||
client = _client(sessionmaker(bind=db_session.get_bind(), expire_on_commit=False))
|
||||
try:
|
||||
assert client.get(f"/projects/{ctx['project_id']}/ks2/{ks2.id}/export/excel").status_code == 200
|
||||
assert client.get(f"/projects/{ctx['project_id']}/ks2/{ks2.id}/export/word").status_code == 200
|
||||
finally:
|
||||
_clear()
|
||||
Reference in New Issue
Block a user