python-zulip-api/bots/giphy/test_giphy.py
Robert Hönig 01c363317e bots: tests: Move http mock to context manager.
This commit decouples the http mock conversation
feature from assert_bot_response(), and moves it
to the context manager mock_http_conversation().
This allows a modular design with context managers
that could be added for assert_bot_response().
2017-06-09 05:25:17 -05:00

61 lines
1.7 KiB
Python

#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import print_function
import os
import sys
import json
our_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.normpath(os.path.join(our_dir)))
# For dev setups, we can find the API in the repo itself.
if os.path.exists(os.path.join(our_dir, '..')):
sys.path.insert(0, '..')
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):
bot_name = "giphy"
def test_bot(self):
# This message calls `send_reply` function of BotHandlerApi
keyword = "Hello"
gif_url = "https://media4.giphy.com/media/3o6ZtpxSZbQRRnwCKQ/giphy.gif"
with self.mock_http_conversation(get_http_request(keyword),
get_http_response_json(gif_url)):
self.assert_bot_response(
message = {'content': keyword},
response = {'content': get_bot_response(gif_url)},
expected_method='send_reply'
)