bots: Add complete tests for offline testing of yoda bot.
Since yoda api returns response of text form, made changes
in test_lib.py to handle responses that can either be json
or plain text.
ADD
zulip_bots/zulip_bots/bots/yoda/fixtures/test_1.json
+{
+ "request": {
+ "api_url": "https://yoda.p.mashape.com/yoda?sentence=You+will+learn+how+to+speak+like+me+someday.",
+ "headers": {
+ "X-Mashape-Key": "12345678",
+ "Accept": "text/plain"
+ }
+ },
+ "response": {"text": "Learn how to speak like me someday, you will. Yes, hmmm."},
+ "response-headers": {
+ "status": 200,
+ "content-type": "text/html; charset=utf-8"
+ }
+}

ADD
zulip_bots/zulip_bots/bots/yoda/test_yoda.py
+#!/usr/bin/env python
+
+from __future__ import absolute_import
+from __future__ import print_function
+
+import json
+
+from zulip_bots.test_lib import BotTestCase
+
+class TestYodaBot(BotTestCase):
+ bot_name = "yoda"
+
+ def test_bot(self):
+ bot_response = "Learn how to speak like me someday, you will. Yes, hmmm."
+
+ with self.mock_config_info({'api_key': '12345678'}), \
+ self.mock_http_conversation('test_1'):
+ self.initialize_bot()
+ self.assert_bot_response(
+ message = {'content': 'You will learn how to speak like me someday.'},
+ response = {'content': bot_response},
+ expected_method='send_reply'
+ )

MODIFY
zulip_bots/zulip_bots/bots/yoda/yoda.py
zulip_bots/zulip_bots/bots/yoda/yoda.py
@@ -58,7 +58,6 @@ class YodaSpeakHandler(object):
def send_to_yoda_api(self, sentence):
# function for sending sentence to api
-
response = requests.get("https://yoda.p.mashape.com/yoda?sentence=" + sentence,
headers={
"X-Mashape-Key": self.api_key,

MODIFY
zulip_bots/zulip_bots/test_lib.py
zulip_bots/zulip_bots/test_lib.py
@@ -122,15 +122,20 @@ class BotTestCase(TestCase):
with patch('requests.get') as mock_get:
mock_result = mock.MagicMock()
mock_result.json.return_value = http_response
+ if 'text' in http_response:
+ mock_result.text = http_response.get('text', None)
mock_result.status_code = http_headers.get('status', 200)
mock_result.ok.return_value = http_headers.get('ok', True)
mock_get.return_value = mock_result
yield
+ if 'params' in http_request:
params = http_request.get('params', None)
- if params is None:
- mock_get.assert_called_with(http_request['api_url'])
- else:
mock_get.assert_called_with(http_request['api_url'], params=params)
+ elif 'headers' in http_request:
+ headers = http_request.get('headers', None)
+ mock_get.assert_called_with(http_request['api_url'], headers=headers)
+ else:
+ mock_get.assert_called_with(http_request['api_url'])
def assert_bot_response(self, message, response, expected_method, state_handler = None):
# type: (Dict[str, Any], Dict[str, Any], str, Optional[StateHandler]) -> None

bots: Add complete tests for offline testing of yoda bot.

Since yoda api returns response of text form, made changes
in test_lib.py to handle responses that can either be json
or plain text.