feat: 企业微信提醒增加“AI智能体”提示
This commit is contained in:
1 parent
f269df7941
commit
26d213c05b
8 files changed
+289
-15
No files matched your search
@@ -3,13 +3,15 @@ backend/tests/test_voucher_check_permissions.py
|
||||
|
||||
用途:验收立账凭证改为 Agent 授权后的 API 拦截。
|
||||
依赖:User、Agent、Flask test client、workspace voucher_check 蓝图。
|
||||
逻辑:授予 voucher_check Agent 后可查询任务列表,但不能触发强制重检、也不能改通知配置。
|
||||
逻辑:授予 voucher_check Agent 后可查询任务列表并下载企微记录 Excel,但不能触发强制重检、也不能改通知配置。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
from app import db
|
||||
from app.models import Agent, User
|
||||
from app.models import Agent, ScheduleRun, ScheduleTask, User, VoucherCheckRun
|
||||
|
||||
|
||||
def _create_user(*, username: str, role: str = 'user') -> User:
|
||||
@@ -176,3 +178,63 @@ def test_voucher_check_notify_config_requires_admin(client, app):
|
||||
)
|
||||
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'
|
||||
@@ -0,0 +1,125 @@
|
||||
"""
|
||||
tests/test_wecom_textcard_source.py
|
||||
|
||||
用途:断言企微 textcard 标题带来源,以及账号类通知不加来源。
|
||||
依赖:app fixture、integrations.wecom.notify。
|
||||
逻辑:直接调用标题函数与发送出口,不经过具体业务模块。
|
||||
"""
|
||||
from app import db
|
||||
from app.models import User
|
||||
from app.services.integrations.wecom.notify import (
|
||||
_with_agent_source_title,
|
||||
send_wecom_event_message,
|
||||
send_wecom_textcard_message,
|
||||
)
|
||||
|
||||
|
||||
def test_agent_source_prefix_stays_when_title_is_truncated():
|
||||
"""超长业务标题截断后,来源前缀仍完整留在最前面。"""
|
||||
title = _with_agent_source_title('缺' * 200)
|
||||
# [断言意图] 前缀不被 120 字上限切掉,业务标题从末尾被截断。
|
||||
assert title.startswith('AI智能体 · ')
|
||||
assert len(title) == 120
|
||||
assert title == f"AI智能体 · {'缺' * 112}"
|
||||
|
||||
|
||||
def test_agent_source_prefix_is_not_repeated():
|
||||
"""已经带来源的标题不再加第二次。"""
|
||||
once = _with_agent_source_title('精益管理培训逾期提醒')
|
||||
twice = _with_agent_source_title(once)
|
||||
# [断言意图] 重复经过发送出口时标题保持单层来源。
|
||||
assert once == 'AI智能体 · 精益管理培训逾期提醒'
|
||||
assert twice == once
|
||||
|
||||
|
||||
def test_textcard_payload_uses_agent_source_title(app, monkeypatch):
|
||||
"""实际发给企业微信的 textcard.title 带来源,调用方传入的仍是业务标题。"""
|
||||
captured = {}
|
||||
|
||||
def _fake_post(_url, params=None, json=None, timeout=None):
|
||||
captured['json'] = json
|
||||
|
||||
class _Resp:
|
||||
def json(self):
|
||||
return {'errcode': 0}
|
||||
|
||||
return _Resp()
|
||||
|
||||
monkeypatch.setattr(
|
||||
'app.services.integrations.wecom.notify._get_access_token',
|
||||
lambda: 'token',
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
'app.services.integrations.wecom.notify.requests.post',
|
||||
_fake_post,
|
||||
)
|
||||
with app.app_context():
|
||||
app.config['WECOM_AGENT_ID'] = '1000002'
|
||||
send_wecom_textcard_message(
|
||||
'wx-user',
|
||||
'销售开票附件缺件提醒',
|
||||
'正文',
|
||||
'https://example.com/detail',
|
||||
)
|
||||
# [断言意图] 企微载荷标题带来源,正文和按钮保持原样。
|
||||
assert captured['json']['textcard']['title'] == 'AI智能体 · 销售开票附件缺件提醒'
|
||||
assert captured['json']['textcard']['description'] == '正文'
|
||||
assert captured['json']['textcard']['btntxt'] == '查看详情'
|
||||
|
||||
|
||||
def test_account_events_keep_business_title(app, monkeypatch):
|
||||
"""密码重置和权限变更不加来源;业务提醒仍然加上。"""
|
||||
captured = {}
|
||||
|
||||
def _fake_send(_userid, title, _description, _jump_url, *, with_agent_source=True):
|
||||
captured['title'] = title
|
||||
captured['with_agent_source'] = with_agent_source
|
||||
return {'errcode': 0}
|
||||
|
||||
monkeypatch.setattr(
|
||||
'app.services.integrations.wecom.notify.send_wecom_textcard_message',
|
||||
_fake_send,
|
||||
)
|
||||
with app.app_context():
|
||||
app.config['WECOM_NOTIFY_ENABLED'] = True
|
||||
app.config['WECOM_NOTIFY_EVENTS'] = (
|
||||
'password_reset,permission_request_approved,lean_training_overdue'
|
||||
)
|
||||
user = User(
|
||||
username='source-user',
|
||||
email='source@example.com',
|
||||
role='user',
|
||||
wecom_userid='wx-source',
|
||||
)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
send_wecom_event_message(
|
||||
event_type='password_reset',
|
||||
user=user,
|
||||
title='和信AI智能工作台 密码重置',
|
||||
content='请重置密码',
|
||||
jump_url='https://example.com/reset',
|
||||
)
|
||||
# [断言意图] 密码重置不是智能体推送,发送出口关闭来源前缀。
|
||||
assert captured['with_agent_source'] is False
|
||||
assert captured['title'] == '和信AI智能工作台 密码重置'
|
||||
|
||||
send_wecom_event_message(
|
||||
event_type='permission_request_approved',
|
||||
user=user,
|
||||
title='权限申请已通过',
|
||||
content='已通过',
|
||||
)
|
||||
# [断言意图] 权限变更与密码重置同一排除名单。
|
||||
assert captured['with_agent_source'] is False
|
||||
|
||||
send_wecom_event_message(
|
||||
event_type='lean_training_overdue',
|
||||
user=user,
|
||||
title='精益管理培训逾期提醒',
|
||||
content='请完成学习',
|
||||
)
|
||||
# [断言意图] 业务提醒仍把来源交给 textcard 出口添加。
|
||||
assert captured['with_agent_source'] is True
|
||||
assert captured['title'] == '精益管理培训逾期提醒'
|
||||
@@ -294,7 +294,7 @@ def test_send_batch_splits_one_card_per_user(app, monkeypatch):
|
||||
po_title, po_body = next((item[1], 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_title == "销售开票附件缺件"
|
||||
assert po_title == "销售开票附件缺件提醒"
|
||||
assert "2026年8月" in po_body
|
||||
assert "缺 2 份,涉及 1 张发票" in po_body
|
||||
assert "业务方" not in po_body
|
||||
|
||||
Reference in new issue
Block a user