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:
Abhijeet Kaur
2017-07-25 22:33:09 +05:30
committed by Tim Abbott
parent 1ae02cce97
commit 1deb58b178
3 changed files with 21 additions and 4 deletions

View File

@@ -26,9 +26,10 @@ from zulip_bots.lib import RateLimit
def get_bot_handler(service_name):
# type: (str) -> Any
# Assert that this service is present in EMBEDDED_BOTS.
assert any(service_name == embedded_bot_service.name for embedded_bot_service in EMBEDDED_BOTS)
# Check that this service is present in EMBEDDED_BOTS, add exception handling.
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 = importlib.import_module(bot_module_name) # type: Any
return bot_module.handler_class()

View File

@@ -954,6 +954,18 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
result = self.client_post("/json/bots", bot_info)
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):
# type: () -> None
# Each bot has its bot handler class name as Bot_nameHandler. For instance encrypt bot has

View File

@@ -491,7 +491,11 @@ class EmbeddedBotWorker(QueueProcessingWorker):
# TODO: Do we actually want to allow multiple Services per bot user?
services = get_bot_services(user_profile_id)
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,
bot_handler=self.get_bot_api_client(user_profile),
state_handler=self.get_state_handler())