def save(self): if self._credentials_changed: store_credentials(Credentials( self._token, self._url, proxies=self.get_proxies({})), overwrite=True) self._credentials_changed = False if self._logging_config_changed or self._packages_changed: with open(self._filepath, 'w') as fp: json.dump(self._preferences, fp, sort_keys=True, indent=4) self._logging_config_changed = False self._packages_changed = False
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')
def save(self): if self._credentials_changed: try: dict = read_credentials_from_qiskitrc() if dict is not None: for credentials in dict.values(): remove_credentials(credentials) except: self._credentials = OrderedDict() for credentials in self._credentials.values(): store_credentials(credentials, overwrite=True) self._credentials_changed = False
def test_store_credentials_overwrite(self): """Test overwriting qiskitrc credentials.""" credentials = Credentials('QISKITRC_TOKEN', url=QE_URL, hub='HUB') credentials2 = Credentials('QISKITRC_TOKEN_2', url=QE_URL) with custom_qiskitrc(): store_credentials(credentials) # Cause all warnings to always be triggered. warnings.simplefilter("always") # Attempt overwriting. with warnings.catch_warnings(record=True) as w: store_credentials(credentials) self.assertIn('already present', str(w[0])) with no_file('Qconfig.py'), no_envs(), mock_ibmq_provider(): # Attempt overwriting. store_credentials(credentials2, overwrite=True) qiskit.IBMQ.load_accounts() # Ensure that the credentials are the overwritten ones - note that the # 'hub' parameter was removed. self.assertEqual(len(qiskit.IBMQ._accounts), 1) self.assertEqual( list(qiskit.IBMQ._accounts.values())[0].credentials.token, 'QISKITRC_TOKEN_2')
def test_store_credentials_overwrite(self): """Test overwriting qiskitrc credentials.""" credentials = Credentials('QISKITRC_TOKEN', url=QE_URL, hub='HUB') credentials2 = Credentials('QISKITRC_TOKEN_2', url=QE_URL) with custom_qiskitrc(): store_credentials(credentials) # Attempt overwriting. with self.assertRaises(QISKitError) as context_manager: store_credentials(credentials) self.assertIn('already present', str(context_manager.exception)) with no_file('Qconfig.py'), no_envs(), mock_ibmq_provider(): # Attempt overwriting. store_credentials(credentials2, overwrite=True) qiskit.IBMQ.load_accounts() # Ensure that the credentials are the overwritten ones - note that the # 'hub' parameter was removed. self.assertEqual(len(qiskit.IBMQ._accounts), 1) self.assertEqual(list(qiskit.IBMQ._accounts.values())[0].credentials.token, 'QISKITRC_TOKEN_2')