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-28 11:50:55 -04:00
|
|
|
def test_successful_request(self) -> 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',
|
2018-05-30 05:29:13 -04:00
|
|
|
'token': 'abcd1234',
|
2017-06-18 05:15:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.assert_bot_server_response(available_bots=available_bots,
|
|
|
|
bots_config=bots_config,
|
2018-05-28 11:06:57 -04:00
|
|
|
event=dict(message={'content': "@**test** test message"},
|
|
|
|
bot_email='helloworld-bot@zulip.com',
|
2018-05-30 05:29:13 -04:00
|
|
|
trigger='mention',
|
|
|
|
token='abcd1234'),
|
2018-05-28 12:47:18 -04:00
|
|
|
expected_response="beep boop",
|
2017-06-18 05:15:40 -04:00
|
|
|
check_success=True)
|
|
|
|
|
2018-05-28 11:50:55 -04:00
|
|
|
def test_successful_request_from_two_bots(self) -> None:
|
2018-05-16 12:33:01 -04:00
|
|
|
available_bots = ['helloworld', 'help']
|
|
|
|
bots_config = {
|
|
|
|
'helloworld': {
|
|
|
|
'email': 'helloworld-bot@zulip.com',
|
|
|
|
'key': '123456789qwertyuiop',
|
|
|
|
'site': 'http://localhost',
|
2018-05-30 05:29:13 -04:00
|
|
|
'token': 'abcd1234',
|
2018-05-16 12:33:01 -04:00
|
|
|
},
|
|
|
|
'help': {
|
|
|
|
'email': 'help-bot@zulip.com',
|
|
|
|
'key': '123456789qwertyuiop',
|
|
|
|
'site': 'http://localhost',
|
2018-05-30 05:29:13 -04:00
|
|
|
'token': 'abcd1234',
|
2018-05-16 12:33:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.assert_bot_server_response(available_bots=available_bots,
|
2018-05-28 11:06:57 -04:00
|
|
|
event=dict(message={'content': "@**test** test message"},
|
|
|
|
bot_email='helloworld-bot@zulip.com',
|
2018-05-30 05:29:13 -04:00
|
|
|
trigger='mention',
|
|
|
|
token='abcd1234'),
|
2018-05-28 12:47:18 -04:00
|
|
|
expected_response="beep boop",
|
2018-05-16 12:33:01 -04:00
|
|
|
bots_config=bots_config,
|
|
|
|
check_success=True)
|
|
|
|
|
2018-05-29 03:52:14 -04:00
|
|
|
def test_request_for_unkown_bot(self) -> None:
|
|
|
|
bots_config = {
|
|
|
|
'helloworld': {
|
|
|
|
'email': 'helloworld-bot@zulip.com',
|
|
|
|
'key': '123456789qwertyuiop',
|
|
|
|
'site': 'http://localhost',
|
2018-05-30 05:29:13 -04:00
|
|
|
'token': 'abcd1234',
|
2018-05-29 03:52:14 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
self.assert_bot_server_response(available_bots=['helloworld'],
|
2018-05-28 11:39:03 -04:00
|
|
|
event=dict(message={'content': "test message"},
|
2018-05-29 03:52:14 -04:00
|
|
|
bot_email='unknown-bot@zulip.com'),
|
|
|
|
bots_config=bots_config,
|
2017-06-18 05:15:40 -04:00
|
|
|
check_success=False)
|
|
|
|
|
2018-05-30 05:29:13 -04:00
|
|
|
def test_wrong_bot_token(self) -> None:
|
|
|
|
available_bots = ['helloworld']
|
|
|
|
bots_config = {
|
|
|
|
'helloworld': {
|
|
|
|
'email': 'helloworld-bot@zulip.com',
|
|
|
|
'key': '123456789qwertyuiop',
|
|
|
|
'site': 'http://localhost',
|
|
|
|
'token': 'abcd1234',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.assert_bot_server_response(available_bots=available_bots,
|
|
|
|
bots_config=bots_config,
|
|
|
|
event=dict(message={'content': "@**test** test message"},
|
|
|
|
bot_email='helloworld-bot@zulip.com',
|
|
|
|
trigger='mention',
|
|
|
|
token='wrongtoken'),
|
|
|
|
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',
|
2018-05-30 05:29:13 -04:00
|
|
|
'token': 'abcd1234',
|
2017-06-18 05:15:40 -04:00
|
|
|
}
|
|
|
|
}
|
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 "
|
2018-05-29 04:19:50 -04:00
|
|
|
"make sure you have set up the botserverrc file correctly.",
|
2018-05-28 11:26:02 -04:00
|
|
|
lambda: self.assert_bot_server_response(
|
|
|
|
available_bots=available_bots,
|
2018-05-28 11:06:57 -04:00
|
|
|
event=dict(message={'content': "@**test** test message"},
|
|
|
|
bot_email='helloworld-bot@zulip.com',
|
2018-05-30 05:29:13 -04:00
|
|
|
trigger='mention',
|
|
|
|
token='abcd1234'),
|
2018-05-28 11:26:02 -04:00
|
|
|
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',
|
2018-05-30 05:29:13 -04:00
|
|
|
'token': 'abcd1234',
|
2018-05-17 12:05:41 -04:00
|
|
|
},
|
|
|
|
'giphy': {
|
|
|
|
'email': 'giphy-bot@zulip.com',
|
|
|
|
'key': 'value2',
|
|
|
|
'site': 'http://localhost',
|
2018-05-30 05:29:13 -04:00
|
|
|
'token': 'abcd1234',
|
2018-05-17 12:05:41 -04:00
|
|
|
}
|
|
|
|
}
|
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',
|
2018-05-30 05:29:13 -04:00
|
|
|
'token': 'abcd1234',
|
2018-05-17 12:16:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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()
|