Test api key in env var, or passed in

This commit is contained in:
Tyrel Souza 2016-05-24 15:07:36 -04:00
parent 3ba246e28c
commit c8c7f39b56
No known key found for this signature in database
GPG Key ID: 2EECB5087209E6A5
2 changed files with 26 additions and 7 deletions

View File

@ -19,8 +19,10 @@ class BackupCeligo(object):
""" """
def __init__(self, def __init__(self,
data_dir, data_dir,
api_key=None,
headers=None, headers=None,
base_url=None,): base_url=None,
):
""" """
:param data_dir: The directory to read and store JSON files :param data_dir: The directory to read and store JSON files
for imports and configurations. for imports and configurations.
@ -32,9 +34,16 @@ class BackupCeligo(object):
if not base_url: if not base_url:
base_url = DEFAULT_BASE_URL base_url = DEFAULT_BASE_URL
self.api_key = os.environ.get("CELIGO_API_KEY", None) # Set api key if it was passed in
self.api_key = api_key
if not self.api_key: if not self.api_key:
raise Exception("Please set CELIGO_API_KEY environment variable") # if this is still falsy, set from environment variable.
self.api_key = os.environ.get("CELIGO_API_KEY")
if not self.api_key:
# Needs an API key, raise exceptions
raise Exception(
"Please pass in api_key, or set the CELIGO_API_KEY "
"environment variable.")
self.data_dir = data_dir self.data_dir = data_dir
self.base_url = base_url self.base_url = base_url
self.imports_cache = {} self.imports_cache = {}

View File

@ -24,16 +24,26 @@ class CeligoTest(unittest.TestCase):
data_dir = "fakedir" data_dir = "fakedir"
celigo.BackupCeligo(data_dir) celigo.BackupCeligo(data_dir)
def test_needs_api_key_env(self): def test_needs_api_key(self):
""" """
Test that we need to pass in a data directory. Test that we need to either pass in the api_key,
or set the CELIGO_API_KEY environment variable.
""" """
data_dir = "fakedir" data_dir = "fakedir"
os.environ['CELIGO_API_KEY'] = '' os.environ['CELIGO_API_KEY'] = ''
with self.assertRaises(Exception): with self.assertRaisesRegexp(Exception,
'Please pass in api_key.*'):
celigo.BackupCeligo(data_dir) celigo.BackupCeligo(data_dir)
os.environ['CELIGO_API_KEY'] = self.FAKE_API_KEY os.environ['CELIGO_API_KEY'] = self.FAKE_API_KEY
celigo.BackupCeligo(data_dir) bc = celigo.BackupCeligo(data_dir)
# Make sure it uses the env var one
self.assertEqual(bc.api_key, self.FAKE_API_KEY)
# Unset env var and make sure it uses the passed in one
os.environ['CELIGO_API_KEY'] = ''
bc = celigo.BackupCeligo(data_dir, api_key='fake_passed_in')
self.assertEqual(bc.api_key, 'fake_passed_in')
def test_ensure_directories(self): def test_ensure_directories(self):
""" """