示例#1
0
    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_file('Qconfig.py'), 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_proxies_factory(self, qe_token, qe_url):
        """Should reach the proxy using factory.enable_account."""
        factory = IBMQFactory()
        provider = factory.enable_account(qe_token, qe_url,
                                          proxies={'urls': VALID_PROXIES})

        self.proxy_process.terminate()  # kill to be able of reading the output

        auth_line = pproxy_desired_access_log_line(qe_url)
        api_line = pproxy_desired_access_log_line(provider.credentials.url)
        proxy_output = self.proxy_process.stdout.read().decode('utf-8')

        # Check if the authentication call went through proxy.
        self.assertIn(auth_line, proxy_output)
        # Check if the API call (querying providers list) went through proxy.
        self.assertIn(api_line, proxy_output)
示例#3
0
 def run(self):
     """ h/g/p thread process """
     try:
         self._hgp = []
         if self._thread_queue is not None:
             self._thread_queue.put(CredentialsView._START)
         # pylint: disable=no-name-in-module, import-error
         if self._token:
             from qiskit.providers.ibmq import IBMQFactory
             ibmq = IBMQFactory()
             ibmq.enable_account(self._token, proxies=self._proxies)
             providers = ibmq.providers()
             for provider in providers:
                 self._hgp.append(
                     (provider.credentials.hub, provider.credentials.group,
                      provider.credentials.project))
     except Exception as ex:  # pylint: disable=broad-except
         logger.warning('IBMQ account Account Failure. Proxies:%s :%s',
                        self._proxies, ex)
     finally:
         if self._thread_queue is not None:
             self._thread_queue.put(CredentialsView._STOP)
示例#4
0
    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")
            # 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)
                factory.load_account()

        # Ensure that the credentials are the overwritten ones.
        self.assertEqual(factory._credentials, credentials2)