def _testBaseTags(self, url, expected):
        # Make sure I1 and O1 are visible in the module namespace
        # so that the classes can be pickled.
        import transaction

        pub = BrowserPublication(self.db)

        ztapi.browserView(I1, 'view', DummyView)
        ztapi.setDefaultViewName(I1, 'view')
        ztapi.browserViewProviding(None, TestTraverser, IBrowserPublisher)

        ob = O1()

        ## the following is for running the tests standalone
        principalRegistry.defineDefaultPrincipal(
            'tim', 'timbot', 'ai at its best')

        # now place our object inside the application

        connection = self.db.open()
        app = connection.root()['Application']
        app.somepath = ob
        transaction.commit()
        connection.close()

        defineChecker(app.__class__, NamesChecker(somepath='xxx'))

        req = self._createRequest(url, pub)
        response = req.response

        publish(req, handle_errors=0)

        self.assertEqual(response.getBase(), expected)
Esempio n. 2
0
    def _testBaseTags(self, url, expected):
        # Make sure I1 and O1 are visible in the module namespace
        # so that the classes can be pickled.
        import transaction

        pub = BrowserPublication(self.db)

        ztapi.browserView(I1, 'view', DummyView)
        ztapi.setDefaultViewName(I1, 'view')
        ztapi.browserViewProviding(None, TestTraverser, IBrowserPublisher)

        ob = O1()

        ## the following is for running the tests standalone
        principalRegistry.defineDefaultPrincipal('tim', 'timbot',
                                                 'ai at its best')

        # now place our object inside the application

        connection = self.db.open()
        app = connection.root()['Application']
        app.somepath = ob
        transaction.commit()
        connection.close()

        defineChecker(app.__class__, NamesChecker(somepath='xxx'))

        req = self._createRequest(url, pub)
        response = req.response

        publish(req, handle_errors=0)

        self.assertEqual(response.getBase(), expected)
Esempio n. 3
0
    def testNoViewOnClassicClassException(self):
        from zope.interface import Interface
        from types import ClassType

        class ClassicError:
            __metaclass__ = ClassType

        class IClassicError(Interface):
            pass

        classImplements(ClassicError, IClassicError)
        ztapi.setDefaultViewName(IClassicError, 'name', self.presentation_type)
        view_text = 'You made a classic error ;-)'
        ztapi.provideView(IClassicError, self.presentation_type, Interface,
                          'name', lambda obj, request: lambda: view_text)
        try:
            raise ClassicError
        except:
            pass
        self.publication.handleException(self.object,
                                         self.request,
                                         sys.exc_info(),
                                         retry_allowed=False)
        # check we don't get the view we registered
        self.failIf(''.join(self.request.response._result) == view_text)
        # check we do actually get something
        self.failIf(''.join(self.request.response._result) == '')
Esempio n. 4
0
    def testViewOnException(self):
        from zope.interface import Interface
        class E1(Exception):
            pass

        ztapi.setDefaultViewName(E1, 'name',
                                 layer=None,
                                 type=self.presentation_type)
        view_text = 'You had a conflict error'
        ztapi.provideView(E1, self.presentation_type, Interface,
                          'name', lambda obj, request: lambda: view_text)
        try:
            raise E1
        except:
            pass
        self.publication.handleException(
            self.object, self.request, sys.exc_info(), retry_allowed=False)
        self.assertEqual(self.request.response._result, view_text)
Esempio n. 5
0
    def testDenyDirectMethodAccess(self):
        pub = self.klass(self.db)
        class I(Interface):
            pass

        class C(object):
            implements(I)

            def foo(self):
                return 'bar'

        class V(object):
            def __init__(self, context, request):
                pass
            implements(IXMLRPCPresentation)

        ob = C()
        r = self._createRequest('/foo', pub)

        ztapi.provideView(I, IXMLRPCPresentation, Interface, 'view', V)
        ztapi.setDefaultViewName(I, 'view', type=IXMLRPCPresentation)
        self.assertRaises(NotFound, pub.traverseName, r, ob, 'foo')
Esempio n. 6
0
 def testNoViewOnClassicClassException(self):
     from zope.interface import Interface
     from types import ClassType
     class ClassicError:
         __metaclass__ = ClassType
     class IClassicError(Interface):
         pass
     classImplements(ClassicError, IClassicError)
     ztapi.setDefaultViewName(IClassicError, 'name', self.presentation_type)
     view_text = 'You made a classic error ;-)'
     ztapi.provideView(IClassicError, self.presentation_type, Interface,
                       'name', lambda obj,request: lambda: view_text)
     try:
         raise ClassicError
     except:
         pass
     self.publication.handleException(
         self.object, self.request, sys.exc_info(), retry_allowed=False)
     # check we don't get the view we registered
     self.failIf(''.join(self.request.response._result) == view_text)
     # check we do actually get something
     self.failIf(''.join(self.request.response._result) == '')
Esempio n. 7
0
    def testViewOnException(self):
        from zope.interface import Interface

        class E1(Exception):
            pass

        ztapi.setDefaultViewName(E1,
                                 'name',
                                 layer=None,
                                 type=self.presentation_type)
        view_text = 'You had a conflict error'
        ztapi.provideView(E1, self.presentation_type, Interface, 'name',
                          lambda obj, request: lambda: view_text)
        try:
            raise E1
        except:
            pass
        self.publication.handleException(self.object,
                                         self.request,
                                         sys.exc_info(),
                                         retry_allowed=False)
        self.assertEqual(self.request.response._result, view_text)
Esempio n. 8
0
    def testDenyDirectMethodAccess(self):
        pub = self.klass(self.db)

        class I(Interface):
            pass

        class C(object):
            implements(I)

            def foo(self):
                return 'bar'

        class V(object):
            def __init__(self, context, request):
                pass

            implements(IXMLRPCPresentation)

        ob = C()
        r = self._createRequest('/foo', pub)

        ztapi.provideView(I, IXMLRPCPresentation, Interface, 'view', V)
        ztapi.setDefaultViewName(I, 'view', type=IXMLRPCPresentation)
        self.assertRaises(NotFound, pub.traverseName, r, ob, 'foo')
    def testDenyDirectMethodAccess(self):
        pub = self.klass(self.db)

        class I(Interface):
            pass

        class C(object):
            implements(I)

            def foo(self):
                return "bar"

        class V(object):
            def __init__(self, context, request):
                pass

            implements(ISOAPPresentation)

        ob = C()
        r = self._createRequest("/foo", pub)

        ztapi.provideView(I, ISOAPPresentation, Interface, "view", V)
        ztapi.setDefaultViewName(I, "view", type=ISOAPPresentation)
        self.assertRaises(NotFound, pub.traverseName, r, ob, "foo")
Esempio n. 10
0
    def testHandlingSystemErrors(self):

        # Generally, when there is a view for an excepton, we assume
        # it is a user error, not a system error and we don't log it.

        from zope.testing import loggingsupport
        handler = loggingsupport.InstalledHandler('SiteError')

        self.testViewOnException()

        self.assertEqual(
            str(handler),
            'SiteError ERROR\n'
            '  Error while reporting an error to the Error Reporting utility')

        # Here we got a single log record, because we havdn't
        # installed an error reporting utility.  That's OK.

        handler.uninstall()
        handler = loggingsupport.InstalledHandler('SiteError')

        # Now, we'll register an exception view that indicates that we
        # have a system error.

        from zope.interface import Interface, implements
        class E2(Exception):
            pass

        ztapi.setDefaultViewName(E2, 'name',
                                 layer=self.presentation_type,
                                 type=self.presentation_type)
        view_text = 'You had a conflict error'

        from zope.app.exception.interfaces import ISystemErrorView
        class MyView:
            implements(ISystemErrorView)
            def __init__(self, context, request):
                pass

            def isSystemError(self):
                return True

            def __call__(self):
                return view_text

        ztapi.provideView(E2, self.presentation_type, Interface,
                          'name', MyView)
        try:
            raise E2
        except:
            self.publication.handleException(
                self.object, self.request, sys.exc_info(), retry_allowed=False)

        # Now, since the view was a system error view, we should have
        # a log entry for the E2 error (as well as the missing
        # error reporting utility).
        self.assertEqual(
            str(handler),
            'SiteError ERROR\n'
            '  Error while reporting an error to the Error Reporting utility\n'
            'SiteError ERROR\n'
            '  http://test.url'
            )

        handler.uninstall()
Esempio n. 11
0
    def testHandlingSystemErrors(self):

        # Generally, when there is a view for an excepton, we assume
        # it is a user error, not a system error and we don't log it.

        from zope.testing import loggingsupport
        handler = loggingsupport.InstalledHandler('SiteError')

        self.testViewOnException()

        self.assertEqual(
            str(handler), 'SiteError ERROR\n'
            '  Error while reporting an error to the Error Reporting utility')

        # Here we got a single log record, because we haven't
        # installed an error reporting utility.  That's OK.

        handler.uninstall()
        handler = loggingsupport.InstalledHandler('SiteError')

        # Now, we'll register an exception view that indicates that we
        # have a system error.

        from zope.interface import Interface, implements

        class E2(Exception):
            pass

        ztapi.setDefaultViewName(E2,
                                 'name',
                                 layer=self.presentation_type,
                                 type=self.presentation_type)
        view_text = 'You had a conflict error'

        from zope.app.exception.interfaces import ISystemErrorView

        class MyView:
            implements(ISystemErrorView)

            def __init__(self, context, request):
                pass

            def isSystemError(self):
                return True

            def __call__(self):
                return view_text

        ztapi.provideView(E2, self.presentation_type, Interface, 'name',
                          MyView)
        try:
            raise E2
        except:
            self.publication.handleException(self.object,
                                             self.request,
                                             sys.exc_info(),
                                             retry_allowed=False)

        # Now, since the view was a system error view, we should have
        # a log entry for the E2 error (as well as the missing
        # error reporting utility).
        self.assertEqual(
            str(handler), 'SiteError ERROR\n'
            '  Error while reporting an error to the Error Reporting utility\n'
            'SiteError ERROR\n'
            '  http://test.url')

        handler.uninstall()