App Engine的python客户端库有made oauth flow really easy以及以下装饰器。
@decorator.oauth_required
但是对单元测试进行模拟/修补并不是一件容易的事。例如,在下面的get处理程序中,我需要删除oauth装饰器。
from auth import decorator
class ListUsersHandler(webapp2.RequestHandler):
@decorator.oauth_required
def get(self):
self.response.write(_RenderUserListTemplate())
我尝试了类似下面的内容。
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))
但是,我看到这个错误,似乎并没有给出太多线索。
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 "/google_appengine/lib/webob-1.2.3/webob/request.py", line 1292, in send
application, catch_exc_info=True)
File "/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