From 81b207795f09bf1bcea3bef2b9da394cfb714f07 Mon Sep 17 00:00:00 2001 From: Nikhil Mishra Date: Sat, 9 Dec 2017 04:04:21 +0530 Subject: [PATCH] followup bot: Fix help response and configure streams. From @showell: We had a PR here with lots going on, and the commits weren't very well organized, and then there were some tricky merge conflicts from another PR. So I just squashed them all into one commit. What this does: * allow you to configure your followup stream * provide help in followup stream * add more testing to followup stream * add get_response() helper for tests Fixes #173 Fixes #174 --- .../zulip_bots/bots/followup/followup.config | 2 + .../zulip_bots/bots/followup/followup.py | 8 ++- .../zulip_bots/bots/followup/test_followup.py | 71 +++++++++++++------ zulip_bots/zulip_bots/test_lib.py | 12 ++++ 4 files changed, 72 insertions(+), 21 deletions(-) create mode 100644 zulip_bots/zulip_bots/bots/followup/followup.config 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