Commit f4a41880 authored by Jérome Perrin's avatar Jérome Perrin

WebDAVSupport: py3

parent 3cd556af
...@@ -35,7 +35,7 @@ from Products.ERP5Type.tests.utils import FileUpload ...@@ -35,7 +35,7 @@ from Products.ERP5Type.tests.utils import FileUpload
from unittest import expectedFailure from unittest import expectedFailure
import six.moves.http_client import six.moves.http_client
from six.moves import cStringIO as StringIO from io import BytesIO
from DateTime import DateTime from DateTime import DateTime
from lxml import etree from lxml import etree
...@@ -46,11 +46,6 @@ def makeFilePath(name): ...@@ -46,11 +46,6 @@ def makeFilePath(name):
return os.path.join(os.path.dirname(Products.ERP5.tests.__file__), return os.path.join(os.path.dirname(Products.ERP5.tests.__file__),
'test_data', name) 'test_data', name)
def makeFileUpload(name, as_name=None):
if as_name is None:
as_name = name
path = makeFilePath(name)
return FileUpload(path, as_name)
class TestWebDavSupport(ERP5TypeTestCase): class TestWebDavSupport(ERP5TypeTestCase):
"""Test for WEBDAV access. """Test for WEBDAV access.
...@@ -58,6 +53,14 @@ class TestWebDavSupport(ERP5TypeTestCase): ...@@ -58,6 +53,14 @@ class TestWebDavSupport(ERP5TypeTestCase):
authentication = 'ERP5TypeTestCase:' authentication = 'ERP5TypeTestCase:'
def makeFileUpload(self, name, as_name=None):
if as_name is None:
as_name = name
path = makeFilePath(name)
fu = FileUpload(path, as_name)
self.addCleanup(fu.close)
return fu
def getTitle(self): def getTitle(self):
return "Test WebDav Support" return "Test WebDav Support"
...@@ -94,11 +97,13 @@ class TestWebDavSupport(ERP5TypeTestCase): ...@@ -94,11 +97,13 @@ class TestWebDavSupport(ERP5TypeTestCase):
""" """
person = self.portal.person_module.newContent() person = self.portal.person_module.newContent()
self.tic() self.tic()
file_object = makeFileUpload('images/erp5_logo.png') file_object = self.makeFileUpload('images/erp5_logo.png')
response = self.publish(person.getPath() + '/erp5_logo.png', response = self.publish(
request_method='PUT', person.getPath() + '/erp5_logo.png',
stdin=file_object, request_method='PUT',
basic=self.authentication) stdin=file_object,
env={"CONTENT_TYPE": 'image/png'},
basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.CREATED) self.assertEqual(response.getStatus(), six.moves.http_client.CREATED)
image = person['erp5_logo.png'] image = person['erp5_logo.png']
self.assertEqual(image.getPortalType(), 'Embedded File') self.assertEqual(image.getPortalType(), 'Embedded File')
...@@ -113,11 +118,13 @@ class TestWebDavSupport(ERP5TypeTestCase): ...@@ -113,11 +118,13 @@ class TestWebDavSupport(ERP5TypeTestCase):
# Create a new document via FTP/DAV # Create a new document via FTP/DAV
path = self.portal.portal_contributions.getPath() path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp' filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename) file_object = self.makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename), response = self.publish(
request_method='PUT', '%s/%s' % (path, filename),
stdin=file_object, request_method='PUT',
basic=self.authentication) stdin=file_object,
env={"CONTENT_TYPE": 'application/vnd.oasis.opendocument.presentation'},
basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.CREATED) self.assertEqual(response.getStatus(), six.moves.http_client.CREATED)
document_module = self.getDocumentModule() document_module = self.getDocumentModule()
...@@ -135,11 +142,13 @@ class TestWebDavSupport(ERP5TypeTestCase): ...@@ -135,11 +142,13 @@ class TestWebDavSupport(ERP5TypeTestCase):
# Create a new document via FTP/DAV # Create a new document via FTP/DAV
path = self.portal.portal_contributions.getPath() path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp' filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename) file_object = self.makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename), response = self.publish(
request_method='PUT', '%s/%s' % (path, filename),
stdin=file_object, request_method='PUT',
basic=self.authentication) stdin=file_object,
env={"CONTENT_TYPE": 'application/vnd.oasis.opendocument.presentation'},
basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.CREATED) self.assertEqual(response.getStatus(), six.moves.http_client.CREATED)
self.tic() self.tic()
...@@ -152,7 +161,7 @@ class TestWebDavSupport(ERP5TypeTestCase): ...@@ -152,7 +161,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
# force usage of manage_FTPget like zwebdav_server does # force usage of manage_FTPget like zwebdav_server does
response = self.publish('%s/%s/manage_FTPget' % (path, document_id), response = self.publish('%s/%s/manage_FTPget' % (path, document_id),
request_method='GET', request_method='GET',
stdin=StringIO(), stdin=BytesIO(),
basic=self.authentication) basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.OK) self.assertEqual(response.getStatus(), six.moves.http_client.OK)
self.assertEqual(response.getBody(), document.getData(), self.assertEqual(response.getBody(), document.getData(),
...@@ -175,7 +184,7 @@ class TestWebDavSupport(ERP5TypeTestCase): ...@@ -175,7 +184,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
self.assertEqual(web_page_module[filename].getPortalType(), 'Web Page') self.assertEqual(web_page_module[filename].getPortalType(), 'Web Page')
# Edit a new document via FTP/DAV # Edit a new document via FTP/DAV
text_content= """<html> text_content = u"""<html>
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head> </head>
...@@ -185,19 +194,20 @@ class TestWebDavSupport(ERP5TypeTestCase): ...@@ -185,19 +194,20 @@ class TestWebDavSupport(ERP5TypeTestCase):
</body> </body>
</html> </html>
""" """
iso_text_content = text_content.decode('utf-8').encode('iso-8859-1') iso_text_content = text_content.encode('iso-8859-1')
path = web_page_module.getPath() path = web_page_module.getPath()
for _ in range(2): # Run twice to check the code that compares # Run twice to check the code that compares old & new data
# old & new data when setting file attribute. # when setting file attribute.
for _ in range(2):
response = self.publish('%s/%s' % (path, filename), response = self.publish('%s/%s' % (path, filename),
request_method='PUT', request_method='PUT',
stdin=StringIO(iso_text_content), stdin=BytesIO(iso_text_content),
basic=self.authentication) basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.NO_CONTENT) self.assertEqual(response.getStatus(), six.moves.http_client.NO_CONTENT)
self.assertEqual(web_page_module[filename].getData(), iso_text_content) self.assertEqual(web_page_module[filename].getData(), iso_text_content)
# Convert to base format and run conversion into utf-8 # Convert to base format and run conversion into utf-8
self.tic() self.tic()
# Content-Type header is replaced if sonversion encoding succeed # Content-Type header is replaced if conversion encoding succeed
new_text_content = text_content.replace('charset=iso-8859-1', 'charset=utf-8') new_text_content = text_content.replace('charset=iso-8859-1', 'charset=utf-8')
self.assertEqual(web_page_module[filename].getTextContent(), new_text_content) self.assertEqual(web_page_module[filename].getTextContent(), new_text_content)
...@@ -206,11 +216,13 @@ class TestWebDavSupport(ERP5TypeTestCase): ...@@ -206,11 +216,13 @@ class TestWebDavSupport(ERP5TypeTestCase):
""" """
path = self.portal.portal_contributions.getPath() path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp' filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename) file_object = self.makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename), response = self.publish(
request_method='PUT', '%s/%s' % (path, filename),
stdin=file_object, request_method='PUT',
basic=self.authentication) stdin=file_object,
env={"CONTENT_TYPE": 'application/vnd.oasis.opendocument.presentation'},
basic=self.authentication)
# Convert to base format and run conversion into utf-8 # Convert to base format and run conversion into utf-8
self.tic() self.tic()
...@@ -219,9 +231,9 @@ class TestWebDavSupport(ERP5TypeTestCase): ...@@ -219,9 +231,9 @@ class TestWebDavSupport(ERP5TypeTestCase):
# This is HTTPServer.zhttp_server not HTTPServer.zwebdav_server # This is HTTPServer.zhttp_server not HTTPServer.zwebdav_server
# force usage of manage_FTPget like zwebdav_server does # force usage of manage_FTPget like zwebdav_server does
response = self.publish(document.getPath()+'/manage_FTPget', response = self.publish(document.getPath() + '/manage_FTPget',
request_method='GET', request_method='GET',
stdin=StringIO(), stdin=BytesIO(),
basic=self.authentication) basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.OK) self.assertEqual(response.getStatus(), six.moves.http_client.OK)
...@@ -233,11 +245,13 @@ class TestWebDavSupport(ERP5TypeTestCase): ...@@ -233,11 +245,13 @@ class TestWebDavSupport(ERP5TypeTestCase):
""" """
path = self.portal.portal_contributions.getPath() path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp' filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename) file_object = self.makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename), response = self.publish(
request_method='PUT', '%s/%s' % (path, filename),
stdin=file_object, request_method='PUT',
basic=self.authentication) stdin=file_object,
env={"CONTENT_TYPE": 'application/vnd.oasis.opendocument.presentation'},
basic=self.authentication)
document_module = self.getDocumentModule() document_module = self.getDocumentModule()
document = document_module[filename] document = document_module[filename]
...@@ -245,7 +259,7 @@ class TestWebDavSupport(ERP5TypeTestCase): ...@@ -245,7 +259,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
response = self.publish(document.getPath(), response = self.publish(document.getPath(),
request_method='PROPFIND', request_method='PROPFIND',
env={'HTTP_DEPTH': '0'}, env={'HTTP_DEPTH': '0'},
stdin=StringIO(), stdin=BytesIO(),
basic=self.authentication) basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.MULTI_STATUS) self.assertEqual(response.getStatus(), six.moves.http_client.MULTI_STATUS)
...@@ -271,11 +285,13 @@ class TestWebDavSupport(ERP5TypeTestCase): ...@@ -271,11 +285,13 @@ class TestWebDavSupport(ERP5TypeTestCase):
""" """
path = self.portal.portal_contributions.getPath() path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp' filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename) file_object = self.makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename), response = self.publish(
request_method='PUT', '%s/%s' % (path, filename),
stdin=file_object, request_method='PUT',
basic=self.authentication) stdin=file_object,
env={"CONTENT_TYPE": 'application/vnd.oasis.opendocument.presentation'},
basic=self.authentication)
document_module = self.getDocumentModule() document_module = self.getDocumentModule()
document = document_module[filename] document = document_module[filename]
...@@ -283,7 +299,7 @@ class TestWebDavSupport(ERP5TypeTestCase): ...@@ -283,7 +299,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
response = self.publish(document.getPath(), response = self.publish(document.getPath(),
request_method='PROPFIND', request_method='PROPFIND',
env={'HTTP_DEPTH': '0'}, env={'HTTP_DEPTH': '0'},
stdin=StringIO(), stdin=BytesIO(),
basic=self.authentication) basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.MULTI_STATUS) self.assertEqual(response.getStatus(), six.moves.http_client.MULTI_STATUS)
...@@ -293,7 +309,3 @@ class TestWebDavSupport(ERP5TypeTestCase): ...@@ -293,7 +309,3 @@ class TestWebDavSupport(ERP5TypeTestCase):
'{DAV:}response/{DAV:}propstat/{DAV:}prop/{DAV:}creationdate').text, '{DAV:}response/{DAV:}propstat/{DAV:}prop/{DAV:}creationdate').text,
document.getCreationDate().HTML4()) document.getCreationDate().HTML4())
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestWebDavSupport))
return suite
...@@ -23,7 +23,7 @@ from Products.ERP5Type.Globals import InitializeClass ...@@ -23,7 +23,7 @@ from Products.ERP5Type.Globals import InitializeClass
from Products.ERP5Type import Permissions from Products.ERP5Type import Permissions
from Products.CMFCore.PortalContent import ResourceLockedError from Products.CMFCore.PortalContent import ResourceLockedError
from zExceptions import Forbidden from zExceptions import Forbidden
from six.moves import cStringIO as StringIO from io import BytesIO
import six import six
security = ModuleSecurityInfo(__name__) security = ModuleSecurityInfo(__name__)
...@@ -50,7 +50,10 @@ class TextContent: ...@@ -50,7 +50,10 @@ class TextContent:
raise Exception raise Exception
except Exception: except Exception:
# this is probably not html code, try rfc822 parsing # this is probably not html code, try rfc822 parsing
message = email.message_from_string(text) if six.PY3:
message = email.message_from_bytes(text)
else:
message = email.message_from_string(text)
return {k.capitalize(): '\n'.join(message.get_all(k)) return {k.capitalize(): '\n'.join(message.get_all(k))
for k in message.keys()} for k in message.keys()}
...@@ -78,7 +81,7 @@ class TextContent: ...@@ -78,7 +81,7 @@ class TextContent:
headers = self.parseHeadersFromText(body) headers = self.parseHeadersFromText(body)
content_type = REQUEST.get_header('Content-Type', '') content_type = REQUEST.get_header('Content-Type', '')
headers.setdefault('content_type', content_type) headers.setdefault('content_type', content_type)
headers['file'] = StringIO(body) headers['file'] = BytesIO(body)
self._edit(**headers) self._edit(**headers)
except ResourceLockedError: except ResourceLockedError:
transaction.abort() transaction.abort()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment