Skip to content

Commit

Permalink
bot_server: Add support for running botserver from bots registry.
Browse files Browse the repository at this point in the history
  • Loading branch information
PIG208 committed Jul 22, 2021
1 parent 8c907aa commit ad578c0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
33 changes: 33 additions & 0 deletions zulip_botserver/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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
Expand Down Expand Up @@ -273,6 +274,38 @@ def test_load_lib_modules(self) -> None:
).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": "[email protected]",
"key": "value",
"site": "http://localhost",
"token": "abcd1234",
}
}

with mock.patch("zulip_botserver.server.read_config_file", return_value=bots_config):
with mock.patch("zulip_botserver.server.lib.ExternalBotHandler", new=mock.Mock()):
with mock.patch(
"zulip_bots.finder.metadata.EntryPoint.load",
return_value=packaged_bot_module,
):
with 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()
20 changes: 11 additions & 9 deletions zulip_botserver/zulip_botserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

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


Expand Down Expand Up @@ -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 = (
'Error: Bot "{}" doesn\'t exist. Please make sure '
"you have set up the botserverrc file correctly.\n".format(bot)
)
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


Expand Down

0 comments on commit ad578c0

Please sign in to comment.