feat(invoice-check): 立账凭证检核增加收件人配置并迁移到chat页面
This commit is contained in:
1 parent
18e957545c
commit
7b80b4c395
72 files changed
+2705
-263
No files matched your search
@@ -19,7 +19,7 @@ def test_agent_type_catalog_contains_three_seed_types():
|
||||
"""Catalog 应包含 generic、shipping_notice、file_check 等注册类型(含后续扩展)。"""
|
||||
assert {
|
||||
'generic', 'shipping_notice', 'file_check',
|
||||
'procurement_contract_review', 'in_transit_transfer', 'contract_payment',
|
||||
'procurement_contract_review', 'in_transit_transfer', 'contract_payment', 'voucher_check',
|
||||
} <= set(AGENT_TYPE_CATALOG.keys())
|
||||
|
||||
|
||||
@@ -28,13 +28,14 @@ def test_list_type_catalog_includes_agent_code_and_label():
|
||||
catalog = list_type_catalog()
|
||||
assert len(catalog) >= 5
|
||||
codes = {entry['agent_code'] for entry in catalog}
|
||||
assert {'generic', 'shipping_notice', 'file_check', 'in_transit_transfer', 'contract_payment'} <= codes
|
||||
assert {'generic', 'shipping_notice', 'file_check', 'in_transit_transfer', 'contract_payment', 'voucher_check'} <= codes
|
||||
labels = {entry['label'] for entry in catalog}
|
||||
assert '通用问答 / 知识库' in labels
|
||||
assert '在途仓自动调拨' in labels
|
||||
assert '货代发货通知' in labels
|
||||
assert '文件检核' in labels
|
||||
assert '合同付款预警' in labels
|
||||
assert '立账凭证' in labels
|
||||
|
||||
|
||||
def test_get_catalog_entry_returns_runtime_handler_for_hybrid_types():
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
"""
|
||||
backend/tests/test_agent_registry_voucher_check.py
|
||||
|
||||
用途:验证 voucher_check 已注册为 builtin Chat Agent 类型。
|
||||
依赖:catalog_loader / catalog_bindings / handlers.bootstrap。
|
||||
逻辑:只读断言 Catalog 声明与 Handler 绑定;不创建 Flask 应用。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app.services.agent_registry import catalog_bindings
|
||||
from app.services.agent_registry.catalog_loader import build_agent_type_catalog, get_catalog_entry
|
||||
from app.services.agent_registry.handlers.base import AgentHandler
|
||||
from app.services.agent_registry.handlers.bootstrap import load_handlers, reset_handlers_for_tests
|
||||
from app.services.agent_registry.handlers.registry import HANDLER_REGISTRY
|
||||
|
||||
|
||||
def test_voucher_check_type_in_catalog():
|
||||
"""catalog 必须包含 voucher_check,默认 builtin + task_chat。"""
|
||||
entry = get_catalog_entry('voucher_check')
|
||||
assert entry is not None
|
||||
assert entry.get('label') == '立账凭证'
|
||||
assert entry.get('runtime_provider_default') == 'builtin'
|
||||
assert entry.get('conversation_mode_default') == 'task_chat'
|
||||
assert entry.get('runtime_handler') == 'voucher_check'
|
||||
|
||||
|
||||
def test_voucher_check_bound_to_runtime_handler():
|
||||
"""catalog_bindings 必须把 voucher_check 绑定到同名 HANDLER_REGISTRY key。"""
|
||||
assert catalog_bindings.RUNTIME_HANDLER_BINDINGS['voucher_check'] == 'voucher_check'
|
||||
|
||||
|
||||
def test_voucher_check_handler_registered():
|
||||
"""bootstrap 加载后 HANDLER_REGISTRY 包含 VoucherCheckHandler。"""
|
||||
reset_handlers_for_tests()
|
||||
load_handlers()
|
||||
handler = HANDLER_REGISTRY.get('voucher_check')
|
||||
assert handler is not None
|
||||
assert isinstance(handler, AgentHandler)
|
||||
assert handler.handler_key == 'voucher_check'
|
||||
reset_handlers_for_tests()
|
||||
|
||||
|
||||
def test_voucher_check_present_in_full_catalog():
|
||||
"""完整 catalog 与既有类型共存。"""
|
||||
catalog = build_agent_type_catalog()
|
||||
assert 'voucher_check' in catalog
|
||||
assert 'contract_payment' in catalog
|
||||
@@ -0,0 +1,178 @@
|
||||
"""
|
||||
backend/tests/test_voucher_check_permissions.py
|
||||
|
||||
用途:验收立账凭证改为 Agent 授权后的 API 拦截。
|
||||
依赖:User、Agent、Flask test client、workspace voucher_check 蓝图。
|
||||
逻辑:授予 voucher_check Agent 后可查询任务列表,但不能触发强制重检、也不能改通知配置。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app import db
|
||||
from app.models import Agent, User
|
||||
|
||||
|
||||
def _create_user(*, username: str, role: str = 'user') -> User:
|
||||
"""创建落库用户,供 voucher-check API 走 legacy header 认证。"""
|
||||
user = User(
|
||||
username=username,
|
||||
email=f'{username}@example.com',
|
||||
role=role,
|
||||
status='active',
|
||||
real_name=f'{username}-实名',
|
||||
employee_id=f'{username.upper()}-EMP',
|
||||
department='财务部',
|
||||
)
|
||||
user.set_password('password123')
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
db.session.refresh(user)
|
||||
return user
|
||||
|
||||
|
||||
def _ensure_voucher_check_agent() -> Agent:
|
||||
"""确保存在启用中的立账凭证 Agent,供授权绑定。"""
|
||||
agent = Agent.query.filter_by(agent_code='voucher_check', is_active=True).first()
|
||||
if agent:
|
||||
return agent
|
||||
agent = Agent(
|
||||
name='立账凭证',
|
||||
agent_code='voucher_check',
|
||||
runtime_provider='builtin',
|
||||
conversation_mode='task_chat',
|
||||
type='api',
|
||||
is_active=True,
|
||||
)
|
||||
db.session.add(agent)
|
||||
db.session.commit()
|
||||
db.session.refresh(agent)
|
||||
return agent
|
||||
|
||||
|
||||
def _grant_voucher_check_agent(user: User) -> User:
|
||||
"""把立账凭证 Agent 授权给指定用户并重新取出,避免会话分离后丢失关系。"""
|
||||
agent = _ensure_voucher_check_agent()
|
||||
user_id = user.id
|
||||
if agent not in user.agents:
|
||||
user.agents.append(agent)
|
||||
db.session.commit()
|
||||
db.session.expunge(user)
|
||||
return db.session.get(User, user_id) or user
|
||||
|
||||
|
||||
def _json_error(response) -> dict:
|
||||
"""读取标准 envelope 的 error 对象。"""
|
||||
body = response.get_json()
|
||||
assert body.get('success') is False
|
||||
assert isinstance(body.get('error'), dict)
|
||||
return body['error']
|
||||
|
||||
|
||||
def test_voucher_check_api_forbids_user_without_agent(client, app):
|
||||
"""未授予 Agent 的普通用户查询任务或强制重检均返回 VOUCHER_CHECK.FORBIDDEN。"""
|
||||
with app.app_context():
|
||||
ordinary = _create_user(username='vc_api_denied')
|
||||
ordinary_id = ordinary.id
|
||||
_ensure_voucher_check_agent()
|
||||
db.session.expunge(ordinary)
|
||||
|
||||
headers = {
|
||||
'X-User-Id': str(ordinary_id),
|
||||
'X-Username': 'vc_api_denied',
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
list_res = client.get('/api/workspace/accounting-voucher-check/runs', headers=headers)
|
||||
# 关键断言:登录不足够,Agent 授权缺失必须 403。
|
||||
assert list_res.status_code == 403
|
||||
assert _json_error(list_res)['code'] == 'VOUCHER_CHECK.FORBIDDEN'
|
||||
|
||||
force_res = client.post(
|
||||
'/api/workspace/accounting-voucher-check/runs',
|
||||
headers=headers,
|
||||
json={'mode': 'force_all'},
|
||||
)
|
||||
assert force_res.status_code == 403
|
||||
assert _json_error(force_res)['code'] == 'VOUCHER_CHECK.FORBIDDEN'
|
||||
|
||||
|
||||
def test_voucher_check_api_allows_granted_user(client, app):
|
||||
"""授予 voucher_check Agent 后普通用户可以查询检核任务。"""
|
||||
with app.app_context():
|
||||
granted = _grant_voucher_check_agent(_create_user(username='vc_api_granted'))
|
||||
granted_id = granted.id
|
||||
|
||||
headers = {
|
||||
'X-User-Id': str(granted_id),
|
||||
'X-Username': 'vc_api_granted',
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
list_res = client.get('/api/workspace/accounting-voucher-check/runs', headers=headers)
|
||||
# 关键断言:Agent 授权后不再 403,返回标准成功 envelope。
|
||||
assert list_res.status_code == 200
|
||||
body = list_res.get_json()
|
||||
assert body.get('success') is True
|
||||
assert 'items' in (body.get('data') or {})
|
||||
|
||||
|
||||
def test_voucher_check_api_allows_admin_without_agent_binding(client, app):
|
||||
"""平台 admin 即使未绑定该 Agent 也可查询检核任务。"""
|
||||
with app.app_context():
|
||||
admin = _create_user(username='vc_api_admin', role='admin')
|
||||
admin_id = admin.id
|
||||
_ensure_voucher_check_agent()
|
||||
db.session.expunge(admin)
|
||||
|
||||
headers = {
|
||||
'X-User-Id': str(admin_id),
|
||||
'X-Username': 'vc_api_admin',
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
list_res = client.get('/api/workspace/accounting-voucher-check/runs', headers=headers)
|
||||
assert list_res.status_code == 200
|
||||
assert list_res.get_json().get('success') is True
|
||||
|
||||
|
||||
def test_voucher_check_force_requires_admin(client, app):
|
||||
"""已授权的普通用户可查任务,但不能触发强制重检。"""
|
||||
with app.app_context():
|
||||
granted = _grant_voucher_check_agent(_create_user(username='vc_api_jobs_denied'))
|
||||
granted_id = granted.id
|
||||
|
||||
headers = {
|
||||
'X-User-Id': str(granted_id),
|
||||
'X-Username': 'vc_api_jobs_denied',
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
# 关键断言:访问权不等于操作权,强制重检仅管理员。
|
||||
res = client.post(
|
||||
'/api/workspace/accounting-voucher-check/runs',
|
||||
headers=headers,
|
||||
json={'mode': 'force_all'},
|
||||
)
|
||||
assert res.status_code == 403
|
||||
assert _json_error(res)['code'] == 'VOUCHER_CHECK.ADMIN_REQUIRED'
|
||||
|
||||
|
||||
def test_voucher_check_notify_config_requires_admin(client, app):
|
||||
"""已授权的普通用户可查任务,但不能读取或保存按类型接收人。"""
|
||||
with app.app_context():
|
||||
granted = _grant_voucher_check_agent(_create_user(username='vc_api_notify_denied'))
|
||||
granted_id = granted.id
|
||||
|
||||
headers = {
|
||||
'X-User-Id': str(granted_id),
|
||||
'X-Username': 'vc_api_notify_denied',
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
get_res = client.get('/api/workspace/accounting-voucher-check/notify-config', headers=headers)
|
||||
# 关键断言:通知配置读写均仅管理员。
|
||||
assert get_res.status_code == 403
|
||||
assert _json_error(get_res)['code'] == 'VOUCHER_CHECK.ADMIN_REQUIRED'
|
||||
|
||||
put_res = client.put(
|
||||
'/api/workspace/accounting-voucher-check/notify-config',
|
||||
headers=headers,
|
||||
json={'profile_code': 'sales', 'recipients': {'po_order': [granted_id]}},
|
||||
)
|
||||
assert put_res.status_code == 403
|
||||
assert _json_error(put_res)['code'] == 'VOUCHER_CHECK.ADMIN_REQUIRED'
|
||||
@@ -0,0 +1,300 @@
|
||||
"""
|
||||
backend/tests/voucher_check/test_notify_config.py
|
||||
|
||||
用途:按文件类型配置缺件接收人,以及把缺件类型拆成每人一封。
|
||||
依赖:pytest、Flask client、notify_config_service、notification.send_batch_missing_summary。
|
||||
逻辑:GET/PUT 仅 admin;未知规则码丢弃;发送时每人只看到自己负责且本批缺失的类型。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
from types import SimpleNamespace
|
||||
|
||||
from tests.conftest import parse_api_data, parse_api_error
|
||||
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask, User
|
||||
from app.models.voucher_check import VoucherCheckNotification, VoucherCheckNotifyConfig, VoucherCheckRun
|
||||
from app.services.voucher_check.notify_config_service import (
|
||||
normalize_recipients_json,
|
||||
plan_missing_recipient_messages,
|
||||
)
|
||||
from app.services.voucher_check.notification import send_batch_missing_summary
|
||||
|
||||
|
||||
def _ensure_user(
|
||||
*,
|
||||
username: str,
|
||||
role: str = "user",
|
||||
wecom_userid: str | None = None,
|
||||
department: str | None = None,
|
||||
) -> User:
|
||||
"""
|
||||
测试用户幂等创建。
|
||||
|
||||
Args:
|
||||
username: 登录名。
|
||||
role: 角色,默认普通用户。
|
||||
wecom_userid: 企微 userid,有值即视为已绑定。
|
||||
department: 部门名,供候选人接口与选人弹窗分组。
|
||||
"""
|
||||
user = db.session.query(User).filter_by(username=username).first()
|
||||
if user:
|
||||
user.role = role
|
||||
user.status = "active"
|
||||
user.wecom_userid = wecom_userid
|
||||
if department is not None:
|
||||
user.department = department
|
||||
db.session.commit()
|
||||
return user
|
||||
user = User(
|
||||
username=username,
|
||||
email=f"{username}@example.com",
|
||||
role=role,
|
||||
status="active",
|
||||
real_name=username,
|
||||
wecom_userid=wecom_userid,
|
||||
department=department,
|
||||
)
|
||||
user.set_password("test")
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user
|
||||
|
||||
|
||||
def test_plan_merges_types_per_user_and_tracks_unmapped():
|
||||
"""同一人管多类时合并到一封;有缺件但无人接收的类型进入 unmapped。"""
|
||||
plan = plan_missing_recipient_messages(
|
||||
by_file_type={
|
||||
"po_order": {
|
||||
"rule_code": "po_order",
|
||||
"rule_name": "PO订单",
|
||||
"missing_file_count": 2,
|
||||
"missing_invoice_count": 1,
|
||||
},
|
||||
"tax_invoice": {
|
||||
"rule_code": "tax_invoice",
|
||||
"rule_name": "发票",
|
||||
"missing_file_count": 3,
|
||||
"missing_invoice_count": 2,
|
||||
},
|
||||
"bill_of_lading": {
|
||||
"rule_code": "bill_of_lading",
|
||||
"rule_name": "提单",
|
||||
"missing_file_count": 1,
|
||||
"missing_invoice_count": 1,
|
||||
},
|
||||
"customs_pack": {
|
||||
"rule_code": "customs_pack",
|
||||
"rule_name": "报关资料",
|
||||
"missing_file_count": 0,
|
||||
"missing_invoice_count": 0,
|
||||
},
|
||||
},
|
||||
recipients_by_rule={
|
||||
"po_order": [11, 12],
|
||||
"tax_invoice": [11],
|
||||
"bill_of_lading": [],
|
||||
},
|
||||
)
|
||||
# 关键断言:11 同时收到 PO 与发票;提单无人配置。
|
||||
assert plan["unmapped_codes"] == ["bill_of_lading"]
|
||||
assert [item["code"] for item in plan["user_rules"][11]] == ["po_order", "tax_invoice"]
|
||||
assert [item["code"] for item in plan["user_rules"][12]] == ["po_order"]
|
||||
|
||||
|
||||
def test_normalize_drops_unknown_rule_codes():
|
||||
"""提交里的未知规则码必须丢弃,不能写进配置表。"""
|
||||
cleaned = normalize_recipients_json(
|
||||
{"po_order": [1, 1, "2", 0, "x"], "unknown": [9]},
|
||||
profile_code="sales",
|
||||
)
|
||||
assert cleaned["po_order"] == [1, 2]
|
||||
assert "unknown" not in cleaned
|
||||
assert "tax_invoice" in cleaned
|
||||
|
||||
|
||||
def test_admin_can_get_and_save_notify_config(app, client, admin_headers):
|
||||
"""管理员可读取六行规则并保存接收人。"""
|
||||
with app.app_context():
|
||||
admin = _ensure_user(username="admin", role="admin")
|
||||
owner = _ensure_user(username="vc_po_owner", wecom_userid="wx-po", department="财务部")
|
||||
owner_id = owner.id
|
||||
admin_id = admin.id
|
||||
|
||||
listed = client.get(
|
||||
"/api/workspace/accounting-voucher-check/notify-config",
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert listed.status_code == 200
|
||||
payload = parse_api_data(listed.get_json())
|
||||
owner_row = next(row for row in payload["users"] if row["id"] == owner_id)
|
||||
# 关键断言:候选人带部门,供公共选人弹窗左侧分组。
|
||||
assert owner_row["department"] == "财务部"
|
||||
assert owner_row["wecom_bound"] is True
|
||||
codes = [row["code"] for row in payload["rules"]]
|
||||
# 关键断言:页面行来自销售 Profile 六类必检文件。
|
||||
assert codes == [
|
||||
"po_order",
|
||||
"customs_pack",
|
||||
"sales_release",
|
||||
"tax_invoice",
|
||||
"customs_declaration",
|
||||
"bill_of_lading",
|
||||
]
|
||||
|
||||
saved = client.put(
|
||||
"/api/workspace/accounting-voucher-check/notify-config",
|
||||
headers=admin_headers,
|
||||
json={
|
||||
"profile_code": "sales",
|
||||
"recipients": {"po_order": [owner_id], "unknown": [admin_id]},
|
||||
},
|
||||
)
|
||||
assert saved.status_code == 200
|
||||
saved_payload = parse_api_data(saved.get_json())
|
||||
po_row = next(row for row in saved_payload["rules"] if row["code"] == "po_order")
|
||||
assert po_row["user_ids"] == [owner_id]
|
||||
unknown_codes = [row["code"] for row in saved_payload["rules"] if row["code"] == "unknown"]
|
||||
assert unknown_codes == []
|
||||
|
||||
with app.app_context():
|
||||
row = db.session.query(VoucherCheckNotifyConfig).filter_by(profile_code="sales").one()
|
||||
assert row.recipients_json["po_order"] == [owner_id]
|
||||
assert "unknown" not in row.recipients_json
|
||||
|
||||
|
||||
def test_notify_config_rejects_non_admin_headers(app, client, user_headers):
|
||||
"""仅凭普通用户 header 不能改通知配置。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
|
||||
res = client.get("/api/workspace/accounting-voucher-check/notify-config", headers=user_headers)
|
||||
assert res.status_code == 403
|
||||
assert parse_api_error(res.get_json())["code"] in {
|
||||
"VOUCHER_CHECK.FORBIDDEN",
|
||||
"VOUCHER_CHECK.ADMIN_REQUIRED",
|
||||
}
|
||||
|
||||
|
||||
def test_send_batch_splits_one_card_per_user(app, monkeypatch):
|
||||
"""缺件通知按人拆信,正文只含该人负责的缺失类型。"""
|
||||
sent: list[tuple[str, str, str]] = []
|
||||
|
||||
def _fake_send(wecom_userid, title, description, jump_url):
|
||||
sent.append((wecom_userid, title, description))
|
||||
return {"errcode": 0}
|
||||
|
||||
monkeypatch.setattr("app.services.wecom_notify.send_wecom_textcard_message", _fake_send)
|
||||
monkeypatch.setattr(
|
||||
"app.services.wecom_notify._create_audit",
|
||||
lambda *args, **kwargs: SimpleNamespace(
|
||||
jump_url=None, status=None, error_code=None, error_message=None
|
||||
),
|
||||
)
|
||||
monkeypatch.setattr("app.services.wecom_notify._build_jump_url", lambda *args, **kwargs: "https://jump")
|
||||
|
||||
with app.app_context():
|
||||
app.config["WECOM_NOTIFY_ENABLED"] = True
|
||||
owner = _ensure_user(username="vc_notify_owner", role="admin", wecom_userid="wx-owner")
|
||||
po_user = _ensure_user(username="vc_po_recv", wecom_userid="wx-po-recv")
|
||||
bl_user = _ensure_user(username="vc_bl_recv", wecom_userid="wx-bl-recv")
|
||||
task = ScheduleTask(
|
||||
schedule_no="SCH-VC-NOTIFY-SPLIT",
|
||||
name="立账凭证检核",
|
||||
schedule_type="platform_job",
|
||||
platform_api_path="accounting-voucher-check",
|
||||
owner_user_id=owner.id,
|
||||
cron_expr="0 6 * * 2",
|
||||
notify_enabled=True,
|
||||
notify_user_ids_json=[],
|
||||
)
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
schedule_run = ScheduleRun(
|
||||
schedule_task_id=task.id,
|
||||
run_no="RUN-VC-NOTIFY-SPLIT",
|
||||
trigger_type="cron",
|
||||
status="success",
|
||||
)
|
||||
db.session.add(schedule_run)
|
||||
db.session.flush()
|
||||
engine_run = VoucherCheckRun(
|
||||
id=1,
|
||||
schedule_run_id=schedule_run.id,
|
||||
run_no="VC-NOTIFY-SPLIT",
|
||||
profile_code="sales",
|
||||
profile_version="1",
|
||||
profile_snapshot_json={"profile_code": "sales"},
|
||||
period_month=date(2026, 8, 1),
|
||||
mode="incremental",
|
||||
status="missing",
|
||||
subject_count=4,
|
||||
missing_file_count=3,
|
||||
missing_invoice_count=2,
|
||||
)
|
||||
db.session.add(engine_run)
|
||||
db.session.flush()
|
||||
notify_row = (
|
||||
db.session.query(VoucherCheckNotifyConfig).filter_by(profile_code="sales").one_or_none()
|
||||
)
|
||||
if notify_row is None:
|
||||
notify_row = VoucherCheckNotifyConfig(profile_code="sales")
|
||||
db.session.add(notify_row)
|
||||
notify_row.recipients_json = {
|
||||
"po_order": [po_user.id],
|
||||
"bill_of_lading": [bl_user.id],
|
||||
}
|
||||
db.session.commit()
|
||||
status = send_batch_missing_summary(
|
||||
task=task,
|
||||
schedule_run=schedule_run,
|
||||
engine_run_id=engine_run.id,
|
||||
profile_code="sales",
|
||||
period_month="2026-08",
|
||||
stats={
|
||||
"subject_count": 4,
|
||||
"missing_file_count": 3,
|
||||
"missing_invoice_count": 2,
|
||||
"by_file_type": {
|
||||
"po_order": {
|
||||
"rule_code": "po_order",
|
||||
"rule_name": "PO订单",
|
||||
"missing_file_count": 2,
|
||||
"missing_invoice_count": 1,
|
||||
},
|
||||
"bill_of_lading": {
|
||||
"rule_code": "bill_of_lading",
|
||||
"rule_name": "提单",
|
||||
"missing_file_count": 1,
|
||||
"missing_invoice_count": 1,
|
||||
},
|
||||
"tax_invoice": {
|
||||
"rule_code": "tax_invoice",
|
||||
"rule_name": "发票",
|
||||
"missing_file_count": 1,
|
||||
"missing_invoice_count": 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
excel_filename="voucher_check_sales_2026-08.xlsx",
|
||||
enabled=True,
|
||||
)
|
||||
assert status == "sent"
|
||||
audit = db.session.query(VoucherCheckNotification).filter_by(run_id=engine_run.id).one()
|
||||
# 关键断言:仍是一批一条审计;skip_reason 记录未配置类型。
|
||||
assert audit.send_status == "sent"
|
||||
assert audit.skip_reason == "部分文件类型未配置接收人"
|
||||
assert set(audit.recipient_user_ids_json or []) == {po_user.id, bl_user.id}
|
||||
assert "tax_invoice" in (audit.summary_json or {}).get("unmapped_rule_codes", [])
|
||||
|
||||
wecom_ids = {item[0] for item in sent}
|
||||
assert wecom_ids == {"wx-po-recv", "wx-bl-recv"}
|
||||
po_body = next(item[2] for item in sent if item[0] == "wx-po-recv")
|
||||
bl_body = next(item[2] for item in sent if item[0] == "wx-bl-recv")
|
||||
assert "PO订单" in po_body
|
||||
assert "提单" not in po_body
|
||||
assert "发票" not in po_body
|
||||
assert "提单" in bl_body
|
||||
assert "PO订单" not in bl_body
|
||||
@@ -104,10 +104,10 @@ def _add_engine_run(
|
||||
return engine
|
||||
|
||||
|
||||
def test_workspace_latest_run_returns_queued_force_all(app, client, user_headers):
|
||||
def test_workspace_latest_run_returns_queued_force_all(app, client, admin_headers):
|
||||
"""入队中的强制重检必须显示 queued,而不是建批前失败口径。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
_ensure_user(username="admin", role="admin")
|
||||
_add_schedule_run(
|
||||
run_no="RUN-LATEST-QUEUED",
|
||||
status="queued",
|
||||
@@ -117,7 +117,7 @@ def test_workspace_latest_run_returns_queued_force_all(app, client, user_headers
|
||||
|
||||
response = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs/latest",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = parse_api_data(response.get_json())
|
||||
@@ -129,10 +129,10 @@ def test_workspace_latest_run_returns_queued_force_all(app, client, user_headers
|
||||
assert payload["current_step"]["step"] == "queued"
|
||||
|
||||
|
||||
def test_workspace_latest_run_returns_completed_metrics(app, client, user_headers):
|
||||
def test_workspace_latest_run_returns_completed_metrics(app, client, admin_headers):
|
||||
"""终态缺件批次返回四指标与 100% 进度。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
_ensure_user(username="admin", role="admin")
|
||||
sched = _add_schedule_run(
|
||||
run_no="RUN-LATEST-DONE",
|
||||
status="success",
|
||||
@@ -149,7 +149,7 @@ def test_workspace_latest_run_returns_completed_metrics(app, client, user_header
|
||||
|
||||
response = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs/latest",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = parse_api_data(response.get_json())
|
||||
@@ -165,10 +165,10 @@ def test_workspace_latest_run_returns_completed_metrics(app, client, user_header
|
||||
assert payload["missing_invoice_count"] == 6
|
||||
|
||||
|
||||
def test_workspace_latest_run_running_progress_ignores_list_period(app, client, user_headers):
|
||||
def test_workspace_latest_run_running_progress_ignores_list_period(app, client, admin_headers):
|
||||
"""执行中进度跟 runner 步骤走,且 latest 不吃列表 period。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
_ensure_user(username="admin", role="admin")
|
||||
old = _add_schedule_run(
|
||||
run_no="RUN-LATEST-OLD-AUG",
|
||||
status="success",
|
||||
@@ -200,7 +200,7 @@ def test_workspace_latest_run_running_progress_ignores_list_period(app, client,
|
||||
|
||||
latest = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs/latest",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert latest.status_code == 200
|
||||
payload = parse_api_data(latest.get_json())
|
||||
@@ -213,7 +213,7 @@ def test_workspace_latest_run_running_progress_ignores_list_period(app, client,
|
||||
|
||||
filtered = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs?period=2026-08",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert filtered.status_code == 200
|
||||
nos = {item["run_no"] for item in parse_api_data(filtered.get_json())["items"]}
|
||||
@@ -222,10 +222,10 @@ def test_workspace_latest_run_running_progress_ignores_list_period(app, client,
|
||||
assert payload["run_no"] == "RUN-LATEST-RUNNING"
|
||||
|
||||
|
||||
def test_workspace_latest_run_pre_engine_failure(app, client, user_headers):
|
||||
def test_workspace_latest_run_pre_engine_failure(app, client, admin_headers):
|
||||
"""建批前失败仍作为最近一次检核,状态为技术失败。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
_ensure_user(username="admin", role="admin")
|
||||
_add_schedule_run(
|
||||
run_no="RUN-LATEST-FAILED",
|
||||
status="failed",
|
||||
@@ -242,7 +242,7 @@ def test_workspace_latest_run_pre_engine_failure(app, client, user_headers):
|
||||
|
||||
response = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs/latest",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = parse_api_data(response.get_json())
|
||||
|
||||
@@ -42,7 +42,7 @@ def _ensure_user(*, username: str, role: str, real_name: str | None = None) -> U
|
||||
return user
|
||||
|
||||
|
||||
def test_workspace_lists_wecom_notifications(app, client, user_headers):
|
||||
def test_workspace_lists_wecom_notifications(app, client, admin_headers):
|
||||
"""企微发送记录列表应带出批次统计、接收人姓名,并按发送状态筛选。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="admin", role="admin")
|
||||
@@ -116,7 +116,7 @@ def test_workspace_lists_wecom_notifications(app, client, user_headers):
|
||||
|
||||
listed = client.get(
|
||||
"/api/workspace/accounting-voucher-check/notifications",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert listed.status_code == 200
|
||||
payload = parse_api_data(listed.get_json())
|
||||
@@ -132,7 +132,7 @@ def test_workspace_lists_wecom_notifications(app, client, user_headers):
|
||||
|
||||
filtered = client.get(
|
||||
"/api/workspace/accounting-voucher-check/notifications?period=2026-07&send_status=skipped_no_missing",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert filtered.status_code == 200
|
||||
filtered_payload = parse_api_data(filtered.get_json())
|
||||
|
||||
@@ -74,10 +74,10 @@ def test_parse_period_filter_year_and_month():
|
||||
assert exc.value.code == "VOUCHER_CHECK.CONFIG_INVALID"
|
||||
|
||||
|
||||
def test_workspace_lists_runs_by_year_or_month(app, client, user_headers):
|
||||
def test_workspace_lists_runs_by_year_or_month(app, client, admin_headers):
|
||||
"""任务列表 year 筛出同年多月,month 仍只返回单月。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
_ensure_user(username="admin", role="admin")
|
||||
db.session.add(_engine_run(run_no="VC-202608-Y", period_month=date(2026, 8, 1)))
|
||||
db.session.add(_engine_run(run_no="VC-202607-Y", period_month=date(2026, 7, 1)))
|
||||
db.session.add(_engine_run(run_no="VC-202512-Y", period_month=date(2025, 12, 1)))
|
||||
@@ -85,7 +85,7 @@ def test_workspace_lists_runs_by_year_or_month(app, client, user_headers):
|
||||
|
||||
year_resp = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs?period=2026",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert year_resp.status_code == 200
|
||||
year_payload = parse_api_data(year_resp.get_json())
|
||||
@@ -97,7 +97,7 @@ def test_workspace_lists_runs_by_year_or_month(app, client, user_headers):
|
||||
|
||||
month_resp = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs?period=2026-08",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert month_resp.status_code == 200
|
||||
month_payload = parse_api_data(month_resp.get_json())
|
||||
@@ -137,10 +137,10 @@ def test_workspace_year_filter_includes_pre_engine_failure(app, client, admin_he
|
||||
assert "RUN-PREENGINE-YEAR" in nos
|
||||
|
||||
|
||||
def test_workspace_lists_notifications_by_year(app, client, user_headers):
|
||||
def test_workspace_lists_notifications_by_year(app, client, admin_headers):
|
||||
"""企微发送记录按年筛选应覆盖该年各月。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
_ensure_user(username="admin", role="admin")
|
||||
august = _engine_run(run_no="VC-202608-N", period_month=date(2026, 8, 1))
|
||||
july = _engine_run(run_no="VC-202607-N", period_month=date(2026, 7, 1), status="completed")
|
||||
db.session.add_all([august, july])
|
||||
@@ -163,7 +163,7 @@ def test_workspace_lists_notifications_by_year(app, client, user_headers):
|
||||
|
||||
response = client.get(
|
||||
"/api/workspace/accounting-voucher-check/notifications?period=2026",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = parse_api_data(response.get_json())
|
||||
@@ -172,13 +172,13 @@ def test_workspace_lists_notifications_by_year(app, client, user_headers):
|
||||
assert "VC-202607-N" in nos
|
||||
|
||||
|
||||
def test_workspace_rejects_invalid_period(app, client, user_headers):
|
||||
def test_workspace_rejects_invalid_period(app, client, admin_headers):
|
||||
"""非法 period 返回配置错误,而不是空列表。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
_ensure_user(username="admin", role="admin")
|
||||
response = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs?period=2026-13",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert response.status_code == 400
|
||||
error = parse_api_error(response.get_json())
|
||||
|
||||
Reference in new issue
Block a user