From 84c1827291c8787f22668fa13c676dc75af1e7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6nig?= Date: Sat, 10 Jun 2017 19:33:22 +0200 Subject: [PATCH] bots: Test get_config_info with giphy bot. This involves both using the new `initialize` method for calling `get_config_info`, and refactoring the testing framework by adding a way for mocking this method. --- bots/giphy/giphy.py | 20 +++++++++----------- bots/giphy/test_giphy.py | 4 +++- bots_api/bots_test_lib.py | 11 +++++++++++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/bots/giphy/giphy.py b/bots/giphy/giphy.py index 159a3e4..5dd95cd 100644 --- a/bots/giphy/giphy.py +++ b/bots/giphy/giphy.py @@ -12,9 +12,10 @@ import re GIPHY_TRANSLATE_API = 'http://api.giphy.com/v1/gifs/translate' -if not os.path.exists(os.environ['HOME'] + '/.giphy_config'): - print('Giphy bot config file not found, please set up it in ~/.giphy_config' - '\n\nUsing format:\n\n[giphy-config]\nkey=\n\n') +if not os.path.exists(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'giphy.conf')): + print('Giphy bot config file not found, please set it up in this bot\'s folder ' + 'with the name \'giphy.conf\'\n\nUsing format:\n\n[giphy-config]\nkey=\n\n') sys.exit(1) @@ -33,6 +34,10 @@ class GiphyHandler(object): The bot responds also to private messages. ''' + def initialize(self, bot_handler): + global config_info + config_info = bot_handler.get_config_info('giphy') + def handle_message(self, message, bot_handler, state_handler): bot_response = get_bot_giphy_response(message, bot_handler) bot_handler.send_reply(message, bot_response) @@ -42,13 +47,6 @@ class GiphyNoResultException(Exception): pass -def get_giphy_api_key_from_config(): - config = SafeConfigParser() - with open(os.environ['HOME'] + '/.giphy_config', 'r') as config_file: - config.readfp(config_file) - return config.get("giphy-config", "key") - - def get_url_gif_giphy(keyword, api_key): # Return a URL for a Giphy GIF based on keywords given. # In case of error, e.g. failure to fetch a GIF URL, it will @@ -78,7 +76,7 @@ def get_bot_giphy_response(message, bot_handler): # The bot will post the appropriate message for the error. keyword = message['content'] try: - gif_url = get_url_gif_giphy(keyword, get_giphy_api_key_from_config()) + gif_url = get_url_gif_giphy(keyword, config_info['key']) except requests.exceptions.ConnectionError: return ('Uh oh, sorry :slightly_frowning_face:, I ' 'cannot process your request right now. But, ' diff --git a/bots/giphy/test_giphy.py b/bots/giphy/test_giphy.py index 847571a..98d277b 100644 --- a/bots/giphy/test_giphy.py +++ b/bots/giphy/test_giphy.py @@ -22,7 +22,9 @@ class TestGiphyBot(BotTestCase): '(https://media4.giphy.com/media/3o6ZtpxSZbQRRnwCKQ/giphy.gif)' \ '[](/static/images/interactive-bot/giphy/powered-by-giphy.png)' # This message calls the `send_reply` function of BotHandlerApi - with self.mock_http_conversation('test_1'): + with self.mock_config_info({'key': '12345678'}), \ + self.mock_http_conversation('test_1'): + self.initialize_bot() self.assert_bot_response( message = {'content': 'Hello'}, response = {'content': bot_response}, diff --git a/bots_api/bots_test_lib.py b/bots_api/bots_test_lib.py index 8bc34f4..bac843a 100644 --- a/bots_api/bots_test_lib.py +++ b/bots_api/bots_test_lib.py @@ -52,6 +52,10 @@ class BotTestCase(TestCase): # type: () -> None self.patcher.stop() + def initialize_bot(self): + # type: () -> None + self.message_handler.initialize(self.MockClass()) + def check_expected_responses(self, expectations, expected_method='send_reply', email="foo_sender@zulip.com", recipient="foo", subject="foo", type="all"): @@ -87,6 +91,13 @@ class BotTestCase(TestCase): else: instance.send_reply.assert_called_with(message, response['content']) + @contextmanager + def mock_config_info(self, config_info): + # type: (Dict[str, str]) -> Any + self.MockClass.return_value.get_config_info.return_value = config_info + yield + self.MockClass.return_value.get_config_info.return_value = None + @contextmanager def mock_http_conversation(self, test_name): # type: (str) -> Any