mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
bot_lib: Raise exception when PM recipient list is empty.
We need to explicitly check for empty recipient lists in send_message to ensure that internal_send_huddle_message doesn't call Addressee.for_private with an empty recipient list.
This commit is contained in:
@@ -15,6 +15,8 @@ from zerver.lib.bot_config import get_bot_config, ConfigError
|
||||
from zerver.lib.integrations import EMBEDDED_BOTS
|
||||
from zerver.lib.topic import get_topic_from_message_info
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
import configparser
|
||||
|
||||
from mypy_extensions import NoReturn
|
||||
@@ -60,6 +62,9 @@ class StateHandler:
|
||||
class EmbeddedBotQuitException(Exception):
|
||||
pass
|
||||
|
||||
class EmbeddedBotEmptyRecipientsList(Exception):
|
||||
pass
|
||||
|
||||
class EmbeddedBotHandler:
|
||||
def __init__(self, user_profile: UserProfile) -> None:
|
||||
# Only expose a subset of our UserProfile's functionality
|
||||
@@ -84,7 +89,9 @@ class EmbeddedBotHandler:
|
||||
# usual 'to' field could be either a List[str] or a str.
|
||||
recipients = ','.join(message['to']).split(',')
|
||||
|
||||
if len(message['to']) == 1:
|
||||
if len(message['to']) == 0:
|
||||
raise EmbeddedBotEmptyRecipientsList(_('Message must have recipients!'))
|
||||
elif len(message['to']) == 1:
|
||||
recipient_user = get_active_user(recipients[0], self.user_profile.realm)
|
||||
internal_send_private_message(self.user_profile.realm, self.user_profile,
|
||||
recipient_user, message['content'])
|
||||
|
||||
@@ -11,7 +11,8 @@ from zerver.lib.actions import (
|
||||
do_create_user,
|
||||
get_service_bot_events,
|
||||
)
|
||||
from zerver.lib.bot_lib import StateHandler, EmbeddedBotHandler
|
||||
from zerver.lib.bot_lib import StateHandler, EmbeddedBotHandler, \
|
||||
EmbeddedBotEmptyRecipientsList
|
||||
from zerver.lib.bot_storage import StateError
|
||||
from zerver.lib.bot_config import set_bot_config, ConfigError, load_bot_config_template
|
||||
from zerver.lib.test_classes import ZulipTestCase
|
||||
@@ -399,6 +400,10 @@ class TestServiceBotConfigHandler(ZulipTestCase):
|
||||
self.assertTrue(isinstance(bot_config, dict))
|
||||
self.assertEqual(len(bot_config), 0)
|
||||
|
||||
def test_bot_send_pm_with_empty_recipients_list(self) -> None:
|
||||
with self.assertRaisesRegex(EmbeddedBotEmptyRecipientsList, 'Message must have recipients!'):
|
||||
self.bot_handler.send_message(message={'type': 'private', 'to': []})
|
||||
|
||||
|
||||
class TestServiceBotEventTriggers(ZulipTestCase):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user