From f3d839fd47b439009d9a20f27e62778b1276589e Mon Sep 17 00:00:00 2001 From: Abhijeet Kaur Date: Sat, 5 Aug 2017 02:37:53 +0530 Subject: [PATCH] bots: Add complete test coverage for yoda bot. Add error handling support for empty messages and invalid input to yoda bot, which was previously making the bot crash. Add comments to describe tests. --- .../yoda/fixtures/test_invalid_input.json | 14 ++++++++ zulip_bots/zulip_bots/bots/yoda/test_yoda.py | 34 +++++++++++++++++++ zulip_bots/zulip_bots/bots/yoda/yoda.py | 6 +++- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 zulip_bots/zulip_bots/bots/yoda/fixtures/test_invalid_input.json diff --git a/zulip_bots/zulip_bots/bots/yoda/fixtures/test_invalid_input.json b/zulip_bots/zulip_bots/bots/yoda/fixtures/test_invalid_input.json new file mode 100644 index 0000000..4cf71b1 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/yoda/fixtures/test_invalid_input.json @@ -0,0 +1,14 @@ +{ + "request": { + "api_url": "https://yoda.p.mashape.com/yoda?sentence=@#$%^&*", + "headers": { + "X-Mashape-Key": "12345678", + "Accept": "text/plain" + } + }, + "response": {"text": ""}, + "response-headers": { + "status": 200, + "content-type": "text/html; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/yoda/test_yoda.py b/zulip_bots/zulip_bots/bots/yoda/test_yoda.py index f0cbe62..b1427f6 100644 --- a/zulip_bots/zulip_bots/bots/yoda/test_yoda.py +++ b/zulip_bots/zulip_bots/bots/yoda/test_yoda.py @@ -12,6 +12,7 @@ class TestYodaBot(BotTestCase): def test_bot(self): + # Test normal sentence (1). bot_response = "Learn how to speak like me someday, you will. Yes, hmmm." with self.mock_config_info({'api_key': '12345678'}), \ @@ -23,6 +24,7 @@ class TestYodaBot(BotTestCase): expected_method='send_reply' ) + # Test normal sentence (2). bot_response = "Much to learn, you still have." with self.mock_config_info({'api_key': '12345678'}), \ @@ -34,6 +36,7 @@ class TestYodaBot(BotTestCase): expected_method='send_reply' ) + # Test only numbers. bot_response = "23456. Herh herh herh." with self.mock_config_info({'api_key': '12345678'}), \ @@ -45,6 +48,7 @@ class TestYodaBot(BotTestCase): expected_method='send_reply' ) + # Test help. bot_response = ''' This bot allows users to translate a sentence into 'Yoda speak'. @@ -62,3 +66,33 @@ class TestYodaBot(BotTestCase): response = {'content': bot_response}, expected_method='send_reply' ) + + # Test empty message. + bot_response = ''' + This bot allows users to translate a sentence into + 'Yoda speak'. + Users should preface messages with '@mention-bot'. + + Before running this, make sure to get a Mashape Api token. + Instructions are in the 'readme.md' file. + Store it in the 'yoda.config' file. + The 'yoda.config' file should be located at '~/yoda.config'. + Example input: + @mention-bot You will learn how to speak like me someday. + ''' + self.assert_bot_response( + message = {'content': ''}, + response = {'content': bot_response}, + expected_method='send_reply' + ) + + # Test invalid input. + bot_response = "Invalid input, please check the sentence you have entered." + with self.mock_config_info({'api_key': '12345678'}), \ + self.mock_http_conversation('test_invalid_input'): + self.initialize_bot() + self.assert_bot_response( + message = {'content': '@#$%^&*'}, + response = {'content': bot_response}, + expected_method='send_reply' + ) diff --git a/zulip_bots/zulip_bots/bots/yoda/yoda.py b/zulip_bots/zulip_bots/bots/yoda/yoda.py index 91d1678..ad1033e 100644 --- a/zulip_bots/zulip_bots/bots/yoda/yoda.py +++ b/zulip_bots/zulip_bots/bots/yoda/yoda.py @@ -91,7 +91,8 @@ class YodaSpeakHandler(object): def handle_input(self, message, bot_handler): original_content = message['content'] - if self.is_help(original_content): + + if self.is_help(original_content) or (original_content == ""): bot_handler.send_reply(message, HELP_MESSAGE) else: @@ -99,6 +100,9 @@ class YodaSpeakHandler(object): try: reply_message = self.send_to_yoda_api(sentence) + if len(reply_message) == 0: + reply_message = 'Invalid input, please check the sentence you have entered.' + except ssl.SSLError or TypeError: reply_message = 'The service is temporarily unavailable, please try again.' logging.error(reply_message)