2017-06-18 05:15:40 -04:00
|
|
|
import json
|
2018-05-14 15:08:03 -04:00
|
|
|
|
|
|
|
from typing import Any, List, Dict, Optional
|
|
|
|
from unittest import TestCase
|
|
|
|
from zulip_botserver import server
|
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
|
|
|
|
|
|
|
def assert_bot_server_response(self,
|
2018-05-14 15:45:54 -04:00
|
|
|
available_bots: Optional[List[str]]=None,
|
|
|
|
bots_config: Optional[Dict[str, Any]]=None,
|
|
|
|
bots_lib_module: Optional[Dict[str, Any]]=None,
|
|
|
|
bot_handlers: Optional[Dict[str, Any]]=None,
|
|
|
|
payload_url: str="/bots/helloworld",
|
|
|
|
message: Optional[Dict[str, Any]]=dict(message={'key': "test message"}),
|
|
|
|
check_success: bool=False,
|
|
|
|
) -> None:
|
2017-06-18 05:15:40 -04:00
|
|
|
if available_bots is not None:
|
2018-05-14 15:08:03 -04:00
|
|
|
server.available_bots = available_bots
|
|
|
|
server.load_lib_modules()
|
2018-05-15 15:27:55 -04:00
|
|
|
server.load_bot_handlers(bots_config)
|
2017-11-07 06:57:47 -05:00
|
|
|
|
2017-06-18 05:15:40 -04:00
|
|
|
response = self.app.post(payload_url, data=json.dumps(message))
|
|
|
|
|
|
|
|
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
|