Ejemplo n.º 1
0
 def test_invalid_config(self) -> None:
     get_bot_message_handler(self.bot_name)
     StubBotHandler()
     with self.mock_http_conversation("test_403"):
         self.validate_invalid_config(
             {"key": "12345678"}, "This is likely due to an invalid key.\n"
         )
Ejemplo n.º 2
0
 def test_bot_responds_to_empty_message(self) -> None:
     with patch("zulip_bots.bots.witai.witai.get_handle",
                return_value=mock_handle):
         with self.mock_config_info(self.MOCK_CONFIG_INFO):
             get_bot_message_handler(self.bot_name).initialize(
                 StubBotHandler())
             with patch("wit.Wit.message",
                        return_value=self.MOCK_WITAI_RESPONSE):
                 self.verify_reply("", "Qwertyuiop!")
Ejemplo n.º 3
0
    def test_normal(self) -> None:
        with patch("zulip_bots.bots.witai.witai.get_handle",
                   return_value=mock_handle):
            with self.mock_config_info(self.MOCK_CONFIG_INFO):
                get_bot_message_handler(self.bot_name).initialize(
                    StubBotHandler())

                with patch("wit.Wit.message",
                           return_value=self.MOCK_WITAI_RESPONSE):
                    self.verify_reply("What is your favorite food?", "pizza")
Ejemplo n.º 4
0
    def test_normal(self) -> None:
        with patch('zulip_bots.bots.witai.witai.get_handle', return_value=mock_handle):
            with self.mock_config_info(self.MOCK_CONFIG_INFO):
                get_bot_message_handler(self.bot_name).initialize(StubBotHandler())

                with patch('wit.Wit.message', return_value=self.MOCK_WITAI_RESPONSE):
                    self.verify_reply(
                        'What is your favorite food?',
                        'pizza'
                    )
Ejemplo n.º 5
0
    def test_invalid_when_handle_message(self) -> None:
        get_bot_message_handler(self.bot_name)
        StubBotHandler()

        with self.mock_config_info({'auth_token': 'someInvalidKey',
                                    'username': '******',
                                    'goalname': 'goal'}), \
                patch('requests.get', side_effect=ConnectionError()), \
                self.mock_http_conversation('test_invalid_when_handle_message'), \
                patch('logging.exception'):
            self.verify_reply('5', 'Error. Check your key!')
Ejemplo n.º 6
0
    def test_bot_responds_to_empty_message(self) -> None:
        with self.mock_config_info(self.MOCK_CONFIG_INFO):
            get_bot_message_handler(self.bot_name).initialize(StubBotHandler())

            with patch('wit.Wit.message') as message:
                message.return_value = self.MOCK_WITAI_RESPONSE

                with patch(
                        'zulip_bots.bots.witai.witai.get_handle') as handler:
                    handler.return_value = mock_handle

                    self.verify_reply('', 'Qwertyuiop!')
Ejemplo n.º 7
0
    def test_normal(self) -> None:
        with self.mock_config_info(self.MOCK_CONFIG_INFO):
            get_bot_message_handler(self.bot_name).initialize(StubBotHandler())

            with patch('wit.Wit.message') as message:
                message.return_value = self.MOCK_WITAI_RESPONSE

                with patch(
                        'zulip_bots.bots.witai.witai.get_handle') as handler:
                    handler.return_value = mock_handle

                    self.verify_reply('What is your favorite food?', 'pizza')
Ejemplo n.º 8
0
    def test_invalid_when_handle_message(self) -> None:
        get_bot_message_handler(self.bot_name)
        StubBotHandler()

        with self.mock_config_info({
                "auth_token": "someInvalidKey",
                "username": "******",
                "goalname": "goal"
        }), patch("requests.get",
                  side_effect=ConnectionError()), self.mock_http_conversation(
                      "test_invalid_when_handle_message"), patch(
                          "logging.exception"):
            self.verify_reply("5", "Error. Check your key!")
Ejemplo n.º 9
0
    def test_multiple(self) -> None:
        get_bot_message_handler(self.bot_name)
        StubBotHandler()

        bot_response = 'Here is what I found for `marvel` : ' \
                       '\n * Marvel Studios\' Avengers: Infinity War Official Trailer - [Watch now](https://www.youtube.com/watch/6ZfuNTqbHE8)' \
                       '\n * Marvel Studios\' Black Panther - Official Trailer - [Watch now](https://www.youtube.com/watch/xjDjIWPwcPU)' \
                       '\n * MARVEL RISING BEGINS! | The Next Generation of Marvel Heroes (EXCLUSIVE) - [Watch now](https://www.youtube.com/watch/6HTPCTtkWoA)' \
                       '\n * Marvel Contest of Champions Taskmaster Spotlight - [Watch now](https://www.youtube.com/watch/-8uqxdcJ9WM)' \
                       '\n * 5* Crystal Opening! SO LUCKY! - Marvel Contest Of Champions - [Watch now](https://www.youtube.com/watch/l7rrsGKJ_O4)'

        with self.mock_config_info(self.normal_config), \
                self.mock_http_conversation('test_multiple'):
            self.verify_reply('list marvel', bot_response)
Ejemplo n.º 10
0
    def test_bot_usage(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info(self.mock_config):
            bot.initialize(bot_handler)

        self.assertIn('This bot posts on twitter from zulip chat itself', bot.usage())
Ejemplo n.º 11
0
    def test_invalid_key(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info({'key': 'somethinginvalid', 'number_of_results': '5', 'video_region': 'US'}), \
                self.mock_http_conversation('test_invalid_key'), \
                self.assertRaises(SystemExit) as se:  # type: ignore
            bot.initialize(bot_handler)
Ejemplo n.º 12
0
    def test_bot_usage(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info(self.mock_config):
            bot.initialize(bot_handler)

        self.assertIn("displays details on github issues", bot.usage())
Ejemplo n.º 13
0
    def test_bot_usage(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info(self.mock_config):
            bot.initialize(bot_handler)

        self.assertIn('displays details on github issues', bot.usage())
Ejemplo n.º 14
0
    def test_403(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info({'key': '12345678'}), \
                self.mock_http_conversation('test_403'),  \
                self.assertRaises(bot_handler.BotQuitException):
            bot.initialize(bot_handler)
Ejemplo n.º 15
0
    def test_invalid_key(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info({'key': 'somethinginvalid', 'number_of_results': '5', 'video_region': 'US'}), \
                self.mock_http_conversation('test_invalid_key'), \
                self.assertRaises(bot_handler.BotQuitException):
                    bot.initialize(bot_handler)
Ejemplo n.º 16
0
    def test_unknown_error(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info(self.normal_config), \
                self.mock_http_conversation('test_unknown_error'), \
                self.assertRaises(HTTPError):
            bot.initialize(bot_handler)
Ejemplo n.º 17
0
    def test_connection_error_during_initialize(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info(self.normal_config), \
                patch('requests.get', side_effect=ConnectionError()), \
                patch('logging.exception') as mock_logging:
            bot.initialize(bot_handler)
            self.assertTrue(mock_logging.called)
Ejemplo n.º 18
0
    def test_bot_usage(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info(self.mock_config):
            bot.initialize(bot_handler)

        self.assertIn('This bot posts on twitter from zulip chat itself',
                      bot.usage())
Ejemplo n.º 19
0
    def test_connection_error_during_initialize(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info(self.normal_config), \
                patch('requests.get', side_effect=ConnectionError()), \
                patch('logging.exception') as mock_logging:
            bot.initialize(bot_handler)
            self.assertTrue(mock_logging.called)
Ejemplo n.º 20
0
    def test_invalid_when_initialize(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info({'auth_token': 'someInvalidKey',
                                    'username': '******',
                                    'goalname': 'goal'}), \
                self.mock_http_conversation('test_invalid_when_initialize'), \
                self.assertRaises(bot_handler.BotQuitException):
            bot.initialize(bot_handler)
Ejemplo n.º 21
0
    def test_invalid_when_initialize(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info({'auth_token': 'someInvalidKey',
                                    'username': '******',
                                    'goalname': 'goal'}), \
                self.mock_http_conversation('test_invalid_when_initialize'), \
                self.assertRaises(bot_handler.BotQuitException):
                    bot.initialize(bot_handler)
Ejemplo n.º 22
0
    def test_invalid_when_handle_message(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info({'auth_token': 'someInvalidKey',
                                    'username': '******',
                                    'goalname': 'goal'}), \
                patch('requests.get', side_effect=ConnectionError()), \
                self.mock_http_conversation('test_invalid_when_handle_message'), \
                patch('logging.exception'):
            self.verify_reply('5', 'Error. Check your key!')
Ejemplo n.º 23
0
    def test_invalid_key(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info({
                "key": "somethinginvalid",
                "number_of_results": "5",
                "video_region": "US"
        }), self.mock_http_conversation("test_invalid_key"), self.assertRaises(
                bot_handler.BotQuitException):
            bot.initialize(bot_handler)
Ejemplo n.º 24
0
    def test_invalid_when_initialize(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info({
                "auth_token": "someInvalidKey",
                "username": "******",
                "goalname": "goal"
        }), self.mock_http_conversation(
                "test_invalid_when_initialize"), self.assertRaises(
                    bot_handler.BotQuitException):
            bot.initialize(bot_handler)
Ejemplo n.º 25
0
    def test_multiple(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        bot_response = 'Here is what I found for `marvel` : ' \
                       '\n * Marvel Studios\' Avengers: Infinity War Official Trailer - [Watch now](https://www.youtube.com/watch/6ZfuNTqbHE8)' \
                       '\n * Marvel Studios\' Black Panther - Official Trailer - [Watch now](https://www.youtube.com/watch/xjDjIWPwcPU)' \
                       '\n * MARVEL RISING BEGINS! | The Next Generation of Marvel Heroes (EXCLUSIVE) - [Watch now](https://www.youtube.com/watch/6HTPCTtkWoA)' \
                       '\n * Marvel Contest of Champions Taskmaster Spotlight - [Watch now](https://www.youtube.com/watch/-8uqxdcJ9WM)' \
                       '\n * 5* Crystal Opening! SO LUCKY! - Marvel Contest Of Champions - [Watch now](https://www.youtube.com/watch/l7rrsGKJ_O4)'

        with self.mock_config_info(self.normal_config), \
                self.mock_http_conversation('test_multiple'):
                    self.verify_reply('list marvel', bot_response)
Ejemplo n.º 26
0
    def test_403(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        with self.mock_config_info({'key': '12345678'}):
            bot.initialize(bot_handler)

        mock_message = {'content': 'Hello'}

        with self.mock_http_conversation('test_403'):
            with self.assertRaises(HTTPError):
                # Call the native  handle_message here,
                # since we don't want to assert a response,
                # but an exception.
                bot.handle_message(mock_message, bot_handler)
Ejemplo n.º 27
0
    def test_bot(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        message = dict(type="stream")

        bot.initialize(bot_handler)
        bot.handle_message(message, bot_handler)

        with patch("zulip_bots.simple_lib.MockMessageServer.update") as m:
            bot.handle_message(message, bot_handler)
            bot.handle_message(message, bot_handler)
            bot.handle_message(message, bot_handler)

        content_updates = [item[0][0]["content"] for item in m.call_args_list]
        self.assertEqual(content_updates, ["2", "3", "4"])
Ejemplo n.º 28
0
    def test_bot(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        message = dict(type='stream')

        bot.initialize(bot_handler)
        bot.handle_message(message, bot_handler)

        with patch('zulip_bots.simple_lib.SimpleMessageServer.update') as m:
            bot.handle_message(message, bot_handler)
            bot.handle_message(message, bot_handler)
            bot.handle_message(message, bot_handler)

        content_updates = [item[0][0]['content'] for item in m.call_args_list]
        self.assertEqual(content_updates, ['2', '3', '4'])
Ejemplo n.º 29
0
    def test_bot(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        message = dict(type='stream')

        bot.initialize(bot_handler)
        bot.handle_message(message, bot_handler)

        with patch('zulip_bots.simple_lib.SimpleMessageServer.update') as m:
            bot.handle_message(message, bot_handler)
            bot.handle_message(message, bot_handler)
            bot.handle_message(message, bot_handler)

        content_updates = [
            item[0][0]['content']
            for item in m.call_args_list
        ]
        self.assertEqual(content_updates, ['2', '3', '4'])
Ejemplo n.º 30
0
    def test_bot_edit_timeout(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        message = dict(type="stream")

        bot.initialize(bot_handler)
        bot.handle_message(message, bot_handler)

        error_msg = dict(msg="The time limit for editing this message has passed", result="error")
        with patch("zulip_bots.test_lib.StubBotHandler.update_message", return_value=error_msg):
            with patch("zulip_bots.simple_lib.MockMessageServer.send") as m:
                bot.handle_message(message, bot_handler)
                bot.handle_message(message, bot_handler)

        # When there is an error, the bot should resend the message with the new value.
        self.assertEqual(m.call_count, 2)

        content_updates = [item[0][0]["content"] for item in m.call_args_list]
        self.assertEqual(content_updates, ["2", "3"])
Ejemplo n.º 31
0
    def test_bot_edit_timeout(self) -> None:
        bot = get_bot_message_handler(self.bot_name)
        bot_handler = StubBotHandler()

        message = dict(type='stream')

        bot.initialize(bot_handler)
        bot.handle_message(message, bot_handler)

        error_msg = dict(
            msg='The time limit for editing this message has passed',
            result='error')
        with patch('zulip_bots.test_lib.StubBotHandler.update_message',
                   return_value=error_msg):
            with patch('zulip_bots.simple_lib.SimpleMessageServer.send') as m:
                bot.handle_message(message, bot_handler)
                bot.handle_message(message, bot_handler)

        # When there is an error, the bot should resend the message with the new value.
        self.assertEqual(m.call_count, 2)

        content_updates = [item[0][0]['content'] for item in m.call_args_list]
        self.assertEqual(content_updates, ['2', '3'])
Ejemplo n.º 32
0
 def test_bot_responds_to_empty_message(self) -> None:
     with patch('zulip_bots.bots.witai.witai.get_handle', return_value=mock_handle):
         with self.mock_config_info(self.MOCK_CONFIG_INFO):
             get_bot_message_handler(self.bot_name).initialize(StubBotHandler())
             with patch('wit.Wit.message', return_value=self.MOCK_WITAI_RESPONSE):
                 self.verify_reply('', 'Qwertyuiop!')
Ejemplo n.º 33
0
 def test_invalid_config(self) -> None:
     bot = get_bot_message_handler(self.bot_name)
     bot_handler = StubBotHandler()
     with self.mock_http_conversation('test_403'):
         self.validate_invalid_config({'key': '12345678'},
                                      "This is likely due to an invalid key.\n")
Ejemplo n.º 34
0
 def test_valid_config(self) -> None:
     bot = get_bot_message_handler(self.bot_name)
     bot_handler = StubBotHandler()
     with self.mock_http_conversation('test_normal'):
         self.validate_valid_config({'key': '12345678'})
Ejemplo n.º 35
0
 def test_valid_config(self) -> None:
     get_bot_message_handler(self.bot_name)
     StubBotHandler()
     with self.mock_http_conversation("test_normal"):
         self.validate_valid_config({"key": "12345678"})
Ejemplo n.º 36
0
 def test_invalid_config(self) -> None:
     bot = get_bot_message_handler(self.bot_name)
     bot_handler = StubBotHandler()
     with self.mock_http_conversation('test_403'):
         self.validate_invalid_config(
             {'key': '12345678'}, "This is likely due to an invalid key.\n")
Ejemplo n.º 37
0
 def test_valid_config(self) -> None:
     bot = get_bot_message_handler(self.bot_name)
     bot_handler = StubBotHandler()
     with self.mock_http_conversation('test_normal'):
         self.validate_valid_config({'key': '12345678'})