def test_credentials(self): datadir = os.path.join(os.path.dirname(__file__), 'data') os.environ['HOME'] = datadir # Make sure we don't have have the test solvebio directory if os.path.isdir(self.solvebiodir): shutil.rmtree(self.solvebiodir) cred_file = creds.netrc.path() self.assertTrue(os.path.exists(cred_file), "cred file created when it doesn't exist first") self.assertEqual(creds.get_credentials(), None, 'Should not find credentials') test_credentials_file = os.path.join(datadir, 'test_creds') shutil.copy(test_credentials_file, cred_file) auths = creds.get_credentials() self.assertTrue(auths is not None, 'Should find credentials') solvebio.api_host = 'https://example.com' auths = creds.get_credentials() self.assertEqual( auths, None, 'Should not find credentials for host {0}'.format( solvebio.api_host)) solvebio.api_host = 'https://api.solvebio.com' creds.delete_credentials() auths = creds.get_credentials() self.assertEqual( auths, None, 'Should not find removed credentials for ' 'host {0}'.format(solvebio.api_host)) pair = ( '*****@*****.**', 'b00b00', ) creds.save_credentials(*pair) auths = creds.get_credentials() self.assertTrue( auths is not None, 'Should get newly set credentials for ' 'host {0}'.format(solvebio.api_host)) expected = (solvebio.api_host, pair[0], 'Token', pair[1]) self.assertEqual(auths, expected, 'Should get back creds we saved')
def test_credentials(self): datadir = os.path.join(os.path.dirname(__file__), 'data') os.environ['HOME'] = datadir # Make sure we don't have have the test solvebio directory if os.path.isdir(self.solvebiodir): shutil.rmtree(self.solvebiodir) cred_file = creds.netrc.path() self.assertTrue(os.path.exists(cred_file), "cred file created when it doesn't exist first") self.assertEqual(creds.get_credentials(), None, 'Should not find credentials') test_credentials_file = os.path.join(datadir, 'test_creds') shutil.copy(test_credentials_file, cred_file) auths = creds.get_credentials() self.assertTrue(auths is not None, 'Should find credentials') solvebio.api_host = 'https://example.com' auths = creds.get_credentials() self.assertEqual(auths, None, 'Should not find credentials for host {0}' .format(solvebio.api_host)) solvebio.api_host = 'https://api.solvebio.com' creds.delete_credentials() auths = creds.get_credentials() self.assertEqual(auths, None, 'Should not find removed credentials for ' 'host {0}'.format(solvebio.api_host)) pair = ('*****@*****.**', 'b00b00',) creds.save_credentials(*pair) auths = creds.get_credentials() self.assertTrue(auths is not None, 'Should get newly set credentials for ' 'host {0}'.format(solvebio.api_host)) self.assertEqual(auths, pair[1], 'Should get back creds we saved')
def _get_api_key(self): """ Helper function to get the current user's API key or None. """ if solvebio.api_key: return solvebio.api_key try: return get_credentials()[1] except: pass return None
from django.conf import settings import solvebio API_HOST = getattr(settings, 'SOLVEBIO_API_HOST', None) API_KEY = getattr(settings, 'SOLVEBIO_API_KEY', None) DATASET_ALIASES = getattr(settings, 'SOLVEBIO_DATASET_ALIASES', {}) if API_HOST: solvebio.api_host = API_HOST if API_KEY: solvebio.api_key = API_KEY else: # attempt to get API key from get_credentials (local .netrc) from solvebio.cli.credentials import get_credentials try: solvebio.api_key = API_KEY = get_credentials()[1] except: pass