mirror of
https://github.com/zulip/zulip.git
synced 2025-11-10 00:46:03 +00:00
bots: Add complete test-coverage for bot_lib.py file.
Also, add error handling for get_bot_handler instead of throwing an assertion error.
This commit is contained in:
committed by
Tim Abbott
parent
1ae02cce97
commit
1deb58b178
@@ -26,9 +26,10 @@ from zulip_bots.lib import RateLimit
|
|||||||
def get_bot_handler(service_name):
|
def get_bot_handler(service_name):
|
||||||
# type: (str) -> Any
|
# type: (str) -> Any
|
||||||
|
|
||||||
# Assert that this service is present in EMBEDDED_BOTS.
|
# Check that this service is present in EMBEDDED_BOTS, add exception handling.
|
||||||
assert any(service_name == embedded_bot_service.name for embedded_bot_service in EMBEDDED_BOTS)
|
is_present_in_registry = any(service_name == embedded_bot_service.name for embedded_bot_service in EMBEDDED_BOTS)
|
||||||
|
if not is_present_in_registry:
|
||||||
|
return None
|
||||||
bot_module_name = 'zulip_bots.bots.%s.%s' % (service_name, service_name)
|
bot_module_name = 'zulip_bots.bots.%s.%s' % (service_name, service_name)
|
||||||
bot_module = importlib.import_module(bot_module_name) # type: Any
|
bot_module = importlib.import_module(bot_module_name) # type: Any
|
||||||
return bot_module.handler_class()
|
return bot_module.handler_class()
|
||||||
|
|||||||
@@ -954,6 +954,18 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
|
|||||||
result = self.client_post("/json/bots", bot_info)
|
result = self.client_post("/json/bots", bot_info)
|
||||||
self.assert_json_error(result, "Enter a valid URL.")
|
self.assert_json_error(result, "Enter a valid URL.")
|
||||||
|
|
||||||
|
def test_get_bot_handler(self):
|
||||||
|
# type: () -> None
|
||||||
|
# Test for valid service.
|
||||||
|
test_service_name = 'converter'
|
||||||
|
test_bot_handler = get_bot_handler(test_service_name)
|
||||||
|
self.assertEqual(str(type(test_bot_handler)), "<class 'zulip_bots.bots.converter.converter.ConverterHandler'>")
|
||||||
|
|
||||||
|
# Test for invalid service.
|
||||||
|
test_service_name = "incorrect_bot_service_foo"
|
||||||
|
test_bot_handler = get_bot_handler(test_service_name)
|
||||||
|
self.assertEqual(test_bot_handler, None)
|
||||||
|
|
||||||
def test_if_each_embedded_bot_service_exists(self):
|
def test_if_each_embedded_bot_service_exists(self):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
# Each bot has its bot handler class name as Bot_nameHandler. For instance encrypt bot has
|
# Each bot has its bot handler class name as Bot_nameHandler. For instance encrypt bot has
|
||||||
|
|||||||
@@ -491,7 +491,11 @@ class EmbeddedBotWorker(QueueProcessingWorker):
|
|||||||
# TODO: Do we actually want to allow multiple Services per bot user?
|
# TODO: Do we actually want to allow multiple Services per bot user?
|
||||||
services = get_bot_services(user_profile_id)
|
services = get_bot_services(user_profile_id)
|
||||||
for service in services:
|
for service in services:
|
||||||
get_bot_handler(str(service.name)).handle_message(
|
bot_handler = get_bot_handler(str(service.name))
|
||||||
|
if bot_handler is None:
|
||||||
|
logging.error("Error: User %s has bot with invalid embedded bot service %s" % (user_profile_id, service.name))
|
||||||
|
continue
|
||||||
|
bot_handler.handle_message(
|
||||||
message=message,
|
message=message,
|
||||||
bot_handler=self.get_bot_api_client(user_profile),
|
bot_handler=self.get_bot_api_client(user_profile),
|
||||||
state_handler=self.get_state_handler())
|
state_handler=self.get_state_handler())
|
||||||
|
|||||||
Reference in New Issue
Block a user