# Path: apitools/base/py/credentials_lib.py
#   FLAGS = flags.FLAGS
#   FLAGS = None
# def GetCredentials(package_name, scopes, client_id, client_secret, user_agent,
#                    credentials_filename=None,
#                    service_account_name=None, service_account_keyfile=None,
#                    api_key=None, client=None):
# def ServiceAccountCredentialsFromFile(
#     service_account_name, private_key_filename, scopes):
# def ServiceAccountCredentials(service_account_name, private_key, scopes):
# def _EnsureFileExists(filename):
# def _OpenNoProxy(request):
#   def __init__(self, scopes=None, service_account_name='default', **kwds):
#   def Get(cls, *args, **kwds):
#   def _CheckCacheFileForMatch(self, cache_filename, scopes):
#   def _WriteCacheFile(self, cache_filename, scopes):
#   def _ScopesFromMetadataServer(self, scopes):
#   def GetServiceAccount(self, account):
#   def GetInstanceScopes(self):
#   def _refresh(self, do_request):
#   def _do_refresh_request(self, unused_http_request):
#   def from_json(cls, json_data):
#   def __init__(self, scopes, **kwds):
#   def Get(cls, *args, **kwds):
#   def from_json(cls, json_data):
#   def _refresh(self, _):
# def CredentialsFromFile(path, client_info):
# def GetUserinfo(credentials, http=None):  # pylint: disable=invalid-name
# class GceAssertionCredentials(oauth2client.gce.AppAssertionCredentials):
# class GaeAssertionCredentials(oauth2client.client.AssertionCredentials):
# 
# Path: apitools/base/py/util.py
# _RESERVED_URI_CHARS = r":/?#[]@!$&'()*+,;="
# def DetectGae():
# def DetectGce():
# def NormalizeScopes(scope_spec):
# def Typecheck(arg, arg_type, msg=None):
# def ExpandRelativePath(method_config, params, relative_path=None):
# def CalculateWaitForRetry(retry_attempt, max_wait=60):
# def AcceptableMimeType(accept_patterns, mime_type):
#   def MimeTypeMatches(pattern, mime_type):
# def MapParamNames(params, request_type):
# def MapRequestParams(params, request_type):


import re
import StringIO
import mock
import unittest2
from six.moves import http_client
from apitools.base.py import credentials_lib
from apitools.base.py import util
    else:
      message = 'Expected uri matching pattern %s' % uri_regexp.pattern
      status = http_client.BAD_REQUEST
    return type('HttpResponse', (object,), {'status': status})(), message
  return CheckUri


class CredentialsLibTest(unittest2.TestCase):

  def _GetServiceCreds(self, service_account_name=None, scopes=None):
    scopes = scopes or ['scope1']
    kwargs = {}
    if service_account_name is not None:
      kwargs['service_account_name'] = service_account_name
    service_account_name = service_account_name or 'default'

    def MockMetadataCalls(request):
      request_url = request.get_full_url()
      if request_url.endswith('scopes'):
        return StringIO.StringIO(''.join(scopes))
      elif request_url.endswith('service-accounts'):
        return StringIO.StringIO(service_account_name)
      elif request_url.endswith(
          '/service-accounts/%s/token' % service_account_name):
        return StringIO.StringIO('{"access_token": "token"}')
      self.fail('Unexpected HTTP request to %s' % request_url)

    with mock.patch.object(credentials_lib, '_OpenNoProxy',
                           side_effect=MockMetadataCalls,
                           autospec=True) as opener_mock:

# Path: apitools/base/py/credentials_lib.py
#   FLAGS = flags.FLAGS
#   FLAGS = None
# def GetCredentials(package_name, scopes, client_id, client_secret, user_agent,
#                    credentials_filename=None,
#                    service_account_name=None, service_account_keyfile=None,
#                    api_key=None, client=None):
# def ServiceAccountCredentialsFromFile(
#     service_account_name, private_key_filename, scopes):
# def ServiceAccountCredentials(service_account_name, private_key, scopes):
# def _EnsureFileExists(filename):
# def _OpenNoProxy(request):
#   def __init__(self, scopes=None, service_account_name='default', **kwds):
#   def Get(cls, *args, **kwds):
#   def _CheckCacheFileForMatch(self, cache_filename, scopes):
#   def _WriteCacheFile(self, cache_filename, scopes):
#   def _ScopesFromMetadataServer(self, scopes):
#   def GetServiceAccount(self, account):
#   def GetInstanceScopes(self):
#   def _refresh(self, do_request):
#   def _do_refresh_request(self, unused_http_request):
#   def from_json(cls, json_data):
#   def __init__(self, scopes, **kwds):
#   def Get(cls, *args, **kwds):
#   def from_json(cls, json_data):
#   def _refresh(self, _):
# def CredentialsFromFile(path, client_info):
# def GetUserinfo(credentials, http=None):  # pylint: disable=invalid-name
# class GceAssertionCredentials(oauth2client.gce.AppAssertionCredentials):
# class GaeAssertionCredentials(oauth2client.client.AssertionCredentials):
# 
# Path: apitools/base/py/util.py
# _RESERVED_URI_CHARS = r":/?#[]@!$&'()*+,;="
# def DetectGae():
# def DetectGce():
# def NormalizeScopes(scope_spec):
# def Typecheck(arg, arg_type, msg=None):
# def ExpandRelativePath(method_config, params, relative_path=None):
# def CalculateWaitForRetry(retry_attempt, max_wait=60):
# def AcceptableMimeType(accept_patterns, mime_type):
#   def MimeTypeMatches(pattern, mime_type):
# def MapParamNames(params, request_type):
# def MapRequestParams(params, request_type):


# Path: apitools/base/py/credentials_lib_test.py
import re
import StringIO
import mock
import unittest2
from six.moves import http_client
from apitools.base.py import credentials_lib
from apitools.base.py import util

    else:
      message = 'Expected uri matching pattern %s' % uri_regexp.pattern
      status = http_client.BAD_REQUEST
    return type('HttpResponse', (object,), {'status': status})(), message
  return CheckUri


class CredentialsLibTest(unittest2.TestCase):

  def _GetServiceCreds(self, service_account_name=None, scopes=None):
    scopes = scopes or ['scope1']
    kwargs = {}
    if service_account_name is not None:
      kwargs['service_account_name'] = service_account_name
    service_account_name = service_account_name or 'default'

    def MockMetadataCalls(request):
      request_url = request.get_full_url()
      if request_url.endswith('scopes'):
        return StringIO.StringIO(''.join(scopes))
      elif request_url.endswith('service-accounts'):
        return StringIO.StringIO(service_account_name)
      elif request_url.endswith(
          '/service-accounts/%s/token' % service_account_name):
        return StringIO.StringIO('{"access_token": "token"}')
      self.fail('Unexpected HTTP request to %s' % request_url)

    with mock.patch.object(credentials_lib, '_OpenNoProxy',
                           side_effect=MockMetadataCalls,
                           autospec=True) as opener_mock:

      with mock.patch.object(util, 'DetectGce', autospec=True) as mock_detect: