Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
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))
Ejemplo n.º 3
0
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()
Ejemplo n.º 4
0
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))
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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))