How to use a test tornado server handler that authenticates a user via a secure cookie

后端 未结 3 1961
孤街浪徒
孤街浪徒 2021-02-04 15:40

How can I write a unit test for a tornado handler that authenticates a user via a secure cookie? Here is the code (and sudo code) for a dummy test that I\'d like to make pass. I

3条回答
  •  滥情空心
    2021-02-04 16:29

    Using mock:

    import mock
    
    ...
    
    class UserAPITest(AsyncHTTPTestCase):
        def get_app(self):
            self.app = Application([('/', MainHandler)],
                        cookie_secret='asdfasdf')
            return self.app
    
        def test_user_profile_annoymous(self):
            with mock.patch.object(MainHandler, 'get_secure_cookie') as m:
                m.return_value = 'user_email'
                response = self.fetch('/', method='GET')
            self.assertEqual('sucess', to_unicode(response.body) )
    

提交回复
热议问题