bots: Add magic method validate_config().

This method allows bots to validate their config info
in a standardized way. Adding the method to a bot is
optional, but recommended for bots with config options
that can be invalid, like api keys. The giphy bot serves
as an example.
The primary reason behind this is to allow the zulip
backend to validate config data for embedded bots. The
backend does not have a permanent bot class instance, so
validate_config() must be a static method.
This commit is contained in:
Robert Hönig 2018-02-10 23:24:37 +01:00 committed by Tim Abbott
parent 7dcec207eb
commit 192e9e101d
5 changed files with 53 additions and 11 deletions

View file

@ -3,6 +3,10 @@ from unittest import TestCase
from typing import List, Dict, Any, Tuple
from zulip_bots.custom_exceptions import (
ConfigValidationError,
)
from zulip_bots.request_test_lib import (
mock_http_conversation,
)
@ -149,6 +153,15 @@ class BotTestCase(TestCase):
response = bot_handler.unique_response()
self.assertEqual(expected_response, response['content'])
def validate_invalid_config(self, config_data: Dict[str, str], error_regexp: str) -> None:
bot_class = type(get_bot_message_handler(self.bot_name))
with self.assertRaisesRegexp(ConfigValidationError, error_regexp):
bot_class.validate_config(config_data)
def validate_valid_config(self, config_data: Dict[str, str]) -> None:
bot_class = type(get_bot_message_handler(self.bot_name))
bot_class.validate_config(config_data)
def test_bot_usage(self):
# type: () -> None
bot = get_bot_message_handler(self.bot_name)