from unittest.mock import patch from django.test import TestCase from basic_info.models import Merchant, MerchantTypeEnum from notifier.models import NotificationEventKeyEnum, Notifier, NotifierChannelEnum from notifier.services import ( dispatch_notification_event, enqueue_notification_event, render_notification_content, send_notification_with_notifier, ) class NotifierServiceTestCase(TestCase): def setUp(self): self.merchant = Merchant.objects.create(name="通知商户", type=MerchantTypeEnum.STORE) self.notifier = Notifier.objects.create( merchant=self.merchant, name="任务创建通知", event_key=NotificationEventKeyEnum.MISSION_CREATED, channel=NotifierChannelEnum.WECOM_WEBHOOK, template_key="mission_created", config={"key": "abc123", "msgtype": "markdown"}, ) def test_render_notification_content(self): content = render_notification_content( notifier=self.notifier, payload={ "mission_id": 12, "created_by_name": "张三", "creator_name": "张三", "category_name": "通用", "is_urgent": False, "participant_names_display": "李四、王五", "description": "检查打印质量", }, ) self.assertIn("任务已创建", content) self.assertIn("任务ID:12", content) self.assertIn("创建人:张三", content) @patch("notifier.backends.send_wecom_webhook_message") def test_send_notification_with_notifier_uses_wecom_backend(self, mock_send): mock_send.return_value.ok = True mock_send.return_value.errcode = 0 mock_send.return_value.errmsg = "ok" result = send_notification_with_notifier( notifier=self.notifier, payload={ "mission_id": 12, "created_by_name": "张三", "creator_name": "张三", "category_name": "通用", "is_urgent": True, "participant_names_display": "无", "description": "检查打印质量", }, ) self.assertEqual(result["status"], "sent") self.assertEqual(result["channel"], NotifierChannelEnum.WECOM_WEBHOOK) mock_send.assert_called_once() @patch("notifier.services.send_notification_with_notifier") def test_dispatch_notification_event_filters_by_event_and_enabled(self, mock_send): mock_send.side_effect = lambda *, notifier, payload: { "notifier_id": notifier.id, "status": "sent", } Notifier.objects.create( merchant=self.merchant, name="任务回应通知", event_key=NotificationEventKeyEnum.MISSION_REPLIED, channel=NotifierChannelEnum.WECOM_WEBHOOK, template_key="mission_replied", config={"key": "def456"}, ) Notifier.objects.create( merchant=self.merchant, name="停用通知器", event_key=NotificationEventKeyEnum.MISSION_CREATED, channel=NotifierChannelEnum.WECOM_WEBHOOK, template_key="mission_created", is_enabled=False, config={"key": "ghi789"}, ) results = dispatch_notification_event( event_key=NotificationEventKeyEnum.MISSION_CREATED, merchant_id=self.merchant.id, payload={"mission_id": 99}, ) self.assertEqual(results, [{"notifier_id": self.notifier.id, "status": "sent"}]) self.assertEqual(mock_send.call_count, 1) self.assertEqual(mock_send.call_args.kwargs["notifier"].id, self.notifier.id) @patch("notifier.tasks.dispatch_notification_event_task.delay") def test_enqueue_notification_event_returns_task_id(self, mock_delay): mock_delay.return_value.id = "task-123" task_id = enqueue_notification_event( event_key=NotificationEventKeyEnum.MISSION_CREATED, merchant_id=self.merchant.id, payload={"mission_id": 1}, ) self.assertEqual(task_id, "task-123") mock_delay.assert_called_once() @patch("notifier.tasks.dispatch_notification_event_task.delay", side_effect=RuntimeError("broker down")) def test_enqueue_notification_event_fails_open(self, mock_delay): task_id = enqueue_notification_event( event_key=NotificationEventKeyEnum.MISSION_CREATED, merchant_id=self.merchant.id, payload={"mission_id": 1}, ) self.assertIsNone(task_id) mock_delay.assert_called_once()