def __init__(self): """Create Preferences object.""" self._preferences = { 'version': Preferences._VERSION } self._packages_changed = False self._credentials_changed = False self._logging_config_changed = False self._token = None self._url = Preferences.URL self._proxy_urls = None credentials = discover_credentials() if credentials is not None: credentials = list(credentials.values()) if len(credentials) > 0: credentials = credentials[0] self._token = credentials.token self._url = credentials.url if 'urls' in credentials.proxies: self._proxy_urls = credentials.proxies['urls'] home = os.path.expanduser("~") self._filepath = os.path.join(home, Preferences._FILENAME) try: with open(self._filepath) as json_pref: self._preferences = json.load(json_pref) except: pass
def _get_credentials(test_object, test_options): """ Finds the credentials for a specific test and options. Args: test_object (QiskitTestCase): The test object asking for credentials test_options (dict): Options after QISKIT_TESTS was parsed by get_test_options. Returns: Credentials: set of credentials Raises: Exception: When the credential could not be set and they are needed for that set of options """ dummy_credentials = Credentials( 'dummyapiusersloginWithTokenid01', 'https://quantumexperience.ng.bluemix.net/api') if test_options['mock_online']: return dummy_credentials if os.getenv('USE_ALTERNATE_ENV_CREDENTIALS', False): # Special case: instead of using the standard credentials mechanism, # load them from different environment variables. This assumes they # will always be in place, as is used by the Travis setup. return Credentials(os.getenv('IBMQ_TOKEN'), os.getenv('IBMQ_URL')) else: # Attempt to read the standard credentials. discovered_credentials = discover_credentials() if discovered_credentials: # Decide which credentials to use for testing. if len(discovered_credentials) > 1: try: # Attempt to use QE credentials. return discovered_credentials[ dummy_credentials.unique_id()] except KeyError: pass # Use the first available credentials. return list(discovered_credentials.values())[0] # No user credentials were found. if test_options['rec']: raise Exception( 'Could not locate valid credentials. You need them for recording ' 'tests against the remote API.') test_object.log.warning( "No user credentials were detected. Running with mocked data.") test_options['mock_online'] = True return dummy_credentials
def test_qconfig_over_all(self): """Test order, with qconfig""" credentials = Credentials('QISKITRC_TOKEN', url=QE_URL) with custom_qiskitrc(): # Prepare the credentials: qconfig, env and qiskitrc present store_credentials(credentials) with custom_qconfig(b"APItoken='QCONFIG_TOKEN'"),\ custom_envs({'QE_TOKEN': 'ENVIRON_TOKEN'}): credentials = discover_credentials() self.assertEqual(len(credentials), 1) self.assertEqual(list(credentials.values())[0].token, 'QCONFIG_TOKEN')
def test_environ_over_qiskitrc(self): """Test order, without qconfig""" credentials = Credentials('QISKITRC_TOKEN', url=QE_URL) with custom_qiskitrc(): # Prepare the credentials: both env and qiskitrc present store_credentials(credentials) with no_file('Qconfig.py'), custom_envs({'QE_TOKEN': 'ENVIRON_TOKEN', 'QE_URL': 'ENVIRON_URL'}): credentials = discover_credentials() self.assertEqual(len(credentials), 1) self.assertEqual(list(credentials.values())[0].token, 'ENVIRON_TOKEN')