2017-06-18 05:15:40 -04:00
|
|
|
from __future__ import absolute_import
|
|
|
|
import mock
|
|
|
|
import unittest
|
|
|
|
from typing import Any
|
2017-07-18 00:01:54 -04:00
|
|
|
from .server_test_lib import BotServerTestCase
|
2017-06-18 05:15:40 -04:00
|
|
|
|
|
|
|
class BotServerTests(BotServerTestCase):
|
|
|
|
class MockMessageHandler(object):
|
2017-10-31 07:24:10 -04:00
|
|
|
def handle_message(self, message, bot_handler):
|
2017-06-18 05:15:40 -04:00
|
|
|
# type: (Any, Any, Any) -> None
|
|
|
|
assert message == {'key': "test message"}
|
|
|
|
|
|
|
|
class MockLibModule(object):
|
|
|
|
def handler_class(self):
|
|
|
|
# type: () -> Any
|
|
|
|
return BotServerTests.MockMessageHandler()
|
|
|
|
|
2017-07-18 00:01:54 -04:00
|
|
|
@mock.patch('zulip_botserver.server.ExternalBotHandler')
|
2017-06-18 05:15:40 -04:00
|
|
|
def test_successful_request(self, mock_ExternalBotHandler):
|
|
|
|
# type: (mock.Mock) -> None
|
|
|
|
available_bots = ['testbot']
|
|
|
|
bots_config = {
|
|
|
|
'testbot': {
|
|
|
|
'email': 'testbot-bot@zulip.com',
|
|
|
|
'key': '123456789qwertyuiop',
|
|
|
|
'site': 'http://localhost',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bots_lib_module = {
|
|
|
|
'testbot': BotServerTests.MockLibModule()
|
|
|
|
}
|
|
|
|
self.assert_bot_server_response(available_bots=available_bots,
|
|
|
|
bots_config=bots_config,
|
|
|
|
bots_lib_module=bots_lib_module,
|
|
|
|
check_success=True)
|
|
|
|
assert mock_ExternalBotHandler.called
|
|
|
|
|
2017-07-16 05:59:21 -04:00
|
|
|
def test_bot_module_not_exists(self):
|
2017-06-18 05:15:40 -04:00
|
|
|
# type: () -> None
|
2017-07-16 05:59:21 -04:00
|
|
|
self.assert_bot_server_response(bots_lib_module={},
|
2017-06-18 05:15:40 -04:00
|
|
|
payload_url="/bots/not_supported_bot",
|
|
|
|
check_success=False)
|
|
|
|
|
|
|
|
def test_wrong_bot_credentials(self):
|
|
|
|
# type: () -> None
|
|
|
|
available_bots = ['testbot']
|
|
|
|
bots_config = {
|
|
|
|
'testbot': {
|
|
|
|
'email': 'testbot-bot@zulip.com',
|
|
|
|
'key': '123456789qwertyuiop',
|
|
|
|
'site': 'http://localhost',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.assert_bot_server_response(available_bots=available_bots,
|
|
|
|
bots_config=bots_config,
|
|
|
|
check_success=False)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|