def get_api_key(): """Retrieve and validate the API Key (from memory, config or user input)""" if API_KEY not in CREDS: key, source = read_or_request_api_key() if not validate_api_key(key): log('The current API key is invalid or expired.', error=True) key, source = request_api_key(), 'request' if not validate_api_key(key): raise ApiError('The API key provided is invalid or expired.') write_api_key(key) return key return CREDS[API_KEY]
def test_read_or_request_api_key_none(mock_prompt): with fake_creds(): purge_api_key() assert read_or_request_api_key() == ('fake_api_key', 'request')
def test_read_or_request_api_key(): with fake_creds(): assert read_or_request_api_key() == ('fake_api_key', 'read')
def test_read_or_request_api_key_none(mock_prompt): with fake_creds('.jovian', 'credentials.json'): purge_api_key() assert read_or_request_api_key() == ('fake_api_key', 'request')
def test_read_or_request_api_key(): with fake_creds('.jovian', 'credentials.json'): assert read_or_request_api_key() == ('fake_api_key', 'read')