Exemplo n.º 1
0
    def test_path_too_long(self):
        """Test that the CMS try to resolve the whole page path to find
        a suitable sub path with delegation."""
        page1 = self.new_page(content={'slug':'page1'})
        page2 = self.new_page(content={'slug':'page2'})
        from pages import urlconf_registry as reg
        reg.register_urlconf('test', 'pages.testproj.documents.urls',
            label='test')
        page2.delegate_to = 'test'
        page1.delegate_to = 'test'
        page1.save()
        page2.save()
        page2.parent = page1
        page2.save()

        from pages.testproj.documents.models import Document
        doc = Document(title='doc title 1', text='text', page=page1)
        doc.save()

        req = get_request_mock()
        self.set_setting("PAGE_HIDE_ROOT_SLUG", True)
        def _get_context_page(path):
            return details(req, path, 'en-us')
        self.assertEqual(_get_context_page('/').status_code, 200)
        self.assertEqual(_get_context_page('/page1').status_code, 200)
        self.assertEqual(_get_context_page('/page1/').status_code, 200)
        self.assertEqual(_get_context_page('/page1/page2').status_code, 200)
        self.assertEqual(_get_context_page('/page1/page2/').status_code, 200)
        self.assertEqual(_get_context_page('/page1/page2/doc-%d' % doc.id
            ).status_code, 200)
        self.assertRaises(Http404, _get_context_page,
            '/page1/page-wrong/doc-%d' % doc.id)

        reg.registry = []
Exemplo n.º 2
0
    def test_default_view_with_language_prefix(self):
        """
        Test that everything is working with the language prefix option
        activated.
        """
        self.set_setting("PAGE_USE_LANGUAGE_PREFIX", True)

        from pages.views import details
        req = get_request_mock()
        self.assertRaises(Http404, details, req, '/pages/')

        page1 = self.new_page(content={'slug': 'page1'})
        page2 = self.new_page(content={'slug': 'page2'})

        self.assertEqual(page1.get_url_path(),
            reverse('pages-details-by-path', args=[],
            kwargs={'lang': 'en-us', 'path': 'page1'})
        )

        self.assertEqual(details(req, page1.get_url_path(),
            only_context=True)['current_page'],
            page1)

        self.assertEqual(details(req, path=page2.get_complete_slug(),
            only_context=True)['current_page'], page2)

        self.assertEqual(details(req, page2.get_url_path(),
            only_context=True)['current_page'],
            page2)

        self.set_setting("PAGE_USE_LANGUAGE_PREFIX", False)

        self.assertEqual(details(req, page2.get_url_path(),
            only_context=True)['current_page'],
            page2)
Exemplo n.º 3
0
 def test_auto_render(self):
     """
     Call an @auto_render decorated view with allowed keyword argument
     combinations.
     """
     @auto_render
     def testview(request, *args, **kwargs):
         assert 'only_context' not in kwargs
         assert 'template_name' not in kwargs
         return 'pages/tests/auto_render.txt', locals()
     request_mock = get_request_mock()
     response = testview(request_mock)
     self.assertEqual(response.__class__, HttpResponse)
     self.assertEqual(response.content,
                      "template_name: 'pages/tests/auto_render.txt', "
                      "only_context: ''\n")
     self.assertEqual(testview(request_mock, only_context=True),
                      {'args': (), 'request': request_mock, 'kwargs': {}})
     response = testview(request_mock, only_context=False)
     self.assertEqual(response.__class__, HttpResponse)
     self.assertEqual(response.content,
                      "template_name: 'pages/tests/auto_render.txt', "
                      "only_context: ''\n")
     response = testview(request_mock, template_name='pages/tests/auto_render2.txt')
     self.assertEqual(response.__class__, HttpResponse)
     self.assertEqual(response.content,
                      "alternate template_name: 'pages/tests/auto_render2.txt', "
                      "only_context: ''\n")
    def test_auto_render(self):
        """
        Call an @auto_render decorated view with allowed keyword argument
        combinations.
        """
        @auto_render
        def testview(request, *args, **kwargs):
            assert 'only_context' not in kwargs
            assert 'template_name' not in kwargs
            return 'pages/tests/auto_render.txt', locals()

        request_mock = get_request_mock()
        response = testview(request_mock)
        self.assertEqual(response.__class__, HttpResponse)
        self.assertEqual(
            response.content, "template_name: 'pages/tests/auto_render.txt', "
            "only_context: ''\n")
        self.assertEqual(testview(request_mock, only_context=True), {
            'args': (),
            'request': request_mock,
            'kwargs': {}
        })
        response = testview(request_mock, only_context=False)
        self.assertEqual(response.__class__, HttpResponse)
        self.assertEqual(
            response.content, "template_name: 'pages/tests/auto_render.txt', "
            "only_context: ''\n")
        response = testview(request_mock,
                            template_name='pages/tests/auto_render2.txt')
        self.assertEqual(response.__class__, HttpResponse)
        self.assertEqual(
            response.content,
            "alternate template_name: 'pages/tests/auto_render2.txt', "
            "only_context: ''\n")
Exemplo n.º 5
0
 def test_context_processor(self):
     """Test that the page's context processor is properly activated."""
     from pages.views import details
     req = get_request_mock()
     page1 = self.new_page(content={'slug': 'page1', 'title': 'hello'})
     page1.save()
     self.set_setting("PAGES_MEDIA_URL", "test_request_context")
     self.assertContains(details(req, path='/'), "test_request_context")
Exemplo n.º 6
0
def get_placeholders(template_name):
    """Return a list of PlaceholderNode found in the given template.

    :param template_name: the name of the template file
    """
    try:
        temp = loader.get_template(template_name)
    except TemplateDoesNotExist:
        return []
        
    request = get_request_mock()
    context = get_context_mock()
    # I need to render the template in order to extract
    # placeholder tags
    temp.render(RequestContext(request, context))
    plist, blist = [], []
    _placeholders_recursif(temp.nodelist, plist, blist)
    return plist
Exemplo n.º 7
0
def get_placeholders(template_name):
    """Return a list of PlaceholderNode found in the given template"""
    try:
        temp = loader.get_template(template_name)
    except TemplateDoesNotExist:
        return []
        
    request = get_request_mock()

    try:
        # to avoid circular import
        from pages.views import details
        context = details(request, only_context=True)
    except Http404:
        context = {}
    temp.render(RequestContext(request, context))
    plist, blist = [], []
    placeholders_recursif(temp.nodelist, plist, blist)
    return plist
Exemplo n.º 8
0
 def test_contact_placeholder(self):
     tpl = ("{% load pages_tags %}{% contactplaceholder contact %}")
     
     template = get_template_from_string(tpl)
     page = self.new_page({'contact': 'hello'})
     context = Context({'current_page': page})
     
     import logging
     logger = logging.getLogger("pages")
     lvl = logger.getEffectiveLevel()
     logger.setLevel(logging.ERROR)
     
     with self.assertRaises(ValueError): 
         self.assertEqual(template.render(context), u'hello')
         
     logger.setLevel(lvl)
         
     context = Context({'current_page': page, 'request':get_request_mock()})
     self.assertTrue("<form" in template.render(context))
Exemplo n.º 9
0
    def test_auto_render_any_httpresponse(self):
        """Call an @auto_render decorated view which returns an
        arbitrary HttpResponse subclass with allowed keyword argument
        combinations."""

        class MyResponse(HttpResponse): pass
        @auto_render
        def testview(request, *args, **kwargs):
            assert 'only_context' not in kwargs
            assert 'template_name' not in kwargs
            return MyResponse("toto")
        request_mock = get_request_mock()
        response = testview(request_mock)
        self.assertEqual(response.__class__, MyResponse)
        self.assertOnlyContextException(testview)
        self.assertEqual(response.content, "toto")
        self.assertEqual(testview(request_mock, only_context=False).__class__,
                         MyResponse)
        response = testview(None, template_name='pages/tests/auto_render2.txt')
        self.assertEqual(response.__class__, MyResponse)
        self.assertEqual(response.content, "toto")
Exemplo n.º 10
0
    def test_path_too_long(self):
        """Test that the CMS try to resolve the whole page path to find
        a suitable sub path with delegation."""
        page1 = self.new_page(content={'slug': 'page1'})
        page2 = self.new_page(content={'slug': 'page2'})
        from pages import urlconf_registry as reg
        reg.register_urlconf('test',
                             'pages.testproj.documents.urls',
                             label='test')
        page2.delegate_to = 'test'
        page1.delegate_to = 'test'
        page1.save()
        page2.save()
        page2.parent = page1
        page2.save()

        from pages.testproj.documents.models import Document
        doc = Document(title='doc title 1', text='text', page=page1)
        doc.save()

        req = get_request_mock()
        self.set_setting("PAGE_HIDE_ROOT_SLUG", False)
        page1.invalidate()
        page2.invalidate()

        def _get_context_page(path):
            return details(req, path, 'en-us')

        self.assertEqual(_get_context_page('/').status_code, 200)
        self.assertEqual(_get_context_page('/page1/').status_code, 200)
        self.assertEqual(_get_context_page('/page1/').status_code, 200)
        self.assertEqual(_get_context_page('/page1/page2').status_code, 200)
        self.assertEqual(_get_context_page('/page1/page2/').status_code, 200)
        self.assertEqual(
            _get_context_page('/page1/page2/doc-%d' % doc.id).status_code, 200)
        self.assertRaises(Http404, _get_context_page,
                          '/page1/page-wrong/doc-%d' % doc.id)

        reg.registry = []
    def test_auto_render_any_httpresponse(self):
        """Call an @auto_render decorated view which returns an
        arbitrary HttpResponse subclass with allowed keyword argument
        combinations."""
        class MyResponse(HttpResponse):
            pass

        @auto_render
        def testview(request, *args, **kwargs):
            assert 'only_context' not in kwargs
            assert 'template_name' not in kwargs
            return MyResponse("toto")

        request_mock = get_request_mock()
        response = testview(request_mock)
        self.assertEqual(response.__class__, MyResponse)
        self.assertOnlyContextException(testview)
        self.assertEqual(response.content, "toto")
        self.assertEqual(
            testview(request_mock, only_context=False).__class__, MyResponse)
        response = testview(None, template_name='pages/tests/auto_render2.txt')
        self.assertEqual(response.__class__, MyResponse)
        self.assertEqual(response.content, "toto")