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) # Use an IBMQProvider instead of a Factory. provider = IBMQProvider() 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( CREDENTIAL_ENV_VARS), mock_ibmq_provider(): # Attempt overwriting. store_credentials(credentials2, overwrite=True) provider.load_accounts() # Ensure that the credentials are the overwritten ones - note that the # 'hub' parameter was removed. self.assertEqual(len(provider._accounts), 1) self.assertEqual( list(provider._accounts.values())[0].credentials.token, 'QISKITRC_TOKEN_2')
def setUp(self): super().setUp() # Reference for saving accounts. self.ibmq = IBMQProvider() # Avoid stdout output during tests. self.patcher = patch('sys.stdout', new=StringIO()) self.patcher.start()
def test_api1_get_backend(self, qe_token, qe_url): """Test backward compatibility for API 1 get_backend().""" ibmq = IBMQFactory() ibmq.enable_account(qe_token, qe_url) ibmq_provider = IBMQProvider() ibmq_provider.enable_account(qe_token, qe_url) backend = ibmq_provider.backends()[0] with self.assertWarns(DeprecationWarning): ibmq_backend = ibmq.get_backend(backend.name()) self.assertEqual(backend.name(), ibmq_backend.name())
def test_api1_delete_accounts(self): """Test backward compatibility for API 1 delete_accounts().""" ibmq_provider = IBMQProvider() ibmq_factory = IBMQFactory() with custom_qiskitrc(): ibmq_provider.save_account('QISKITRC_TOKEN', url=API1_URL) with self.assertWarns(DeprecationWarning): ibmq_factory.delete_accounts() with self.assertWarns(DeprecationWarning): stored_accounts = ibmq_factory.stored_accounts() self.assertEqual(len(stored_accounts), 0)
def test_api1_backends(self, qe_token, qe_url): """Test backward compatibility for API 1 backends().""" ibmq = IBMQFactory() ibmq.enable_account(qe_token, qe_url) ibmq_provider = IBMQProvider() ibmq_provider.enable_account(qe_token, qe_url) ibmq_provider_backend_names = [ b.name() for b in ibmq_provider.backends() ] with self.assertWarns(DeprecationWarning): ibmq_backend_names = [b.name() for b in ibmq.backends()] self.assertEqual(set(ibmq_backend_names), set(ibmq_provider_backend_names))
def setUp(self): super().setUp() # Reference for saving accounts. self.factory = IBMQFactory() self.provider = IBMQProvider()
class TestIBMQFactoryAccounts(IBMQTestCase): """Tests for the IBMQ account handling.""" @classmethod def setUpClass(cls): cls.v2_token = 'API2_TOKEN' cls.v1_token = 'API1_TOKEN' def setUp(self): super().setUp() # Reference for saving accounts. self.factory = IBMQFactory() self.provider = IBMQProvider() def test_save_account_v2(self): """Test saving an API 2 account.""" with custom_qiskitrc(): self.factory.save_account(self.v2_token, url=AUTH_URL) stored_cred = self.factory.stored_account() self.assertEqual(stored_cred['token'], self.v2_token) self.assertEqual(stored_cred['url'], AUTH_URL) def test_stored_account_v1(self): """Test listing a stored API 1 account.""" with custom_qiskitrc(): self.provider.save_account(self.v1_token, url=API1_URL) with self.assertRaises(IBMQAccountError): self.factory.stored_account() def test_delete_account_v2(self): """Test deleting an API 2 account.""" with custom_qiskitrc(): self.factory.save_account(self.v2_token, url=AUTH_URL) self.factory.delete_account() stored_cred = self.factory.stored_account() self.assertEqual(len(stored_cred), 0) def test_delete_account_v1(self): """Test deleting an API 1 account.""" with custom_qiskitrc(): self.provider.save_account(self.v1_token, url=API1_URL) with self.assertRaises(IBMQAccountError): self.factory.delete_account() @requires_qe_access @requires_new_api_auth def test_load_account_v2(self, qe_token, qe_url): """Test loading an API 2 account.""" if qe_url != QX_AUTH_URL: # .save_account() expects an auth 2 production URL. self.skipTest('Test requires production auth URL') with no_file('Qconfig.py'), custom_qiskitrc(), no_envs( CREDENTIAL_ENV_VARS): self.factory.save_account(qe_token, url=qe_url) self.factory.load_account() self.assertEqual(self.factory._credentials.token, qe_token) self.assertEqual(self.factory._credentials.url, qe_url) self.assertEqual(self.factory._v1_provider._accounts, {}) def test_load_account_v1(self): """Test loading an API 1 account.""" with no_file('Qconfig.py'), custom_qiskitrc(), no_envs( CREDENTIAL_ENV_VARS): self.provider.save_account(self.v1_token, url=API1_URL) with self.assertRaises(IBMQAccountError): self.factory.load_account() @requires_qe_access @requires_new_api_auth def test_disable_account_v2(self, qe_token, qe_url): """Test disabling an API 2 account """ self.factory.enable_account(qe_token, qe_url) self.factory.disable_account() self.assertIsNone(self.factory._credentials) @requires_qe_access @requires_classic_api def test_disable_account_v1(self, qe_token, qe_url): """Test disabling an API 1 account """ self.factory.enable_account(qe_token, qe_url) with self.assertRaises(IBMQAccountError): self.factory.disable_account() @requires_qe_access @requires_new_api_auth def test_active_account_v2(self, qe_token, qe_url): """Test active_account for an API 2 account """ self.assertIsNone(self.factory.active_account()) self.factory.enable_account(qe_token, qe_url) active_account = self.factory.active_account() self.assertIsNotNone(active_account) self.assertEqual(active_account['token'], qe_token) self.assertEqual(active_account['url'], qe_url) @requires_qe_access @requires_classic_api def test_active_account_v1(self, qe_token, qe_url): """Test active_account for an API 1 account """ self.factory.enable_account(qe_token, qe_url) with self.assertRaises(IBMQAccountError): self.factory.active_account()
def setUp(self): super().setUp() # Use an IBMQProvider instead of a Factory. self.provider = IBMQProvider()
class TestIBMQProviderAccounts(IBMQTestCase): """Tests for the IBMQProvider account handling.""" def setUp(self): super().setUp() # Use an IBMQProvider instead of a Factory. self.provider = IBMQProvider() def test_enable_account(self): """Test enabling one account.""" with custom_qiskitrc(), mock_ibmq_provider(): self.provider.enable_account('QISKITRC_TOKEN', url='someurl', proxies=PROXIES) # Compare the session accounts with the ones stored in file. loaded_accounts = read_credentials_from_qiskitrc() _, provider = list(self.provider._accounts.items())[0] self.assertEqual(loaded_accounts, {}) self.assertEqual('QISKITRC_TOKEN', provider.credentials.token) self.assertEqual('someurl', provider.credentials.url) self.assertEqual(PROXIES, provider.credentials.proxies) def test_enable_multiple_accounts(self): """Test enabling multiple accounts, combining QX and IBMQ.""" with custom_qiskitrc(), mock_ibmq_provider(): self.provider.enable_account('QISKITRC_TOKEN') self.provider.enable_account('QISKITRC_TOKEN', url=IBMQ_TEMPLATE.format( 'a', 'b', 'c')) self.provider.enable_account('QISKITRC_TOKEN', url=IBMQ_TEMPLATE.format( 'a', 'b', 'X')) # Compare the session accounts with the ones stored in file. loaded_accounts = read_credentials_from_qiskitrc() self.assertEqual(loaded_accounts, {}) self.assertEqual(len(self.provider._accounts), 3) def test_enable_duplicate_accounts(self): """Test enabling the same credentials twice.""" with custom_qiskitrc(), mock_ibmq_provider(): self.provider.enable_account('QISKITRC_TOKEN') self.assertEqual(len(self.provider._accounts), 1) def test_save_account(self): """Test saving one account.""" with custom_qiskitrc(), mock_ibmq_provider(): self.provider.save_account('QISKITRC_TOKEN', url=QE_URL, proxies=PROXIES) # Compare the session accounts with the ones stored in file. stored_accounts = read_credentials_from_qiskitrc() self.assertEqual(len(stored_accounts.keys()), 1) def test_save_multiple_accounts(self): """Test saving several accounts, combining QX and IBMQ""" with custom_qiskitrc(), mock_ibmq_provider(): self.provider.save_account('QISKITRC_TOKEN') self.provider.save_account('QISKITRC_TOKEN', url=IBMQ_TEMPLATE.format('a', 'b', 'c')) self.provider.save_account('QISKITRC_TOKEN', IBMQ_TEMPLATE.format('a', 'b', 'X')) # Compare the session accounts with the ones stored in file. stored_accounts = read_credentials_from_qiskitrc() self.assertEqual(len(stored_accounts), 3) for account_name, provider in self.provider._accounts.items(): self.assertEqual(provider.credentials, stored_accounts[account_name]) def test_save_duplicate_accounts(self): """Test saving the same credentials twice.""" with custom_qiskitrc(), mock_ibmq_provider(): self.provider.save_account('QISKITRC_TOKEN') with self.assertWarns(UserWarning) as context_manager: self.provider.save_account('QISKITRC_TOKEN') self.assertIn('Set overwrite', str(context_manager.warning)) # Compare the session accounts with the ones stored in file. stored_accounts = read_credentials_from_qiskitrc() self.assertEqual(len(stored_accounts), 1) def test_disable_accounts(self): """Test disabling an account in a session.""" with custom_qiskitrc(), mock_ibmq_provider(): self.provider.enable_account('QISKITRC_TOKEN') self.provider.disable_accounts(token='QISKITRC_TOKEN') self.assertEqual(len(self.provider._accounts), 0) def test_delete_accounts(self): """Test deleting an account from disk.""" with custom_qiskitrc(), mock_ibmq_provider(): self.provider.save_account('QISKITRC_TOKEN') self.assertEqual(len(read_credentials_from_qiskitrc()), 1) self.provider._accounts.clear() self.provider.delete_accounts(token='QISKITRC_TOKEN') self.assertEqual(len(read_credentials_from_qiskitrc()), 0) def test_disable_all_accounts(self): """Test disabling all accounts from session.""" with custom_qiskitrc(), mock_ibmq_provider(): self.provider.enable_account('QISKITRC_TOKEN') self.provider.enable_account('QISKITRC_TOKEN', url=IBMQ_TEMPLATE.format( 'a', 'b', 'c')) self.provider.disable_accounts() self.assertEqual(len(self.provider._accounts), 0) def test_delete_all_accounts(self): """Test deleting all accounts from disk.""" with custom_qiskitrc(), mock_ibmq_provider(): self.provider.save_account('QISKITRC_TOKEN') self.provider.save_account('QISKITRC_TOKEN', url=IBMQ_TEMPLATE.format('a', 'b', 'c')) self.assertEqual(len(read_credentials_from_qiskitrc()), 2) self.provider.delete_accounts() self.assertEqual(len(self.provider._accounts), 0) self.assertEqual(len(read_credentials_from_qiskitrc()), 0) def test_pass_bad_proxy(self): """Test proxy pass through.""" with self.assertRaises(ConnectionError) as context_manager: self.provider.enable_account('dummy_token', 'https://dummy_url', proxies=PROXIES) self.assertIn('ProxyError', str(context_manager.exception))
class TestIBMQAccountUpdater(IBMQTestCase): """Tests for the update_credentials() helper.""" def setUp(self): super().setUp() # Reference for saving accounts. self.ibmq = IBMQProvider() # Avoid stdout output during tests. self.patcher = patch('sys.stdout', new=StringIO()) self.patcher.start() def tearDown(self): super().tearDown() # Reenable stdout output. self.patcher.stop() def assertCorrectApi2Credentials(self, token, credentials_dict): """Asserts that there is only one credentials belonging to API 2.""" self.assertEqual(len(credentials_dict), 1) credentials = list(credentials_dict.values())[0] self.assertEqual(credentials.url, QE2_AUTH_URL) self.assertIsNone(credentials.hub) self.assertIsNone(credentials.group) self.assertIsNone(credentials.project) if token: self.assertEqual(credentials.token, token) def test_qe_credentials(self): """Test converting QE credentials.""" with custom_qiskitrc(): self.ibmq.save_account('A', url=QE_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(): self.ibmq.save_account('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_proxy_credentials(self): """Test converting credentials with proxy values.""" with custom_qiskitrc(): self.ibmq.save_account('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_multiple_credentials(self): """Test converting multiple credentials.""" with custom_qiskitrc(): self.ibmq.save_account('A', url=QE_URL) self.ibmq.save_account('B', url=IBMQ_TEMPLATE.format('a', 'b', 'c')) self.ibmq.save_account('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) def test_api2_non_auth_credentials(self): """Test converting api 2 non auth credentials.""" with custom_qiskitrc(): self.ibmq.save_account('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_auth2_credentials(self): """Test converting already API 2 auth credentials.""" with custom_qiskitrc(): self.ibmq.save_account('A', url=QE2_AUTH_URL) credentials = update_credentials(force=True) # No credentials should be returned. self.assertIsNone(credentials) def test_unknown_credentials(self): """Test converting credentials with an unknown URL.""" with custom_qiskitrc(): self.ibmq.save_account('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')