2018-05-16 12:33:54 -04:00
|
|
|
import configparser
|
2017-06-18 05:15:40 -04:00
|
|
|
import json
|
2018-05-14 15:08:03 -04:00
|
|
|
|
2018-05-16 12:06:36 -04:00
|
|
|
from typing import Any, List, Dict, Optional
|
2018-05-14 15:08:03 -04:00
|
|
|
from unittest import TestCase
|
|
|
|
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
|
|
|
|
2018-05-16 12:26:51 -04:00
|
|
|
def assert_bot_server_response(
|
|
|
|
self,
|
|
|
|
available_bots: Optional[List[str]]=None,
|
|
|
|
bots_config: Optional[Dict[str, Dict[str, str]]]=None,
|
|
|
|
bot_handlers: Optional[Dict[str, Any]]=None,
|
2018-05-28 11:39:03 -04:00
|
|
|
event: Optional[Dict[str, Any]]=None,
|
2018-05-16 12:26:51 -04:00
|
|
|
check_success: bool=False,
|
2018-05-16 12:33:54 -04:00
|
|
|
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:
|
2018-05-16 13:35:14 -04:00
|
|
|
bot_handlers = server.load_bot_handlers(available_bots, bots_config, third_party_bot_conf)
|
|
|
|
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:39:03 -04:00
|
|
|
response = self.app.post(data=json.dumps(event))
|
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
|