zulip_botserver: Make tests more realistic.
Specifically, this invokes server.load_lib_modules() and server.load_bot_handlers() to generate the modules and handlers, instead of hardcoding them.
This commit is contained in:
parent
55332d8cbc
commit
fb116259d0
|
@ -15,23 +15,16 @@ class BotServerTestCase(TestCase):
|
||||||
bots_config=None,
|
bots_config=None,
|
||||||
bots_lib_module=None,
|
bots_lib_module=None,
|
||||||
bot_handlers=None,
|
bot_handlers=None,
|
||||||
payload_url="/bots/testbot",
|
payload_url="/bots/helloworld",
|
||||||
message=dict(message={'key': "test message"}),
|
message=dict(message={'key': "test message"}),
|
||||||
check_success=False,
|
check_success=False,
|
||||||
):
|
):
|
||||||
# type: (List[str], Dict[str, Any], Dict[str, Any], Dict[str, Any], str, Dict[str, Dict[str, Any]], bool) -> None
|
# type: (List[str], Dict[str, Any], Dict[str, Any], Dict[str, Any], str, Dict[str, Dict[str, Any]], bool) -> None
|
||||||
|
|
||||||
if available_bots is not None:
|
if available_bots is not None:
|
||||||
zulip_botserver.server.available_bots = available_bots
|
zulip_botserver.server.available_bots = available_bots
|
||||||
|
|
||||||
if bots_config is not None:
|
|
||||||
zulip_botserver.server.bots_config = bots_config
|
zulip_botserver.server.bots_config = bots_config
|
||||||
|
zulip_botserver.server.load_lib_modules()
|
||||||
if bots_lib_module is not None:
|
zulip_botserver.server.load_bot_handlers()
|
||||||
zulip_botserver.server.bots_lib_module = bots_lib_module
|
|
||||||
|
|
||||||
if bot_handlers is not None:
|
|
||||||
zulip_botserver.server.bot_handlers = bot_handlers
|
|
||||||
|
|
||||||
response = self.app.post(payload_url, data=json.dumps(message))
|
response = self.app.post(payload_url, data=json.dumps(message))
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ from __future__ import absolute_import
|
||||||
import mock
|
import mock
|
||||||
import unittest
|
import unittest
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from werkzeug.exceptions import BadRequest
|
|
||||||
from zulip_botserver import server
|
from zulip_botserver import server
|
||||||
from .server_test_lib import BotServerTestCase
|
from .server_test_lib import BotServerTestCase
|
||||||
|
|
||||||
|
@ -20,39 +19,31 @@ class BotServerTests(BotServerTestCase):
|
||||||
@mock.patch('zulip_botserver.server.ExternalBotHandler')
|
@mock.patch('zulip_botserver.server.ExternalBotHandler')
|
||||||
def test_successful_request(self, mock_ExternalBotHandler):
|
def test_successful_request(self, mock_ExternalBotHandler):
|
||||||
# type: (mock.Mock) -> None
|
# type: (mock.Mock) -> None
|
||||||
available_bots = ['testbot']
|
available_bots = ['helloworld']
|
||||||
bots_config = {
|
bots_config = {
|
||||||
'testbot': {
|
'helloworld': {
|
||||||
'email': 'testbot-bot@zulip.com',
|
'email': 'helloworld-bot@zulip.com',
|
||||||
'key': '123456789qwertyuiop',
|
'key': '123456789qwertyuiop',
|
||||||
'site': 'http://localhost',
|
'site': 'http://localhost',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bots_lib_module = {
|
|
||||||
'testbot': BotServerTests.MockLibModule()
|
|
||||||
}
|
|
||||||
bot_handlers = {
|
|
||||||
'testbot': mock_ExternalBotHandler()
|
|
||||||
}
|
|
||||||
self.assert_bot_server_response(available_bots=available_bots,
|
self.assert_bot_server_response(available_bots=available_bots,
|
||||||
bots_config=bots_config,
|
bots_config=bots_config,
|
||||||
bots_lib_module=bots_lib_module,
|
|
||||||
bot_handlers=bot_handlers,
|
|
||||||
check_success=True)
|
check_success=True)
|
||||||
|
|
||||||
def test_bot_module_not_exists(self):
|
def test_bot_module_not_exists(self):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
self.assert_bot_server_response(bots_lib_module={},
|
self.assert_bot_server_response(available_bots=[],
|
||||||
payload_url="/bots/not_supported_bot",
|
payload_url="/bots/not_supported_bot",
|
||||||
check_success=False)
|
check_success=False)
|
||||||
|
|
||||||
@mock.patch('logging.error')
|
@mock.patch('logging.error')
|
||||||
def test_wrong_bot_credentials(self, mock_LoggingError):
|
def test_wrong_bot_credentials(self, mock_LoggingError):
|
||||||
# type: (mock.Mock) -> None
|
# type: (mock.Mock) -> None
|
||||||
available_bots = ['testbot']
|
available_bots = ['helloworld']
|
||||||
bots_config = {
|
bots_config = {
|
||||||
'testbot': {
|
'helloworld': {
|
||||||
'email': 'testbot-bot@zulip.com',
|
'email': 'helloworld-bot@zulip.com',
|
||||||
'key': '123456789qwertyuiop',
|
'key': '123456789qwertyuiop',
|
||||||
'site': 'http://localhost',
|
'site': 'http://localhost',
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue