bot_server: Add support for running botserver from bots registry.
This commit is contained in:
parent
745f2cd173
commit
285a946a1f
|
@ -8,6 +8,7 @@ from types import ModuleType
|
|||
from typing import Any, Dict
|
||||
from unittest import mock
|
||||
|
||||
from zulip_bots.finder import metadata
|
||||
from zulip_bots.lib import BotHandler
|
||||
from zulip_botserver import server
|
||||
from zulip_botserver.input_parameters import parse_args
|
||||
|
@ -273,6 +274,37 @@ class BotServerTests(BotServerTestCase):
|
|||
).as_posix()
|
||||
module = server.load_lib_modules([path])[path]
|
||||
|
||||
@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}
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
@ -16,7 +16,7 @@ from werkzeug.exceptions import BadRequest, Unauthorized
|
|||
|
||||
from zulip import Client
|
||||
from zulip_bots import lib
|
||||
from zulip_bots.finder import import_module_from_source
|
||||
from zulip_bots.finder import import_module_from_source, import_module_from_zulip_bot_registry
|
||||
from zulip_botserver.input_parameters import parse_args
|
||||
|
||||
|
||||
|
@ -122,15 +122,17 @@ def load_lib_modules(available_bots: List[str]) -> Dict[str, ModuleType]:
|
|||
lib_module = import_module(module_name)
|
||||
bots_lib_module[bot] = lib_module
|
||||
except ImportError:
|
||||
error_message = (
|
||||
'Error: Bot "{}" doesn\'t exist. Please make sure '
|
||||
"you have set up the botserverrc file correctly.\n".format(bot)
|
||||
)
|
||||
if bot == "api":
|
||||
error_message += (
|
||||
"Did you forget to specify the bot you want to run with -b <botname> ?"
|
||||
_, bots_lib_module[bot] = import_module_from_zulip_bot_registry(bot)
|
||||
if bots_lib_module[bot] is None:
|
||||
error_message = (
|
||||
f'Error: Bot "{bot}" doesn\'t exist. Please make sure '
|
||||
"you have set up the botserverrc file correctly.\n"
|
||||
)
|
||||
sys.exit(error_message)
|
||||
if bot == "api":
|
||||
error_message += (
|
||||
"Did you forget to specify the bot you want to run with -b <botname> ?"
|
||||
)
|
||||
sys.exit(error_message)
|
||||
return bots_lib_module
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue