test_wechat_extra_delivery.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """新增两模板从真实业务快照到发送器的集成测试;微信传输使用假客户端。"""
  2. import json
  3. from datetime import timedelta
  4. import pytest
  5. from test_wechat_cert_notices import cert_notices
  6. from test_wechat_completion_notices import db as completion_db, seed, sync, NOW
  7. class Client:
  8. def __init__(self):
  9. self.calls = []
  10. def send(self, **kwargs):
  11. self.calls.append(kwargs)
  12. return 'wx-message'
  13. def binding(db, user_id, tenant_no='t1'):
  14. from app.core.config import settings
  15. from app.models.wechat_notice import WechatBinding
  16. WechatBinding.__table__.create(db.connection())
  17. db.add(WechatBinding(id='binding', user_id=user_id, tenant_no=tenant_no,
  18. appid=settings.WECHAT_OA_APPID, subscribed=1, openid='oa-openid'))
  19. db.commit()
  20. def test_daily_worker_refreshes_real_counts_and_send_time(cert_notices, monkeypatch):
  21. from app.services.wechat_approval_notices import deliver_pending
  22. svc, db, now, add_cert, Notice = cert_notices
  23. add_cert('old')
  24. binding(db, 't1-a')
  25. assert svc.enqueue_daily_cert_notices(db, now=now) == 1
  26. db.commit()
  27. add_cert('new', days=-1)
  28. db.commit()
  29. send_time = now + timedelta(minutes=5)
  30. monkeypatch.setattr(svc, '_local_time', lambda value: send_time)
  31. client = Client()
  32. assert deliver_pending(db, client=client) == 1
  33. assert client.calls[0]['data'] == {
  34. 'thing5': {'value': '1项即将到期,1项已到期'},
  35. 'thing3': {'value': '系统默认发出'},
  36. 'time6': {'value': '2026-10-26 10:05:00'},
  37. }
  38. assert db.query(Notice).one().status == 'sent'
  39. assert deliver_pending(db, client=client) == 0
  40. @pytest.mark.parametrize('kind', ['bx_advance', 'bx_reimburse', 'pl_freight', 'pl_recharge'])
  41. def test_completion_delivered_once_without_approval_cycle(completion_db, kind):
  42. from app.services.wechat_approval_notices import deliver_pending
  43. from app.models.wechat_notice import WechatNotice
  44. db = completion_db
  45. seed(db, kind)
  46. kwargs = {'event_id': 'record', 'occurred_at': NOW} if kind.startswith('pl_') else {}
  47. row = sync(db, kind, **kwargs)
  48. assert row is not None
  49. recipient = row.recipient_id
  50. expected = json.loads(row.payload)
  51. binding(db, recipient)
  52. client = Client()
  53. assert deliver_pending(db, client=client) == 1
  54. assert client.calls[0]['data'] == expected
  55. assert 'amount' not in json.dumps(expected)
  56. assert db.query(WechatNotice).one().status == 'sent'
  57. assert deliver_pending(db, client=client) == 0