bots: Move mock http conversations to fixtures.

This commit is contained in:
Robert Hönig 2017-06-13 14:50:24 +02:00 committed by Tim Abbott
parent 2d94fd16ba
commit c193443541
4 changed files with 49 additions and 50 deletions

View file

@ -0,0 +1,7 @@
{
"api_url": "http://api.giphy.com/v1/gifs/translate",
"params": {
"s": "Hello",
"api_key": "12345678"
}
}

View file

@ -0,0 +1,12 @@
{
"meta": {
"status": 200
},
"data": {
"images": {
"original": {
"url": "https://media4.giphy.com/media/3o6ZtpxSZbQRRnwCKQ/giphy.gif"
}
}
}
}

View file

@ -13,48 +13,18 @@ sys.path.insert(0, os.path.normpath(os.path.join(our_dir)))
if os.path.exists(os.path.join(our_dir, '..')): if os.path.exists(os.path.join(our_dir, '..')):
sys.path.insert(0, '..') sys.path.insert(0, '..')
from bots_test_lib import BotTestCase from bots_test_lib import BotTestCase
from bots.giphy import giphy
def get_http_response_json(gif_url):
response_json = {
'meta': {
'status': 200
},
'data': {
'images': {
'original': {
'url': gif_url
}
}
}
}
return response_json
def get_bot_response(gif_url):
return ('[Click to enlarge](%s)'
'[](/static/images/interactive-bot/giphy/powered-by-giphy.png)'
% (gif_url))
def get_http_request(keyword):
return {
'api_url': giphy.GIPHY_TRANSLATE_API,
'params': {
's': keyword,
'api_key': giphy.get_giphy_api_key_from_config()
}
}
class TestGiphyBot(BotTestCase): class TestGiphyBot(BotTestCase):
bot_name = "giphy" bot_name = "giphy"
def test_bot(self): def test_bot(self):
# This message calls `send_reply` function of BotHandlerApi bot_response = '[Click to enlarge]' \
keyword = "Hello" '(https://media4.giphy.com/media/3o6ZtpxSZbQRRnwCKQ/giphy.gif)' \
gif_url = "https://media4.giphy.com/media/3o6ZtpxSZbQRRnwCKQ/giphy.gif" '[](/static/images/interactive-bot/giphy/powered-by-giphy.png)'
with self.mock_http_conversation(get_http_request(keyword), # This message calls the `send_reply` function of BotHandlerApi
get_http_response_json(gif_url)): with self.mock_http_conversation('test_1'):
self.assert_bot_response( self.assert_bot_response(
message = {'content': keyword}, message = {'content': 'Hello'},
response = {'content': get_bot_response(gif_url)}, response = {'content': bot_response},
expected_method='send_reply' expected_method='send_reply'
) )

View file

@ -5,10 +5,12 @@ from __future__ import print_function
import os import os
import sys import sys
import unittest
import json
import logging import logging
import requests
import mock import mock
import requests
import unittest
from mock import MagicMock, patch from mock import MagicMock, patch
@ -76,15 +78,23 @@ class BotTestCase(TestCase):
instance.send_reply.assert_called_with(message, response['content']) instance.send_reply.assert_called_with(message, response['content'])
@contextmanager @contextmanager
def mock_http_conversation(self, http_request, http_response): def mock_http_conversation(self, test_name):
# type: (Dict[str, Any], Dict[str, Any]) -> Any # type: (str) -> Any
""" """
Use this context manager to mock and verify a bot's HTTP Use this context manager to mock and verify a bot's HTTP
requests to the third-party API (and provide the correct requests to the third-party API (and provide the correct
third-party API response. This allows us to test things third-party API response. This allows us to test things
that would require the Internet without it). that would require the Internet without it).
""" """
assert http_response is not None and http_request is not None assert test_name is not None
base_path = os.path.realpath(os.path.join(os.path.dirname(
os.path.abspath(__file__)), '..', 'bots', self.bot_name, 'fixtures'))
http_request_path = os.path.join(base_path, '{}_request.json'.format(test_name))
http_response_path = os.path.join(base_path, '{}_response.json'.format(test_name))
with open(http_request_path, 'r') as http_request_file, \
open(http_response_path, 'r') as http_response_file:
http_request = json.load(http_request_file)
http_response = json.load(http_response_file)
with patch('requests.get') as mock_get: with patch('requests.get') as mock_get:
mock_result = mock.MagicMock() mock_result = mock.MagicMock()
mock_result.json.return_value = http_response mock_result.json.return_value = http_response