示例#1
0
 def test_send_reply(self):
     client = FakeClient()
     profile = client.get_profile()
     handler = ExternalBotHandler(
         client=client, root_dir=None, bot_details=None, bot_config_file=None
     )
     to = {"id": 43}
     expected = [
         (
             {"type": "private", "display_recipient": [to]},
             {"type": "private", "to": [to["id"]]},
             None,
         ),
         (
             {"type": "private", "display_recipient": [to, profile]},
             {"type": "private", "to": [to["id"], profile["id"]]},
             "widget_content",
         ),
         (
             {"type": "stream", "display_recipient": "Stream name", "subject": "Topic"},
             {"type": "stream", "to": "Stream name", "subject": "Topic"},
             "test widget",
         ),
     ]
     response_text = "Response"
     for test in expected:
         client.send_message = MagicMock()
         handler.send_reply(test[0], response_text, test[2])
         client.send_message.assert_called_once_with(
             dict(test[1], content=response_text, widget_content=test[2])
         )
def main():
    # type: () -> None
    args = parse_args()
    if os.path.isfile(args.bot):
        bot_path = os.path.abspath(args.bot)
        bot_name = os.path.splitext(os.path.basename(bot_path))[0]
    else:
        bot_path = os.path.abspath(
            os.path.join(current_dir, 'bots', args.bot, args.bot + '.py'))
        bot_name = args.bot
    bot_dir = os.path.dirname(bot_path)
    if args.provision:
        provision_bot(os.path.dirname(bot_path), args.force)
    lib_module = import_module_from_source(bot_path, bot_name)

    message = {'content': args.message, 'sender_email': '*****@*****.**'}
    message_handler = lib_module.handler_class()

    with patch('zulip.Client') as mock_client:
        mock_bot_handler = ExternalBotHandler(mock_client, bot_dir)
        mock_bot_handler.send_reply = MagicMock()
        mock_bot_handler.send_message = MagicMock()
        mock_bot_handler.update_message = MagicMock()
        if hasattr(message_handler, 'initialize') and callable(
                message_handler.initialize):
            message_handler.initialize(mock_bot_handler)
        message_handler.handle_message(message=message,
                                       bot_handler=mock_bot_handler,
                                       state_handler=StateHandler())
        print("On sending {} bot the message \"{}\"".format(
            bot_name, args.message))
        # 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:
            output_message = list(mock_bot_handler.send_reply.call_args)[0][1]
        elif mock_bot_handler.send_message.called:
            output_message = list(
                mock_bot_handler.send_message.call_args)[0][0]
        elif mock_bot_handler.update_message.called:
            output_message = list(
                mock_bot_handler.update_message.call_args)[0][0]['content']
            print(
                "the bot updates a message with the following text (in quotes):\n\"{}\""
                .format(output_message))
            sys.exit()
        else:
            print("the bot sent no reply.")
            sys.exit()
        print(
            "the bot gives the following output message (in quotes):\n\"{}\"".
            format(output_message))
示例#3
0
 def test_send_reply(self):
     client = FakeClient()
     profile = client.get_profile()
     handler = ExternalBotHandler(
         client=client,
         root_dir=None,
         bot_details=None,
         bot_config_file=None
     )
     to = {'id': 43}
     expected = [({'type': 'private', 'display_recipient': [to]},
                  {'type': 'private', 'to': [to['id']]}, None),
                 ({'type': 'private', 'display_recipient': [to, profile]},
                  {'type': 'private', 'to': [to['id'], profile['id']]}, 'widget_content'),
                 ({'type': 'stream', 'display_recipient': 'Stream name', 'subject': 'Topic'},
                  {'type': 'stream', 'to': 'Stream name', 'subject': 'Topic'}, 'test widget')]
     response_text = "Response"
     for test in expected:
         client.send_message = MagicMock()
         handler.send_reply(test[0], response_text, test[2])
         client.send_message.assert_called_once_with(dict(
             test[1], content=response_text, widget_content=test[2]))
示例#4
0
 def test_send_reply(self):
     client = FakeClient()
     profile = client.get_profile()
     handler = ExternalBotHandler(
         client=client,
         root_dir=None,
         bot_details=None,
         bot_config_file=None
     )
     to = {'email': 'Some@User'}
     expected = [({'type': 'private', 'display_recipient': [to]},
                  {'type': 'private', 'to': [to['email']]}, None),
                 ({'type': 'private', 'display_recipient': [to, profile]},
                  {'type': 'private', 'to': [to['email']]}, 'widget_content'),
                 ({'type': 'stream', 'display_recipient': 'Stream name', 'subject': 'Topic'},
                  {'type': 'stream', 'to': 'Stream name', 'subject': 'Topic'}, 'test widget')]
     response_text = "Response"
     for test in expected:
         client.send_message = MagicMock()
         handler.send_reply(test[0], response_text, test[2])
         client.send_message.assert_called_once_with(dict(
             test[1], content=response_text, widget_content=test[2]))