zulip/zerver/tests/test_embedded_bot_system.py

111 lines
4.5 KiB
Python
Raw Normal View History

from unittest.mock import patch
2017-10-25 17:18:43 +02:00
import orjson
from typing_extensions import override
from zerver.lib.bot_lib import EmbeddedBotQuitError
2017-10-25 17:18:43 +02:00
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import UserProfile, get_display_recipient, get_service_profile
from zerver.models.realms import get_realm
from zerver.models.users import get_user
2017-10-25 17:18:43 +02:00
2017-10-25 17:18:43 +02:00
class TestEmbeddedBotMessaging(ZulipTestCase):
@override
def setUp(self) -> None:
super().setUp()
2017-10-25 17:18:43 +02:00
self.user_profile = self.example_user("othello")
self.bot_profile = self.create_test_bot(
"embedded",
self.user_profile,
full_name="Embedded bot",
bot_type=UserProfile.EMBEDDED_BOT,
service_name="helloworld",
config_data=orjson.dumps({"foo": "bar"}).decode(),
)
2017-10-25 17:18:43 +02:00
def test_pm_to_embedded_bot(self) -> None:
assert self.bot_profile is not None
self.send_personal_message(self.user_profile, self.bot_profile, content="help")
2017-10-25 17:18:43 +02:00
last_message = self.get_last_message()
self.assertEqual(last_message.content, "beep boop")
self.assertEqual(last_message.sender_id, self.bot_profile.id)
display_recipient = get_display_recipient(last_message.recipient)
assert isinstance(display_recipient, list)
self.assert_length(display_recipient, 1)
self.assertEqual(display_recipient[0]["email"], self.user_profile.email)
2017-10-25 17:18:43 +02:00
def test_stream_message_to_embedded_bot(self) -> None:
assert self.bot_profile is not None
self.send_stream_message(
self.user_profile,
"Denmark",
content=f"@**{self.bot_profile.full_name}** foo",
topic_name="bar",
)
2017-10-25 17:18:43 +02:00
last_message = self.get_last_message()
self.assertEqual(last_message.content, "beep boop")
self.assertEqual(last_message.sender_id, self.bot_profile.id)
2018-11-10 16:11:12 +01:00
self.assertEqual(last_message.topic_name(), "bar")
self.assert_message_stream_name(last_message, "Denmark")
2017-10-25 17:18:43 +02:00
def test_stream_message_not_to_embedded_bot(self) -> None:
self.send_stream_message(self.user_profile, "Denmark", content="foo", topic_name="bar")
2017-10-25 17:18:43 +02:00
last_message = self.get_last_message()
self.assertEqual(last_message.content, "foo")
def test_message_to_embedded_bot_with_initialize(self) -> None:
assert self.bot_profile is not None
tests: Ensure stream senders get a UserMessage row. We now complain if a test author sends a stream message that does not result in the sender getting a UserMessage row for the message. This is basically 100% equivalent to complaining that the author failed to subscribe the sender to the stream as part of the test setup, as far as I can tell, so the AssertionError instructs the author to subscribe the sender to the stream. We exempt bots from this check, although it is plausible we should only exempt the system bots like the notification bot. I considered auto-subscribing the sender to the stream, but that can be a little more expensive than the current check, and we generally want test setup to be explicit. If there is some legitimate way than a subscribed human sender can't get a UserMessage, then we probably want an explicit test for that, or we may want to change the backend to just write a UserMessage row in that hypothetical situation. For most tests, including almost all the ones fixed here, the author just wants their test setup to realistically reflect normal operation, and often devs may not realize that Cordelia is not subscribed to Denmark or not realize that Hamlet is not subscribed to Scotland. Some of us don't remember our Shakespeare from high school, and our stream subscriptions don't even necessarily reflect which countries the Bard placed his characters in. There may also be some legitimate use case where an author wants to simulate sending a message to an unsubscribed stream, but for those edge cases, they can always set allow_unsubscribed_sender to True.
2021-12-10 13:55:48 +01:00
self.subscribe(self.user_profile, "Denmark")
with patch(
"zulip_bots.bots.helloworld.helloworld.HelloWorldHandler.initialize", create=True
) as mock_initialize:
self.send_stream_message(
self.user_profile,
"Denmark",
content=f"@**{self.bot_profile.full_name}** foo",
topic_name="bar",
)
mock_initialize.assert_called_once()
def test_embedded_bot_quit_exception(self) -> None:
assert self.bot_profile is not None
with patch(
"zulip_bots.bots.helloworld.helloworld.HelloWorldHandler.handle_message",
side_effect=EmbeddedBotQuitError("I'm quitting!"),
):
with self.assertLogs(level="WARNING") as m:
self.send_stream_message(
self.user_profile,
"Denmark",
content=f"@**{self.bot_profile.full_name}** foo",
topic_name="bar",
)
self.assertEqual(m.output, ["WARNING:root:I'm quitting!"])
class TestEmbeddedBotFailures(ZulipTestCase):
def test_message_embedded_bot_with_invalid_service(self) -> None:
user_profile = self.example_user("othello")
self.create_test_bot(
short_name="embedded",
user_profile=user_profile,
bot_type=UserProfile.EMBEDDED_BOT,
service_name="helloworld",
)
bot_profile = get_user("embedded-bot@zulip.testserver", get_realm("zulip"))
service_profile = get_service_profile(bot_profile.id, "helloworld")
service_profile.name = "invalid"
service_profile.save()
with self.assertLogs(level="ERROR") as m:
self.send_stream_message(
user_profile,
"Denmark",
content=f"@**{bot_profile.full_name}** foo",
topic_name="bar",
)
self.assertEqual(
m.output[0],
f"ERROR:root:Error: User {bot_profile.id} has bot with invalid embedded bot service invalid",
)