From 29f5c1a101802e0ba66d72195fbe4e9d340a96a0 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Thu, 10 Dec 2009 15:54:43 +0000 Subject: - Added a "Special Exceptions" section to the "Views" narrative documentation chapter explaining the effect of raising ``repoze.bfg.exceptions.NotFound`` and ``repoze.bfg.exceptions.Forbidden`` from within view code. - When the ``repoze.bfg.exceptions.NotFound`` or ``repoze.bfg.exceptions.Forbidden`` error is raised from within a custom root factory or the ``factory`` of a route, the appropriate response is now sent to the requesting user agent (the result of the notfound view or the forbidden view, respectively). When these errors are raised from within a root factory, the ``context`` passed to the notfound or forbidden view will be ``None``. Also, the request will not be decorated with ``view_name``, ``subpath``, ``context``, etc. as would normally be the case if traversal had been allowed to take place. --- repoze/bfg/tests/test_router.py | 43 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'repoze/bfg/tests') diff --git a/repoze/bfg/tests/test_router.py b/repoze/bfg/tests/test_router.py index 45e66b8fe..5352c6d79 100644 --- a/repoze/bfg/tests/test_router.py +++ b/repoze/bfg/tests/test_router.py @@ -42,7 +42,8 @@ class TestRouter(unittest.TestCase): def _registerTraverserFactory(self, context, view_name='', subpath=None, traversed=None, virtual_root=None, - virtual_root_path=None, **kw): + virtual_root_path=None, raise_error=None, + **kw): from repoze.bfg.interfaces import ITraverser if virtual_root is None: @@ -59,6 +60,8 @@ class TestRouter(unittest.TestCase): self.root = root def __call__(self, request): + if raise_error: + raise raise_error values = {'root':self.root, 'context':context, 'view_name':view_name, @@ -504,6 +507,44 @@ class TestRouter(unittest.TestCase): self.failUnless(req_iface.providedBy(request)) self.failUnless(IFoo.providedBy(request)) + def test_root_factory_raises_notfound(self): + from repoze.bfg.interfaces import IRootFactory + from repoze.bfg.exceptions import NotFound + from zope.interface import Interface + from zope.interface import directlyProvides + def rootfactory(request): + raise NotFound('from root factory') + self.registry.registerUtility(rootfactory, IRootFactory) + class IContext(Interface): + pass + context = DummyContext() + directlyProvides(context, IContext) + environ = self._makeEnviron() + router = self._makeOne() + start_response = DummyStartResponse() + app_iter = router(environ, start_response) + self.assertEqual(start_response.status, '404 Not Found') + self.failUnless('from root factory' in app_iter[0]) + + def test_root_factory_raises_forbidden(self): + from repoze.bfg.interfaces import IRootFactory + from repoze.bfg.exceptions import Forbidden + from zope.interface import Interface + from zope.interface import directlyProvides + def rootfactory(request): + raise Forbidden('from root factory') + self.registry.registerUtility(rootfactory, IRootFactory) + class IContext(Interface): + pass + context = DummyContext() + directlyProvides(context, IContext) + environ = self._makeEnviron() + router = self._makeOne() + start_response = DummyStartResponse() + app_iter = router(environ, start_response) + self.assertEqual(start_response.status, '401 Unauthorized') + self.failUnless('from root factory' in app_iter[0]) + class DummyContext: pass -- cgit v1.2.3