241 lines
8.9 KiB
Python
241 lines
8.9 KiB
Python
"""
|
|
backend/tests/test_voucher_check_permissions.py
|
|
|
|
用途:验收立账凭证改为 Agent 授权后的 API 拦截。
|
|
依赖:User、Agent、Flask test client、workspace voucher_check 蓝图。
|
|
逻辑:授予 voucher_check Agent 后可查询任务列表并下载企微记录 Excel,但不能触发强制重检、也不能改通知配置。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import date
|
|
|
|
from app import db
|
|
from app.models import Agent, ScheduleRun, ScheduleTask, User, VoucherCheckRun
|
|
|
|
|
|
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'
|
|
|
|
|
|
def test_voucher_check_excel_download_uses_agent_access_not_schedule_notify(client, app):
|
|
"""企微发送记录的 Excel 按立账凭证访问权下载,不要求是排程负责人或通知人。"""
|
|
payload = b'voucher-excel'
|
|
with app.app_context():
|
|
owner = _create_user(username='vc_excel_owner')
|
|
granted = _grant_voucher_check_agent(_create_user(username='vc_excel_viewer'))
|
|
denied = _create_user(username='vc_excel_denied')
|
|
task = ScheduleTask(
|
|
schedule_no='VC-EXCEL-1',
|
|
name='立账凭证周检',
|
|
owner_user_id=owner.id,
|
|
cron_expr='0 6 * * 2',
|
|
notify_user_ids_json=[],
|
|
)
|
|
db.session.add(task)
|
|
db.session.flush()
|
|
schedule_run = ScheduleRun(
|
|
run_no='RUN-VC-EXCEL-1',
|
|
schedule_task_id=task.id,
|
|
status='success',
|
|
excel_file_data=payload,
|
|
excel_file_name='voucher_check.xlsx',
|
|
excel_file_size=len(payload),
|
|
)
|
|
db.session.add(schedule_run)
|
|
db.session.flush()
|
|
db.session.add(VoucherCheckRun(
|
|
id=88001,
|
|
schedule_run_id=schedule_run.id,
|
|
run_no='RUN-VC-EXCEL-1',
|
|
profile_code='sales',
|
|
profile_version='1',
|
|
profile_snapshot_json={},
|
|
period_month=date(2026, 9, 1),
|
|
mode='incremental',
|
|
status='success',
|
|
))
|
|
db.session.commit()
|
|
schedule_run_id = schedule_run.id
|
|
granted_id = granted.id
|
|
denied_id = denied.id
|
|
|
|
granted_headers = {'X-User-Id': str(granted_id), 'X-Username': 'vc_excel_viewer'}
|
|
url = f'/api/workspace/accounting-voucher-check/schedule-runs/{schedule_run_id}/excel'
|
|
ok_res = client.get(url, headers=granted_headers)
|
|
# 关键断言:不是排程通知人时,立账凭证授权用户仍能拿到 Excel 正文。
|
|
assert ok_res.status_code == 200
|
|
assert ok_res.data == payload
|
|
|
|
denied_headers = {'X-User-Id': str(denied_id), 'X-Username': 'vc_excel_denied'}
|
|
denied_res = client.get(url, headers=denied_headers)
|
|
assert denied_res.status_code == 403
|
|
assert _json_error(denied_res)['code'] == 'VOUCHER_CHECK.FORBIDDEN'
|
|
|
|
legacy = client.get(f'/api/workspace/schedule-runs/{schedule_run_id}/excel', headers=granted_headers)
|
|
# 关键断言:旧排程下载口仍只认负责人/通知人,企微记录不能再走这条。
|
|
assert legacy.status_code == 403
|
|
assert _json_error(legacy)['code'] == 'AUTH.FORBIDDEN'
|