123 lines
5.0 KiB
Python
123 lines
5.0 KiB
Python
#!/usr/bin/env python3
|
||
"""附件整包下载的离线验收:内存 SQLite + 临时本地存储,不碰真库、不碰真字节。
|
||
|
||
cd backend && PYTHONPATH=. python3 scripts/check_attachment_bundle.py
|
||
|
||
钉的是:同名附件不互相覆盖、路径分隔符不跑出 ZIP、超限直接拒,
|
||
以及"看得到这张单的人就能打整包":发起人、待办/已办、待阅/已阅、管理员。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import io
|
||
import os
|
||
import pathlib
|
||
import sys
|
||
import tempfile
|
||
import zipfile
|
||
from types import SimpleNamespace
|
||
|
||
HERE = pathlib.Path(__file__).resolve().parent
|
||
sys.path.insert(0, str(HERE.parent))
|
||
os.environ["OA_DB_HOST"] = "127.0.0.1"
|
||
os.environ["OA_DB_PORT"] = "1"
|
||
os.environ["OA_DB_NAME"] = "unisoa_offline_check"
|
||
os.environ["OA_SCHEDULER"] = "off"
|
||
os.environ["OA_AUTO_CREATE_SCHEMA"] = "false"
|
||
|
||
from app import models # noqa: E402
|
||
from app.service import attachment_bundle as bundle # noqa: E402
|
||
from sqlalchemy import create_engine # noqa: E402
|
||
from sqlalchemy.orm import Session # noqa: E402
|
||
from sqlalchemy.pool import StaticPool # noqa: E402
|
||
|
||
FAILS: list[str] = []
|
||
|
||
|
||
def eq(name, got, want):
|
||
if got != want:
|
||
FAILS.append(f"{name}:期望 {want!r},实得 {got!r}")
|
||
|
||
|
||
def ok(name, condition, detail=""):
|
||
if not condition:
|
||
FAILS.append(f"{name}:{detail or '条件不成立'}")
|
||
|
||
|
||
def main() -> int:
|
||
root = pathlib.Path(tempfile.mkdtemp(prefix="unisoa-bundle-"))
|
||
os.environ["OA_LOCAL_ROOT"] = str(root)
|
||
from app.storage import local_store
|
||
|
||
store = local_store()
|
||
uris = []
|
||
for name, body in [("a.txt", b"first"), ("b.txt", b"second")]:
|
||
stored = store.save(purpose="check", name=name, data=body)
|
||
uris.append(stored.uri)
|
||
|
||
rows = [
|
||
SimpleNamespace(name="报告.pdf", storage=uris[0], length=5),
|
||
SimpleNamespace(name="报告.pdf", storage=uris[1], length=6),
|
||
SimpleNamespace(name="../outside.txt", storage=uris[1], length=6),
|
||
]
|
||
response = bundle.build(rows, "差旅报销")
|
||
eq("响应类型", response.media_type, "application/zip")
|
||
with zipfile.ZipFile(io.BytesIO(response.body)) as archive:
|
||
eq("同名不覆盖", archive.namelist(), ["报告.pdf", "报告 (2).pdf", "outside.txt"])
|
||
eq("内容不错位", archive.read("报告 (2).pdf"), b"second")
|
||
|
||
huge = [SimpleNamespace(name="big.bin", storage=uris[0], length=bundle.MAX_TOTAL_BYTES + 1)]
|
||
try:
|
||
bundle.build(huge, "big")
|
||
FAILS.append("超限应当直接拒,不能拼出巨大 ZIP")
|
||
except bundle.BundleTooLarge:
|
||
pass
|
||
|
||
engine = create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool)
|
||
models.Base.metadata.create_all(
|
||
engine,
|
||
tables=[
|
||
models.Work.__table__, models.WorkCompleted.__table__,
|
||
models.Task.__table__, models.TaskCompleted.__table__,
|
||
models.Read.__table__, models.ReadCompleted.__table__,
|
||
],
|
||
)
|
||
subject = models.Work(id="w1", job="J1", creator="starter", creator_identity="starter", process="p1", title="t")
|
||
with Session(engine) as db:
|
||
db.add(subject)
|
||
db.commit()
|
||
eq("发起人能打", bundle.is_visible(db, subject,
|
||
SimpleNamespace(ref="starter", person_id="starter", is_admin=False)), True)
|
||
eq("管理员能打", bundle.is_visible(db, subject,
|
||
SimpleNamespace(ref="admin", person_id="admin", is_admin=True)), True)
|
||
eq("无关的人不能打", bundle.is_visible(db, subject,
|
||
SimpleNamespace(ref="stranger", person_id="stranger", is_admin=False)), False)
|
||
with Session(engine) as db:
|
||
db.add(models.Task(id="t1", job="J1", work="w1", identity="handler", status="pending",
|
||
process="p1", activity="a1"))
|
||
db.add(models.TaskCompleted(id="tc1", job="J1", work="w1", identity="done-guy",
|
||
process="p1", activity="a1"))
|
||
db.add(models.Read(id="r1", job="J1", work="w1", identity="cc-guy",
|
||
process="p1", activity="a1"))
|
||
db.add(models.ReadCompleted(id="rc1", job="J1", work="w1", identity="read-guy",
|
||
process="p1", activity="a1"))
|
||
db.commit()
|
||
eq("待办人能打(真有记录)", bundle.is_visible(db, subject,
|
||
SimpleNamespace(ref="handler", person_id="handler", is_admin=False)), True)
|
||
eq("已办人能打", bundle.is_visible(db, subject,
|
||
SimpleNamespace(ref="done-guy", person_id="done-guy", is_admin=False)), True)
|
||
eq("已阅能打(真有记录)", bundle.is_visible(db, subject,
|
||
SimpleNamespace(ref="read-guy", person_id="read-guy", is_admin=False)), True)
|
||
eq("抄送待阅能打", bundle.is_visible(db, subject,
|
||
SimpleNamespace(ref="cc-guy", person_id="cc-guy", is_admin=False)), True)
|
||
|
||
if FAILS:
|
||
print("\n".join(FAILS))
|
||
return 1
|
||
print("通过:整包下载口径、ZIP 条目、超限与权限")
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|