I have a tornado web application. Many pages require a valid cookie to access. It makes it impossible to test those handlers.
I want to use the mock
library to mock the tornado.web.RequestHandler.get_current_user
method. But I just cannot get right.
This is how I am doing right now:
class MyUT(tornado.testing.AsyncHTTPTestCase):
def get_app(self):
settings = {
"template_path": '../../../templates',
"cookie_secret": 'secret',
"login_url": '/admin/login',
"debug": True
}
return Application([
(r'/admin/create/super', handlers.CreateSuperUserHandler)
], **settings)
def testGet(self):
with mock.patch.object(handlers.CreateSuperUserHandler, 'get_current_user') as m:
m.return_value = {}
response = self.fetch('/admin/create/super')
print(response.body)
self.assertGreater(response.body.index('create'), 0)
If I execute this code, I got a 404 error. Because I did not define the login hanlder. But I am expecting the mocked get_current_user
method return a user object, so do not go to the login page while testing.
One strange thing I found is, if I add m.assert_any_call()
in the with
block, there's no assertion error.
The @authenticated
decorator redirects to the login page if get_current_user
returns a falsy value. Try returning a non-empty dict.
You also use handlers.CreateSuperUserHandler
in one place and handlers.AdminArea.CreateSuperUserHandler
in another; are those the same class object?