def setUp(self): manager = ExtensionManager('') self.extension = \ TestExtensionWithRegistration(extension_manager=manager) self.hook_no_applies_name = "template-hook-no-applies-name" self.template_hook_no_applies = TemplateHook( self.extension, self.hook_no_applies_name, "test_module/some_template.html", []) self.hook_with_applies_name = "template-hook-with-applies-name" self.template_hook_with_applies = TemplateHook( self.extension, self.hook_with_applies_name, "test_module/some_template.html", [ 'test-url-name', 'url_2', 'url_3', ] ) self.request = Mock() self.request._djblets_extensions_kwargs = {} self.request.path_info = '/' self.request.resolver_match = Mock() self.request.resolver_match.url_name = 'root'
class TemplateHookTest(TestCase): def setUp(self): manager = ExtensionManager('') self.extension = \ TestExtensionWithRegistration(extension_manager=manager) self.hook_with_applies_name = "template-hook-with-applies-name" self.hook_no_applies_name = "template-hook-no-applies-name" self.template_hook_no_applies = TemplateHook(self.extension, self.hook_no_applies_name, "test_module/some_template.html", []) self.template_hook_with_applies = TemplateHook(self.extension, self.hook_with_applies_name, "test_module/some_template.html", [ 'test-url-name', 'url_2', 'url_3', ] ) self.fake_request = Mock() self.fake_request._djblets_extensions_kwargs = {} self.fake_request.path_info = '/' self.context = { 'request': self.fake_request, } def test_hook_added_to_class_by_name(self): """Testing TemplateHook registration""" self.assertTrue(self.template_hook_with_applies in self.template_hook_with_applies.__class__ ._by_name[self.hook_with_applies_name]) # The TemplateHook should also be added to the Extension's collection # of hooks. self.assertTrue(self.template_hook_with_applies in self.extension.hooks) def test_hook_shutdown(self): """Testing TemplateHook shutdown""" self.template_hook_with_applies.shutdown() self.assertTrue(self.template_hook_with_applies not in self.template_hook_with_applies.__class__ ._by_name[self.hook_with_applies_name]) # The TemplateHook should still be in the Extension's collection # of hooks. self.assertTrue(self.template_hook_with_applies in self.extension.hooks) def test_applies_to_default(self): """Testing TemplateHook.applies_to defaults to everything""" self.assertTrue(self.template_hook_no_applies.applies_to(self.context)) self.assertTrue(self.template_hook_no_applies.applies_to(None)) def test_applies_to(self): """Testing TemplateHook.applies_to customization""" self.fake_request.path_info = '/some_other/url' self.assertFalse( self.template_hook_with_applies.applies_to(self.context))
def _render_hooks(): request = context['request'] for hook in TemplateHook.by_name(name): try: if hook.applies_to(request): context.push() try: yield hook.render_to_string(request, context) except Exception as e: logging.error('Error rendering TemplateHook %r: %s', hook, e, exc_info=1) context.pop() except Exception as e: logging.error( 'Error when calling applies_to for ' 'TemplateHook %r: %s', hook, e, exc_info=1)
def setUp(self): self.extension = TestExtensionWithRegistration() self.hook_with_applies_name = "template-hook-with-applies-name" self.hook_no_applies_name = "template-hook-no-applies-name" self.template_hook_no_applies = TemplateHook( self.extension, self.hook_no_applies_name, "test_module/some_template.html", [] ) self.template_hook_with_applies = TemplateHook( self.extension, self.hook_with_applies_name, "test_module/some_template.html", ["test-url-name", "url_2", "url_3"], ) self.fake_request = Mock() self.fake_request._djblets_extensions_kwargs = {} self.fake_request.path_info = "/" self.context = {"request": self.fake_request}
def initialize(self): self.hook_no_applies_name = 'template-hook-no-applies-name' self.template_hook_no_applies = TemplateHook( self, self.hook_no_applies_name, 'test_module/some_template.html', []) self.hook_with_applies_name = 'template-hook-with-applies-name' self.template_hook_with_applies = TemplateHook( self, self.hook_with_applies_name, 'test_module/some_template.html', [ 'test-url-name', 'url_2', 'url_3', ] )
def template_hook_point(context, name): """ Registers a template hook point that TemplateHook instances can attach to. """ s = "" for hook in TemplateHook.by_name(name): if hook.applies_to(context): s += render_to_string(hook.template_name, context) return s
def template_hook_point(context, name): """ Registers a template hook point that TemplateHook instances can attach to. """ s = "" for hook in TemplateHook.by_name(name): if hook.applies_to(context): s += hook.render_to_string(context.get('request', None), context) return s
def setUp(self): self.extension = TestExtensionWithRegistration() self.hook_with_applies_name = "template-hook-with-applies-name" self.hook_no_applies_name = "template-hook-no-applies-name" self.template_hook_no_applies = TemplateHook( self.extension, self.hook_no_applies_name, "test_module/some_template.html", []) self.template_hook_with_applies = TemplateHook( self.extension, self.hook_with_applies_name, "test_module/some_template.html", [ 'test-url-name', 'url_2', 'url_3', ]) self.fake_request = Mock() self.fake_request._djblets_extensions_kwargs = {} self.fake_request.path_info = '/' self.context = { 'request': self.fake_request, }
def template_hook_point(context, name): """ Registers a template hook point that TemplateHook instances can attach to. """ request = context['request'] return ''.join([ hook.render_to_string(request, context) for hook in TemplateHook.by_name(name) if hook.applies_to(request) ])
def _render_hooks(): request = context['request'] for hook in TemplateHook.by_name(name): if hook.applies_to(request): context.push() try: yield hook.render_to_string(request, context) except Exception as e: logging.error('Error rendering TemplateHook %r: %s', hook, e, exc_info=1) context.pop()
def setUp(self): manager = ExtensionManager('') self.extension = \ TestExtensionWithRegistration(extension_manager=manager) self.hook_no_applies_name = "template-hook-no-applies-name" self.template_hook_no_applies = TemplateHook( self.extension, self.hook_no_applies_name, "test_module/some_template.html", []) self.hook_with_applies_name = "template-hook-with-applies-name" self.template_hook_with_applies = TemplateHook( self.extension, self.hook_with_applies_name, "test_module/some_template.html", [ 'test-url-name', 'url_2', 'url_3', ]) self.request = Mock() self.request._djblets_extensions_kwargs = {} self.request.path_info = '/' self.request.resolver_match = Mock() self.request.resolver_match.url_name = 'root'
def test_render_to_string(self): """Testing TemplateHook.render_to_string""" hook = TemplateHook( self.extension, name='test', template_name='deco/box.html', extra_context={ 'content': 'Hello world', }) request = RequestFactory().request() result = hook.render_to_string(request, RequestContext(request, { 'classname': 'test', })) self.assertHTMLEqual( result, '<div class="box-container">' ' <div class="box test">' ' <div class="box-inner">' ' Hello world' ' </div>' ' </div>' '</div>')
def _render_hooks(): request = context["request"] for hook in TemplateHook.by_name(name): try: if hook.applies_to(request): context.push() try: yield hook.render_to_string(request, context) except Exception as e: logging.error("Error rendering TemplateHook %r: %s", hook, e, exc_info=1) context.pop() except Exception as e: logging.error("Error when calling applies_to for " "TemplateHook %r: %s", hook, e, exc_info=1)
def _render_hooks(): request = context['request'] for hook in TemplateHook.by_name(name): try: if hook.applies_to(request): context.push() try: yield hook.render_to_string(request, context) except Exception as e: logger.exception('Error rendering TemplateHook %r: %s', hook, e) context.pop() except Exception as e: logger.exception('Error when calling applies_to for ' 'TemplateHook %r: %s', hook, e)
class TemplateHookTest(TestCase): def setUp(self): self.extension = TestExtensionWithRegistration() self.hook_with_applies_name = "template-hook-with-applies-name" self.hook_no_applies_name = "template-hook-no-applies-name" self.template_hook_no_applies = TemplateHook( self.extension, self.hook_no_applies_name, "test_module/some_template.html", [] ) self.template_hook_with_applies = TemplateHook( self.extension, self.hook_with_applies_name, "test_module/some_template.html", ["test-url-name", "url_2", "url_3"], ) self.fake_request = Mock() self.fake_request._djblets_extensions_kwargs = {} self.fake_request.path_info = "/" self.context = {"request": self.fake_request} def test_hook_added_to_class_by_name(self): """The TemplateHook should be added to the _by_name collection in the TemplateHook class.""" self.assertTrue( self.template_hook_with_applies in self.template_hook_with_applies.__class__._by_name[self.hook_with_applies_name] ) # The TemplateHook should also be added to the Extension's collection # of hooks. self.assertTrue(self.template_hook_with_applies in self.extension.hooks) def test_hook_shutdown(self): """The TemplateHook should remove itself from the _by_name collection in the TemplateHook class if the TemplateHook is shutdown.""" self.template_hook_with_applies.shutdown() self.assertTrue( self.template_hook_with_applies not in self.template_hook_with_applies.__class__._by_name[self.hook_with_applies_name] ) # The TemplateHook should still be in the Extension's collection # of hooks. self.assertTrue(self.template_hook_with_applies in self.extension.hooks) def test_applies_by_default(self): """If a TemplateHook was constructed without an apply_to collection, then the applies_to method should automatically return True.""" self.assertTrue(self.template_hook_no_applies.applies_to(self.context)) self.assertTrue(self.template_hook_no_applies.applies_to(None)) def test_applies_with_apply_to(self): """If a TemplateHook was constructed with an apply_to collection, then the applies_to method should return True if and only if the desired URL in the context maps to one of the named URL's in the apply_to collection.""" self.assertTrue(self.template_hook_with_applies.applies_to(self.context)) def test_doesnt_apply_appropriately(self): """If a TemplateHook was constructed with an apply_to collection, and the desired URL in the context does not map to one of the named URL's in the apply_to collection, then the applies_to method should return False.""" self.fake_request.path_info = "/some_other/url" self.assertFalse(self.template_hook_with_applies.applies_to(self.context))
class TemplateHookTest(SpyAgency, TestCase): def setUp(self): manager = ExtensionManager('') self.extension = \ TestExtensionWithRegistration(extension_manager=manager) self.hook_no_applies_name = "template-hook-no-applies-name" self.template_hook_no_applies = TemplateHook( self.extension, self.hook_no_applies_name, "test_module/some_template.html", []) self.hook_with_applies_name = "template-hook-with-applies-name" self.template_hook_with_applies = TemplateHook( self.extension, self.hook_with_applies_name, "test_module/some_template.html", [ 'test-url-name', 'url_2', 'url_3', ]) self.request = Mock() self.request._djblets_extensions_kwargs = {} self.request.path_info = '/' self.request.resolver_match = Mock() self.request.resolver_match.url_name = 'root' def test_hook_added_to_class_by_name(self): """Testing TemplateHook registration""" self.assertTrue( self.template_hook_with_applies in self.template_hook_with_applies. __class__._by_name[self.hook_with_applies_name]) # The TemplateHook should also be added to the Extension's collection # of hooks. self.assertTrue( self.template_hook_with_applies in self.extension.hooks) def test_hook_shutdown(self): """Testing TemplateHook shutdown""" self.template_hook_with_applies.shutdown() self.assertTrue(self.template_hook_with_applies not in self.template_hook_with_applies.__class__._by_name[ self.hook_with_applies_name]) # The TemplateHook should still be in the Extension's collection # of hooks. self.assertTrue( self.template_hook_with_applies in self.extension.hooks) def test_applies_to_default(self): """Testing TemplateHook.applies_to defaults to everything""" self.assertTrue(self.template_hook_no_applies.applies_to(self.request)) self.assertTrue(self.template_hook_no_applies.applies_to(None)) def test_applies_to(self): """Testing TemplateHook.applies_to customization""" self.assertFalse( self.template_hook_with_applies.applies_to(self.request)) self.request.resolver_match.url_name = 'test-url-name' self.assertTrue( self.template_hook_with_applies.applies_to(self.request)) def test_context_doesnt_leak(self): """Testing TemplateHook's context won't leak state""" class MyTemplateHook(TemplateHook): def render_to_string(self, request, context): context['leaky'] = True return '' MyTemplateHook(self.extension, 'test') context = Context({}) context['request'] = None t = Template('{% load djblets_extensions %}' '{% template_hook_point "test" %}') t.render(context).strip() self.assertNotIn('leaky', context) def test_render_to_string_sandbox(self): """Testing TemplateHook sandboxing""" class MyTemplateHook(TemplateHook): def render_to_string(self, request, context): raise Exception('Oh noes') MyTemplateHook(self.extension, 'test') context = Context({}) context['request'] = None t = Template('{% load djblets_extensions %}' '{% template_hook_point "test" %}') t.render(context).strip() # Didn't crash. We're good. def test_applies_to_sandbox(self): """Testing TemplateHook for applies_to""" class MyTemplateHook(TemplateHook): def applies_to(self, request): raise Exception hook = MyTemplateHook(extension=self.extension, name='test') context = Context({}) context['request'] = self.request self.spy_on(hook.applies_to) t = Template('{% load djblets_extensions %}' '{% template_hook_point "test" %}') string = t.render(context).strip() self.assertEqual(string, '') self.assertTrue(hook.applies_to.called)
class TemplateHookTest(TestCase): def setUp(self): manager = ExtensionManager('') self.extension = \ TestExtensionWithRegistration(extension_manager=manager) self.hook_with_applies_name = "template-hook-with-applies-name" self.hook_no_applies_name = "template-hook-no-applies-name" self.template_hook_no_applies = TemplateHook(self.extension, self.hook_no_applies_name, "test_module/some_template.html", []) self.template_hook_with_applies = TemplateHook(self.extension, self.hook_with_applies_name, "test_module/some_template.html", [ 'test-url-name', 'url_2', 'url_3', ] ) self.request = Mock() self.request._djblets_extensions_kwargs = {} self.request.path_info = '/' self.request.resolver_match = Mock() self.request.resolver_match.url_name = 'root' def test_hook_added_to_class_by_name(self): """Testing TemplateHook registration""" self.assertTrue(self.template_hook_with_applies in self.template_hook_with_applies.__class__ ._by_name[self.hook_with_applies_name]) # The TemplateHook should also be added to the Extension's collection # of hooks. self.assertTrue(self.template_hook_with_applies in self.extension.hooks) def test_hook_shutdown(self): """Testing TemplateHook shutdown""" self.template_hook_with_applies.shutdown() self.assertTrue(self.template_hook_with_applies not in self.template_hook_with_applies.__class__ ._by_name[self.hook_with_applies_name]) # The TemplateHook should still be in the Extension's collection # of hooks. self.assertTrue(self.template_hook_with_applies in self.extension.hooks) def test_applies_to_default(self): """Testing TemplateHook.applies_to defaults to everything""" self.assertTrue(self.template_hook_no_applies.applies_to(self.request)) self.assertTrue(self.template_hook_no_applies.applies_to(None)) def test_applies_to(self): """Testing TemplateHook.applies_to customization""" self.assertFalse( self.template_hook_with_applies.applies_to(self.request)) self.request.resolver_match.url_name = 'test-url-name' self.assertTrue( self.template_hook_with_applies.applies_to(self.request)) def test_context_doesnt_leak(self): """Testing TemplateHook's context won't leak state""" class MyTemplateHook(TemplateHook): def render_to_string(self, request, context): context['leaky'] = True return '' hook = MyTemplateHook(self.extension, 'test') context = Context({}) context['request'] = None t = Template( '{% load djblets_extensions %}' '{% template_hook_point "test" %}') t.render(context).strip() self.assertNotIn('leaky', context) def test_sandbox(self): """Testing TemplateHook sandboxing""" class MyTemplateHook(TemplateHook): def render_to_string(self, request, context): raise Exception('Oh noes') hook = MyTemplateHook(self.extension, 'test') context = Context({}) context['request'] = None t = Template( '{% load djblets_extensions %}' '{% template_hook_point "test" %}') t.render(context).strip()
class TemplateHookTest(TestCase): def setUp(self): self.extension = TestExtensionWithRegistration() self.hook_with_applies_name = "template-hook-with-applies-name" self.hook_no_applies_name = "template-hook-no-applies-name" self.template_hook_no_applies = TemplateHook( self.extension, self.hook_no_applies_name, "test_module/some_template.html", []) self.template_hook_with_applies = TemplateHook( self.extension, self.hook_with_applies_name, "test_module/some_template.html", [ 'test-url-name', 'url_2', 'url_3', ]) self.fake_request = Mock() self.fake_request._djblets_extensions_kwargs = {} self.fake_request.path_info = '/' self.context = { 'request': self.fake_request, } def test_hook_added_to_class_by_name(self): """The TemplateHook should be added to the _by_name collection in the TemplateHook class.""" self.assertTrue( self.template_hook_with_applies in self.template_hook_with_applies. __class__._by_name[self.hook_with_applies_name]) # The TemplateHook should also be added to the Extension's collection # of hooks. self.assertTrue( self.template_hook_with_applies in self.extension.hooks) def test_hook_shutdown(self): """The TemplateHook should remove itself from the _by_name collection in the TemplateHook class if the TemplateHook is shutdown.""" self.template_hook_with_applies.shutdown() self.assertTrue(self.template_hook_with_applies not in self.template_hook_with_applies.__class__._by_name[ self.hook_with_applies_name]) # The TemplateHook should still be in the Extension's collection # of hooks. self.assertTrue( self.template_hook_with_applies in self.extension.hooks) def test_applies_by_default(self): """If a TemplateHook was constructed without an apply_to collection, then the applies_to method should automatically return True.""" self.assertTrue(self.template_hook_no_applies.applies_to(self.context)) self.assertTrue(self.template_hook_no_applies.applies_to(None)) def test_applies_with_apply_to(self): """If a TemplateHook was constructed with an apply_to collection, then the applies_to method should return True if and only if the desired URL in the context maps to one of the named URL's in the apply_to collection.""" self.assertTrue( self.template_hook_with_applies.applies_to(self.context)) def test_doesnt_apply_appropriately(self): """If a TemplateHook was constructed with an apply_to collection, and the desired URL in the context does not map to one of the named URL's in the apply_to collection, then the applies_to method should return False.""" self.fake_request.path_info = '/some_other/url' self.assertFalse( self.template_hook_with_applies.applies_to(self.context))