mardi 6 octobre 2015

How to Mock/Patch App Engine's Oauth Decorator?

App Engine's python client library has made oauth flow really easy with the following decorator.

@decorator.oauth_required

But it's really not straightforward to mock/patch for unit testing. For example in the following get handler, I need to stub out oauth decorator.

from auth import decorator

class ListUsersHandler(webapp2.RequestHandler):

  @decorator.oauth_required
  def get(self):
    self.response.write(_RenderUserListTemplate())

I have tried something like below.

from mock import patch
patch('decorator.oauth_required', lambda x: x).start()
import user

class MyTest(unittest.TestCase):
  def setUp(self):
    app = webapp2.WSGIApplication([('/', user.ListUsersHandler)])
    self.testapp = webtest.TestApp(app)

  def testListUsersHandler(self):
    response = self.testapp.get('/')
    self.assertTrue(('list tokens' in response))

But, what I'm seeing this error, which doesn't seem to give much clue.

Traceback (most recent call last):
  File "user_test.py", line 44, in testAbc
    response = self.testapp.get('/')
  File "/usr/local/lib/python2.7/dist-packages/webtest/app.py", line 322, in get
    expect_errors=expect_errors)
  File "/usr/local/lib/python2.7/dist-packages/webtest/app.py", line 605, in do_request
    res = req.get_response(app, catch_exc_info=True)
  File "/usr/local/google/home/henryc/google_appengine/lib/webob-1.2.3/webob/request.py", line 1292, in send
application, catch_exc_info=True)
  File "/usr/local/google/home/henryc/google_appengine/lib/webob-1.2.3/webob/request.py", line 1269, in call_application
    return (captured[0], captured[1], app_iter, captured[2])
IndexError: list index out of range

Aucun commentaire:

Enregistrer un commentaire