2017-06-18 05:15:40 -04:00
|
|
|
import mock
|
2018-05-17 12:05:41 -04:00
|
|
|
import os
|
2018-05-14 15:45:54 -04:00
|
|
|
from typing import Any, Dict
|
2017-06-18 05:15:40 -04:00
|
|
|
import unittest
|
2017-07-18 00:01:54 -04:00
|
|
|
from .server_test_lib import BotServerTestCase
|
2017-11-15 12:44:52 -05:00
|
|
|
import six
|
2018-05-17 12:05:41 -04:00
|
|
|
import json
|
2017-06-18 05:15:40 -04:00
|
|
|
|
2018-05-17 12:05:41 -04:00
|
|
|
from zulip_botserver import server
|
2018-05-17 11:41:31 -04:00
|
|
|
from zulip_botserver.input_parameters import parse_args
|
|
|
|
|
2018-05-15 10:55:58 -04:00
|
|
|
|
2017-06-18 05:15:40 -04:00
|
|
|
class BotServerTests(BotServerTestCase):
|
|
|
|
class MockMessageHandler(object):
|
2018-05-14 15:45:54 -04:00
|
|
|
def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None:
|
2017-06-18 05:15:40 -04:00
|
|
|
assert message == {'key': "test message"}
|
|
|
|
|
|
|
|
class MockLibModule(object):
|
2018-05-14 15:45:54 -04:00
|
|
|
def handler_class(self) -> Any:
|
2017-06-18 05:15:40 -04:00
|
|
|
return BotServerTests.MockMessageHandler()
|
|
|
|
|
2018-05-17 10:35:09 -04:00
|
|
|
@mock.patch('zulip_bots.lib.ExternalBotHandler')
|
2018-05-14 15:45:54 -04:00
|
|
|
def test_successful_request(self, mock_ExternalBotHandler: 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,
|
2018-05-22 07:46:40 -04:00
|
|
|
message=dict(message={'content': "test message"},
|
|
|
|
bot_email='helloworld-bot@zulip.com'),
|
2017-06-18 05:15:40 -04:00
|
|
|
check_success=True)
|
|
|
|
|
2018-05-17 10:35:09 -04:00
|
|
|
@mock.patch('zulip_bots.lib.ExternalBotHandler')
|
2018-05-16 12:33:01 -04:00
|
|
|
def test_successful_request_from_two_bots(self, mock_ExternalBotHandler: mock.Mock) -> None:
|
|
|
|
available_bots = ['helloworld', 'help']
|
|
|
|
bots_config = {
|
|
|
|
'helloworld': {
|
|
|
|
'email': 'helloworld-bot@zulip.com',
|
|
|
|
'key': '123456789qwertyuiop',
|
|
|
|
'site': 'http://localhost',
|
|
|
|
},
|
|
|
|
'help': {
|
|
|
|
'email': 'help-bot@zulip.com',
|
|
|
|
'key': '123456789qwertyuiop',
|
|
|
|
'site': 'http://localhost',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.assert_bot_server_response(available_bots=available_bots,
|
2018-05-22 07:46:40 -04:00
|
|
|
message=dict(message={'content': "test message"},
|
|
|
|
bot_email='helloworld-bot@zulip.com'),
|
2018-05-16 12:33:01 -04:00
|
|
|
bots_config=bots_config,
|
|
|
|
check_success=True)
|
|
|
|
|
2018-05-14 15:45:54 -04:00
|
|
|
def test_bot_module_not_exists(self) -> None:
|
2017-11-07 07:17:32 -05:00
|
|
|
self.assert_bot_server_response(available_bots=[],
|
2018-05-22 07:46:40 -04:00
|
|
|
message=dict(message={'content': "test message"},
|
|
|
|
bot_email='nonexistent-bot@zulip.com'),
|
2017-06-18 05:15:40 -04:00
|
|
|
check_success=False)
|
|
|
|
|
2017-11-07 06:57:47 -05:00
|
|
|
@mock.patch('logging.error')
|
2017-11-01 08:26:20 -04:00
|
|
|
@mock.patch('zulip_bots.lib.StateHandler')
|
2018-05-14 15:45:54 -04:00
|
|
|
def test_wrong_bot_credentials(self, mock_StateHandler: mock.Mock, mock_LoggingError: 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-15 12:44:52 -05:00
|
|
|
# TODO: The following passes mypy, though the six stubs don't match the
|
|
|
|
# unittest ones, so we could file a mypy bug to improve this.
|
|
|
|
six.assertRaisesRegex(self,
|
|
|
|
ImportError,
|
|
|
|
"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
|
|
|
|
2018-05-17 11:41:31 -04:00
|
|
|
@mock.patch('sys.argv', ['zulip-bot-server', '--config-file', '/foo/bar/baz.conf'])
|
|
|
|
def test_argument_parsing_defaults(self) -> None:
|
|
|
|
opts = parse_args()
|
|
|
|
assert opts.config_file == '/foo/bar/baz.conf'
|
|
|
|
assert opts.bot_name is None
|
|
|
|
assert opts.bot_config_file is None
|
|
|
|
assert opts.hostname == '127.0.0.1'
|
|
|
|
assert opts.port == 5002
|
|
|
|
|
2018-05-17 12:05:41 -04:00
|
|
|
def test_read_config_file(self) -> None:
|
2018-05-17 12:16:18 -04:00
|
|
|
with self.assertRaises(IOError):
|
|
|
|
server.read_config_file("nonexistentfile.conf")
|
2018-05-17 12:05:41 -04:00
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
2018-05-17 12:16:18 -04:00
|
|
|
bot_conf1 = server.read_config_file(os.path.join(current_dir, "test.conf"))
|
|
|
|
expected_config1 = {
|
2018-05-17 12:05:41 -04:00
|
|
|
'helloworld': {
|
|
|
|
'email': 'helloworld-bot@zulip.com',
|
|
|
|
'key': 'value',
|
|
|
|
'site': 'http://localhost',
|
|
|
|
},
|
|
|
|
'giphy': {
|
|
|
|
'email': 'giphy-bot@zulip.com',
|
|
|
|
'key': 'value2',
|
|
|
|
'site': 'http://localhost',
|
|
|
|
}
|
|
|
|
}
|
2018-05-17 12:16:18 -04:00
|
|
|
assert json.dumps(bot_conf1, sort_keys=True) == json.dumps(expected_config1, sort_keys=True)
|
|
|
|
bot_conf2 = server.read_config_file(os.path.join(current_dir, "test.conf"), "redefined_bot")
|
|
|
|
expected_config2 = {
|
|
|
|
'redefined_bot': {
|
|
|
|
'email': 'helloworld-bot@zulip.com',
|
|
|
|
'key': 'value',
|
|
|
|
'site': 'http://localhost',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert json.dumps(bot_conf2, sort_keys=True) == json.dumps(expected_config2, sort_keys=True)
|
2018-05-17 12:05:41 -04:00
|
|
|
|
2018-05-17 11:41:31 -04:00
|
|
|
|
2017-06-18 05:15:40 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|