diff --git a/zulip_bots/zulip_bots/bots/followup/followup.config b/zulip_bots/zulip_bots/bots/followup/followup.config new file mode 100644 index 0000000..0ceacaf --- /dev/null +++ b/zulip_bots/zulip_bots/bots/followup/followup.config @@ -0,0 +1,2 @@ +[followup] +stream = 'followup' diff --git a/zulip_bots/zulip_bots/bots/followup/followup.py b/zulip_bots/zulip_bots/bots/followup/followup.py index 95eeb57..453059e 100644 --- a/zulip_bots/zulip_bots/bots/followup/followup.py +++ b/zulip_bots/zulip_bots/bots/followup/followup.py @@ -23,15 +23,21 @@ class FollowupHandler(object): called "followup" that your API user can send to. ''' + def initialize(self, bot_handler: Any) -> None: + self.config_info = bot_handler.get_config_info('followup', optional=False) + self.stream = self.config_info.get("stream", 'followup') + def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None: if message['content'] == '': bot_response = "Please specify the message you want to send to followup stream after @mention-bot" bot_handler.send_reply(message, bot_response) + elif message['content'] == 'help': + bot_handler.send_reply(message, self.usage()) else: bot_response = self.get_bot_followup_response(message) bot_handler.send_message(dict( type='stream', - to='followup', + to=self.stream, subject=message['sender_email'], content=bot_response, )) diff --git a/zulip_bots/zulip_bots/bots/followup/test_followup.py b/zulip_bots/zulip_bots/bots/followup/test_followup.py index 599bf92..2019605 100755 --- a/zulip_bots/zulip_bots/bots/followup/test_followup.py +++ b/zulip_bots/zulip_bots/bots/followup/test_followup.py @@ -3,29 +3,60 @@ from __future__ import absolute_import from __future__ import print_function -from zulip_bots.test_lib import BotTestCase +from zulip_bots.test_lib import ( + StubBotHandler, + StubBotTestCase, + get_bot_message_handler, +) from typing import Any -class TestFollowUpBot(BotTestCase): + +class TestFollowUpBot(StubBotTestCase): bot_name = "followup" - def test_bot(self) -> None: - expected_send_reply = [ - ("", 'Please specify the message you want to send to followup stream after @mention-bot') - ] - self.check_expected_responses(expected_send_reply, expected_method='send_reply') + def test_followup_stream(self) -> None: + message = dict( + content='feed the cat', + type='stream', + sender_email='foo@example.com', + ) - expected_send_message = [ - ("foo", - {'type': 'stream', - 'to': 'followup', - 'subject': 'foo_sender@zulip.com', - 'content': 'from foo_sender@zulip.com: foo'}), - ("I have completed my task", - {'type': 'stream', - 'to': 'followup', - 'subject': 'foo_sender@zulip.com', - 'content': 'from foo_sender@zulip.com: I have completed my task'}), - ] - self.check_expected_responses(expected_send_message, expected_method='send_message') + with self.mock_config_info({'stream': 'followup'}): + response = self.get_response(message) + + self.assertEqual(response['content'], 'from foo@example.com: feed the cat') + self.assertEqual(response['to'], 'followup') + + def test_different_stream(self) -> None: + message = dict( + content='feed the cat', + type='stream', + sender_email='foo@example.com', + ) + + with self.mock_config_info({'stream': 'issue'}): + response = self.get_response(message) + + self.assertEqual(response['content'], 'from foo@example.com: feed the cat') + self.assertEqual(response['to'], 'issue') + + def test_bot_responds_to_empty_message(self) -> None: + bot_response = 'Please specify the message you want to send to followup stream after @mention-bot' + + with self.mock_config_info({'stream': 'followup'}): + self.verify_reply('', bot_response) + + def test_help_text(self) -> None: + request = 'help' + bot_response = ''' + This plugin will allow users to flag messages + as being follow-up items. Users should preface + messages with "@mention-bot". + + Before running this, make sure to create a stream + called "followup" that your API user can send to. + ''' + + with self.mock_config_info({'stream': 'followup'}): + self.verify_reply(request, bot_response) diff --git a/zulip_bots/zulip_bots/test_lib.py b/zulip_bots/zulip_bots/test_lib.py index 2a36e1f..1135e14 100755 --- a/zulip_bots/zulip_bots/test_lib.py +++ b/zulip_bots/zulip_bots/test_lib.py @@ -100,6 +100,18 @@ class StubBotTestCase(TestCase): bot_name = '' + def get_response(self, message): + # type: (Dict[str, Any]) -> Dict[str, Any] + bot = get_bot_message_handler(self.bot_name) + bot_handler = StubBotHandler() + + if hasattr(bot, 'initialize'): + bot.initialize(bot_handler) + + bot_handler.reset_transcript() + bot.handle_message(message, bot_handler) + return bot_handler.unique_response() + def verify_reply(self, request, response): # type: (str, str) -> None