[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/ZopePublication/tests - testZopePublication.py:1.1.2.8
Jim Fulton
jim@zope.com
Sun, 3 Feb 2002 18:55:34 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/App/ZopePublication/tests
In directory cvs.zope.org:/tmp/cvs-serv17190
Modified Files:
Tag: Zope-3x-branch
testZopePublication.py
Log Message:
Kapil Thangavelu
Added test for correct base tag handling, especially wrt new
browser_default semantics. Also stubs a bit.
=== Zope3/lib/python/Zope/App/ZopePublication/tests/testZopePublication.py 1.1.2.7 => 1.1.2.8 ===
from Zope.App.Security.SecurityManager import setSecurityPolicy
from Zope.App.Security import SimpleSecurityPolicies
+from Zope.App.ZopePublication.Traversers import DefaultTraverser
+from Interface import verify, instancesOfObjectImplements, Interface
+
+from Zope.ComponentArchitecture import provideView, setDefaultViewName, _clear
+
+
+from Zope.App.Security.PrincipalRegistry import principalRegistry
+from Zope.App.Security.PrincipalRoleManager import principalRoleManager
-from Interface import verify, instancesOfObjectImplements
from StringIO import StringIO
+def foo():
+ return '<html><body>hello base fans</body></html>'
+
class DummyPublished:
+
__implements__ = IBrowserPublisher
+
def browser_traverse(self, request, name):
if name == 'bruce':
- return self.__class__()
+ return foo
if name == 'edit;view':
return 'bob'
raise KeyError, name
@@ -41,15 +53,47 @@
def browser_default(self, request):
return self, ['bruce']
+class DummyView(DummyPublished):
+
+ __implements__ = IBrowserPublisher
+
+ def __init__(self, context):
+ self._context = context
+
+ def getContext(self):
+ return self._context
+
+
class BasePublicationTests(unittest.TestCase):
+ klass = ZopePublication
+
def setUp(self):
self.policy = setSecurityPolicy(
SimpleSecurityPolicies.PermissiveSecurityPolicy()
)
self.db = ZODB.DB(MappingStorage('foo'))
+ connection = self.db.open()
+ root = connection.root()
+ app = getattr(root, ZopePublication.root_name, None)
+
+ if app is None:
+ from Zope.App.OFS.Folder.RootFolder import RootFolder
+ from Transaction import get_transaction
+
+ app = RootFolder()
+ root[ZopePublication.root_name] = app
+
+ get_transaction().commit()
+
+ connection.close()
+ principalRegistry._clear()
+ principalRoleManager._clear()
+
def tearDown(self):
setSecurityPolicy(self.policy)
+ principalRegistry._clear()
+ principalRoleManager._clear()
def testInterfacesVerify(self):
for interface in instancesOfObjectImplements(self.klass):
@@ -67,6 +111,77 @@
class ZopePublicationTests(BasePublicationTests):
klass = ZopePublication
+class BrowserDefaultTests(BasePublicationTests):
+ """
+ test browser default
+
+ many views lead to a default view
+ <base href="/somepath/view;view/view_method">
+
+ """
+ klass = BrowserPublication
+
+ _expected = "/somepath/view;view/bruce"
+
+ _test_urls = [
+ '/somepath/view;view/',
+ '/somepath/view;view',
+ '/somepath/',
+ '/somepath'
+ ]
+
+ def testBaseTags(self):
+
+ class I1(Interface): pass
+
+ from Persistence import Persistent
+
+ class O1(Persistent):
+ __implements__ = I1
+
+
+ pub = BrowserPublication(self.db)
+
+ provideView(I1, 'view', IBrowserPublisher, DummyView)
+ #provideView(I1, '', IBrowserPublisher, DummyView)
+ setDefaultViewName(I1, IBrowserPublisher, 'view')
+ provideView(None, '_traverse', IBrowserPublisher, DefaultTraverser)
+
+ ob = O1()
+
+ ## the following is for running the tests standalone
+ principalRegistry.defineDefaultPrincipal('tim',
+ 'timbot',
+ 'ai at its best')
+
+ principalRoleManager.assignRoleToPrincipal('Manager', 'tim')
+
+
+ # now place our object inside the application
+ from Transaction import get_transaction
+
+ connection = self.db.open()
+ app = connection.root()['Application']
+ app.somepath = ob
+ get_transaction().commit()
+ connection.close()
+
+ for url in self._test_urls:
+ req = self._createRequest(url, pub)
+ publish(req)
+
+ self.assertEqual(req.response.getBase(),
+ self._expected)
+
+ def _createRequest(self, path, publication):
+ outstream = StringIO()
+ response = BaseResponse(outstream)
+ instream = StringIO("")
+ request = BaseRequest(response, instream, publication)
+ request.setViewType( IBrowserPublisher )
+ request.other['PATH_INFO'] = path
+ return request
+
class BrowserPublicationTests(BasePublicationTests):
klass = BrowserPublication
@@ -153,7 +268,8 @@
def test_suite():
t1 = unittest.makeSuite(ZopePublicationTests, 'test')
t2 = unittest.makeSuite(BrowserPublicationTests, 'test')
- return unittest.TestSuite((t1, t2))
+ t3 = unittest.makeSuite(BrowserDefaultTests, 'test')
+ return unittest.TestSuite((t1,t2,t3))
if __name__=='__main__':
unittest.TextTestRunner().run( test_suite() )