message_send: Use stream object for name in "no subscribers" message.

In the case where a stream existed but had no subscribers, the error
message used to send to the owner always used `stream_name`, which
may have been None.

Switch to using `stream.name` rather than `stream_name` for this case.
This commit is contained in:
Alex Vandiver 2023-02-13 14:32:06 +00:00 committed by Tim Abbott
parent 6427d85cf6
commit f78c2eab2c
2 changed files with 87 additions and 6 deletions

View File

@ -1240,22 +1240,28 @@ def send_pm_if_empty_stream(
if not sender.is_bot or sender.bot_owner is None:
return
arg_dict = {
"bot_identity": f"`{sender.delivery_email}`",
"stream_id": stream_id,
"stream_name": f"#**{stream_name}**",
"new_stream_link": "#streams/new",
}
if sender.bot_owner is not None:
with override_language(sender.bot_owner.default_language):
arg_dict: Dict[str, Any] = {
"bot_identity": f"`{sender.delivery_email}`",
}
if stream is None:
if stream_id is not None:
arg_dict = {
**arg_dict,
"stream_id": stream_id,
}
content = _(
"Your bot {bot_identity} tried to send a message to stream ID "
"{stream_id}, but there is no stream with that ID."
).format(**arg_dict)
else:
assert stream_name is not None
arg_dict = {
**arg_dict,
"stream_name": f"#**{stream_name}**",
"new_stream_link": "#streams/new",
}
content = _(
"Your bot {bot_identity} tried to send a message to stream "
"{stream_name}, but that stream does not exist. "
@ -1264,6 +1270,10 @@ def send_pm_if_empty_stream(
else:
if num_subscribers_for_stream_id(stream.id) > 0:
return
arg_dict = {
**arg_dict,
"stream_name": f"#**{stream.name}**",
}
content = _(
"Your bot {bot_identity} tried to send a message to "
"stream {stream_name}. The stream exists but "

View File

@ -35,6 +35,7 @@ from zerver.lib.addressee import Addressee
from zerver.lib.cache import cache_delete, get_stream_cache_key
from zerver.lib.exceptions import JsonableError
from zerver.lib.message import MessageDict, get_raw_unread_data, get_recent_private_conversations
from zerver.lib.streams import create_stream_if_needed
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import (
get_user_messages,
@ -145,6 +146,76 @@ class MessagePOSTTest(ZulipTestCase):
)
self.assertEqual(msg.content, expected)
def test_message_to_stream_with_no_subscribers(self) -> None:
"""
Sending a message to an empty stream succeeds, but sends a warning
to the owner.
"""
realm = get_realm("zulip")
cordelia = self.example_user("cordelia")
bot = self.create_test_bot(
short_name="whatever",
user_profile=cordelia,
)
stream = create_stream_if_needed(realm, "Acropolis")[0]
result = self.api_post(
bot,
"/api/v1/messages",
{
"type": "stream",
"to": orjson.dumps(stream.name).decode(),
"content": "Stream message to an empty stream by name.",
"topic": "Test topic for empty stream name message",
},
)
self.assert_json_success(result)
msg = self.get_last_message()
expected = "Stream message to an empty stream by name."
self.assertEqual(msg.content, expected)
msg = self.get_second_to_last_message()
expected = (
"Your bot `whatever-bot@zulip.testserver` tried to send a message to "
"stream #**Acropolis**. The stream exists but does not have any subscribers."
)
self.assertEqual(msg.content, expected)
def test_message_to_stream_with_no_subscribers_by_id(self) -> None:
"""
Sending a message to an empty stream succeeds, but sends a warning
to the owner.
"""
realm = get_realm("zulip")
cordelia = self.example_user("cordelia")
bot = self.create_test_bot(
short_name="whatever",
user_profile=cordelia,
)
stream = create_stream_if_needed(realm, "Acropolis")[0]
result = self.api_post(
bot,
"/api/v1/messages",
{
"type": "stream",
"to": orjson.dumps([stream.id]).decode(),
"content": "Stream message to an empty stream by id.",
"topic": "Test topic for empty stream id message",
},
)
self.assert_json_success(result)
msg = self.get_last_message()
expected = "Stream message to an empty stream by id."
self.assertEqual(msg.content, expected)
msg = self.get_second_to_last_message()
expected = (
"Your bot `whatever-bot@zulip.testserver` tried to send a message to "
"stream #**Acropolis**. The stream exists but does not have any subscribers."
)
self.assertEqual(msg.content, expected)
def test_message_to_stream_by_id(self) -> None:
"""
Sending a message to a stream (by stream ID) to which you are