Files
shanxia2.0/backend/tests/test_wechat_extra_delivery.py
T

73 lines
2.7 KiB
Python

"""新增两模板从真实业务快照到发送器的集成测试;微信传输使用假客户端。"""
import json
from datetime import timedelta
import pytest
from test_wechat_cert_notices import cert_notices
from test_wechat_completion_notices import db as completion_db, seed, sync, NOW
class Client:
def __init__(self):
self.calls = []
def send(self, **kwargs):
self.calls.append(kwargs)
return 'wx-message'
def binding(db, user_id, tenant_no='t1'):
from app.core.config import settings
from app.models.wechat_notice import WechatBinding
WechatBinding.__table__.create(db.connection())
db.add(WechatBinding(id='binding', user_id=user_id, tenant_no=tenant_no,
appid=settings.WECHAT_OA_APPID, subscribed=1, openid='oa-openid'))
db.commit()
def test_daily_worker_refreshes_real_counts_and_send_time(cert_notices, monkeypatch):
from app.services.wechat_approval_notices import deliver_pending
svc, db, now, add_cert, Notice = cert_notices
add_cert('old')
binding(db, 't1-a')
assert svc.enqueue_daily_cert_notices(db, now=now) == 1
db.commit()
add_cert('new', days=-1)
db.commit()
send_time = now + timedelta(minutes=5)
monkeypatch.setattr(svc, '_local_time', lambda value: send_time)
client = Client()
assert deliver_pending(db, client=client) == 1
assert client.calls[0]['data'] == {
'thing5': {'value': '证件到期1项,已到期1项'},
'thing3': {'value': '系统默认发出'},
'time6': {'value': '2026-10-26 10:05:00'},
}
assert db.query(Notice).one().status == 'sent'
assert not client.calls[0].get('url')
assert deliver_pending(db, client=client) == 0
@pytest.mark.parametrize('kind', ['bx_advance', 'bx_reimburse', 'pl_freight', 'pl_recharge'])
def test_completion_delivered_once_without_approval_cycle(completion_db, kind):
from app.services.wechat_approval_notices import deliver_pending
from app.models.wechat_notice import WechatNotice
db = completion_db
seed(db, kind)
kwargs = {'event_id': 'record', 'occurred_at': NOW} if kind.startswith('pl_') else {}
row = sync(db, kind, **kwargs)
assert row is not None
recipient = row.recipient_id
expected = json.loads(row.payload)
expected_page = row.page
binding(db, recipient)
client = Client()
assert deliver_pending(db, client=client) == 1
assert client.calls[0]['data'] == expected
assert client.calls[0]['page'] == expected_page
assert bool(expected_page) == (kind != 'pl_freight')
assert 'amount' not in json.dumps(expected)
assert db.query(WechatNotice).one().status == 'sent'
assert deliver_pending(db, client=client) == 0