[Zope-CVS] CVS: Packages/Moztop/moztopsupport/dav/tests - test_propfind.py:1.2

Sidnei da Silva sidnei@x3ng.com.br
Mon, 24 Mar 2003 10:38:23 -0500


Update of /cvs-repository/Packages/Moztop/moztopsupport/dav/tests
In directory cvs.zope.org:/tmp/cvs-serv25843/dav/tests

Modified Files:
	test_propfind.py 
Log Message:
First steps on implementing dav source support. Its rather messy, but i will clean it up later today

=== Packages/Moztop/moztopsupport/dav/tests/test_propfind.py 1.1 => 1.2 ===
--- Packages/Moztop/moztopsupport/dav/tests/test_propfind.py:1.1	Mon Mar 24 06:26:31 2003
+++ Packages/Moztop/moztopsupport/dav/tests/test_propfind.py	Mon Mar 24 10:38:23 2003
@@ -19,11 +19,20 @@
 from unittest import TestCase, TestSuite, main, makeSuite
 from StringIO import StringIO
 from zope.interface import Interface
+from zope.component import getService
+from zope.app.services.servicenames import Adapters, Views
+from zope.app.traversing import traverse
 from zope.publisher.browser import TestRequest
 from zope.app.interfaces.file import IWriteFile
+from zope.app.interfaces.content.zpt import IZPTPage
 from zope.app.tests.placelesssetup import PlacelessSetup
+from zope.app.services.tests.placefulsetup import PlacefulSetup
+from zope.publisher.interfaces.browser import IBrowserPresentation
+from zope.app.browser.absoluteurl import AbsoluteURL
 
 from moztopsupport.dav import propfind 
+from moztopsupport.dav.views import ZPTDAVSource
+from moztopsupport.dav.interfaces import IDAVSource
 
 class File:
 
@@ -37,37 +46,61 @@
     def write(self, data):
         self.data = data
 
-def setHeader(request, header, value):
-    header = header.upper()
-    request._environ.update({header:value})
+class FooZPT:
 
-class TestPROPFIND(PlacelessSetup, TestCase):
+    __implements__ = IZPTPage
 
-    def setUp(self):
-        PlacelessSetup.setUp(self)
-        self.body = '''\
-        <?xml version="1.0" encoding="utf-8" ?>
-          <D:propfind xmlns:D="DAV:">
-            <D:prop xmlns:R="http://www.foo.bar/boxschema/">
-              <R:bigbox/>
-              <R:author/>
-              <R:DingALing/>
-              <R:Random/>
-            </D:prop>
-         </D:propfind>
+    def getSource(self):
+        return 'bla bla bla'
+
+
+def _createRequest(body=None, headers=None):
+    if body is None:
+        body = '''<?xml version="1.0" encoding="utf-8" ?>
+        
+        <D:propfind xmlns:D="DAV:">
+        <D:prop xmlns:R="http://www.foo.bar/boxschema/">
+        <R:bigbox/>
+        <R:author/>
+        <R:DingALing/>
+        <R:Random/>
+        </D:prop>
+        </D:propfind>
         '''
-        self.content = "some content\n for testing"
-        self.file = File('spam', 'text/plain', self.content)
-        self.request = TestRequest(StringIO(self.body), StringIO(),
-                                   {'CONTENT_TYPE': 'text/xml',
-                                    'CONTENT_LENGTH': str(len(self.body)),
-                                    })
 
+    _environ = {'CONTENT_TYPE': 'text/xml',
+                'CONTENT_LENGTH': str(len(body))}
+
+    if headers is not None:
+        for key, value in headers.items():
+            _environ[key.upper()] = value
+            
+    request = TestRequest(StringIO(body), StringIO(), _environ)
+    request.setViewType(IBrowserPresentation)
+    return request
+
+class TestPlacefulPROPFIND(PlacefulSetup, TestCase):
+
+    def setUp(self):
+        PlacefulSetup.setUp(self)
+        PlacefulSetup.buildFolders(self)
+        root = self.rootFolder
+        zpt = FooZPT()
+        self.content = "some content\n for testing"
+        file = File('spam', 'text/plain', self.content)
+        root.setObject('file', file)
+        root.setObject('zpt', zpt)
+        self.zpt = traverse(root, 'zpt')
+        self.file = traverse(root, 'file')
+        provideView=getService(None, Views).provideView
+        provideView(IZPTPage, 'DAVsource', IBrowserPresentation,
+                    [ZPTDAVSource])
+        provideView(None, 'absolute_url', IBrowserPresentation,
+                    [AbsoluteURL])
+    
     def test_contenttype1(self):
         file = self.file
-        request = self.request
-        setHeader(request, 'Content-type', 'text/xml')
-
+        request = _createRequest(headers={'Content-type':'text/xml'})
         pfind = propfind.PROPFIND(file, request)
         pfind.PROPFIND()
         # Check HTTP Response
@@ -75,8 +108,7 @@
 
     def test_contenttype2(self):
         file = self.file
-        request = self.request
-        setHeader(request, 'Content-type', 'application/xml')
+        request = _createRequest(headers={'Content-type':'application/xml'})
 
         pfind = propfind.PROPFIND(file, request)
         pfind.PROPFIND()
@@ -85,8 +117,7 @@
 
     def test_bad_contenttype(self):
         file = self.file
-        request = self.request
-        setHeader(request, 'Content-type', 'text/foo')
+        request = _createRequest(headers={'Content-type':'text/foo'})
 
         pfind = propfind.PROPFIND(file, request)
         pfind.PROPFIND()
@@ -95,8 +126,7 @@
 
     def test_nodepth(self):
         file = self.file
-        request = self.request
-        setHeader(request, 'Content-type', 'text/xml')
+        request = _createRequest(headers={'Content-type':'text/xml'})
 
         pfind = propfind.PROPFIND(file, request)
         pfind.PROPFIND()
@@ -106,9 +136,8 @@
 
     def test_depth0(self):
         file = self.file
-        request = self.request
-        setHeader(request, 'Content-type', 'text/xml')
-        setHeader(request, 'Depth', '0')
+        request = _createRequest(headers={'Content-type':'text/xml',
+                                               'Depth':'0'})
 
         pfind = propfind.PROPFIND(file, request)
         pfind.PROPFIND()
@@ -118,9 +147,8 @@
 
     def test_depth1(self):
         file = self.file
-        request = self.request
-        setHeader(request, 'Content-type', 'text/xml')
-        setHeader(request, 'Depth', '1')
+        request = _createRequest(headers={'Content-type':'text/xml',
+                                               'Depth':'1'})
 
         pfind = propfind.PROPFIND(file, request)
         pfind.PROPFIND()
@@ -130,9 +158,8 @@
 
     def test_depthinf(self):
         file = self.file
-        request = self.request
-        setHeader(request, 'Content-type', 'text/xml')
-        setHeader(request, 'Depth', 'infinity')
+        request = _createRequest(headers={'Content-type':'text/xml',
+                                               'Depth':'infinity'})
 
         pfind = propfind.PROPFIND(file, request)
         pfind.PROPFIND()
@@ -142,9 +169,8 @@
 
     def test_depthinvalid(self):
         file = self.file
-        request = self.request
-        setHeader(request, 'Content-type', 'text/xml')
-        setHeader(request, 'Depth', 'full')
+        request = _createRequest(headers={'Content-type':'text/xml',
+                                               'Depth':'full'})
 
         pfind = propfind.PROPFIND(file, request)
         pfind.PROPFIND()
@@ -152,9 +178,51 @@
         self.assertEqual(request.response.getStatus(), 400)
         self.assertEqual(pfind.getDepth(), 'infinity')
 
+    def test_zptdavsrc(self):
+        root = self.rootFolder
+        zpt = traverse(root, 'zpt')
+        body = '''<?xml version="1.0" encoding="utf-8" ?>
+          <D:propfind xmlns:D="DAV:">
+            <D:prop>
+              <D:source/>
+            </D:prop>
+          </D:propfind>
+        '''
+        expect = '''<?xml version="1.0" encoding="utf-8" ?>
+        <D:multistatus xmlns:D="DAV:">
+        <D:response>
+        <D:href>http://localhost:9080/zpt/</D:href>
+        <D:propstat>
+        <D:prop>
+        <D:source>
+        <D:link>
+        <D:src>http://localhost:9080/zpt</D:src>
+        <D:dst>http://localhost:9080/zpt/getSource</D:dst>
+        </D:link>
+        </D:source>
+        </D:prop>
+        <D:status>HTTP/1.1 200 OK</D:status>
+        </D:propstat>
+        </D:response>
+        </D:multistatus>
+        '''
+        
+        request = _createRequest(body=body,
+                                 headers={'Content-type':'text/xml',
+                                          'Depth':'0'})
+
+        pfind = propfind.PROPFIND(zpt, request)
+
+        pfind.PROPFIND()
+        # Check HTTP Response
+        self.assertEqual(request.response.getStatus(), 207)
+        self.assertEqual(pfind.getDepth(), '0')
+        print request.response._body
+        self.assertEqual(request.response._body, expect)
+
 def test_suite():
     return TestSuite((
-        makeSuite(TestPROPFIND),
+        makeSuite(TestPlacefulPROPFIND),
         ))
 
 if __name__ == '__main__':