Commit 959f7ae3 authored by Jérome Perrin's avatar Jérome Perrin

WebDAVSupport: py3

parent 0b8725e6
......@@ -35,7 +35,7 @@ from Products.ERP5Type.tests.utils import FileUpload
from unittest import expectedFailure
import six.moves.http_client
from six.moves import cStringIO as StringIO
from io import BytesIO
from DateTime import DateTime
from lxml import etree
......@@ -46,11 +46,6 @@ def makeFilePath(name):
return os.path.join(os.path.dirname(Products.ERP5.tests.__file__),
'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):
"""Test for WEBDAV access.
......@@ -58,6 +53,14 @@ class TestWebDavSupport(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):
return "Test WebDav Support"
......@@ -94,11 +97,13 @@ class TestWebDavSupport(ERP5TypeTestCase):
"""
person = self.portal.person_module.newContent()
self.tic()
file_object = makeFileUpload('images/erp5_logo.png')
response = self.publish(person.getPath() + '/erp5_logo.png',
request_method='PUT',
stdin=file_object,
basic=self.authentication)
file_object = self.makeFileUpload('images/erp5_logo.png')
response = self.publish(
person.getPath() + '/erp5_logo.png',
request_method='PUT',
stdin=file_object,
env={"CONTENT_TYPE": 'image/png'},
basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.CREATED)
image = person['erp5_logo.png']
self.assertEqual(image.getPortalType(), 'Embedded File')
......@@ -113,11 +118,13 @@ class TestWebDavSupport(ERP5TypeTestCase):
# Create a new document via FTP/DAV
path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
basic=self.authentication)
file_object = self.makeFileUpload(filename)
response = self.publish(
'%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
env={"CONTENT_TYPE": 'application/vnd.oasis.opendocument.presentation'},
basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.CREATED)
document_module = self.getDocumentModule()
......@@ -135,11 +142,13 @@ class TestWebDavSupport(ERP5TypeTestCase):
# Create a new document via FTP/DAV
path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
basic=self.authentication)
file_object = self.makeFileUpload(filename)
response = self.publish(
'%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
env={"CONTENT_TYPE": 'application/vnd.oasis.opendocument.presentation'},
basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.CREATED)
self.tic()
......@@ -152,7 +161,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
# force usage of manage_FTPget like zwebdav_server does
response = self.publish('%s/%s/manage_FTPget' % (path, document_id),
request_method='GET',
stdin=StringIO(),
stdin=BytesIO(),
basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.OK)
self.assertEqual(response.getBody(), document.getData(),
......@@ -175,7 +184,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
self.assertEqual(web_page_module[filename].getPortalType(), 'Web Page')
# Edit a new document via FTP/DAV
text_content= """<html>
text_content = u"""<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
......@@ -185,19 +194,20 @@ class TestWebDavSupport(ERP5TypeTestCase):
</body>
</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()
for _ in range(2): # Run twice to check the code that compares
# old & new data when setting file attribute.
# Run twice to check the code that compares old & new data
# when setting file attribute.
for _ in range(2):
response = self.publish('%s/%s' % (path, filename),
request_method='PUT',
stdin=StringIO(iso_text_content),
stdin=BytesIO(iso_text_content),
basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.NO_CONTENT)
self.assertEqual(web_page_module[filename].getData(), iso_text_content)
# Convert to base format and run conversion into utf-8
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')
self.assertEqual(web_page_module[filename].getTextContent(), new_text_content)
......@@ -206,11 +216,13 @@ class TestWebDavSupport(ERP5TypeTestCase):
"""
path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
basic=self.authentication)
file_object = self.makeFileUpload(filename)
response = self.publish(
'%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
env={"CONTENT_TYPE": 'application/vnd.oasis.opendocument.presentation'},
basic=self.authentication)
# Convert to base format and run conversion into utf-8
self.tic()
......@@ -219,9 +231,9 @@ class TestWebDavSupport(ERP5TypeTestCase):
# This is HTTPServer.zhttp_server not HTTPServer.zwebdav_server
# 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',
stdin=StringIO(),
stdin=BytesIO(),
basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.OK)
......@@ -233,11 +245,13 @@ class TestWebDavSupport(ERP5TypeTestCase):
"""
path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
basic=self.authentication)
file_object = self.makeFileUpload(filename)
response = self.publish(
'%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
env={"CONTENT_TYPE": 'application/vnd.oasis.opendocument.presentation'},
basic=self.authentication)
document_module = self.getDocumentModule()
document = document_module[filename]
......@@ -245,7 +259,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
response = self.publish(document.getPath(),
request_method='PROPFIND',
env={'HTTP_DEPTH': '0'},
stdin=StringIO(),
stdin=BytesIO(),
basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.MULTI_STATUS)
......@@ -271,11 +285,13 @@ class TestWebDavSupport(ERP5TypeTestCase):
"""
path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
basic=self.authentication)
file_object = self.makeFileUpload(filename)
response = self.publish(
'%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
env={"CONTENT_TYPE": 'application/vnd.oasis.opendocument.presentation'},
basic=self.authentication)
document_module = self.getDocumentModule()
document = document_module[filename]
......@@ -283,7 +299,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
response = self.publish(document.getPath(),
request_method='PROPFIND',
env={'HTTP_DEPTH': '0'},
stdin=StringIO(),
stdin=BytesIO(),
basic=self.authentication)
self.assertEqual(response.getStatus(), six.moves.http_client.MULTI_STATUS)
......@@ -293,7 +309,3 @@ class TestWebDavSupport(ERP5TypeTestCase):
'{DAV:}response/{DAV:}propstat/{DAV:}prop/{DAV:}creationdate').text,
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
from Products.ERP5Type import Permissions
from Products.CMFCore.PortalContent import ResourceLockedError
from zExceptions import Forbidden
from six.moves import cStringIO as StringIO
from io import BytesIO
import six
security = ModuleSecurityInfo(__name__)
......@@ -50,7 +50,10 @@ class TextContent:
raise Exception
except Exception:
# 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))
for k in message.keys()}
......@@ -78,7 +81,7 @@ class TextContent:
headers = self.parseHeadersFromText(body)
content_type = REQUEST.get_header('Content-Type', '')
headers.setdefault('content_type', content_type)
headers['file'] = StringIO(body)
headers['file'] = BytesIO(body)
self._edit(**headers)
except ResourceLockedError:
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