2018-05-16 12:33:54 -04:00
|
|
|
import configparser
|
2017-06-18 05:15:40 -04:00
|
|
|
import json
|
2021-05-28 05:00:04 -04:00
|
|
|
from typing import Any, Dict, List, Optional
|
2021-05-28 07:19:40 -04:00
|
|
|
from unittest import TestCase, mock
|
2018-05-14 15:08:03 -04:00
|
|
|
|
|
|
|
from zulip_botserver import server
|
2017-06-18 05:15:40 -04:00
|
|
|
|
2018-05-15 10:55:58 -04:00
|
|
|
|
2017-06-18 05:15:40 -04:00
|
|
|
class BotServerTestCase(TestCase):
|
2018-05-14 15:45:54 -04:00
|
|
|
def setUp(self) -> None:
|
2018-05-14 15:08:03 -04:00
|
|
|
server.app.testing = True
|
|
|
|
self.app = server.app.test_client()
|
2017-06-18 05:15:40 -04:00
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
@mock.patch("zulip_bots.lib.ExternalBotHandler")
|
2018-05-16 12:26:51 -04:00
|
|
|
def assert_bot_server_response(
|
|
|
|
self,
|
2018-05-28 11:50:55 -04:00
|
|
|
mock_ExternalBotHandler: mock.Mock,
|
2020-04-18 20:16:37 -04:00
|
|
|
available_bots: Optional[List[str]] = None,
|
|
|
|
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,
|
2018-05-16 12:26:51 -04:00
|
|
|
) -> None:
|
2018-05-15 10:55:58 -04:00
|
|
|
if available_bots is not None and bots_config is not None:
|
2018-05-22 07:46:40 -04:00
|
|
|
server.bots_config = bots_config
|
2018-05-16 13:35:14 -04:00
|
|
|
bots_lib_modules = server.load_lib_modules(available_bots)
|
2018-05-15 10:55:58 -04:00
|
|
|
server.app.config["BOTS_LIB_MODULES"] = bots_lib_modules
|
|
|
|
if bot_handlers is None:
|
2021-05-28 05:03:46 -04:00
|
|
|
bot_handlers = server.load_bot_handlers(
|
2021-07-21 23:14:52 -04:00
|
|
|
available_bots, bots_lib_modules, bots_config, third_party_bot_conf
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
|
|
|
message_handlers = server.init_message_handlers(
|
|
|
|
available_bots, bots_lib_modules, bot_handlers
|
|
|
|
)
|
2018-05-16 10:55:31 -04:00
|
|
|
server.app.config["BOT_HANDLERS"] = bot_handlers
|
|
|
|
server.app.config["MESSAGE_HANDLERS"] = message_handlers
|
2017-11-07 06:57:47 -05:00
|
|
|
|
2018-05-28 11:06:57 -04:00
|
|
|
mock_ExternalBotHandler.return_value.full_name = "test"
|
2018-05-28 11:39:03 -04:00
|
|
|
response = self.app.post(data=json.dumps(event))
|
2017-06-18 05:15:40 -04:00
|
|
|
|
2018-05-28 12:47:18 -04:00
|
|
|
# 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)
|
|
|
|
|
2017-06-18 05:15:40 -04:00
|
|
|
if check_success:
|
2018-05-14 15:08:03 -04:00
|
|
|
assert 200 <= response.status_code < 300
|
2017-06-18 05:15:40 -04:00
|
|
|
else:
|
2018-05-14 15:08:03 -04:00
|
|
|
assert 400 <= response.status_code < 500
|