Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

users: Split bot config validation by bot type.

Niloth P7da4f314b51a
diff --git a/zerver/lib/users.py b/zerver/lib/users.py
index 1849328ab92..f8155508360 100644
--- a/zerver/lib/users.py
+++ b/zerver/lib/users.py
@@ -131,57 +131,55 @@ def validate_short_name_and_construct_bot_email(
return short_name, email
-def check_valid_bot_config(
- bot_type: int, service_name: str, config_data: Mapping[str, str]
+def check_valid_incoming_webhook_bot_config(
+ service_name: str, config_data: Mapping[str, str]
) -> None:
- if bot_type == UserProfile.INCOMING_WEBHOOK_BOT:
- from zerver.lib.integrations import INCOMING_WEBHOOK_INTEGRATIONS
-
- config_options = None
- for integration in INCOMING_WEBHOOK_INTEGRATIONS:
- if integration.name == service_name:
- # key: validator
- config_options = {
- option.name: option.validator for option in integration.config_options
- }
- break
- if config_options is None:
- raise JsonableError(
- _("Invalid integration '{integration_name}'.").format(integration_name=service_name)
+ from zerver.lib.integrations import INCOMING_WEBHOOK_INTEGRATIONS
+
+ config_options = None
+ for integration in INCOMING_WEBHOOK_INTEGRATIONS:
+ if integration.name == service_name:
+ # key: validator
+ config_options = {
+ option.name: option.validator for option in integration.config_options
+ }
+ break
+ if config_options is None:
+ raise JsonableError(
+ _("Invalid integration '{integration_name}'.").format(integration_name=service_name)
+ )
+
+ missing_keys = set(config_options.keys()) - set(config_data.keys())
+ if missing_keys:
+ raise JsonableError(
+ _("Missing configuration parameters: {keys}").format(
+ keys=missing_keys,
)
+ )
- missing_keys = set(config_options.keys()) - set(config_data.keys())
- if missing_keys:
+ for key, validator in config_options.items():
+ value = config_data[key]
+ error = validator(key, value)
+ if error is not None:
raise JsonableError(
- _("Missing configuration parameters: {keys}").format(
- keys=missing_keys,
- )
+ _("Invalid {key} value {value} ({error})").format(key=key, value=value, error=error)
)
- for key, validator in config_options.items():
- value = config_data[key]
- error = validator(key, value)
- if error is not None:
- raise JsonableError(
- _("Invalid {key} value {value} ({error})").format(
- key=key, value=value, error=error
- )
- )
- elif bot_type == UserProfile.EMBEDDED_BOT:
- try:
- from zerver.lib.bot_lib import get_bot_handler
-
- bot_handler = get_bot_handler(service_name)
- if hasattr(bot_handler, "validate_config"):
- bot_handler.validate_config(config_data)
- except ConfigValidationError:
- # The exception provides a specific error message, but that
- # message is not tagged translatable, because it is
- # triggered in the external zulip_bots package.
- # TODO: Think of some clever way to provide a more specific
- # error message.
- raise JsonableError(_("Invalid configuration data!"))
+def check_valid_embedded_bot_config(service_name: str, config_data: Mapping[str, str]) -> None:
+ try:
+ from zerver.lib.bot_lib import get_bot_handler
+
+ bot_handler = get_bot_handler(service_name)
+ if hasattr(bot_handler, "validate_config"):
+ bot_handler.validate_config(config_data)
+ except ConfigValidationError:
+ # The exception provides a specific error message, but that
+ # message is not tagged translatable, because it is
+ # triggered in the external zulip_bots package.
+ # TODO: Think of some clever way to provide a more specific
+ # error message.
+ raise JsonableError(_("Invalid configuration data!"))
# Adds an outgoing webhook or embedded bot service.
diff --git a/zerver/views/users.py b/zerver/views/users.py
index 51eb9648845..adc80ceb692 100644
--- a/zerver/views/users.py
+++ b/zerver/views/users.py
@@ -87,8 +87,9 @@ from zerver.lib.users import (
check_can_access_user,
check_can_create_bot,
check_full_name,
- check_valid_bot_config,
check_valid_bot_type,
+ check_valid_embedded_bot_config,
+ check_valid_incoming_webhook_bot_config,
check_valid_interface_type,
get_users_for_api,
max_message_id_for_user,
@@ -716,8 +717,10 @@ def add_bot_backend(
user_profile, default_events_register_stream_name
)
- if bot_type in (UserProfile.INCOMING_WEBHOOK_BOT, UserProfile.EMBEDDED_BOT) and service_name:
- check_valid_bot_config(bot_type, service_name, config_data)
+ if bot_type == UserProfile.INCOMING_WEBHOOK_BOT and service_name:
+ check_valid_incoming_webhook_bot_config(service_name, config_data)
+ elif bot_type == UserProfile.EMBEDDED_BOT and service_name:
+ check_valid_embedded_bot_config(service_name, config_data)
bot_profile = do_create_user(
email=email,