2018-05-17 10:49:16 -04:00
|
|
|
import unittest
|
2017-05-24 23:12:57 -04:00
|
|
|
|
2018-05-05 02:18:56 -04:00
|
|
|
from typing import List, Dict, Any, Tuple, Optional, IO
|
2017-05-26 00:58:19 -04:00
|
|
|
|
2018-02-10 17:24:37 -05:00
|
|
|
from zulip_bots.custom_exceptions import (
|
|
|
|
ConfigValidationError,
|
|
|
|
)
|
|
|
|
|
2017-12-11 10:05:06 -05:00
|
|
|
from zulip_bots.request_test_lib import (
|
|
|
|
mock_http_conversation,
|
2018-03-07 12:04:05 -05:00
|
|
|
mock_request_exception
|
2017-12-11 10:05:06 -05:00
|
|
|
)
|
|
|
|
|
2017-11-30 17:09:28 -05:00
|
|
|
from zulip_bots.simple_lib import (
|
|
|
|
SimpleStorage,
|
|
|
|
SimpleMessageServer,
|
|
|
|
)
|
|
|
|
|
2017-12-11 10:23:24 -05:00
|
|
|
from zulip_bots.test_file_utils import (
|
|
|
|
get_bot_message_handler,
|
|
|
|
read_bot_fixture_data,
|
|
|
|
)
|
|
|
|
|
2017-08-01 17:09:36 -04:00
|
|
|
from zulip_bots.lib import BotIdentity
|
2018-05-17 10:49:16 -04:00
|
|
|
|
2017-11-30 17:09:28 -05:00
|
|
|
class StubBotHandler:
|
2018-05-17 10:49:16 -04:00
|
|
|
def __init__(self) -> None:
|
2017-11-30 17:09:28 -05:00
|
|
|
self.storage = SimpleStorage()
|
2018-01-19 10:55:18 -05:00
|
|
|
self.full_name = 'test-bot'
|
|
|
|
self.email = 'test-bot@example.com'
|
2017-11-30 17:09:28 -05:00
|
|
|
self.message_server = SimpleMessageServer()
|
2017-12-01 14:49:47 -05:00
|
|
|
self.reset_transcript()
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def reset_transcript(self) -> None:
|
2017-12-01 14:49:47 -05:00
|
|
|
self.transcript = [] # type: List[Tuple[str, Dict[str, Any]]]
|
2017-11-30 17:09:28 -05:00
|
|
|
|
2017-08-01 17:09:36 -04:00
|
|
|
def identity(self) -> BotIdentity:
|
|
|
|
return BotIdentity(self.full_name, self.email)
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def send_message(self, message: Dict[str, Any]) -> Dict[str, Any]:
|
2017-12-01 14:49:47 -05:00
|
|
|
self.transcript.append(('send_message', message))
|
2017-11-30 17:09:28 -05:00
|
|
|
return self.message_server.send(message)
|
|
|
|
|
2018-05-21 06:04:36 -04:00
|
|
|
def send_reply(self, message: Dict[str, Any], response: str,
|
|
|
|
widget_content: Optional[str]=None) -> Dict[str, Any]:
|
2017-11-30 17:09:28 -05:00
|
|
|
response_message = dict(
|
2018-05-21 06:04:36 -04:00
|
|
|
content=response,
|
|
|
|
widget_content=widget_content
|
2017-11-30 17:09:28 -05:00
|
|
|
)
|
2017-12-01 14:49:47 -05:00
|
|
|
self.transcript.append(('send_reply', response_message))
|
2017-11-30 17:09:28 -05:00
|
|
|
return self.message_server.send(response_message)
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def update_message(self, message: Dict[str, Any]) -> None:
|
2017-11-30 17:09:28 -05:00
|
|
|
self.message_server.update(message)
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def upload_file_from_path(self, file_path: str) -> Dict[str, Any]:
|
2018-05-05 02:18:56 -04:00
|
|
|
with open(file_path, 'rb') as file:
|
|
|
|
return self.message_server.upload_file(file)
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def upload_file(self, file: IO[Any]) -> Dict[str, Any]:
|
2018-05-05 02:18:56 -04:00
|
|
|
return self.message_server.upload_file(file)
|
|
|
|
|
2017-12-30 15:40:24 -05:00
|
|
|
class BotQuitException(Exception):
|
|
|
|
pass
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def quit(self, message: str="") -> None:
|
2017-12-30 15:40:24 -05:00
|
|
|
raise self.BotQuitException()
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def get_config_info(self, bot_name: str, optional: bool=False) -> Dict[str, Any]:
|
2017-12-23 00:41:17 -05:00
|
|
|
return {}
|
2017-12-06 22:41:19 -05:00
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def unique_reply(self) -> Dict[str, Any]:
|
2017-12-01 15:53:03 -05:00
|
|
|
responses = [
|
|
|
|
message
|
|
|
|
for (method, message)
|
|
|
|
in self.transcript
|
|
|
|
if method == 'send_reply'
|
|
|
|
]
|
|
|
|
self.ensure_unique_response(responses)
|
|
|
|
return responses[0]
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def unique_response(self) -> Dict[str, Any]:
|
2017-12-01 14:49:47 -05:00
|
|
|
responses = [
|
|
|
|
message
|
|
|
|
for (method, message)
|
|
|
|
in self.transcript
|
|
|
|
]
|
|
|
|
self.ensure_unique_response(responses)
|
|
|
|
return responses[0]
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def ensure_unique_response(self, responses: List[Dict[str, Any]]) -> None:
|
2017-12-01 14:49:47 -05:00
|
|
|
if not responses:
|
|
|
|
raise Exception('The bot is not responding for some reason.')
|
|
|
|
if len(responses) > 1:
|
|
|
|
raise Exception('The bot is giving too many responses for some reason.')
|
2017-11-30 17:09:28 -05:00
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
|
2018-06-08 17:30:50 -04:00
|
|
|
class DefaultTests:
|
|
|
|
bot_name = ''
|
|
|
|
|
|
|
|
def make_request_message(self, content: str) -> Dict[str, Any]:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def get_response(self, message: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def test_bot_usage(self) -> None:
|
|
|
|
bot = get_bot_message_handler(self.bot_name)
|
|
|
|
assert bot.usage() != ''
|
|
|
|
|
|
|
|
def test_bot_responds_to_empty_message(self) -> None:
|
|
|
|
message = self.make_request_message('')
|
|
|
|
|
|
|
|
# get_response will fail if we don't respond at all
|
|
|
|
response = self.get_response(message)
|
|
|
|
|
|
|
|
# we also want a non-blank response
|
|
|
|
assert len(response['content']) >= 1
|
|
|
|
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
class BotTestCase(unittest.TestCase):
|
2017-11-30 17:09:28 -05:00
|
|
|
bot_name = ''
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def _get_handlers(self) -> Tuple[Any, StubBotHandler]:
|
2017-12-08 17:34:21 -05:00
|
|
|
bot = get_bot_message_handler(self.bot_name)
|
|
|
|
bot_handler = StubBotHandler()
|
|
|
|
|
|
|
|
if hasattr(bot, 'initialize'):
|
|
|
|
bot.initialize(bot_handler)
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
return bot, bot_handler
|
2017-12-10 09:35:51 -05:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def get_response(self, message: Dict[str, Any]) -> Dict[str, Any]:
|
2017-12-10 09:35:51 -05:00
|
|
|
bot, bot_handler = self._get_handlers()
|
2017-12-08 17:34:21 -05:00
|
|
|
bot_handler.reset_transcript()
|
|
|
|
bot.handle_message(message, bot_handler)
|
|
|
|
return bot_handler.unique_response()
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def make_request_message(self, content: str) -> Dict[str, Any]:
|
|
|
|
"""
|
2017-12-11 10:39:48 -05:00
|
|
|
This is mostly used internally but
|
|
|
|
tests can override this behavior by
|
|
|
|
mocking/subclassing.
|
2018-05-17 10:49:16 -04:00
|
|
|
"""
|
2017-12-01 15:53:03 -05:00
|
|
|
message = dict(
|
2017-12-11 10:39:48 -05:00
|
|
|
display_recipient='foo_stream',
|
2017-12-01 15:53:03 -05:00
|
|
|
sender_email='foo@example.com',
|
2017-12-10 18:25:57 -05:00
|
|
|
sender_full_name='Foo Test User',
|
2017-12-09 15:11:18 -05:00
|
|
|
sender_id='123',
|
2017-12-11 10:39:48 -05:00
|
|
|
content=content,
|
2017-12-01 15:53:03 -05:00
|
|
|
)
|
2017-12-11 10:39:48 -05:00
|
|
|
return message
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def get_reply_dict(self, request: str) -> Dict[str, Any]:
|
2017-12-11 10:39:48 -05:00
|
|
|
bot, bot_handler = self._get_handlers()
|
|
|
|
message = self.make_request_message(request)
|
2017-12-01 15:53:03 -05:00
|
|
|
bot_handler.reset_transcript()
|
|
|
|
bot.handle_message(message, bot_handler)
|
|
|
|
reply = bot_handler.unique_reply()
|
2018-01-10 11:36:05 -05:00
|
|
|
return reply
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def verify_reply(self, request: str, response: str) -> None:
|
2018-01-10 11:36:05 -05:00
|
|
|
reply = self.get_reply_dict(request)
|
2017-12-01 15:53:03 -05:00
|
|
|
self.assertEqual(response, reply['content'])
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def verify_dialog(self, conversation: List[Tuple[str, str]]) -> None:
|
2017-11-30 17:09:28 -05:00
|
|
|
# Start a new message handler for the full conversation.
|
2017-12-10 09:35:51 -05:00
|
|
|
bot, bot_handler = self._get_handlers()
|
2017-11-30 17:09:28 -05:00
|
|
|
|
|
|
|
for (request, expected_response) in conversation:
|
2017-12-11 10:39:48 -05:00
|
|
|
message = self.make_request_message(request)
|
2017-12-01 14:49:47 -05:00
|
|
|
bot_handler.reset_transcript()
|
2017-11-30 17:09:28 -05:00
|
|
|
bot.handle_message(message, bot_handler)
|
2017-12-01 14:49:47 -05:00
|
|
|
response = bot_handler.unique_response()
|
2017-11-30 17:09:28 -05:00
|
|
|
self.assertEqual(expected_response, response['content'])
|
|
|
|
|
2018-02-10 17:24:37 -05:00
|
|
|
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)
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def mock_http_conversation(self, test_name: str) -> Any:
|
2017-12-01 15:49:40 -05:00
|
|
|
assert test_name is not None
|
|
|
|
http_data = read_bot_fixture_data(self.bot_name, test_name)
|
|
|
|
return mock_http_conversation(http_data)
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def mock_request_exception(self) -> Any:
|
2018-03-07 12:04:05 -05:00
|
|
|
return mock_request_exception()
|
|
|
|
|
2018-05-17 10:49:16 -04:00
|
|
|
def mock_config_info(self, config_info: Dict[str, str]) -> Any:
|
|
|
|
return unittest.mock.patch('zulip_bots.test_lib.StubBotHandler.get_config_info', return_value=config_info)
|