Пример #1
0
 def testBatchTokenRefreshError(self):
     refresh_mock = self.StartObjectPatch(credentials.Credentials,
                                          'before_request')
     refresh_mock.side_effect = google_auth_exceptions.RefreshError
     http_client = creds_requests.GetApitoolsRequests(
         creds_requests.GetSession())
     batch_http_request = batch.BatchHttpRequest(
         'https://www.googleapis.com/batch/compute')
     with self.assertRaisesRegex(
             store.TokenRefreshError,
             'There was a problem refreshing your current auth tokens'):
         batch_http_request.Execute(http_client)
Пример #2
0
 def testBatchTokenRefreshDeniedByCAAError(self):
     refresh_mock = self.StartObjectPatch(credentials.Credentials,
                                          'before_request')
     refresh_mock.side_effect = google_auth_exceptions.RefreshError(
         'access_denied: Account restricted')
     http_client = creds_requests.GetApitoolsRequests(
         creds_requests.GetSession())
     batch_http_request = batch.BatchHttpRequest(
         'https://www.googleapis.com/batch/compute')
     with self.assertRaisesRegex(
             store.TokenRefreshDeniedByCAAError,
             'Access was blocked due to an organization policy'):
         batch_http_request.Execute(http_client)
Пример #3
0
def GetApitoolsTransport(timeout='unset',
                         enable_resource_quota=True,
                         force_resource_quota=False,
                         response_encoding=None,
                         ca_certs=None,
                         allow_account_impersonation=True,
                         use_google_auth=None):
  """Get an transport client for use with apitools.

  Args:
    timeout: double, The timeout in seconds to pass to httplib2.  This is the
        socket level timeout.  If timeout is None, timeout is infinite.  If
        default argument 'unset' is given, a sensible default is selected.
    enable_resource_quota: bool, By default, we are going to tell APIs to use
        the quota of the project being operated on. For some APIs we want to use
        gcloud's quota, so you can explicitly disable that behavior by passing
        False here.
    force_resource_quota: bool, If true resource project quota will be used by
      this client regardless of the settings in gcloud. This should be used for
      newer APIs that cannot work with legacy project quota.
    response_encoding: str, the encoding to use to decode the response.
    ca_certs: str, absolute filename of a ca_certs file that overrides the
        default
    allow_account_impersonation: bool, True to allow use of impersonated service
      account credentials for calls made with this client. If False, the active
      user credentials will always be used.
    use_google_auth: bool, True if the calling command indicates to use
      google-auth library for authentication. If False, authentication will
      fallback to using the oauth2client library.

  Returns:
    1. A httplib2.Http-like object backed by httplib2 or requests.
  """
  if base.UseRequests():
    session = requests.GetSession(
        timeout=timeout,
        enable_resource_quota=enable_resource_quota,
        force_resource_quota=force_resource_quota,
        response_encoding=response_encoding,
        ca_certs=ca_certs,
        allow_account_impersonation=allow_account_impersonation)

    return requests.GetApitoolsRequests(session)

  return http.Http(timeout=timeout,
                   enable_resource_quota=enable_resource_quota,
                   force_resource_quota=force_resource_quota,
                   response_encoding=response_encoding,
                   ca_certs=ca_certs,
                   allow_account_impersonation=allow_account_impersonation,
                   use_google_auth=use_google_auth)
Пример #4
0
 def testResponseEncoding(self, encoding, expected_response):
     self.request_mock = self.StartObjectPatch(
         requests.Session,
         'request',
         return_value=MakeRequestsResponse(httplib.OK, {
             'header': 'value',
         }, b'data'))
     session = creds_requests.GetSession(response_encoding=encoding)
     apitools_requests = creds_requests.GetApitoolsRequests(session)
     response = apitools_requests.request('url')
     self.assertEqual(
         response[0],
         httplib2.Response({
             'status': httplib.OK,
             'header': 'value',
         }))
     self.assertEqual(response[1], expected_response)
Пример #5
0
    def testBatchTokenRefresh(self):
        refresh_mock = self.StartObjectPatch(google_auth_creds.Credentials,
                                             'refresh')
        mock_service = FakeService()

        desired_url = 'https://www.example.com'
        batch_api_request = batch.BatchApiRequest(batch_url=desired_url)
        # The request to be added. The actual request sent will be somewhat
        # larger, as this is added to a batch.
        desired_request = http_wrapper.Request(
            desired_url, 'POST', {
                'content-type': 'multipart/mixed; boundary="None"',
                'content-length': 80,
            }, 'x' * 80)

        mock_request = self.StartObjectPatch(http_wrapper,
                                             'MakeRequest',
                                             autospec=True)
        self.__ConfigureMock(
            mock_request,
            http_wrapper.Request(
                desired_url, 'POST', {
                    'content-type': 'multipart/mixed; boundary="None"',
                    'content-length': 419,
                }, 'x' * 419),
            [
                http_wrapper.Response(
                    {
                        'status': '200',
                        'content-type': 'multipart/mixed; boundary="boundary"',
                    },
                    textwrap.dedent("""\
            --boundary
            content-type: text/plain
            content-id: <id+0>

            HTTP/1.1 401 UNAUTHORIZED
            Invalid grant

            --boundary--"""), None),
                http_wrapper.Response(
                    {
                        'status': '200',
                        'content-type': 'multipart/mixed; boundary="boundary"',
                    },
                    textwrap.dedent("""\
            --boundary
            content-type: text/plain
            content-id: <id+0>

            HTTP/1.1 200 OK
            content
            --boundary--"""), None)
            ])

        batch_api_request.Add(mock_service, 'unused', None, {
            'desired_request': desired_request,
        })

        http_client = creds_requests.GetApitoolsRequests(
            creds_requests.GetSession())
        batch_api_request.Execute(http_client)
        refresh_mock.assert_called_once()