]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Fixed laziness of current_user in UI modules, and added tests for current_user. #820 848/head
authorSuprDewd <suprdewd@gmail.com>
Wed, 10 Jul 2013 00:10:34 +0000 (00:10 +0000)
committerSuprDewd <suprdewd@gmail.com>
Wed, 10 Jul 2013 00:12:25 +0000 (00:12 +0000)
tornado/test/web_test.py
tornado/web.py

index 942a8917ea23ab73cd584c1dec800c570c56234c..6777f47cd8dd5a5860007c8055e43fc05642a22e 100644 (file)
@@ -1459,6 +1459,91 @@ class SetCurrentUserTest(SimpleHandlerTestCase):
         self.assertEqual(response.body, b'Hello Ben')
 
 
+@wsgi_safe
+class GetCurrentUserTest(WebTestCase):
+    def get_app_kwargs(self):
+        class WithoutUserModule(UIModule):
+            def render(self):
+                return ''
+
+        class WithUserModule(UIModule):
+            def render(self):
+                return str(self.current_user)
+
+        loader = DictLoader({
+            'without_user.html': '',
+            'with_user.html': '{{ current_user }}',
+            'without_user_module.html': '{% module WithoutUserModule() %}',
+            'with_user_module.html': '{% module WithUserModule() %}',
+        })
+        return dict(template_loader=loader,
+                    ui_modules={'WithUserModule': WithUserModule,
+                                'WithoutUserModule': WithoutUserModule})
+
+    def tearDown(self):
+        super(GetCurrentUserTest, self).tearDown()
+        RequestHandler._template_loaders.clear()
+
+    def get_handlers(self):
+        class CurrentUserHandler(RequestHandler):
+            def prepare(self):
+                self.has_loaded_current_user = False
+
+            def get_current_user(self):
+                self.has_loaded_current_user = True
+                return ''
+
+        class WithoutUserHandler(CurrentUserHandler):
+            def get(self):
+                self.render_string('without_user.html')
+                self.finish(str(self.has_loaded_current_user))
+
+        class WithUserHandler(CurrentUserHandler):
+            def get(self):
+                self.render_string('with_user.html')
+                self.finish(str(self.has_loaded_current_user))
+
+        class CurrentUserModuleHandler(CurrentUserHandler):
+            def get_template_namespace(self):
+                # If RequestHandler.get_template_namespace is called, then
+                # get_current_user is evaluated. Until #820 is fixed, this
+                # is a small hack to circumvent the issue.
+                return self.ui
+
+        class WithoutUserModuleHandler(CurrentUserModuleHandler):
+            def get(self):
+                self.render_string('without_user_module.html')
+                self.finish(str(self.has_loaded_current_user))
+
+        class WithUserModuleHandler(CurrentUserModuleHandler):
+            def get(self):
+                self.render_string('with_user_module.html')
+                self.finish(str(self.has_loaded_current_user))
+
+        return [('/without_user', WithoutUserHandler),
+                ('/with_user', WithUserHandler),
+                ('/without_user_module', WithoutUserModuleHandler),
+                ('/with_user_module', WithUserModuleHandler)]
+
+    @unittest.skip('needs fix')
+    def test_get_current_user_is_lazy(self):
+        # TODO: Make this test pass. See #820.
+        response = self.fetch('/without_user')
+        self.assertEqual(response.body, b'False')
+
+    def test_get_current_user_works(self):
+        response = self.fetch('/with_user')
+        self.assertEqual(response.body, b'True')
+
+    def test_get_current_user_from_ui_module_is_lazy(self):
+        response = self.fetch('/without_user_module')
+        self.assertEqual(response.body, b'False')
+
+    def test_get_current_user_from_ui_module_works(self):
+        response = self.fetch('/with_user_module')
+        self.assertEqual(response.body, b'True')
+
+
 @wsgi_safe
 class UnimplementedHTTPMethodsTest(SimpleHandlerTestCase):
     class Handler(RequestHandler):
index eade230ef82c98120b9c8c37d02f64a2c5b375bb..b27e23aedf1eb32f20d3d15ae153f987873451a6 100644 (file)
@@ -2305,9 +2305,12 @@ class UIModule(object):
         self.handler = handler
         self.request = handler.request
         self.ui = handler.ui
-        self.current_user = handler.current_user
         self.locale = handler.locale
 
+    @property
+    def current_user(self):
+        return self.handler.current_user
+
     def render(self, *args, **kwargs):
         """Overridden in subclasses to return this module's output."""
         raise NotImplementedError()