Example #1
0
  def testDifferentServiceAccounts(self):
    api1 = rest_api._RestApi('scope', 123)
    api2 = rest_api._RestApi('scope', 456)

    t1 = api1.get_token()
    t2 = api2.get_token()
    self.assertNotEqual(t1, t2)
Example #2
0
    def testSameServiceAccount(self):
        api1 = rest_api._RestApi('scope', 123)
        api2 = rest_api._RestApi('scope', 123)

        t1 = api1.get_token()
        t2 = api2.get_token()
        self.assertEqual(t1, t2)
Example #3
0
  def testSameServiceAccount(self):
    api1 = rest_api._RestApi('scope', 123)
    api2 = rest_api._RestApi('scope', 123)

    t1 = api1.get_token()
    t2 = api2.get_token()
    self.assertEqual(t1, t2)
Example #4
0
    def testDifferentServiceAccounts(self):
        api1 = rest_api._RestApi('scope', 123)
        api2 = rest_api._RestApi('scope', 456)

        t1 = api1.get_token()
        t2 = api2.get_token()
        self.assertNotEqual(t1, t2)
  def testTokenMemoized(self):

    ndb_ctx = ndb.get_context()
    ndb_ctx.set_cache_policy(lambda key: False)
    ndb_ctx.set_memcache_policy(lambda key: False)

    api = rest_api._RestApi('scope')
    t1 = api.get_token()
    self.assertNotEqual(None, t1)

    api = rest_api._RestApi('scope')
    t2 = api.get_token()
    self.assertEqual(t2, t1)
Example #6
0
  def testTokenMemoized(self):

    ndb_ctx = ndb.get_context()
    ndb_ctx.set_cache_policy(lambda key: False)
    ndb_ctx.set_memcache_policy(lambda key: False)

    api = rest_api._RestApi('scope')
    t1 = api.get_token()
    self.assertNotEqual(None, t1)

    api = rest_api._RestApi('scope')
    t2 = api.get_token()
    self.assertEqual(t2, t1)
  def testNoToken(self):
    api = rest_api._RestApi('scope')
    self.assertEqual(api.scopes, ['scope'])

    fut_get_token = ndb.Future()
    fut_get_token.set_result(None)
    api.get_token_async = mock.create_autospec(api.get_token_async,
                                               return_value=fut_get_token)

    fut_urlfetch = ndb.Future()
    fut_urlfetch.set_result(
        test_utils.MockUrlFetchResult(200, {'foo': 'bar'}, 'yoohoo'))
    ctx_urlfetch = mock.Mock(return_value=fut_urlfetch)
    ndb.get_context().urlfetch = ctx_urlfetch

    res = api.do_request('http://example.com')

    self.assertEqual(res, (200, {'foo': 'bar'}, 'yoohoo'))
    ctx_urlfetch.assert_called_once_with(
        'http://example.com',
        headers={'User-Agent': 'App Engine Python GCS Client'},
        follow_redirects=False,
        payload=None,
        method='GET',
        deadline=None,
        callback=None)
Example #8
0
  def testBasicCallWithUserAgent(self):
    user_agent = 'Test User Agent String'
    retry_params = api_utils.RetryParams(_user_agent=user_agent)
    api = rest_api._RestApi('scope', retry_params=retry_params)
    self.assertEqual(api.scopes, ['scope'])

    fut_get_token = ndb.Future()
    fut_get_token.set_result('blah')
    api.get_token_async = mock.create_autospec(api.get_token_async,
                                               return_value=fut_get_token)

    fut_urlfetch = ndb.Future()
    fut_urlfetch.set_result(
        test_utils.MockUrlFetchResult(200, {'foo': 'bar'}, 'yoohoo'))
    ctx_urlfetch = mock.Mock(return_value=fut_urlfetch)
    ndb.get_context().urlfetch = ctx_urlfetch

    res = api.do_request('http://example.com')

    self.assertEqual(res, (200, {'foo': 'bar'}, 'yoohoo'))
    ctx_urlfetch.assert_called_once_with(
        'http://example.com',
        headers={'authorization': 'OAuth blah',
                 'User-Agent': user_agent},
        follow_redirects=False,
        payload=None,
        method='GET',
        deadline=None,
        callback=None)
Example #9
0
  def testBasicCall(self):
    api = rest_api._RestApi('scope')
    self.assertEqual(api.scopes, ['scope'])

    fut_get_token = ndb.Future()
    fut_get_token.set_result('blah')
    api.get_token_async = mock.create_autospec(api.get_token_async,
                                               return_value=fut_get_token)

    fut_urlfetch = ndb.Future()
    fut_urlfetch.set_result(
        test_utils.MockUrlFetchResult(200, {'foo': 'bar'}, 'yoohoo'))
    api.urlfetch_async = mock.create_autospec(api.urlfetch_async,
                                              return_value=fut_urlfetch)

    res = api.do_request('http://example.com')

    self.assertEqual(res, (200, {'foo': 'bar'}, 'yoohoo'))
    api.urlfetch_async.assert_called_once_with(
        'http://example.com',
        headers={'authorization': 'OAuth blah'},
        follow_redirects=False,
        payload=None,
        method='GET',
        deadline=None,
        callback=None)
Example #10
0
  def testTokenSaved(self):
    retry_params = api_utils.RetryParams(save_access_token=True)
    api = rest_api._RestApi('scope', retry_params=retry_params)
    t1 = api.get_token()
    self.assertNotEqual(None, t1)

    api = rest_api._RestApi('scope', retry_params=retry_params)
    t2 = api.get_token()
    self.assertEqual(t2, t1)

    memcache.flush_all()
    ndb.get_context().clear_cache()

    api = rest_api._RestApi('scope', retry_params=retry_params)
    t3 = api.get_token()
    self.assertEqual(t3, t1)
Example #11
0
    def testBasicCall(self):
        api = rest_api._RestApi('scope')
        self.assertEqual(api.scopes, ['scope'])

        fut_get_token = ndb.Future()
        fut_get_token.set_result('blah')
        api.get_token_async = mock.create_autospec(api.get_token_async,
                                                   return_value=fut_get_token)

        fut_urlfetch = ndb.Future()
        fut_urlfetch.set_result(
            test_utils.MockUrlFetchResult(200, {'foo': 'bar'}, 'yoohoo'))
        api.urlfetch_async = mock.create_autospec(api.urlfetch_async,
                                                  return_value=fut_urlfetch)

        res = api.do_request('http://example.com')

        self.assertEqual(res, (200, {'foo': 'bar'}, 'yoohoo'))
        api.urlfetch_async.assert_called_once_with(
            'http://example.com',
            headers={'authorization': 'OAuth blah'},
            follow_redirects=False,
            payload=None,
            method='GET',
            deadline=None,
            callback=None)
Example #12
0
    def testPickling(self):
        retry_params = api_utils.RetryParams(max_retries=1000)
        api = rest_api._RestApi('scope',
                                service_account_id=1,
                                retry_params=retry_params)
        self.assertNotEqual(None, api.get_token())

        pickled_api = pickle.loads(pickle.dumps(api))
        self.assertEqual(
            0,
            len(set(api.__dict__.keys()) ^ set(pickled_api.__dict__.keys())))
        for k, v in api.__dict__.iteritems():
            if not hasattr(v, '__call__'):
                self.assertEqual(v, pickled_api.__dict__[k])

        pickled_api.token = None

        fut_urlfetch = ndb.Future()
        fut_urlfetch.set_result(
            test_utils.MockUrlFetchResult(200, {'foo': 'bar'}, 'yoohoo'))
        pickled_api.urlfetch_async = mock.create_autospec(
            pickled_api.urlfetch_async, return_value=fut_urlfetch)

        res = pickled_api.do_request('http://example.com')
        self.assertEqual(res, (200, {'foo': 'bar'}, 'yoohoo'))
Example #13
0
  def testNoToken(self):
    api = rest_api._RestApi('scope')
    self.assertEqual(api.scopes, ['scope'])

    fut_get_token = ndb.Future()
    fut_get_token.set_result(None)
    api.get_token_async = mock.create_autospec(api.get_token_async,
                                               return_value=fut_get_token)

    fut_urlfetch = ndb.Future()
    fut_urlfetch.set_result(
        test_utils.MockUrlFetchResult(200, {'foo': 'bar'}, 'yoohoo'))
    ctx_urlfetch = mock.Mock(return_value=fut_urlfetch)
    ndb.get_context().urlfetch = ctx_urlfetch

    res = api.do_request('http://example.com')

    self.assertEqual(res, (200, {'foo': 'bar'}, 'yoohoo'))
    ctx_urlfetch.assert_called_once_with(
        'http://example.com',
        headers={'User-Agent': 'AppEngine-Python-GCS'},
        follow_redirects=False,
        payload=None,
        method='GET',
        deadline=None,
        callback=None)
  def testTokenSaved(self):
    retry_params = api_utils.RetryParams(save_access_token=True)
    api = rest_api._RestApi('scope', retry_params=retry_params)
    t1 = api.get_token()
    self.assertNotEqual(None, t1)

    api = rest_api._RestApi('scope', retry_params=retry_params)
    t2 = api.get_token()
    self.assertEqual(t2, t1)

    memcache.flush_all()
    ndb.get_context().clear_cache()

    api = rest_api._RestApi('scope', retry_params=retry_params)
    t3 = api.get_token()
    self.assertEqual(t3, t1)
  def testBasicCallWithUserAgent(self):
    user_agent = 'Test User Agent String'
    retry_params = api_utils.RetryParams(_user_agent=user_agent)
    api = rest_api._RestApi('scope', retry_params=retry_params)
    self.assertEqual(api.scopes, ['scope'])

    fut_get_token = ndb.Future()
    fut_get_token.set_result('blah')
    api.get_token_async = mock.create_autospec(api.get_token_async,
                                               return_value=fut_get_token)

    fut_urlfetch = ndb.Future()
    fut_urlfetch.set_result(
        test_utils.MockUrlFetchResult(200, {'foo': 'bar'}, 'yoohoo'))
    ctx_urlfetch = mock.Mock(return_value=fut_urlfetch)
    ndb.get_context().urlfetch = ctx_urlfetch

    res = api.do_request('http://example.com')

    self.assertEqual(res, (200, {'foo': 'bar'}, 'yoohoo'))
    ctx_urlfetch.assert_called_once_with(
        'http://example.com',
        headers={'authorization': 'OAuth blah',
                 'User-Agent': user_agent},
        follow_redirects=False,
        payload=None,
        method='GET',
        deadline=None,
        callback=None)
Example #16
0
    def testNoRetryAfterDoRequestUrlFetchTimeout(self):
        retry_params = api_utils.RetryParams(max_retries=0)
        api = rest_api._RestApi('scope', retry_params=retry_params)

        fut = ndb.Future()
        fut.set_exception(urlfetch.DownloadError())
        ndb.Context.urlfetch = mock.create_autospec(ndb.Context.urlfetch,
                                                    return_value=fut)
        self.assertRaises(urlfetch.DownloadError, api.do_request, 'foo')
Example #17
0
  def testNoRetryAfterDoRequestUrlFetchTimeout(self):
    retry_params = api_utils.RetryParams(max_retries=0)
    api = rest_api._RestApi('scope', retry_params=retry_params)

    fut = ndb.Future()
    fut.set_exception(urlfetch.DownloadError())
    ndb.Context.urlfetch = mock.create_autospec(
        ndb.Context.urlfetch,
        return_value=fut)
    self.assertRaises(urlfetch.DownloadError, api.do_request, 'foo')
Example #18
0
    def testNoRetryAfterDoRequestResponseTimeout(self):
        retry_params = api_utils.RetryParams(max_retries=0)
        api = rest_api._RestApi('scope', retry_params=retry_params)

        fut = ndb.Future()
        fut.set_result(
            test_utils.MockUrlFetchResult(httplib.REQUEST_TIMEOUT, None, None))
        ndb.Context.urlfetch = mock.create_autospec(ndb.Context.urlfetch,
                                                    return_value=fut)
        self.assertEqual(httplib.REQUEST_TIMEOUT, api.do_request('foo')[0])
 def testNegativeTimeout(self):
   api = rest_api._RestApi('scope')
   fut1 = ndb.Future()
   fut1.set_result(('token1', 0))
   fut2 = ndb.Future()
   fut2.set_result(('token2', 0))
   api.make_token_async = mock.create_autospec(
       api.make_token_async, side_effect=[fut1, fut2])
   token1 = api.get_token()
   token2 = api.get_token()
   self.assertNotEqual(token1, token2)
Example #20
0
    def testTokenMemoized(self):
        api = rest_api._RestApi('scope')
        self.assertEqual(api.token, None)
        t1 = api.get_token()
        self.assertEqual(api.token, t1)
        t2 = api.get_token()
        self.assertEqual(t2, t1)

        t3 = api.get_token(refresh=True)
        self.assertNotEqual(t2, t3)
        self.assertEqual(api.token, t3)
Example #21
0
 def testNegativeTimeout(self):
   api = rest_api._RestApi('scope')
   fut1 = ndb.Future()
   fut1.set_result(('token1', 0))
   fut2 = ndb.Future()
   fut2.set_result(('token2', 0))
   api.make_token_async = mock.create_autospec(
       api.make_token_async, side_effect=[fut1, fut2])
   token1 = api.get_token()
   token2 = api.get_token()
   self.assertNotEqual(token1, token2)
Example #22
0
  def testTokenMemoized(self):
    api = rest_api._RestApi('scope')
    self.assertEqual(api.token, None)
    t1 = api.get_token()
    self.assertEqual(api.token, t1)
    t2 = api.get_token()
    self.assertEqual(t2, t1)

    t3 = api.get_token(refresh=True)
    self.assertNotEqual(t2, t3)
    self.assertEqual(api.token, t3)
Example #23
0
  def testNoRetryAfterDoRequestResponseTimeout(self):
    retry_params = api_utils.RetryParams(max_retries=0)
    api = rest_api._RestApi('scope', retry_params=retry_params)

    fut = ndb.Future()
    fut.set_result(test_utils.MockUrlFetchResult(httplib.REQUEST_TIMEOUT,
                                                 None, None))
    ndb.Context.urlfetch = mock.create_autospec(
        ndb.Context.urlfetch,
        return_value=fut)
    self.assertEqual(httplib.REQUEST_TIMEOUT, api.do_request('foo')[0])
Example #24
0
    def testRetryAfterDoRequestUrlFetchTimeout(self):
        api = rest_api._RestApi('scope')

        fut = ndb.Future()
        fut.set_exception(urlfetch.DownloadError())
        ndb.Context.urlfetch = mock.create_autospec(ndb.Context.urlfetch,
                                                    return_value=fut)

        with mock.patch('google.appengine.api.urlfetch' '.fetch') as f:
            f.return_value = test_utils.MockUrlFetchResult(
                httplib.ACCEPTED, None, None)
            self.assertEqual(httplib.ACCEPTED, api.do_request('foo')[0])
Example #25
0
  def testCallUrlFetch(self):
    api = rest_api._RestApi('scope')

    fut = ndb.Future()
    fut.set_result(test_utils.MockUrlFetchResult(200, {}, 'response'))
    ndb.Context.urlfetch = mock.create_autospec(
        ndb.Context.urlfetch,
        return_value=fut)

    res = api.urlfetch('http://example.com', method='PUT', headers={'a': 'b'})

    self.assertEqual(res.status_code, 200)
    self.assertEqual(res.content, 'response')
Example #26
0
  def testCallUrlFetch(self):
    api = rest_api._RestApi('scope')

    fut = ndb.Future()
    fut.set_result(test_utils.MockUrlFetchResult(200, {}, 'response'))
    ndb.Context.urlfetch = mock.create_autospec(
        ndb.Context.urlfetch,
        return_value=fut)

    res = api.urlfetch('http://example.com', method='PUT', headers={'a': 'b'})

    self.assertEqual(res.status_code, 200)
    self.assertEqual(res.content, 'response')
Example #27
0
    def testRetryAfterNoRequsetResponseTimeout(self):
        api = rest_api._RestApi('scope')

        fut = ndb.Future()
        fut.set_result(
            test_utils.MockUrlFetchResult(httplib.REQUEST_TIMEOUT, None, None))
        ndb.Context.urlfetch = mock.create_autospec(ndb.Context.urlfetch,
                                                    return_value=fut)

        with mock.patch('google.appengine.api.urlfetch' '.fetch') as f:
            f.return_value = test_utils.MockUrlFetchResult(
                httplib.ACCEPTED, None, None)
            self.assertEqual(httplib.ACCEPTED, api.do_request('foo')[0])
    def testRetryAfterDoRequestUrlFetchTimeout(self):
        api = rest_api._RestApi('scope')

        resp_fut1 = ndb.Future()
        resp_fut1.set_exception(urlfetch.DownloadError())
        resp_fut2 = ndb.Future()
        resp_fut2.set_result(
            test_utils.MockUrlFetchResult(httplib.ACCEPTED, None, None))
        ndb.Context.urlfetch = mock.create_autospec(
            ndb.Context.urlfetch, side_effect=[resp_fut1, resp_fut2])

        self.assertEqual(httplib.ACCEPTED, api.do_request('foo')[0])
        self.assertEqual(2, ndb.Context.urlfetch.call_count)
  def testRetryAfterDoRequestUrlFetchTimeout(self):
    api = rest_api._RestApi('scope')

    resp_fut1 = ndb.Future()
    resp_fut1.set_exception(urlfetch.DownloadError())
    resp_fut2 = ndb.Future()
    resp_fut2.set_result(test_utils.MockUrlFetchResult(httplib.ACCEPTED,
                                                       None, None))
    ndb.Context.urlfetch = mock.create_autospec(
        ndb.Context.urlfetch,
        side_effect=[resp_fut1, resp_fut2])

    self.assertEqual(httplib.ACCEPTED, api.do_request('foo')[0])
    self.assertEqual(2, ndb.Context.urlfetch.call_count)
Example #30
0
  def testRetryAfterDoRequestUrlFetchTimeout(self):
    api = rest_api._RestApi('scope')

    fut = ndb.Future()
    fut.set_exception(urlfetch.DownloadError())
    ndb.Context.urlfetch = mock.create_autospec(
        ndb.Context.urlfetch,
        return_value=fut)

    with mock.patch('google.appengine.api.urlfetch'
                    '.fetch') as f:
      f.return_value = test_utils.MockUrlFetchResult(httplib.ACCEPTED,
                                                     None, None)
      self.assertEqual(httplib.ACCEPTED, api.do_request('foo')[0])
Example #31
0
  def testRetryAfterNoRequsetResponseTimeout(self):
    api = rest_api._RestApi('scope')

    fut = ndb.Future()
    fut.set_result(test_utils.MockUrlFetchResult(httplib.REQUEST_TIMEOUT,
                                                 None, None))
    ndb.Context.urlfetch = mock.create_autospec(
        ndb.Context.urlfetch,
        return_value=fut)

    with mock.patch('google.appengine.api.urlfetch'
                    '.fetch') as f:
      f.return_value = test_utils.MockUrlFetchResult(httplib.ACCEPTED,
                                                     None, None)
      self.assertEqual(httplib.ACCEPTED, api.do_request('foo')[0])
Example #32
0
  def testNoExpiredToken(self):
    with mock.patch('time.time') as t:
      t.side_effect = [2, 4, 5, 6]
      api = rest_api._RestApi('scope')
      fut1 = ndb.Future()
      fut1.set_result(('token1', 3 + api.expiration_headroom))
      fut2 = ndb.Future()
      fut2.set_result(('token2', 7 + api.expiration_headroom))
      api.make_token_async = mock.create_autospec(
          api.make_token_async, side_effect=[fut1, fut2])

      token = api.get_token()
      self.assertEqual('token1', token)
      token = api.get_token()
      self.assertEqual('token2', token)
      token = api.get_token()
      self.assertEqual('token2', token)
  def testNoExpiredToken(self):
    with mock.patch('time.time') as t:
      t.side_effect = [2, 4, 5, 6]
      api = rest_api._RestApi('scope')
      fut1 = ndb.Future()
      fut1.set_result(('token1', 3 + api.expiration_headroom))
      fut2 = ndb.Future()
      fut2.set_result(('token2', 7 + api.expiration_headroom))
      api.make_token_async = mock.create_autospec(
          api.make_token_async, side_effect=[fut1, fut2])

      token = api.get_token()
      self.assertEqual('token1', token)
      token = api.get_token()
      self.assertEqual('token2', token)
      token = api.get_token()
      self.assertEqual('token2', token)
    def testUrlFetchCalledWithUserProvidedDeadline(self):
        retry_params = api_utils.RetryParams(urlfetch_timeout=90)
        api = rest_api._RestApi('scope', retry_params=retry_params)

        resp_fut1 = ndb.Future()
        resp_fut1.set_exception(urlfetch.DownloadError())
        resp_fut2 = ndb.Future()
        resp_fut2.set_result(
            test_utils.MockUrlFetchResult(httplib.ACCEPTED, None, None))
        ndb.Context.urlfetch = mock.create_autospec(
            ndb.Context.urlfetch, side_effect=[resp_fut1, resp_fut2])

        self.assertEqual(httplib.ACCEPTED, api.do_request('foo')[0])
        self.assertEqual(90,
                         ndb.Context.urlfetch.call_args_list[0][1]['deadline'])
        self.assertEqual(90,
                         ndb.Context.urlfetch.call_args_list[1][1]['deadline'])
Example #35
0
  def testUrlFetchCalledWithUserProvidedDeadline(self):
    retry_params = api_utils.RetryParams(urlfetch_timeout=90)
    api = rest_api._RestApi('scope', retry_params=retry_params)

    fut = ndb.Future()
    fut.set_exception(urlfetch.DownloadError())
    ndb.Context.urlfetch = mock.create_autospec(
        ndb.Context.urlfetch,
        return_value=fut)

    with mock.patch('google.appengine.api.urlfetch'
                    '.fetch') as f:
      f.return_value = test_utils.MockUrlFetchResult(httplib.ACCEPTED,
                                                     None, None)
      self.assertEqual(httplib.ACCEPTED, api.do_request('foo')[0])
      self.assertEqual(90, f.call_args[1]['deadline'])
      self.assertEqual(90, ndb.Context.urlfetch.call_args[1]['deadline'])
  def testUrlFetchCalledWithUserProvidedDeadline(self):
    retry_params = api_utils.RetryParams(urlfetch_timeout=90)
    api = rest_api._RestApi('scope', retry_params=retry_params)

    resp_fut1 = ndb.Future()
    resp_fut1.set_exception(urlfetch.DownloadError())
    resp_fut2 = ndb.Future()
    resp_fut2.set_result(test_utils.MockUrlFetchResult(httplib.ACCEPTED,
                                                       None, None))
    ndb.Context.urlfetch = mock.create_autospec(
        ndb.Context.urlfetch,
        side_effect=[resp_fut1, resp_fut2])

    self.assertEqual(httplib.ACCEPTED, api.do_request('foo')[0])
    self.assertEqual(
        90, ndb.Context.urlfetch.call_args_list[0][1]['deadline'])
    self.assertEqual(
        90, ndb.Context.urlfetch.call_args_list[1][1]['deadline'])
Example #37
0
    def testAsyncCall(self):
        api = rest_api._RestApi('scope')

        fut_urlfetch = ndb.Future()
        fut_urlfetch.set_result(
            test_utils.MockUrlFetchResult(200, {'foo': 'bar'}, 'yoohoo'))
        api.urlfetch_async = mock.create_autospec(api.urlfetch_async,
                                                  return_value=fut_urlfetch)

        fut = api.do_request_async('http://example.com')
        res = fut.get_result()

        self.assertEqual(res, (200, {'foo': 'bar'}, 'yoohoo'))
        api.urlfetch_async.assert_called_once_with('http://example.com',
                                                   headers=mock.ANY,
                                                   follow_redirects=False,
                                                   payload=None,
                                                   method='GET',
                                                   deadline=None,
                                                   callback=None)
Example #38
0
  def testRefreshToken(self):
    api = rest_api._RestApi('scope')

    fut_get_token1 = ndb.Future()
    fut_get_token1.set_result('blah')
    fut_get_token2 = ndb.Future()
    fut_get_token2.set_result('bleh')

    api.get_token_async = mock.create_autospec(
        api.get_token_async,
        side_effect=[fut_get_token1, fut_get_token2])

    fut_urlfetch1 = ndb.Future()
    fut_urlfetch1.set_result(test_utils.MockUrlFetchResult(401, {}, ''))
    fut_urlfetch2 = ndb.Future()
    fut_urlfetch2.set_result(
        test_utils.MockUrlFetchResult(200, {'foo': 'bar'}, 'yoohoo'))

    api.urlfetch_async = mock.create_autospec(
        api.urlfetch_async,
        side_effect=[fut_urlfetch1, fut_urlfetch2])

    res = api.do_request('http://example.com')

    self.assertEqual(res, (200, {'foo': 'bar'}, 'yoohoo'))

    self.assertEqual(api.urlfetch_async.call_args_list,
                     [mock.call('http://example.com',
                                headers={'authorization': 'OAuth bleh'},
                                follow_redirects=False,
                                payload=None,
                                method='GET',
                                deadline=None,
                                callback=None),
                      mock.call('http://example.com',
                                headers={'authorization': 'OAuth bleh'},
                                follow_redirects=False,
                                payload=None,
                                method='GET',
                                deadline=None,
                                callback=None)])
Example #39
0
  def testAsyncCall(self):
    api = rest_api._RestApi('scope')

    fut_urlfetch = ndb.Future()
    fut_urlfetch.set_result(
        test_utils.MockUrlFetchResult(200, {'foo': 'bar'}, 'yoohoo'))
    api.urlfetch_async = mock.create_autospec(api.urlfetch_async,
                                              return_value=fut_urlfetch)

    fut = api.do_request_async('http://example.com')
    res = fut.get_result()

    self.assertEqual(res, (200, {'foo': 'bar'}, 'yoohoo'))
    api.urlfetch_async.assert_called_once_with(
        'http://example.com',
        headers=mock.ANY,
        follow_redirects=False,
        payload=None,
        method='GET',
        deadline=None,
        callback=None)
Example #40
0
    def testRefreshToken(self):
        api = rest_api._RestApi('scope')

        fut_get_token1 = ndb.Future()
        fut_get_token1.set_result('blah')
        fut_get_token2 = ndb.Future()
        fut_get_token2.set_result('bleh')

        api.get_token_async = mock.create_autospec(
            api.get_token_async, side_effect=[fut_get_token1, fut_get_token2])

        fut_urlfetch1 = ndb.Future()
        fut_urlfetch1.set_result(test_utils.MockUrlFetchResult(401, {}, ''))
        fut_urlfetch2 = ndb.Future()
        fut_urlfetch2.set_result(
            test_utils.MockUrlFetchResult(200, {'foo': 'bar'}, 'yoohoo'))

        api.urlfetch_async = mock.create_autospec(
            api.urlfetch_async, side_effect=[fut_urlfetch1, fut_urlfetch2])

        res = api.do_request('http://example.com')

        self.assertEqual(res, (200, {'foo': 'bar'}, 'yoohoo'))

        self.assertEqual(api.urlfetch_async.call_args_list, [
            mock.call('http://example.com',
                      headers={'authorization': 'OAuth bleh'},
                      follow_redirects=False,
                      payload=None,
                      method='GET',
                      deadline=None,
                      callback=None),
            mock.call('http://example.com',
                      headers={'authorization': 'OAuth bleh'},
                      follow_redirects=False,
                      payload=None,
                      method='GET',
                      deadline=None,
                      callback=None)
        ])
Example #41
0
  def testRetryAfterAppIdentityError(self):
    api = rest_api._RestApi('scope')

    token_fut = ndb.Future()
    token_fut.set_result('token1')
    api.get_token_async = mock.create_autospec(
        api.get_token_async,
        side_effect=[app_identity.InternalError,
                     app_identity.InternalError,
                     token_fut])

    resp_fut = ndb.Future()
    resp_fut.set_result(test_utils.MockUrlFetchResult(httplib.ACCEPTED,
                                                      None, None))
    ndb.Context.urlfetch = mock.create_autospec(
        ndb.Context.urlfetch,
        side_effect=[resp_fut])

    self.assertEqual(httplib.ACCEPTED, api.do_request('foo')[0])
    self.assertEqual(
        'OAuth token1',
        ndb.Context.urlfetch.call_args[1]['headers']['authorization'])
    self.assertEqual(3, api.get_token_async.call_count)
  def testRetryAfterAppIdentityError(self):
    api = rest_api._RestApi('scope')

    token_fut = ndb.Future()
    token_fut.set_result('token1')
    api.get_token_async = mock.create_autospec(
        api.get_token_async,
        side_effect=[app_identity.InternalError,
                     app_identity.InternalError,
                     token_fut])

    resp_fut = ndb.Future()
    resp_fut.set_result(test_utils.MockUrlFetchResult(httplib.ACCEPTED,
                                                      None, None))
    ndb.Context.urlfetch = mock.create_autospec(
        ndb.Context.urlfetch,
        side_effect=[resp_fut])

    self.assertEqual(httplib.ACCEPTED, api.do_request('foo')[0])
    self.assertEqual(
        'OAuth token1',
        ndb.Context.urlfetch.call_args[1]['headers']['authorization'])
    self.assertEqual(3, api.get_token_async.call_count)
Example #43
0
  def testPickling(self):
    retry_params = api_utils.RetryParams(max_retries=1000)
    api = rest_api._RestApi('scope', service_account_id=1,
                            retry_params=retry_params)
    self.assertNotEqual(None, api.get_token())

    pickled_api = pickle.loads(pickle.dumps(api))
    self.assertEqual(0, len(set(api.__dict__.keys()) ^
                            set(pickled_api.__dict__.keys())))
    for k, v in api.__dict__.iteritems():
      if not hasattr(v, '__call__'):
        self.assertEqual(v, pickled_api.__dict__[k])

    pickled_api.token = None

    fut_urlfetch = ndb.Future()
    fut_urlfetch.set_result(
        test_utils.MockUrlFetchResult(200, {'foo': 'bar'}, 'yoohoo'))
    pickled_api.urlfetch_async = mock.create_autospec(
        pickled_api.urlfetch_async, return_value=fut_urlfetch)

    res = pickled_api.do_request('http://example.com')
    self.assertEqual(res, (200, {'foo': 'bar'}, 'yoohoo'))
Example #44
0
 def testMultipleScopes(self):
   api = rest_api._RestApi(['scope1', 'scope2'])
   self.assertEqual(api.scopes, ['scope1', 'scope2'])
Example #45
0
 def testMultipleScopes(self):
     api = rest_api._RestApi(['scope1', 'scope2'])
     self.assertEqual(api.scopes, ['scope1', 'scope2'])