Example #1
0
    def test_mint_jwt_based_token_failure(self):
        rsa_sign_calls = []

        def mocked_rsa_sign(*args):
            rsa_sign_calls.append(args)
            return '\x00signature\x00'

        self.mock(service_account, '_rsa_sign', mocked_rsa_sign)

        fetch_calls = []

        def mocked_fetch(**kwargs):
            fetch_calls.append(kwargs)
            response = collections.namedtuple('Response',
                                              'status_code content')
            return response(500, 'error')

        self.mock(service_account.urlfetch, 'fetch', mocked_fetch)

        with self.assertRaises(service_account.AccessTokenError):
            service_account._mint_jwt_based_token(['scope1', 'scope2'],
                                                  FAKE_SECRET_KEY)

        # Sign once, try to send request N times.
        self.assertEqual(1, len(rsa_sign_calls))
        self.assertEqual(5, len(fetch_calls))
Example #2
0
    def test_mint_jwt_based_token(self):
        self.mock_now(datetime.datetime(2015, 1, 2, 3))

        rsa_sign_calls = []

        def mocked_rsa_sign(*args):
            rsa_sign_calls.append(args)
            return '\x00signature\x00'

        self.mock(service_account, '_rsa_sign', mocked_rsa_sign)

        fetch_calls = []

        def mocked_fetch(**kwargs):
            fetch_calls.append(kwargs)
            response = collections.namedtuple('Response',
                                              'status_code content')
            return response(
                200, json.dumps({
                    'access_token': 'token',
                    'expires_in': 3600
                }))

        self.mock(service_account.urlfetch, 'fetch', mocked_fetch)

        token = service_account._mint_jwt_based_token(['scope1', 'scope2'],
                                                      FAKE_SECRET_KEY)
        self.assertEqual({
            'access_token': 'token',
            'exp_ts': 1420171200.0
        }, token)

        self.assertEqual(
            [('eyJhbGciOiJSUzI1NiIsImtpZCI6InBrZXlfaWQiLCJ0eXAiOiJKV1QifQ.'
              'eyJhdWQiOiJodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9vYXV0aDIvdjM'
              'vdG9rZW4iLCJleHAiOjE0MjAxNzEyMDAsImlhdCI6MTQyMDE2NzYwMCwiaX'
              'NzIjoiZW1haWwiLCJzY29wZSI6InNjb3BlMSBzY29wZTIifQ', 'pkey')],
            rsa_sign_calls)

        self.assertEqual([{
            'url':
            'https://www.googleapis.com/oauth2/v3/token',
            'follow_redirects':
            False,
            'method':
            'POST',
            'headers': {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            'deadline':
            10,
            'validate_certificate':
            True,
            'payload':
            'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&'
            'assertion=eyJhbGciOiJSUzI1NiIsImtpZCI6InBrZXlfaWQiLCJ0eXAiOiJKV1Q'
            'ifQ.eyJhdWQiOiJodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9vYXV0aDIvdjMvd'
            'G9rZW4iLCJleHAiOjE0MjAxNzEyMDAsImlhdCI6MTQyMDE2NzYwMCwiaXNzIjoiZW'
            '1haWwiLCJzY29wZSI6InNjb3BlMSBzY29wZTIifQ.AHNpZ25hdHVyZQA',
        }], fetch_calls)
Example #3
0
  def test_mint_jwt_based_token_failure(self):
    rsa_sign_calls = []
    def mocked_rsa_sign(*args):
      rsa_sign_calls.append(args)
      return '\x00signature\x00'
    self.mock(service_account, '_rsa_sign', mocked_rsa_sign)

    fetch_calls = []
    def mocked_fetch(**kwargs):
      fetch_calls.append(kwargs)
      response = collections.namedtuple('Response', 'status_code content')
      return response(500, 'error')
    self.mock(service_account.urlfetch, 'fetch', mocked_fetch)

    with self.assertRaises(service_account.AccessTokenError):
      service_account._mint_jwt_based_token(
          ['scope1', 'scope2'], FAKE_SECRET_KEY)

    # Sign once, try to send request N times.
    self.assertEqual(1, len(rsa_sign_calls))
    self.assertEqual(5, len(fetch_calls))
Example #4
0
  def test_mint_jwt_based_token(self):
    self.mock_now(datetime.datetime(2015, 1, 2, 3))

    rsa_sign_calls = []
    def mocked_rsa_sign(*args):
      rsa_sign_calls.append(args)
      return '\x00signature\x00'
    self.mock(service_account, '_rsa_sign', mocked_rsa_sign)

    fetch_calls = []
    def mocked_fetch(**kwargs):
      fetch_calls.append(kwargs)
      response = collections.namedtuple('Response', 'status_code content')
      return response(
          200, json.dumps({'access_token': 'token', 'expires_in': 3600}))
    self.mock(service_account.urlfetch, 'fetch', mocked_fetch)

    token = service_account._mint_jwt_based_token(
        ['scope1', 'scope2'], FAKE_SECRET_KEY)
    self.assertEqual({'access_token': 'token', 'exp_ts': 1420171200.0}, token)

    self.assertEqual(
        [('eyJhbGciOiJSUzI1NiIsImtpZCI6InBrZXlfaWQiLCJ0eXAiOiJKV1QifQ.'
          'eyJhdWQiOiJodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9vYXV0aDIvdjM'
          'vdG9rZW4iLCJleHAiOjE0MjAxNzEyMDAsImlhdCI6MTQyMDE2NzYwMCwiaX'
          'NzIjoiZW1haWwiLCJzY29wZSI6InNjb3BlMSBzY29wZTIifQ', 'pkey')],
        rsa_sign_calls)

    self.assertEqual([
      {
        'url': 'https://www.googleapis.com/oauth2/v3/token',
        'follow_redirects': False,
        'method': 'POST',
        'headers': {'Content-Type': 'application/x-www-form-urlencoded'},
        'deadline': 10,
        'validate_certificate': True,
        'payload':
            'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&'
            'assertion=eyJhbGciOiJSUzI1NiIsImtpZCI6InBrZXlfaWQiLCJ0eXAiOiJKV1Q'
            'ifQ.eyJhdWQiOiJodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9vYXV0aDIvdjMvd'
            'G9rZW4iLCJleHAiOjE0MjAxNzEyMDAsImlhdCI6MTQyMDE2NzYwMCwiaXNzIjoiZW'
            '1haWwiLCJzY29wZSI6InNjb3BlMSBzY29wZTIifQ.AHNpZ25hdHVyZQA',
      }], fetch_calls)