2020-05-26 07:16:25 +02:00
|
|
|
from unittest.mock import patch
|
2017-10-25 17:18:43 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
import ujson
|
|
|
|
|
2019-02-02 23:53:44 +01:00
|
|
|
from zerver.lib.bot_lib import EmbeddedBotQuitException
|
2017-10-25 17:18:43 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2018-02-25 19:33:49 +01:00
|
|
|
from zerver.models import (
|
2020-06-11 00:54:34 +02:00
|
|
|
UserProfile,
|
|
|
|
get_display_recipient,
|
|
|
|
get_realm,
|
|
|
|
get_service_profile,
|
|
|
|
get_user,
|
2018-02-25 19:33:49 +01:00
|
|
|
)
|
2017-10-25 17:18:43 +02:00
|
|
|
|
2018-01-30 17:05:14 +01:00
|
|
|
|
2017-10-25 17:18:43 +02:00
|
|
|
class TestEmbeddedBotMessaging(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2019-10-19 20:47:00 +02:00
|
|
|
super().setUp()
|
2017-10-25 17:18:43 +02:00
|
|
|
self.user_profile = self.example_user("othello")
|
2018-01-30 17:05:14 +01:00
|
|
|
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=ujson.dumps({'foo': 'bar'}))
|
2017-10-25 17:18:43 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_pm_to_embedded_bot(self) -> None:
|
2018-06-17 07:12:51 +02:00
|
|
|
assert self.bot_profile is not None
|
2020-03-07 11:43:05 +01:00
|
|
|
self.send_personal_message(self.user_profile, self.bot_profile,
|
2017-10-28 16:31:02 +02:00
|
|
|
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)
|
2020-06-23 07:20:47 +02:00
|
|
|
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
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_stream_message_to_embedded_bot(self) -> None:
|
2018-06-17 07:12:51 +02:00
|
|
|
assert self.bot_profile is not None
|
2020-03-07 11:43:05 +01:00
|
|
|
self.send_stream_message(self.user_profile, "Denmark",
|
2020-06-09 00:25:09 +02:00
|
|
|
content=f"@**{self.bot_profile.full_name}** foo",
|
2017-10-28 16:31:02 +02:00
|
|
|
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")
|
2017-10-25 17:18:43 +02:00
|
|
|
display_recipient = get_display_recipient(last_message.recipient)
|
|
|
|
self.assertEqual(display_recipient, "Denmark")
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_stream_message_not_to_embedded_bot(self) -> None:
|
2020-03-07 11:43:05 +01:00
|
|
|
self.send_stream_message(self.user_profile, "Denmark",
|
2017-10-28 16:31:02 +02:00
|
|
|
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")
|
2017-11-03 21:08:34 +01:00
|
|
|
|
2018-02-25 20:25:48 +01:00
|
|
|
def test_message_to_embedded_bot_with_initialize(self) -> None:
|
2018-06-17 07:12:51 +02:00
|
|
|
assert self.bot_profile is not None
|
2018-02-25 20:25:48 +01:00
|
|
|
with patch('zulip_bots.bots.helloworld.helloworld.HelloWorldHandler.initialize',
|
|
|
|
create=True) as mock_initialize:
|
2020-03-07 11:43:05 +01:00
|
|
|
self.send_stream_message(self.user_profile, "Denmark",
|
2020-06-09 00:25:09 +02:00
|
|
|
content=f"@**{self.bot_profile.full_name}** foo",
|
2018-02-25 20:25:48 +01:00
|
|
|
topic_name="bar")
|
|
|
|
mock_initialize.assert_called_once()
|
|
|
|
|
2018-02-08 15:51:38 +01:00
|
|
|
def test_embedded_bot_quit_exception(self) -> None:
|
2018-06-17 07:12:51 +02:00
|
|
|
assert self.bot_profile is not None
|
2018-02-08 15:51:38 +01:00
|
|
|
with patch('zulip_bots.bots.helloworld.helloworld.HelloWorldHandler.handle_message',
|
|
|
|
side_effect=EmbeddedBotQuitException("I'm quitting!")):
|
|
|
|
with patch('logging.warning') as mock_logging:
|
2020-03-07 11:43:05 +01:00
|
|
|
self.send_stream_message(self.user_profile, "Denmark",
|
2020-06-09 00:25:09 +02:00
|
|
|
content=f"@**{self.bot_profile.full_name}** foo",
|
2018-02-08 15:51:38 +01:00
|
|
|
topic_name="bar")
|
|
|
|
mock_logging.assert_called_once_with("I'm quitting!")
|
|
|
|
|
2017-11-03 21:08:34 +01:00
|
|
|
class TestEmbeddedBotFailures(ZulipTestCase):
|
2018-02-25 19:33:49 +01:00
|
|
|
def test_message_embedded_bot_with_invalid_service(self) -> None:
|
2017-11-03 21:08:34 +01:00
|
|
|
user_profile = self.example_user("othello")
|
2018-01-30 17:05:14 +01:00
|
|
|
self.create_test_bot(short_name='embedded', user_profile=user_profile,
|
|
|
|
bot_type=UserProfile.EMBEDDED_BOT,
|
2018-02-25 19:33:49 +01:00
|
|
|
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 patch('logging.error') as logging_error_mock:
|
2020-03-07 11:43:05 +01:00
|
|
|
self.send_stream_message(user_profile, "Denmark",
|
2020-06-09 00:25:09 +02:00
|
|
|
content=f"@**{bot_profile.full_name}** foo",
|
2018-02-25 19:33:49 +01:00
|
|
|
topic_name="bar")
|
|
|
|
logging_error_mock.assert_called_once_with(
|
2020-05-02 08:44:14 +02:00
|
|
|
"Error: User %s has bot with invalid embedded bot service %s",
|
|
|
|
bot_profile.id, "invalid",
|
|
|
|
)
|