def test_auth2_credentials(self): """Test converting already API 2 auth credentials.""" with custom_qiskitrc(): store_credentials(Credentials('A', url=QE2_AUTH_URL)) credentials = update_credentials(force=True) # No credentials should be returned. self.assertIsNone(credentials)
def test_api2_non_auth_credentials(self): """Test converting api 2 non auth credentials.""" with custom_qiskitrc(): store_credentials(Credentials('A', url=QE2_URL)) _ = update_credentials(force=True) # Assert over the stored (updated) credentials. loaded_accounts, _ = read_credentials_from_qiskitrc() self.assertCorrectApi2Credentials('A', loaded_accounts)
def test_qconsole_credentials(self): """Test converting Qconsole credentials.""" with custom_qiskitrc(): store_credentials(Credentials('A', url=IBMQ_TEMPLATE.format('a', 'b', 'c'))) _ = update_credentials(force=True) # Assert over the stored (updated) credentials. loaded_accounts, _ = read_credentials_from_qiskitrc() self.assertCorrectApi2Credentials('A', loaded_accounts)
def test_unknown_credentials(self): """Test converting credentials with an unknown URL.""" with custom_qiskitrc(): store_credentials(Credentials('A', url='UNKNOWN_URL')) credentials = update_credentials(force=True) # No credentials should be returned nor updated. self.assertIsNone(credentials) loaded_accounts, _ = read_credentials_from_qiskitrc() self.assertEqual(list(loaded_accounts.values())[0].url, 'UNKNOWN_URL')
def test_environ_over_qiskitrc(self): """Test credential discovery order.""" credentials = Credentials('QISKITRC_TOKEN', url=QE2_AUTH_URL) with custom_qiskitrc(): # Prepare the credentials: both env and qiskitrc present store_credentials(credentials) with 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 test_proxy_credentials(self): """Test converting credentials with proxy values.""" with custom_qiskitrc(): store_credentials(Credentials('A', url=IBMQ_TEMPLATE.format('a', 'b', 'c'), proxies=PROXIES)) _ = update_credentials(force=True) # Assert over the stored (updated) credentials. loaded_accounts, _ = read_credentials_from_qiskitrc() self.assertCorrectApi2Credentials('A', loaded_accounts) # Extra assert on preserving proxies. credentials = list(loaded_accounts.values())[0] self.assertEqual(credentials.proxies, PROXIES)
def test_store_credentials_overwrite(self): """Test overwriting qiskitrc credentials.""" credentials = Credentials('QISKITRC_TOKEN', url=QE2_AUTH_URL) credentials2 = Credentials('QISKITRC_TOKEN_2', url=QE2_AUTH_URL) factory = IBMQFactory() with custom_qiskitrc(): store_credentials(credentials) # Cause all warnings to always be triggered. warnings.simplefilter("always") # Get the logger for `store_credentials`. config_rc_logger = logging.getLogger(store_credentials.__module__) # Attempt overwriting. with self.assertLogs(logger=config_rc_logger, level='WARNING') as log_records: store_credentials(credentials) self.assertIn('already present', log_records.output[0]) with no_envs(CREDENTIAL_ENV_VARS), mock_ibmq_provider(): # Attempt overwriting. store_credentials(credentials2, overwrite=True) factory.load_account() # Ensure that the credentials are the overwritten ones. self.assertEqual(factory._credentials, credentials2)
def test_multiple_credentials(self): """Test converting multiple credentials.""" with custom_qiskitrc(): store_credentials(Credentials('A', url=QE2_AUTH_URL)) store_credentials(Credentials('B', url=IBMQ_TEMPLATE.format('a', 'b', 'c'))) store_credentials(Credentials('C', url=IBMQ_TEMPLATE.format('d', 'e', 'f'))) _ = update_credentials(force=True) # Assert over the stored (updated) credentials. loaded_accounts, _ = read_credentials_from_qiskitrc() # We don't assert over the token, as it depends on the order of # the qiskitrc, which is not guaranteed. self.assertCorrectApi2Credentials(None, loaded_accounts)