2021-05-28 05:00:04 -04:00
|
|
|
import json
|
2018-05-17 12:05:41 -04:00
|
|
|
import os
|
2017-06-18 05:15:40 -04:00
|
|
|
import unittest
|
2021-01-05 04:09:48 -05:00
|
|
|
from collections import OrderedDict
|
2019-08-27 19:19:29 -04:00
|
|
|
from importlib import import_module
|
2021-03-02 03:52:12 -05:00
|
|
|
from pathlib import Path
|
2021-05-28 05:00:04 -04:00
|
|
|
from types import ModuleType
|
|
|
|
from typing import Any, Dict
|
2021-05-28 07:19:40 -04:00
|
|
|
from unittest import mock
|
2017-06-18 05:15:40 -04:00
|
|
|
|
2021-07-22 01:04:54 -04:00
|
|
|
from zulip_bots.finder import metadata
|
2021-05-28 05:00:04 -04:00
|
|
|
from zulip_bots.lib import BotHandler
|
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
|
|
|
|
|
2021-05-28 05:00:04 -04:00
|
|
|
from .server_test_lib import BotServerTestCase
|
|
|
|
|
2018-05-15 10:55:58 -04:00
|
|
|
|
2017-06-18 05:15:40 -04:00
|
|
|
class BotServerTests(BotServerTestCase):
|
2020-04-09 20:14:01 -04:00
|
|
|
class MockMessageHandler:
|
2021-03-03 14:54:28 -05:00
|
|
|
def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None:
|
2021-05-28 05:05:11 -04:00
|
|
|
assert message == {"key": "test message"}
|
2017-06-18 05:15:40 -04:00
|
|
|
|
2020-04-09 20:14:01 -04:00
|
|
|
class MockLibModule:
|
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:
|
2021-05-28 05:05:11 -04:00
|
|
|
available_bots = ["helloworld"]
|
2017-06-18 05:15:40 -04:00
|
|
|
bots_config = {
|
2021-05-28 05:05:11 -04:00
|
|
|
"helloworld": {
|
|
|
|
"email": "helloworld-bot@zulip.com",
|
|
|
|
"key": "123456789qwertyuiop",
|
|
|
|
"site": "http://localhost",
|
|
|
|
"token": "abcd1234",
|
2017-06-18 05:15:40 -04:00
|
|
|
}
|
|
|
|
}
|
2021-05-28 05:03:46 -04:00
|
|
|
self.assert_bot_server_response(
|
|
|
|
available_bots=available_bots,
|
|
|
|
bots_config=bots_config,
|
|
|
|
event=dict(
|
2021-05-28 05:05:11 -04:00
|
|
|
message={"content": "@**test** test message"},
|
|
|
|
bot_email="helloworld-bot@zulip.com",
|
|
|
|
trigger="mention",
|
|
|
|
token="abcd1234",
|
2021-05-28 05:03:46 -04:00
|
|
|
),
|
|
|
|
expected_response="beep boop",
|
|
|
|
check_success=True,
|
|
|
|
)
|
2017-06-18 05:15:40 -04:00
|
|
|
|
2018-05-28 11:50:55 -04:00
|
|
|
def test_successful_request_from_two_bots(self) -> None:
|
2021-05-28 05:05:11 -04:00
|
|
|
available_bots = ["helloworld", "help"]
|
2018-05-16 12:33:01 -04:00
|
|
|
bots_config = {
|
2021-05-28 05:05:11 -04:00
|
|
|
"helloworld": {
|
|
|
|
"email": "helloworld-bot@zulip.com",
|
|
|
|
"key": "123456789qwertyuiop",
|
|
|
|
"site": "http://localhost",
|
|
|
|
"token": "abcd1234",
|
2018-05-16 12:33:01 -04:00
|
|
|
},
|
2021-05-28 05:05:11 -04:00
|
|
|
"help": {
|
|
|
|
"email": "help-bot@zulip.com",
|
|
|
|
"key": "123456789qwertyuiop",
|
|
|
|
"site": "http://localhost",
|
|
|
|
"token": "abcd1234",
|
2021-05-28 05:03:46 -04:00
|
|
|
},
|
2018-05-16 12:33:01 -04:00
|
|
|
}
|
2021-05-28 05:03:46 -04:00
|
|
|
self.assert_bot_server_response(
|
|
|
|
available_bots=available_bots,
|
|
|
|
event=dict(
|
2021-05-28 05:05:11 -04:00
|
|
|
message={"content": "@**test** test message"},
|
|
|
|
bot_email="helloworld-bot@zulip.com",
|
|
|
|
trigger="mention",
|
|
|
|
token="abcd1234",
|
2021-05-28 05:03:46 -04:00
|
|
|
),
|
|
|
|
expected_response="beep boop",
|
|
|
|
bots_config=bots_config,
|
|
|
|
check_success=True,
|
|
|
|
)
|
2018-05-16 12:33:01 -04:00
|
|
|
|
2018-05-29 03:52:14 -04:00
|
|
|
def test_request_for_unkown_bot(self) -> None:
|
|
|
|
bots_config = {
|
2021-05-28 05:05:11 -04:00
|
|
|
"helloworld": {
|
|
|
|
"email": "helloworld-bot@zulip.com",
|
|
|
|
"key": "123456789qwertyuiop",
|
|
|
|
"site": "http://localhost",
|
|
|
|
"token": "abcd1234",
|
2018-05-29 03:52:14 -04:00
|
|
|
},
|
|
|
|
}
|
2021-05-28 05:03:46 -04:00
|
|
|
self.assert_bot_server_response(
|
2021-05-28 05:05:11 -04:00
|
|
|
available_bots=["helloworld"],
|
|
|
|
event=dict(message={"content": "test message"}, bot_email="unknown-bot@zulip.com"),
|
2021-05-28 05:03:46 -04:00
|
|
|
bots_config=bots_config,
|
|
|
|
check_success=False,
|
|
|
|
)
|
2017-06-18 05:15:40 -04:00
|
|
|
|
2018-05-30 05:29:13 -04:00
|
|
|
def test_wrong_bot_token(self) -> None:
|
2021-05-28 05:05:11 -04:00
|
|
|
available_bots = ["helloworld"]
|
2018-05-30 05:29:13 -04:00
|
|
|
bots_config = {
|
2021-05-28 05:05:11 -04:00
|
|
|
"helloworld": {
|
|
|
|
"email": "helloworld-bot@zulip.com",
|
|
|
|
"key": "123456789qwertyuiop",
|
|
|
|
"site": "http://localhost",
|
|
|
|
"token": "abcd1234",
|
2018-05-30 05:29:13 -04:00
|
|
|
}
|
|
|
|
}
|
2021-05-28 05:03:46 -04:00
|
|
|
self.assert_bot_server_response(
|
|
|
|
available_bots=available_bots,
|
|
|
|
bots_config=bots_config,
|
|
|
|
event=dict(
|
2021-05-28 05:05:11 -04:00
|
|
|
message={"content": "@**test** test message"},
|
|
|
|
bot_email="helloworld-bot@zulip.com",
|
|
|
|
trigger="mention",
|
|
|
|
token="wrongtoken",
|
2021-05-28 05:03:46 -04:00
|
|
|
),
|
|
|
|
check_success=False,
|
|
|
|
)
|
2018-05-30 05:29:13 -04:00
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
@mock.patch("logging.error")
|
|
|
|
@mock.patch("zulip_bots.lib.StateHandler")
|
2021-05-28 05:03:46 -04:00
|
|
|
def test_wrong_bot_credentials(
|
|
|
|
self, mock_StateHandler: mock.Mock, mock_LoggingError: mock.Mock
|
|
|
|
) -> None:
|
2021-05-28 05:05:11 -04:00
|
|
|
available_bots = ["nonexistent-bot"]
|
2017-06-18 05:15:40 -04:00
|
|
|
bots_config = {
|
2021-05-28 05:05:11 -04:00
|
|
|
"nonexistent-bot": {
|
|
|
|
"email": "helloworld-bot@zulip.com",
|
|
|
|
"key": "123456789qwertyuiop",
|
|
|
|
"site": "http://localhost",
|
|
|
|
"token": "abcd1234",
|
2017-06-18 05:15:40 -04:00
|
|
|
}
|
|
|
|
}
|
2021-08-24 20:20:51 -04:00
|
|
|
with self.assertRaisesRegex(
|
2021-05-28 05:03:46 -04:00
|
|
|
SystemExit,
|
|
|
|
'Error: Bot "nonexistent-bot" doesn\'t exist. Please make '
|
2021-05-28 05:05:11 -04:00
|
|
|
"sure you have set up the botserverrc file correctly.",
|
2021-05-28 05:03:46 -04:00
|
|
|
):
|
2018-06-01 08:26:20 -04:00
|
|
|
self.assert_bot_server_response(
|
|
|
|
available_bots=available_bots,
|
2021-05-28 05:03:46 -04:00
|
|
|
event=dict(
|
2021-05-28 05:05:11 -04:00
|
|
|
message={"content": "@**test** test message"},
|
|
|
|
bot_email="helloworld-bot@zulip.com",
|
|
|
|
trigger="mention",
|
|
|
|
token="abcd1234",
|
2021-05-28 05:03:46 -04:00
|
|
|
),
|
|
|
|
bots_config=bots_config,
|
|
|
|
)
|
2017-06-18 05:15:40 -04:00
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
@mock.patch("sys.argv", ["zulip-botserver", "--config-file", "/foo/bar/baz.conf"])
|
2018-05-17 11:41:31 -04:00
|
|
|
def test_argument_parsing_defaults(self) -> None:
|
|
|
|
opts = parse_args()
|
2021-05-28 05:05:11 -04:00
|
|
|
assert opts.config_file == "/foo/bar/baz.conf"
|
2018-05-17 11:41:31 -04:00
|
|
|
assert opts.bot_name is None
|
|
|
|
assert opts.bot_config_file is None
|
2021-05-28 05:05:11 -04:00
|
|
|
assert opts.hostname == "127.0.0.1"
|
2018-05-17 11:41:31 -04:00
|
|
|
assert opts.port == 5002
|
|
|
|
|
2021-01-05 04:09:48 -05:00
|
|
|
def test_read_config_from_env_vars(self) -> None:
|
|
|
|
# We use an OrderedDict so that the order of the entries in
|
|
|
|
# the stringified environment variable is standard even on
|
|
|
|
# Python 3.7 and earlier.
|
|
|
|
bots_config = OrderedDict()
|
2021-05-28 05:05:11 -04:00
|
|
|
bots_config["hello_world"] = {
|
|
|
|
"email": "helloworld-bot@zulip.com",
|
|
|
|
"key": "value",
|
|
|
|
"site": "http://localhost",
|
|
|
|
"token": "abcd1234",
|
2021-01-05 04:09:48 -05:00
|
|
|
}
|
2021-05-28 05:05:11 -04:00
|
|
|
bots_config["giphy"] = {
|
|
|
|
"email": "giphy-bot@zulip.com",
|
|
|
|
"key": "value2",
|
|
|
|
"site": "http://localhost",
|
|
|
|
"token": "abcd1234",
|
2021-01-05 04:09:48 -05:00
|
|
|
}
|
2021-05-28 05:05:11 -04:00
|
|
|
os.environ["ZULIP_BOTSERVER_CONFIG"] = json.dumps(bots_config)
|
2021-01-05 04:09:48 -05:00
|
|
|
|
|
|
|
# No bot specified; should read all bot configs
|
|
|
|
assert server.read_config_from_env_vars() == bots_config
|
|
|
|
|
|
|
|
# Specified bot exists; should read only that section.
|
2021-05-28 05:05:11 -04:00
|
|
|
assert server.read_config_from_env_vars("giphy") == {"giphy": bots_config["giphy"]}
|
2021-01-05 04:09:48 -05:00
|
|
|
|
|
|
|
# Specified bot doesn't exist; should read the first section of the config.
|
2021-05-28 05:03:46 -04:00
|
|
|
assert server.read_config_from_env_vars("redefined_bot") == {
|
2021-05-28 05:05:11 -04:00
|
|
|
"redefined_bot": bots_config["hello_world"]
|
2021-05-28 05:03:46 -04:00
|
|
|
}
|
2021-01-05 04:09:48 -05:00
|
|
|
|
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-07-27 04:11:18 -04:00
|
|
|
|
|
|
|
# No bot specified; should read all bot configs.
|
2018-05-17 12:16:18 -04:00
|
|
|
bot_conf1 = server.read_config_file(os.path.join(current_dir, "test.conf"))
|
|
|
|
expected_config1 = {
|
2021-05-28 05:05:11 -04:00
|
|
|
"helloworld": {
|
|
|
|
"email": "helloworld-bot@zulip.com",
|
|
|
|
"key": "value",
|
|
|
|
"site": "http://localhost",
|
|
|
|
"token": "abcd1234",
|
2018-05-17 12:05:41 -04:00
|
|
|
},
|
2021-05-28 05:05:11 -04:00
|
|
|
"giphy": {
|
|
|
|
"email": "giphy-bot@zulip.com",
|
|
|
|
"key": "value2",
|
|
|
|
"site": "http://localhost",
|
|
|
|
"token": "abcd1234",
|
2021-05-28 05:03:46 -04:00
|
|
|
},
|
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)
|
2018-07-27 04:11:18 -04:00
|
|
|
|
|
|
|
# Specified bot exists; should read only that section.
|
|
|
|
bot_conf3 = server.read_config_file(os.path.join(current_dir, "test.conf"), "giphy")
|
|
|
|
expected_config3 = {
|
2021-05-28 05:05:11 -04:00
|
|
|
"giphy": {
|
|
|
|
"email": "giphy-bot@zulip.com",
|
|
|
|
"key": "value2",
|
|
|
|
"site": "http://localhost",
|
|
|
|
"token": "abcd1234",
|
2018-07-27 04:11:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert json.dumps(bot_conf3, sort_keys=True) == json.dumps(expected_config3, sort_keys=True)
|
|
|
|
|
|
|
|
# Specified bot doesn't exist; should read the first section of the config.
|
2018-05-17 12:16:18 -04:00
|
|
|
bot_conf2 = server.read_config_file(os.path.join(current_dir, "test.conf"), "redefined_bot")
|
|
|
|
expected_config2 = {
|
2021-05-28 05:05:11 -04:00
|
|
|
"redefined_bot": {
|
|
|
|
"email": "helloworld-bot@zulip.com",
|
|
|
|
"key": "value",
|
|
|
|
"site": "http://localhost",
|
|
|
|
"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
|
|
|
|
2019-08-27 19:19:29 -04:00
|
|
|
def test_load_lib_modules(self) -> None:
|
|
|
|
# This testcase requires hardcoded paths, which here is a good thing so if we ever
|
|
|
|
# restructure zulip_bots, this test would fail and we would also update Botserver
|
|
|
|
# at the same time.
|
2021-05-28 05:05:11 -04:00
|
|
|
helloworld = import_module("zulip_bots.bots.{bot}.{bot}".format(bot="helloworld"))
|
2021-03-02 03:52:12 -05:00
|
|
|
root_dir = Path(__file__).parents[2].as_posix()
|
2019-08-27 19:19:29 -04:00
|
|
|
# load valid module name
|
2021-05-28 05:05:11 -04:00
|
|
|
module = server.load_lib_modules(["helloworld"])["helloworld"]
|
2019-08-27 19:19:29 -04:00
|
|
|
assert module == helloworld
|
|
|
|
|
|
|
|
# load valid file path
|
2021-05-28 05:03:46 -04:00
|
|
|
path = Path(
|
2021-05-28 05:05:11 -04:00
|
|
|
root_dir, "zulip_bots/zulip_bots/bots/{bot}/{bot}.py".format(bot="helloworld")
|
2021-05-28 05:03:46 -04:00
|
|
|
).as_posix()
|
2019-08-27 19:19:29 -04:00
|
|
|
module = server.load_lib_modules([path])[path]
|
2021-05-28 05:05:11 -04:00
|
|
|
assert module.__name__ == "custom_bot_module"
|
2019-08-27 19:19:29 -04:00
|
|
|
assert module.__file__ == path
|
|
|
|
assert isinstance(module, ModuleType)
|
|
|
|
|
|
|
|
# load invalid module name
|
2021-08-24 20:20:51 -04:00
|
|
|
with self.assertRaisesRegex(
|
2021-05-28 05:03:46 -04:00
|
|
|
SystemExit,
|
|
|
|
'Error: Bot "botserver-test-case-random-bot" doesn\'t exist. '
|
2021-05-28 05:05:11 -04:00
|
|
|
"Please make sure you have set up the botserverrc file correctly.",
|
2021-05-28 05:03:46 -04:00
|
|
|
):
|
2021-05-28 05:05:11 -04:00
|
|
|
module = server.load_lib_modules(["botserver-test-case-random-bot"])[
|
|
|
|
"botserver-test-case-random-bot"
|
2021-05-28 05:03:46 -04:00
|
|
|
]
|
2019-08-27 19:19:29 -04:00
|
|
|
|
|
|
|
# load invalid file path
|
2021-08-24 20:20:51 -04:00
|
|
|
with self.assertRaisesRegex(
|
2021-05-28 05:03:46 -04:00
|
|
|
SystemExit,
|
|
|
|
'Error: Bot "{}/zulip_bots/zulip_bots/bots/helloworld.py" doesn\'t exist. '
|
2021-05-28 05:05:11 -04:00
|
|
|
"Please make sure you have set up the botserverrc file correctly.".format(root_dir),
|
2021-05-28 05:03:46 -04:00
|
|
|
):
|
|
|
|
path = Path(
|
2021-05-28 05:05:11 -04:00
|
|
|
root_dir, "zulip_bots/zulip_bots/bots/{bot}.py".format(bot="helloworld")
|
2021-05-28 05:03:46 -04:00
|
|
|
).as_posix()
|
2019-08-27 19:19:29 -04:00
|
|
|
module = server.load_lib_modules([path])[path]
|
|
|
|
|
2021-07-22 01:04:54 -04:00
|
|
|
@mock.patch("zulip_botserver.server.app")
|
|
|
|
@mock.patch("sys.argv", ["zulip-botserver", "--config-file", "/foo/bar/baz.conf"])
|
|
|
|
def test_load_from_registry(self, mock_app: mock.Mock) -> None:
|
|
|
|
packaged_bot_module = mock.MagicMock(__version__="1.0.0", __file__="asd")
|
|
|
|
packaged_bot_entrypoint = metadata.EntryPoint(
|
|
|
|
"packaged_bot", "module_name", "zulip_bots.registry"
|
|
|
|
)
|
|
|
|
bots_config = {
|
|
|
|
"packaged_bot": {
|
|
|
|
"email": "packaged-bot@zulip.com",
|
|
|
|
"key": "value",
|
|
|
|
"site": "http://localhost",
|
|
|
|
"token": "abcd1234",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
with mock.patch(
|
|
|
|
"zulip_botserver.server.read_config_file", return_value=bots_config
|
|
|
|
), mock.patch("zulip_botserver.server.lib.ExternalBotHandler", new=mock.Mock()), mock.patch(
|
|
|
|
"zulip_bots.finder.metadata.EntryPoint.load",
|
|
|
|
return_value=packaged_bot_module,
|
|
|
|
), mock.patch(
|
|
|
|
"zulip_bots.finder.metadata.entry_points",
|
|
|
|
return_value=(packaged_bot_entrypoint,),
|
|
|
|
):
|
|
|
|
server.main()
|
|
|
|
|
|
|
|
mock_app.config.__setitem__.assert_any_call(
|
|
|
|
"BOTS_LIB_MODULES", {"packaged_bot": packaged_bot_module}
|
|
|
|
)
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
if __name__ == "__main__":
|
2017-06-18 05:15:40 -04:00
|
|
|
unittest.main()
|