From f6f09202acef8feb0011b05201430cc9568ad4a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6nig?= Date: Thu, 4 Jan 2018 16:14:35 +0100 Subject: [PATCH] baremetrics bot: Quit on invalid API key. --- zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py | 6 +++--- .../zulip_bots/bots/baremetrics/test_baremetrics.py | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py b/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py index a41097a..5d29add 100644 --- a/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py +++ b/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py @@ -21,16 +21,16 @@ class BaremetricsHandler(object): 'List the sources', 'List the plans for the source', 'List the customers in the source', 'List the subscriptions in the source'] - self.check_api_key() + self.check_api_key(bot_handler) - def check_api_key(self) -> None: + def check_api_key(self, bot_handler) -> None: url = "https://api.baremetrics.com/v1/account" test_query_response = requests.get(url, headers=self.auth_header) test_query_data = test_query_response.json() try: if test_query_data['error'] == "Unauthorized. Token not found": - logging.error('API Key not valid. Please see doc.md to find out how to get it.') + bot_handler.quit('API Key not valid. Please see doc.md to find out how to get it.') except KeyError: pass diff --git a/zulip_bots/zulip_bots/bots/baremetrics/test_baremetrics.py b/zulip_bots/zulip_bots/bots/baremetrics/test_baremetrics.py index cd0b0e4..6751d11 100644 --- a/zulip_bots/zulip_bots/bots/baremetrics/test_baremetrics.py +++ b/zulip_bots/zulip_bots/bots/baremetrics/test_baremetrics.py @@ -1,14 +1,17 @@ +from unittest.mock import patch from zulip_bots.test_lib import BotTestCase class TestBaremetricsBot(BotTestCase): bot_name = "baremetrics" def test_bot_responds_to_empty_message(self) -> None: - with self.mock_config_info({'api_key': 'TEST'}): + with self.mock_config_info({'api_key': 'TEST'}), \ + patch('requests.get'): self.verify_reply('', 'No Command Specified') def test_help_query(self) -> None: - with self.mock_config_info({'api_key': 'TEST'}): + with self.mock_config_info({'api_key': 'TEST'}), \ + patch('requests.get'): self.verify_reply('help', ''' This bot gives updates about customer behavior, financial performance, and analytics for an organization using the Baremetrics Api.\n @@ -17,7 +20,8 @@ class TestBaremetricsBot(BotTestCase): ''') def test_list_commands_command(self) -> None: - with self.mock_config_info({'api_key': 'TEST'}): + with self.mock_config_info({'api_key': 'TEST'}), \ + patch('requests.get'): self.verify_reply('list-commands', '**Available Commands:** \n - help : Display bot info\n - list-commands ' ': Display the list of available commands\n - account-info : Display ' 'the account info\n - list-sources : List the sources\n - list-plans '