def get_test_quiz(self) -> Tuple[Dict[str, Any], Any]: bot_handler = StubBotHandler() quiz_payload = read_bot_fixture_data('trivia_quiz', 'test_new_question')['response'] with patch('random.shuffle'): quiz = get_quiz_from_payload(quiz_payload) return quiz, bot_handler
def test_exception_when_api_key_is_invalid(self) -> None: bot_test_instance = MentionHandler() with self.mock_config_info({"access_token": "TEST"}): with self.mock_http_conversation("invalid_api_key"): with self.assertRaises(StubBotHandler.BotQuitException): bot_test_instance.initialize(StubBotHandler())
def test_exception_when_api_key_is_invalid(self) -> None: bot_test_instance = BaremetricsHandler() with self.mock_config_info({'api_key': 'TEST'}): with self.mock_http_conversation('invalid_api_key'): with self.assertRaises(StubBotHandler.BotQuitException): bot_test_instance.initialize(StubBotHandler())
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" )
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)
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)
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())
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)
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)
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())
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!")
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")
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)
def test_get_all_boards_command(self) -> None: with self.mock_config_info(mock_config), patch("requests.get"): with self.mock_http_conversation("get_all_boards"): self.verify_reply("get-all-boards", "**Boards:**\n") with self.mock_http_conversation("get_board_descs"): bot_instance = TrelloHandler() bot_instance.initialize(StubBotHandler()) self.assertEqual(bot_instance.get_board_descs(["TEST"]), "1.[TEST](TEST) (`TEST`)")
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!')
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)
def test_get_all_boards_command(self) -> None: with self.mock_config_info(mock_config), patch('requests.get'): with self.mock_http_conversation('get_all_boards'): self.verify_reply('get-all-boards', '**Boards:**\n') with self.mock_http_conversation('get_board_descs'): bot_instance = TrelloHandler() bot_instance.initialize(StubBotHandler()) self.assertEqual(bot_instance.get_board_descs(['TEST']), '1.[TEST](TEST) (`TEST`)')
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')
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!')
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)
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!")
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)
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)
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'])
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"])
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"])
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'])
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")
def test_exception_when_api_key_is_invalid(self)-> None: bot_test_instance = LinkShortenerHandler() with self.mock_config_info({'key': 'qwertyuiopx'}): with self.mock_http_conversation('test_invalid_access_token'): with self.assertRaises(StubBotHandler.BotQuitException): bot_test_instance.initialize(StubBotHandler())
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'})