use environment variable

This commit is contained in:
Tyrel Souza 2016-05-24 14:18:20 -04:00
parent a452e1854e
commit 3ba246e28c
No known key found for this signature in database
GPG Key ID: 2EECB5087209E6A5
2 changed files with 20 additions and 9 deletions

View File

@ -19,14 +19,11 @@ class BackupCeligo(object):
""" """
def __init__(self, def __init__(self,
data_dir, data_dir,
api_key,
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.
:param api_key: String of api key to add to the header for
the api calls.
:param headers: Single level dict of headers to add the the :param headers: Single level dict of headers to add the the
api call session. api call session.
:param base_url: Url for the base of the api to call to. :param base_url: Url for the base of the api to call to.
@ -35,7 +32,9 @@ class BackupCeligo(object):
if not base_url: if not base_url:
base_url = DEFAULT_BASE_URL base_url = DEFAULT_BASE_URL
self.api_key = api_key self.api_key = os.environ.get("CELIGO_API_KEY", None)
if not self.api_key:
raise Exception("Please set 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

@ -13,22 +13,34 @@ class CeligoTest(unittest.TestCase):
def setUp(self): def setUp(self):
self.FAKE_API_KEY = 'fake' self.FAKE_API_KEY = 'fake'
os.environ['CELIGO_API_KEY'] = self.FAKE_API_KEY
def test_needs_all_params(self): def test_needs_all_params(self):
""" """
Test that we need to pass in both an api key and a data directory. Test that we need to pass in a data directory.
""" """
with pytest.raises(TypeError): with self.assertRaises(TypeError):
celigo.BackupCeligo() celigo.BackupCeligo()
data_dir = "fakedir" data_dir = "fakedir"
celigo.BackupCeligo(data_dir, self.FAKE_API_KEY) celigo.BackupCeligo(data_dir)
def test_needs_api_key_env(self):
"""
Test that we need to pass in a data directory.
"""
data_dir = "fakedir"
os.environ['CELIGO_API_KEY'] = ''
with self.assertRaises(Exception):
celigo.BackupCeligo(data_dir)
os.environ['CELIGO_API_KEY'] = self.FAKE_API_KEY
celigo.BackupCeligo(data_dir)
def test_ensure_directories(self): def test_ensure_directories(self):
""" """
Test that the ensure_directories_exist works properly. Test that the ensure_directories_exist works properly.
""" """
tempdir = tempfile.mkdtemp('celigo_testing') tempdir = tempfile.mkdtemp('celigo_testing')
bc = celigo.BackupCeligo(tempdir, self.FAKE_API_KEY) bc = celigo.BackupCeligo(tempdir)
imports_dir = os.path.join(tempdir, "imports") imports_dir = os.path.join(tempdir, "imports")
connections_dir = os.path.join(tempdir, "connections") connections_dir = os.path.join(tempdir, "connections")
# Check that the directories don't exist already. # Check that the directories don't exist already.
@ -56,4 +68,4 @@ class CeligoTest(unittest.TestCase):
def test_fake_requests(self, rqm): def test_fake_requests(self, rqm):
rqm.get(celigo.DEFAULT_BASE_URL, text='resp') rqm.get(celigo.DEFAULT_BASE_URL, text='resp')
tempdir = tempfile.mkdtemp('celigo_testing') tempdir = tempfile.mkdtemp('celigo_testing')
bc = celigo.BackupCeligo(tempdir, self.FAKE_API_KEY) bc = celigo.BackupCeligo(tempdir)