39 lines
2.1 KiB
Python
39 lines
2.1 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, ShippingNotice, 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 = ShippingNotice(task_no='SN-CAP-OWN', user_id=user.id, source_type='test')
|
|
other = ShippingNotice(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()
|