2017-06-18 05:15:40 -04:00
|
|
|
from __future__ import absolute_import
|
|
|
|
import mock
|
|
|
|
import unittest
|
|
|
|
from typing import Any
|
2017-11-07 06:57:47 -05:00
|
|
|
from zulip_botserver import server
|
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
|
2017-11-07 07:17:32 -05:00
|
|
|
available_bots = ['helloworld']
|
2017-06-18 05:15:40 -04:00
|
|
|
bots_config = {
|
2017-11-07 07:17:32 -05:00
|
|
|
'helloworld': {
|
|
|
|
'email': 'helloworld-bot@zulip.com',
|
2017-06-18 05:15:40 -04:00
|
|
|
'key': '123456789qwertyuiop',
|
|
|
|
'site': 'http://localhost',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.assert_bot_server_response(available_bots=available_bots,
|
|
|
|
bots_config=bots_config,
|
|
|
|
check_success=True)
|
|
|
|
|
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-11-07 07:17:32 -05:00
|
|
|
self.assert_bot_server_response(available_bots=[],
|
2017-06-18 05:15:40 -04:00
|
|
|
payload_url="/bots/not_supported_bot",
|
|
|
|
check_success=False)
|
|
|
|
|
2017-11-07 06:57:47 -05:00
|
|
|
@mock.patch('logging.error')
|
|
|
|
def test_wrong_bot_credentials(self, mock_LoggingError):
|
|
|
|
# type: (mock.Mock) -> None
|
2017-11-07 08:57:52 -05:00
|
|
|
available_bots = ['nonexistent-bot']
|
2017-06-18 05:15:40 -04:00
|
|
|
bots_config = {
|
2017-11-07 08:57:52 -05:00
|
|
|
'nonexistent-bot': {
|
2017-11-07 07:17:32 -05:00
|
|
|
'email': 'helloworld-bot@zulip.com',
|
2017-06-18 05:15:40 -04:00
|
|
|
'key': '123456789qwertyuiop',
|
|
|
|
'site': 'http://localhost',
|
|
|
|
}
|
|
|
|
}
|
2017-11-07 08:57:52 -05:00
|
|
|
# This complains about mismatching argument types, yet they are all correct?
|
|
|
|
# We should investigate and file an issue in mypy.
|
|
|
|
self.assertRaisesRegexp(ImportError, # type: ignore
|
|
|
|
"Bot \"nonexistent-bot\" doesn't exists. Please "
|
|
|
|
"make sure you have set up the flaskbotrc file correctly.",
|
|
|
|
lambda: self.assert_bot_server_response(available_bots=available_bots,
|
|
|
|
bots_config=bots_config))
|
2017-06-18 05:15:40 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|