mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
embedded bots: Add tests for verification of embedded bot services.
Add test to check if the embedded bot service being used is in the registry or not. Add test to check if the bot being added to the registry has a valid bot corresponding to it. Move 'get_bot_handler' to 'zerver/lib/bot_lib.py' as it is an independent function, not related to the 'EmbeddedBotWorker' class that it was previously a part of.
This commit is contained in:
committed by
Tim Abbott
parent
c13d466f68
commit
6f60c65a65
@@ -7,8 +7,10 @@ import signal
|
||||
import sys
|
||||
import time
|
||||
import re
|
||||
import importlib
|
||||
from zerver.lib.actions import internal_send_message
|
||||
from zerver.models import UserProfile
|
||||
from zerver.lib.integrations import EMBEDDED_BOTS
|
||||
|
||||
from six.moves import configparser
|
||||
|
||||
@@ -21,6 +23,16 @@ our_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
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)
|
||||
|
||||
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()
|
||||
|
||||
class EmbeddedBotHandler(object):
|
||||
def __init__(self, user_profile):
|
||||
# type: (UserProfile) -> None
|
||||
|
||||
@@ -18,6 +18,8 @@ from zerver.lib.test_classes import ZulipTestCase, UploadSerializeMixin
|
||||
from zerver.lib.test_helpers import (
|
||||
avatar_disk_path, get_test_image_file, tornado_redirected_to_list,
|
||||
)
|
||||
from zerver.lib.integrations import EMBEDDED_BOTS
|
||||
from zerver.lib.bot_lib import get_bot_handler
|
||||
|
||||
class BotTest(ZulipTestCase, UploadSerializeMixin):
|
||||
def assert_num_bots_equal(self, count):
|
||||
@@ -951,3 +953,14 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
|
||||
bot_info['payload_url'] = ujson.dumps('http://127.0.0.:5002/bots/followup')
|
||||
result = self.client_post("/json/bots", bot_info)
|
||||
self.assert_json_error(result, "Enter a valid URL.")
|
||||
|
||||
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
|
||||
# its class name as EncryptHandler.
|
||||
class_bot_handler = "<class 'zulip_bots.bots.{name}.{name}.{Name}Handler'>"
|
||||
for embedded_bot in EMBEDDED_BOTS:
|
||||
embedded_bot_handler = get_bot_handler(embedded_bot.name)
|
||||
bot_name = embedded_bot.name
|
||||
bot_handler_class_name = class_bot_handler.format(name=bot_name, Name=bot_name.title())
|
||||
self.assertEqual(str(type(embedded_bot_handler)), bot_handler_class_name)
|
||||
|
||||
@@ -41,7 +41,7 @@ from zerver.context_processors import common_context
|
||||
from zerver.lib.outgoing_webhook import do_rest_call, get_outgoing_webhook_service_handler
|
||||
from zerver.models import get_bot_services
|
||||
from zulip import Client
|
||||
from zerver.lib.bot_lib import EmbeddedBotHandler
|
||||
from zerver.lib.bot_lib import EmbeddedBotHandler, get_bot_handler
|
||||
|
||||
import os
|
||||
import sys
|
||||
@@ -476,12 +476,6 @@ class EmbeddedBotWorker(QueueProcessingWorker):
|
||||
# type: (UserProfile) -> EmbeddedBotHandler
|
||||
return EmbeddedBotHandler(user_profile)
|
||||
|
||||
def get_bot_handler(self, service):
|
||||
# type: (Service) -> Any
|
||||
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()
|
||||
|
||||
# TODO: Handle stateful bots properly
|
||||
def get_state_handler(self):
|
||||
# type: () -> StateHandler
|
||||
@@ -497,7 +491,7 @@ 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:
|
||||
self.get_bot_handler(service).handle_message(
|
||||
get_bot_handler(str(service.name)).handle_message(
|
||||
message=message,
|
||||
bot_handler=self.get_bot_api_client(user_profile),
|
||||
state_handler=self.get_state_handler())
|
||||
|
||||
Reference in New Issue
Block a user