From 5c062cee0d0223bcfa85d03767ab9edc9c1b2cc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6nig?= Date: Mon, 28 May 2018 18:47:18 +0200 Subject: [PATCH] botserver: Verify bot response. This allows for more realistic testing. --- zulip_botserver/tests/server_test_lib.py | 11 +++++++++++ zulip_botserver/tests/test_server.py | 2 ++ 2 files changed, 13 insertions(+) diff --git a/zulip_botserver/tests/server_test_lib.py b/zulip_botserver/tests/server_test_lib.py index 2fb3538..8823772 100644 --- a/zulip_botserver/tests/server_test_lib.py +++ b/zulip_botserver/tests/server_test_lib.py @@ -21,6 +21,7 @@ class BotServerTestCase(TestCase): bots_config: Optional[Dict[str, Dict[str, str]]]=None, bot_handlers: Optional[Dict[str, Any]]=None, event: Optional[Dict[str, Any]]=None, + expected_response: Optional[str]=None, check_success: bool=False, third_party_bot_conf: Optional[configparser.ConfigParser]=None, ) -> None: @@ -36,6 +37,16 @@ class BotServerTestCase(TestCase): response = self.app.post(data=json.dumps(event)) + # NOTE: Currently, assert_bot_server_response can only check the expected_response + # for bots that use send_reply. However, the vast majority of bots use send_reply. + # Therefore, the Botserver can be still be effectively tested. + bot_send_reply_call = mock_ExternalBotHandler.return_value.send_reply + if expected_response is not None: + self.assertTrue(bot_send_reply_call.called) + self.assertEqual(expected_response, bot_send_reply_call.call_args[0][1]) + else: + self.assertFalse(bot_send_reply_call.called) + if check_success: assert 200 <= response.status_code < 300 else: diff --git a/zulip_botserver/tests/test_server.py b/zulip_botserver/tests/test_server.py index 95c3cd4..3ce2b42 100644 --- a/zulip_botserver/tests/test_server.py +++ b/zulip_botserver/tests/test_server.py @@ -32,6 +32,7 @@ class BotServerTests(BotServerTestCase): bots_config=bots_config, event=dict(message={'content': "test message"}, bot_email='helloworld-bot@zulip.com'), + expected_response="beep boop", check_success=True) def test_successful_request_from_two_bots(self) -> None: @@ -51,6 +52,7 @@ class BotServerTests(BotServerTestCase): self.assert_bot_server_response(available_bots=available_bots, event=dict(message={'content': "test message"}, bot_email='helloworld-bot@zulip.com'), + expected_response="beep boop", bots_config=bots_config, check_success=True)