/usr/lib/python2.7/dist-packages/zope/browserresource/tests/test_directory.py is in python-zope.browserresource 3.12.0-0ubuntu2.
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 174 | ##############################################################################
#
# 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.
#
##############################################################################
"""Directory-based resources test
"""
import os
import tempfile
import shutil
from unittest import TestCase, main, makeSuite
from zope.publisher.interfaces import NotFound
from zope.proxy import isProxy
from zope.publisher.browser import TestRequest
from zope.security import proxy
from zope.security.checker import NamesChecker, ProxyFactory
from zope.interface import implements
from zope.location.interfaces import IContained
from zope.traversing.browser.absoluteurl import AbsoluteURL
from zope.traversing.browser.interfaces import IAbsoluteURL
from zope.component import provideAdapter, provideUtility
from zope.testing import cleanup
from zope.browserresource.directory import \
DirectoryResourceFactory, DirectoryResource
from zope.browserresource.file import FileResource
import zope.browserresource.tests as p
from zope.browserresource.tests import support
test_directory = os.path.dirname(p.__file__)
checker = NamesChecker(
('get', '__getitem__', 'request', 'publishTraverse')
)
class Ob(object):
implements(IContained)
__parent__ = __name__ = None
ob = Ob()
class Test(support.SiteHandler, cleanup.CleanUp, TestCase):
def setUp(self):
super(Test, self).setUp()
provideAdapter(AbsoluteURL, (None, None), IAbsoluteURL)
def testNotFound(self):
path = os.path.join(test_directory, 'testfiles')
request = TestRequest()
factory = DirectoryResourceFactory(path, checker, 'testfiles')
resource = factory(request)
self.assertRaises(NotFound, resource.publishTraverse,
resource.request, 'doesnotexist')
self.assertRaises(NotFound, resource.get, 'doesnotexist')
def testBrowserDefault(self):
path = os.path.join(test_directory, 'testfiles')
request = TestRequest()
factory = DirectoryResourceFactory(path, checker, 'testfiles')
resource = factory(request)
view, next = resource.browserDefault(request)
self.assertEquals(view(), '')
self.assertEquals(next, ())
def testGetitem(self):
path = os.path.join(test_directory, 'testfiles')
request = TestRequest()
factory = DirectoryResourceFactory(path, checker, 'testfiles')
resource = factory(request)
self.assertRaises(KeyError, resource.__getitem__, 'doesnotexist')
file = resource['test.txt']
def testForbiddenNames(self):
request = TestRequest()
old_forbidden_names = DirectoryResource.forbidden_names
path = tempfile.mkdtemp()
try:
os.mkdir(os.path.join(path, '.svn'))
open(os.path.join(path, 'test.txt'), 'w').write('')
factory = DirectoryResourceFactory(path, checker, 'testfiles')
resource = factory(request)
self.assertEquals(resource.get('.svn', None), None)
self.assertNotEquals(resource.get('test.txt', None), None)
DirectoryResource.forbidden_names = ('*.txt', )
self.assertEquals(resource.get('test.txt', None), None)
self.assertNotEquals(resource.get('.svn', None), None)
finally:
shutil.rmtree(path)
DirectoryResource.forbidden_names = old_forbidden_names
def testProxy(self):
path = os.path.join(test_directory, 'testfiles')
request = TestRequest()
factory = DirectoryResourceFactory(path, checker, 'testfiles')
resource = factory(request)
file = ProxyFactory(resource['test.txt'])
self.assert_(isProxy(file))
def testURL(self):
request = TestRequest()
request._vh_root = support.site
path = os.path.join(test_directory, 'testfiles')
files = DirectoryResourceFactory(path, checker, 'test_files')(request)
files.__parent__ = support.site
file = files['test.gif']
self.assertEquals(file(), 'http://127.0.0.1/@@/test_files/test.gif')
def testURL2Level(self):
request = TestRequest()
request._vh_root = support.site
ob.__parent__ = support.site
ob.__name__ = 'ob'
path = os.path.join(test_directory, 'testfiles')
files = DirectoryResourceFactory(path, checker, 'test_files')(request)
files.__parent__ = ob
file = files['test.gif']
self.assertEquals(file(), 'http://127.0.0.1/@@/test_files/test.gif')
def testURL3Level(self):
request = TestRequest()
request._vh_root = support.site
ob.__parent__ = support.site
ob.__name__ = 'ob'
path = os.path.join(test_directory, 'testfiles')
files = DirectoryResourceFactory(path, checker, 'test_files')(request)
files.__parent__ = ob
file = files['test.gif']
self.assertEquals(file(), 'http://127.0.0.1/@@/test_files/test.gif')
subdir = files['subdir']
self.assert_(proxy.isinstance(subdir, DirectoryResource))
file = subdir['test.gif']
self.assertEquals(file(),
'http://127.0.0.1/@@/test_files/subdir/test.gif')
def testPluggableFactories(self):
path = os.path.join(test_directory, 'testfiles')
request = TestRequest()
resource = DirectoryResourceFactory(path, checker, 'files')(request)
class ImageResource(object):
def __init__(self, image, request):
pass
class ImageResourceFactory(object):
def __init__(self, path, checker, name):
pass
def __call__(self, request):
return ImageResource(None, request)
from zope.browserresource.interfaces import IResourceFactoryFactory
provideUtility(ImageResourceFactory, IResourceFactoryFactory, 'gif')
image = resource['test.gif']
self.assert_(proxy.isinstance(image, ImageResource))
file = resource['test.txt']
self.assert_(proxy.isinstance(file, FileResource))
def test_suite():
return makeSuite(Test)
|