bots: Mock 'send_message' function for 'zulip-bot-output'.

Very few bots like followup bot directly call 'send_message'
function from the bot code. Since ExternalBotHandler class is
mocked, we'll have to mock 'send_message' function as well.

Added dummy field value of 'sender_email' for the message to be
as followup bot requires that field while processing
the message.
Since send_message is directly called from a specific bot's code,
so it can be sent to a different stream or under a different topic
than that of the incoming message. So, print the entire message
along with stream name.

A bot calling 'send_reply' function will reply to the incoming
message in the same stream under the same topic. So, only printing
bot's response message content for that.
This commit is contained in:
Abhijeet Kaur 2017-08-26 04:09:44 +05:30 committed by Tim Abbott
parent 36f4982e54
commit 402dda87fe

View file

@ -80,7 +80,7 @@ def main():
provision_bot(bot_dir, options.force) provision_bot(bot_dir, options.force)
lib_module = import_module('zulip_bots.bots.{bot}.{bot}'.format(bot=bot_name)) lib_module = import_module('zulip_bots.bots.{bot}.{bot}'.format(bot=bot_name))
message = {'content': options.message} message = {'content': options.message, 'sender_email': 'foo_sender@zulip.com'}
message_handler = lib_module.handler_class() message_handler = lib_module.handler_class()
with patch('zulip_bots.lib.ExternalBotHandler') as mock_bot_handler: with patch('zulip_bots.lib.ExternalBotHandler') as mock_bot_handler:
@ -100,17 +100,28 @@ def main():
return dict(config.items(section)) return dict(config.items(section))
mock_bot_handler.get_config_info = get_config_info mock_bot_handler.get_config_info = get_config_info
if (hasattr(message_handler, 'initialize') and callable(message_handler.initialize)): if hasattr(message_handler, 'initialize') and callable(message_handler.initialize):
message_handler.initialize(mock_bot_handler) message_handler.initialize(mock_bot_handler)
mock_bot_handler.send_reply = MagicMock() mock_bot_handler.send_reply = MagicMock()
mock_bot_handler.send_message = MagicMock()
message_handler.handle_message( message_handler.handle_message(
message=message, message=message,
bot_handler=mock_bot_handler, bot_handler=mock_bot_handler,
state_handler=StateHandler() state_handler=StateHandler()
) )
print("On sending ", options.name, " bot the following message:\n\"", options.message, "\"") print("On sending ", options.name, " bot the following message:\n\"", options.message, "\"")
print("\nThe bot gives the following output message:\n\"", list(mock_bot_handler.send_reply.call_args)[0][1], "\"")
# send_reply and send_message have slightly arguments; the
# following takes that into account.
# send_reply(original_message, response)
# send_message(response_message)
if mock_bot_handler.send_reply.called:
print("\nThe bot gives the following output message:\n\"", list(mock_bot_handler.send_reply.call_args)[0][1], "\"")
elif mock_bot_handler.send_message.called:
print("\nThe bot sends the following output to zulip:\n\"", list(mock_bot_handler.send_message.call_args)[0][0], "\"")
else:
print("\nThe bot sent no reply.")
if __name__ == '__main__': if __name__ == '__main__':
main() main()