baremetrics bot: Quit on invalid API key.

This commit is contained in:
Robert Hönig 2018-01-04 16:14:35 +01:00 committed by showell
parent c86e62a06a
commit f6f09202ac
2 changed files with 10 additions and 6 deletions

View file

@ -21,16 +21,16 @@ class BaremetricsHandler(object):
'List the sources', 'List the plans for the source', 'List the customers in the source', 'List the sources', 'List the plans for the source', 'List the customers in the source',
'List the subscriptions 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" url = "https://api.baremetrics.com/v1/account"
test_query_response = requests.get(url, headers=self.auth_header) test_query_response = requests.get(url, headers=self.auth_header)
test_query_data = test_query_response.json() test_query_data = test_query_response.json()
try: try:
if test_query_data['error'] == "Unauthorized. Token not found": 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: except KeyError:
pass pass

View file

@ -1,14 +1,17 @@
from unittest.mock import patch
from zulip_bots.test_lib import BotTestCase from zulip_bots.test_lib import BotTestCase
class TestBaremetricsBot(BotTestCase): class TestBaremetricsBot(BotTestCase):
bot_name = "baremetrics" bot_name = "baremetrics"
def test_bot_responds_to_empty_message(self) -> None: 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') self.verify_reply('', 'No Command Specified')
def test_help_query(self) -> None: 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', ''' self.verify_reply('help', '''
This bot gives updates about customer behavior, financial performance, and analytics This bot gives updates about customer behavior, financial performance, and analytics
for an organization using the Baremetrics Api.\n for an organization using the Baremetrics Api.\n
@ -17,7 +20,8 @@ class TestBaremetricsBot(BotTestCase):
''') ''')
def test_list_commands_command(self) -> None: 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 ' 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 ' ': Display the list of available commands\n - account-info : Display '
'the account info\n - list-sources : List the sources\n - list-plans ' 'the account info\n - list-sources : List the sources\n - list-plans '