zulip_botserver: Don't reinstantiate ExternalBotHandlers.

Fixes #147.
This commit is contained in:
derAnfaenger 2017-11-07 12:57:47 +01:00 committed by showell
parent e05ce661c5
commit bcc8bd7d38
3 changed files with 35 additions and 20 deletions

View file

@ -14,11 +14,12 @@ class BotServerTestCase(TestCase):
available_bots=None,
bots_config=None,
bots_lib_module=None,
bot_handlers=None,
payload_url="/bots/testbot",
message=dict(message={'key': "test message"}),
check_success=False,
):
# type: (List[str], 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:
zulip_botserver.server.available_bots = available_bots
@ -29,6 +30,9 @@ class BotServerTestCase(TestCase):
if bots_lib_module is not None:
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))
if check_success:

View file

@ -2,6 +2,8 @@ from __future__ import absolute_import
import mock
import unittest
from typing import Any
from werkzeug.exceptions import BadRequest
from zulip_botserver import server
from .server_test_lib import BotServerTestCase
class BotServerTests(BotServerTestCase):
@ -29,11 +31,14 @@ class BotServerTests(BotServerTestCase):
bots_lib_module = {
'testbot': BotServerTests.MockLibModule()
}
bot_handlers = {
'testbot': mock_ExternalBotHandler()
}
self.assert_bot_server_response(available_bots=available_bots,
bots_config=bots_config,
bots_lib_module=bots_lib_module,
bot_handlers=bot_handlers,
check_success=True)
assert mock_ExternalBotHandler.called
def test_bot_module_not_exists(self):
# type: () -> None
@ -41,8 +46,9 @@ class BotServerTests(BotServerTestCase):
payload_url="/bots/not_supported_bot",
check_success=False)
def test_wrong_bot_credentials(self):
# type: () -> None
@mock.patch('logging.error')
def test_wrong_bot_credentials(self, mock_LoggingError):
# type: (mock.Mock) -> None
available_bots = ['testbot']
bots_config = {
'testbot': {
@ -51,9 +57,10 @@ class BotServerTests(BotServerTestCase):
'site': 'http://localhost',
}
}
self.assert_bot_server_response(available_bots=available_bots,
bots_config=bots_config,
check_success=False)
server.available_bots = available_bots
server.load_bot_handlers()
mock_LoggingError.assert_called_with("Cannot fetch user profile, make sure you have set up the zuliprc file correctly.")
if __name__ == '__main__':
unittest.main()