Files
syops/backend/tests/test_shipping_chat_tool_capability.py
T
hugo 1f3ecb87b7 feat(shipping_notice): 新增货代订单全流程业务模块及配套基建
本次提交新增货代订单处理全流程的完整业务能力、数据库迁移与测试用例:
1.  统一追加式审计事件体系,支持全链路不可篡改的业务追溯
2.  货代任务撤销、版本替代管控逻辑,规范已上传订单的合规版本更替
3.  订单抽取路由审计、模板自动冻结资格、路由计数等核心业务基建
4.  EML订单补充表解析契约与合并逻辑,完善订单补全能力
5.  Chatflow只读能力令牌校验机制,保障工具调用安全
6.  人工复核证据校验服务,规范人工审核的证据留存
所有变更均配套完整的单元测试与迁移契约测试,支持受控回退
2026-07-28 08:56:58 +08:00

39 lines
2.0 KiB
Python

"""阶段九 Chatflow 只读能力令牌与唯一应用约束。"""
from __future__ import annotations
import pytest
from sqlalchemy.exc import IntegrityError
from app import db
from app.core.errors import ForbiddenError
from app.models import Agent, ChatSession, ShippingTask, User
from app.services.shipping_notice.chat_tool_capability import issue_chat_capability, require_chat_capability
def test_chat_capability_rejects_cross_task_and_expired_scope(app):
"""关键断言:令牌不能读取他人任务,且 scope/对象必须与本轮上下文完全一致。"""
with app.app_context():
user = User(username='cap-user', email='cap-user@example.test', password_hash='x')
agent = Agent(name='cap-agent', agent_code='shipping_notice', is_active=True)
db.session.add_all([user, agent]); db.session.flush()
session = ChatSession(session_id='cap-session', user_id=user.id, agent_id=agent.id)
own = ShippingTask(task_no='SN-CAP-OWN', user_id=user.id, source_type='test')
other = ShippingTask(task_no='SN-CAP-OTHER', source_type='test')
db.session.add_all([session, own, other]); db.session.commit()
token = issue_chat_capability(user_id=user.id, session_id=session.session_id, agent_id=agent.id, task_no=own.task_no, batch_no=None)
assert require_chat_capability(token=token, scope='get_shipping_status', task_no=own.task_no)['uid'] == user.id
with pytest.raises(ForbiddenError):
require_chat_capability(token=token, scope='get_shipping_status', task_no=other.task_no)
def test_second_active_shipping_chatflow_is_rejected(app):
"""关键断言:数据库部分唯一索引阻止第二个启用的 shipping_notice Chatflow。"""
with app.app_context():
db.session.add(Agent(name='shipping-one', agent_code='shipping_notice', is_active=True))
db.session.commit()
db.session.add(Agent(name='shipping-two', agent_code='shipping_notice', is_active=True))
with pytest.raises(IntegrityError):
db.session.commit()
db.session.rollback()