Files
syops/backend/tests/test_freight_rpa_outbound_notifications.py
hugo 911afa0f73 feat(freight-rpa): 增加出站报表配置与回调支持,重构相关服务
- 将 RPA 管理 API 扩展至出站报表配置,新增出站相关的回调接口。
- 重构 freight_rpa_report_config_service,支持入站与出站报表配置的管理。
- 更新 ORM 模型,增加出站报表相关的持久化模型。
- 统一命名规范,确保在手、入站与出站的概念一致性。
2026-10-19 18:45:16 +08:00

104 lines
4.4 KiB
Python

"""
backend/tests/test_freight_rpa_outbound_notifications.py
用途:锁定出站排程终态企微通知必须附带 raw outbound ZIP(与入站对称)。
依赖:scheduler_service._notify_schedule_run_wecom、outbound archive 下载名、企微 notify 门禁。
逻辑:直接调用通用排程出口;断言 textcard 文案、ZIP 上传/发送与 outbound_raw_zip 审计事件。
"""
from __future__ import annotations
def _patch_unified_wecom(monkeypatch):
"""拦截通用排程出口的企微调用,返回可断言的捕获列表。"""
textcards: list[tuple[str, str]] = []
media_uploads: list[str] = []
file_sends: list[str] = []
notify_mod = 'app.services.integrations.wecom.notify'
def _fake_send_textcard(_userid, _title, description, _jump_url):
textcards.append((_title, description))
return {'errcode': 0}
monkeypatch.setattr(f'{notify_mod}.send_wecom_textcard_message', _fake_send_textcard)
monkeypatch.setattr(f'{notify_mod}._get_enabled_events', lambda: {'schedule_completed'})
monkeypatch.setattr(
f'{notify_mod}.upload_wecom_temp_media',
lambda _data, filename: media_uploads.append(filename) or f'media-{len(media_uploads)}',
)
monkeypatch.setattr(
f'{notify_mod}.send_wecom_file_message',
lambda _userid, media_id: file_sends.append(media_id) or {'errcode': 0},
)
return textcards, media_uploads, file_sends
def test_notify_schedule_run_wecom_attaches_outbound_raw_zip(app, monkeypatch, tmp_path):
"""出站 run 企微完成通知须上传并发送 raw outbound ZIP,审计事件为 schedule_completed_outbound_raw_zip。"""
from app import db
from app.models import ScheduleRun, ScheduleTask, User, WecomNotifyAudit
from app.services.schedule.freight_rpa_outbound_archive import outbound_raw_zip_download_name
from app.services.schedule.scheduler_service import _notify_schedule_run_wecom
archive_dir = tmp_path / 'outbound-archives'
archive_dir.mkdir()
zip_path = archive_dir / 'RUNOUTWECOM1.zip'
zip_path.write_bytes(b'PK\x03\x04outbound-notify')
with app.app_context():
app.config['FREIGHT_RPA_OUTBOUND_ARCHIVE_DIR'] = str(archive_dir)
app.config['WECOM_NOTIFY_ENABLED'] = True
app.config['WECOM_PUBLIC_BASE_URL'] = 'https://hxops.example.com'
recipient = User(
username='outbound-zip-notify',
email='outbound-zip-notify@example.com',
role='user',
status='active',
wecom_userid='outbound-zip-user',
)
db.session.add(recipient)
db.session.flush()
task = ScheduleTask(
schedule_no='SCH-OUT-WECOM-ZIP',
name='Outbound wecom zip',
schedule_type='platform_job',
platform_api_path='freight-rpa-outbound',
owner_user_id=recipient.id,
cron_expr='0 8 * * *',
notify_enabled=True,
notify_user_ids_json=[recipient.id],
)
db.session.add(task)
db.session.flush()
run = ScheduleRun(
run_no='RUNOUTWECOM1',
schedule_task_id=task.id,
status='success',
excel_file_data=b'excel-bytes',
excel_file_name='出站RPA-20261019-1546-结果.xlsx',
response_summary_json={
'raw_outbound_zip_available': True,
'raw_outbound_zip_path': str(zip_path.resolve()),
},
)
db.session.add(run)
db.session.commit()
expected_zip_name = outbound_raw_zip_download_name(run)
textcards, media_uploads, file_sends = _patch_unified_wecom(monkeypatch)
_notify_schedule_run_wecom(task, run)
# [断言意图] 与入站对称:textcard 提示包已随通知发送,并上传 Excel + 出站 ZIP。
assert len(textcards) == 1
assert '原始出站包:已随本通知发送' in textcards[0][1]
assert media_uploads[0] == run.excel_file_name
assert media_uploads[1] == expected_zip_name
assert file_sends == ['media-1', 'media-2']
assert run.wecom_notified is True
zip_audits = WecomNotifyAudit.query.filter_by(
event_type='schedule_completed_outbound_raw_zip',
).all()
assert len(zip_audits) == 1
assert zip_audits[0].content == f'[原始出站包] {expected_zip_name}'
assert zip_audits[0].status == 'sent'