Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
scheduled_messages: Skip failure DM for cross-realm system bot senders.
Alex Vandivereb74b146b3de
diff --git a/zerver/actions/scheduled_messages.py b/zerver/actions/scheduled_messages.pyindex 26220646ca1..c7d83af63f7 100644--- a/zerver/actions/scheduled_messages.py+++ b/zerver/actions/scheduled_messages.py@@ -29,7 +29,7 @@ from zerver.lib.scheduled_messages import access_scheduled_messagefrom zerver.lib.string_validation import check_stream_topicfrom zerver.lib.timestamp import datetime_to_global_timefrom zerver.models import Client, Realm, ScheduledMessage, Subscription, UserProfile-from zerver.models.users import get_system_bot+from zerver.models.users import get_system_bot, is_cross_realm_bot_emailfrom zerver.tornado.django_api import send_event_on_commitSCHEDULED_MESSAGE_LATE_CUTOFF_MINUTES = 10@@ -472,6 +472,12 @@ def try_deliver_one_scheduled_message() -> bool:# the sending user account has been deactivated.and not isinstance(e, RealmDeactivatedError)and not isinstance(e, UserDeactivatedError)+ # Cross-realm system bots (welcome-bot, notification-bot,+ # emailgateway) have no human behind them to read the+ # notification, and notifying them leaves a stray+ # notification-bot <-> system-bot DirectMessageGroup+ # attached to no realm.+ and not is_cross_realm_bot_email(scheduled_message.sender.delivery_email)):notify_update_scheduled_message(scheduled_message.sender, scheduled_message)send_failed_scheduled_message_notification(diff --git a/zerver/tests/test_scheduled_messages.py b/zerver/tests/test_scheduled_messages.pyindex 0c40268a6db..3015e98ddd8 100644--- a/zerver/tests/test_scheduled_messages.py+++ b/zerver/tests/test_scheduled_messages.py@@ -7,6 +7,7 @@ from unittest import mockimport orjsonimport time_machine+from django.conf import settingsfrom django.utils.timezone import now as timezone_nowfrom zerver.actions.scheduled_messages import (@@ -20,6 +21,7 @@ from zerver.lib.test_helpers import most_recent_messagefrom zerver.lib.timestamp import timestamp_to_datetimefrom zerver.models import Attachment, Message, Recipient, ScheduledMessage, UserMessagefrom zerver.models.recipients import get_or_create_direct_message_group+from zerver.models.users import get_system_botif TYPE_CHECKING:from django.test.client import _MonkeyPatchedWSGIResponse as TestHttpResponse@@ -356,6 +358,47 @@ class ScheduledMessageTest(ZulipTestCase):self.assertEqual(msg.sender_id, self.notification_bot(realm).id)self.assertIn("Internal server error", msg.content)+ def test_failed_to_deliver_scheduled_message_from_system_bot(self) -> None:+ # Cross-realm system bots have no human reading their DMs, so+ # they should not receive a failure notification -- doing so+ # would leave a stray notification-bot <-> system-bot+ # DirectMessageGroup attached to no realm.+ self.create_scheduled_message()+ scheduled_message = self.last_scheduled_message()+ realm = scheduled_message.realm+ welcome_bot = get_system_bot(settings.WELCOME_BOT, realm.id)+ scheduled_message.sender = welcome_bot+ scheduled_message.save(update_fields=["sender"])++ more_than_scheduled_delivery_datetime = scheduled_message.scheduled_timestamp + timedelta(+ minutes=1+ )+ with (+ mock.patch(+ "zerver.actions.scheduled_messages.send_scheduled_message",+ side_effect=Exception(),+ ),+ time_machine.travel(more_than_scheduled_delivery_datetime, tick=False),+ self.assertLogs(level="INFO"),+ ):+ self.assertTrue(try_deliver_one_scheduled_message())+ scheduled_message.refresh_from_db()+ self.assertTrue(scheduled_message.failed)++ # No notification-bot -> welcome-bot message should have been+ # sent. internal_prep_private_message lands such a message in+ # the sender's realm (the system bot realm) when the recipient+ # is a cross-realm bot, so scope the check there.+ notification_bot = self.notification_bot(realm)+ notify_group = get_or_create_direct_message_group(+ id_list=[welcome_bot.id, notification_bot.id]+ )+ self.assertFalse(+ Message.objects.filter(+ realm=notification_bot.realm, recipient=notify_group.recipient+ ).exists()+ )+def test_editing_failed_send_scheduled_message(self) -> None:expected_failure_message = "Message could not be sent at the scheduled time."self.create_scheduled_message()