From: SuprDewd Date: Wed, 10 Jul 2013 00:10:34 +0000 (+0000) Subject: Fixed laziness of current_user in UI modules, and added tests for current_user. #820 X-Git-Tag: v3.2.0b1~106^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=29e067f087b8666ebe4ab67d7d786bd9389e7dbc;p=thirdparty%2Ftornado.git Fixed laziness of current_user in UI modules, and added tests for current_user. #820 --- diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index 942a8917e..6777f47cd 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -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): diff --git a/tornado/web.py b/tornado/web.py index eade230ef..b27e23aed 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -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()