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