/usr/share/pyshared/zope/publisher/tests/test_publisher.py is in python-zope.publisher 3.12.6-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | ##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Test Publisher
"""
import unittest
from zope import component
from zope.publisher.publish import publish, DoNotReRaiseException
from zope.publisher.base import TestRequest
from zope.publisher.base import DefaultPublication
from zope.publisher.interfaces import Unauthorized, NotFound, DebugError
from zope.publisher.interfaces import IPublication, IReRaiseException, \
Retry
from zope.interface.verify import verifyClass
from zope.interface import implementedBy
from StringIO import StringIO
class ErrorToRetry(Exception):
"""A sample exception that should be retried."""
class PublisherTests(unittest.TestCase):
def setUp(self):
class AppRoot(object):
"""Required docstring for the publisher."""
class Folder(object):
"""Required docstring for the publisher."""
class Item(object):
"""Required docstring for the publisher."""
def __call__(self):
return "item"
class NoDocstringItem:
def __call__(self):
return "Yo! No docstring!"
class RetryItem:
"""An item that the publication will attempt to retry."""
def __call__(self):
raise ErrorToRetry()
self.app = AppRoot()
self.app.folder = Folder()
self.app.folder.item = Item()
self.app._item = Item()
self.app.noDocString = NoDocstringItem()
self.app.retryItem = RetryItem()
def _createRequest(self, path, **kw):
publication = DefaultPublication(self.app)
path = path.split('/')
path.reverse()
request = TestRequest(StringIO(''), **kw)
request.setTraversalStack(path)
request.setPublication(publication)
return request
def _publisherResults(self, path, **kw):
request = self._createRequest(path, **kw)
response = request.response
publish(request, handle_errors=False)
return response._result
def _registerExcAdapter(self, factory):
component.provideAdapter(factory, (Unauthorized,), IReRaiseException)
def _unregisterExcAdapter(self, factory):
gsm = component.getGlobalSiteManager()
gsm.unregisterAdapter(
factory=factory, required=(Unauthorized,),
provided=IReRaiseException)
def testImplementsIPublication(self):
self.failUnless(IPublication.providedBy(
DefaultPublication(self.app)))
def testInterfacesVerify(self):
for interface in implementedBy(DefaultPublication):
verifyClass(interface, DefaultPublication)
def testTraversalToItem(self):
res = self._publisherResults('/folder/item')
self.failUnlessEqual(res, 'item')
res = self._publisherResults('/folder/item/')
self.failUnlessEqual(res, 'item')
res = self._publisherResults('folder/item')
self.failUnlessEqual(res, 'item')
def testUnderscoreUnauthorizedException(self):
self.assertRaises(Unauthorized, self._publisherResults, '/_item')
def testNotFoundException(self):
self.assertRaises(NotFound, self._publisherResults, '/foo')
def testDebugError(self):
self.assertRaises(DebugError, self._publisherResults, '/noDocString')
def testIReRaiseExceptionAdapters(self):
self._registerExcAdapter(DoNotReRaiseException)
try:
self._publisherResults('/_item')
except Unauthorized:
self._unregisterExcAdapter(DoNotReRaiseException)
self.fail('Unauthorized raised though this should '
'not happen')
self._unregisterExcAdapter(DoNotReRaiseException)
def doReRaiseAdapter(context):
def shouldBeReRaised():
return True
return shouldBeReRaised
self._registerExcAdapter(doReRaiseAdapter)
raised = True
try:
self._publisherResults('/_item')
raised = False
except:
pass
self._unregisterExcAdapter(doReRaiseAdapter)
self.failUnlessEqual(raised, True)
def testRetryErrorIsUnwrapped(self):
test = self
class RetryPublication(DefaultPublication):
def handleException(self, object, request, exc_info,
retry_allowed=True):
test.assertTrue(issubclass(exc_info[0], ErrorToRetry))
raise Retry(exc_info)
request = self._createRequest('/retryItem')
request.setPublication(RetryPublication(self.app))
self.assertFalse(request.supportsRetry())
self.assertRaises(ErrorToRetry, publish, request, handle_errors=False)
def testBareRetryErrorPassedThrough(self):
test = self
class RetryPublication(DefaultPublication):
def handleException(self, object, request, exc_info,
retry_allowed=True):
test.assertTrue(issubclass(exc_info[0], ErrorToRetry))
raise Retry()
request = self._createRequest('/retryItem')
request.setPublication(RetryPublication(self.app))
self.assertFalse(request.supportsRetry())
# Retry exception is passed through because it doesn't contain
# an original exception.
self.assertRaises(Retry, publish, request, handle_errors=False)
def test_suite():
loader = unittest.TestLoader()
return loader.loadTestsFromTestCase(PublisherTests)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
|