136 lines
5.2 KiB
Python
136 lines
5.2 KiB
Python
#!/usr/bin/env python3
|
|
"""草稿箱里再次发起时,嵌入页那一趟必须还认得草稿占下的单号。
|
|
|
|
PYTHONPATH=. python3 scripts/check_draft_embed_serial.py
|
|
|
|
全程跑在内存 sqlite 上,不连开发库、更不碰生产库。钉的是 ``POST /embed/launch``
|
|
与 ``POST /embed/link-draft`` 两条路:从草稿箱打开发起页时前端不再占号(号在草稿行上),
|
|
后端必须把草稿的号补回会话,第三方拿这个号回写时才能认回这一趟、再用草稿底稿发起。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from datetime import datetime, timedelta
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
|
|
from sqlalchemy import create_engine, select # noqa: E402
|
|
from sqlalchemy.orm import Session # noqa: E402
|
|
from sqlalchemy.pool import StaticPool # noqa: E402
|
|
|
|
from app import models # noqa: E402
|
|
from app.api.embeds import _draft_serial, link_draft_to_sessions # noqa: E402
|
|
from app.schemas import EmbedLaunchBody, EmbedLinkDraftBody # noqa: E402
|
|
from app.service import embed_session # noqa: E402
|
|
|
|
FAILS: list[str] = []
|
|
PASSED = 0
|
|
|
|
|
|
def check(label: str, condition, detail="") -> None:
|
|
global PASSED
|
|
if condition:
|
|
PASSED += 1
|
|
print(f"[通过] {label}")
|
|
else:
|
|
FAILS.append(label)
|
|
print(f"[FAIL] {label}{f' -> {detail}' if detail else ''}")
|
|
|
|
|
|
def new_session() -> Session:
|
|
for table in models.Base.metadata.tables.values():
|
|
for column in table.columns:
|
|
if column.server_default is not None:
|
|
column.server_default = None
|
|
bind = create_engine("sqlite://", poolclass=StaticPool,
|
|
connect_args={"check_same_thread": False})
|
|
models.Base.metadata.create_all(bind)
|
|
return Session(bind)
|
|
|
|
|
|
def main() -> int:
|
|
db = new_session()
|
|
person_id = "1148"
|
|
db.add(models.User(id=1, account="admin", display_name="管理员", password_hash="",
|
|
status=1, role=models.ROLE_ADMIN))
|
|
db.add(models.User(id=int(person_id), account="longcongrong", display_name="龙琮荣",
|
|
password_hash="", status=1))
|
|
db.add(models.DataSource(id="embed-source", code="reimb_shfp", name="发票审核",
|
|
provider=models.DataSource.PROVIDER_EMBED,
|
|
url="https://erp.invalid/page?no={arg.processNo}",
|
|
enabled=True, url_links=[]))
|
|
db.commit()
|
|
|
|
draft = models.Draft(id="draft-1", title="草稿", process="p1", process_name="流程",
|
|
form="f1", data={"processNo": "FYBX202611240010"},
|
|
serial="FYBX202611240010", person=person_id,
|
|
person_name="龙琮荣", identity="longcongrong")
|
|
db.add(draft)
|
|
db.commit()
|
|
|
|
check("草稿自带的号能按本人取回",
|
|
_draft_serial(db, draft.id, person_id) == "FYBX202611240010",
|
|
_draft_serial(db, draft.id, person_id))
|
|
check("别人的草稿号不被拿来认趟",
|
|
_draft_serial(db, draft.id, "9999") == "",
|
|
_draft_serial(db, draft.id, "9999"))
|
|
|
|
field = {"key": "field_tabs", "embed": {
|
|
"source": "reimb_shfp", "params": {"processNo": "{form.processNo}"},
|
|
"writeback": {"enabled": True, "fields": ["processNo"]},
|
|
"actions": {"enabled": True, "allow": ["submit", "save"]},
|
|
}}
|
|
row = db.get(models.DataSource, "embed-source")
|
|
session, _token = embed_session.open_session(
|
|
db, row=row, field_key="field_tabs", field=field, person=person_id,
|
|
draft="", serial="", args={"processNo": "FYBX202611240010"},
|
|
targets=["processNo"], decisions={},
|
|
)
|
|
db.commit()
|
|
|
|
class User:
|
|
person_id = "1148"
|
|
|
|
res = link_draft_to_sessions(
|
|
EmbedLinkDraftBody(prekeys=[session.prekey], draft=draft.id),
|
|
user=User(), # type: ignore[arg-type]
|
|
db=db,
|
|
)
|
|
db.expire(session)
|
|
check("存草稿挂接时把草稿的号补回会话",
|
|
str(session.serial or "") == "FYBX202611240010", str(session.serial or ""))
|
|
check("挂接成功", res.get("data", {}).get("linked") == 1, res)
|
|
|
|
body = EmbedLaunchBody(
|
|
code="reimb_shfp", field="field_tabs", draft=draft.id, process="p1",
|
|
serial="", params={"processNo": "FYBX202611240010"},
|
|
)
|
|
check("重新打开发起页那一份报文里草稿号被补上",
|
|
body.serial == "" and _draft_serial(db, body.draft, person_id) == "FYBX202611240010",
|
|
body.serial)
|
|
|
|
session.serial = ""
|
|
db.commit()
|
|
try:
|
|
link_draft_to_sessions(
|
|
EmbedLinkDraftBody(prekeys=[session.prekey], draft=draft.id),
|
|
user=User(), # type: ignore[arg-type]
|
|
db=db,
|
|
)
|
|
db.expire(session)
|
|
check("已挂过草稿但号为空的旧会话,再挂一次也能补回号",
|
|
str(session.serial or "") == "FYBX202611240010", str(session.serial or ""))
|
|
except Exception as exc: # noqa: BLE001
|
|
check("已挂过草稿但号为空的旧会话,再挂一次也能补回号", False, repr(exc))
|
|
|
|
print(f"\n共 {PASSED + len(FAILS)} 项:通过 {PASSED},失败 {len(FAILS)}")
|
|
for item in FAILS:
|
|
print(f" - {item}")
|
|
return 1 if FAILS else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|