141 lines
5.1 KiB
Python
141 lines
5.1 KiB
Python
"""
|
|
backend/tests/test_contract_payment_permissions.py
|
|
|
|
用途:验收合同付款预警改为 Agent 授权后的 API 拦截。
|
|
依赖:User、Agent、Flask test client、contract_payment 蓝图。
|
|
逻辑:admin 无需绑定即可查询;普通用户默认 403;授予 contract_payment Agent 后可查询台账,
|
|
但不能触发同步/扫描/发送。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from app import db
|
|
from app.models import Agent, User
|
|
|
|
|
|
def _create_user(*, username: str, role: str = 'user') -> User:
|
|
"""创建落库用户,供 contract-payment 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_contract_payment_agent() -> Agent:
|
|
"""确保存在启用中的合同付款预警 Agent,供授权绑定。"""
|
|
agent = Agent.query.filter_by(agent_code='contract_payment', is_active=True).first()
|
|
if agent:
|
|
return agent
|
|
agent = Agent(
|
|
name='合同付款预警',
|
|
agent_code='contract_payment',
|
|
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_contract_payment_agent(user: User) -> User:
|
|
"""把合同付款预警 Agent 授权给指定用户并重新取出,避免会话分离后丢失关系。"""
|
|
agent = _ensure_contract_payment_agent()
|
|
if agent not in user.agents:
|
|
user.agents.append(agent)
|
|
db.session.commit()
|
|
db.session.refresh(user)
|
|
db.session.expunge(user)
|
|
return user
|
|
|
|
|
|
def _auth_headers(user: User) -> dict[str, str]:
|
|
"""生成测试鉴权头,显式携带 user_id 以稳定命中 legacy 用户解析。"""
|
|
return {
|
|
'X-User-Id': str(user.id),
|
|
'X-Username': user.username,
|
|
'Content-Type': 'application/json',
|
|
}
|
|
|
|
|
|
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_contract_payment_api_forbids_user_without_agent(client, app):
|
|
"""未授予 Agent 的普通用户查询台账或触发同步均返回 CONTRACT_PAYMENT.FORBIDDEN。"""
|
|
with app.app_context():
|
|
ordinary = _create_user(username='cp_api_denied')
|
|
_ensure_contract_payment_agent()
|
|
db.session.expunge(ordinary)
|
|
|
|
headers = _auth_headers(ordinary)
|
|
list_res = client.get('/api/contract-payment/contracts', headers=headers)
|
|
# 关键断言:登录不足够,Agent 授权缺失必须 403。
|
|
assert list_res.status_code == 403
|
|
assert _json_error(list_res)['code'] == 'CONTRACT_PAYMENT.FORBIDDEN'
|
|
|
|
sync_res = client.post('/api/contract-payment/sync', headers=headers, json={})
|
|
assert sync_res.status_code == 403
|
|
assert _json_error(sync_res)['code'] == 'CONTRACT_PAYMENT.FORBIDDEN'
|
|
|
|
|
|
def test_contract_payment_api_allows_granted_user(client, app):
|
|
"""授予 contract_payment Agent 后普通用户可以查询台账。"""
|
|
with app.app_context():
|
|
granted = _grant_contract_payment_agent(_create_user(username='cp_api_granted'))
|
|
|
|
headers = _auth_headers(granted)
|
|
list_res = client.get('/api/contract-payment/contracts', 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_contract_payment_api_allows_admin_without_agent_binding(client, app):
|
|
"""平台 admin 即使未绑定该 Agent 也可查询台账。"""
|
|
with app.app_context():
|
|
admin = _create_user(username='cp_api_admin', role='admin')
|
|
_ensure_contract_payment_agent()
|
|
db.session.expunge(admin)
|
|
|
|
headers = _auth_headers(admin)
|
|
list_res = client.get('/api/contract-payment/contracts', headers=headers)
|
|
assert list_res.status_code == 200
|
|
assert list_res.get_json().get('success') is True
|
|
|
|
|
|
def test_contract_payment_jobs_require_admin(client, app):
|
|
"""已授权的普通用户可查台账,但不能触发同步、扫描或周汇总发送。"""
|
|
with app.app_context():
|
|
granted = _grant_contract_payment_agent(_create_user(username='cp_api_jobs_denied'))
|
|
|
|
headers = _auth_headers(granted)
|
|
# 关键断言:访问权不等于操作权,手动任务仅管理员。
|
|
for path in (
|
|
'/api/contract-payment/sync',
|
|
'/api/contract-payment/alerts/run',
|
|
'/api/contract-payment/alerts/weekly-run',
|
|
):
|
|
res = client.post(path, headers=headers, json={})
|
|
assert res.status_code == 403, path
|
|
assert _json_error(res)['code'] == 'CONTRACT_PAYMENT.ADMIN_REQUIRED'
|