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

syncml: only use files from the subscriptions and publications

This comes with a small breaking change that when using file:// URLs,
they need to be well formed ( file:// + path and not file:/ + path )
parent d3706099
......@@ -27,10 +27,11 @@
#
##############################################################################
from six.moves.urllib.parse import urlparse
import itertools
import os
from base64 import b16encode
from unittest import expectedFailure
import unittest
from AccessControl.SecurityManagement import newSecurityManager
......@@ -99,9 +100,9 @@ class TestERP5DocumentSyncMLMixin(TestERP5SyncMLMixin):
xml_mapping = 'asXML'
pub_conduit = 'ERP5DocumentConduit'
sub_conduit1 = 'ERP5DocumentConduit'
publication_url = 'file:/%s/sync_server' % tests_home
subscription_url = {'two_way': 'file:/%s/sync_client1' % tests_home,
'from_server': 'file:/%s/sync_client_from_server' % tests_home}
publication_url = 'file://%s/sync_server' % tests_home
subscription_url = {'two_way': 'file://%s/sync_client1' % tests_home,
'from_server': 'file://%s/sync_client_from_server' % tests_home}
#for this tests
nb_message_first_synchronization = 6
nb_message_multi_first_synchronization = 12
......@@ -142,14 +143,10 @@ class TestERP5DocumentSyncMLMixin(TestERP5SyncMLMixin):
def clearFiles(self):
# reset files, because we do sync by files
for filename in self.subscription_url.values():
file_ = open(filename[len('file:/'):], 'w')
file_.write('')
file_.close()
file_ = open(self.publication_url[len('file:/'):], 'w')
file_.write('')
file_.close()
for filename in itertools.chain(
self.subscription_url.values(), [self.publication_url]):
with open(urlparse(filename).path, 'w') as f:
f.write('')
def setSystemPreferences(self):
default_pref = self.portal.portal_preferences.default_site_preference
......@@ -858,7 +855,3 @@ class TestERP5DocumentSyncML(TestERP5DocumentSyncMLMixin):
self.assertXMLViewIsEqual(self.sub_id1, document_s, document_c, force=True,
ignore_processing_status_workflow=True)
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestERP5DocumentSyncML))
return suite
\ No newline at end of file
......@@ -27,7 +27,6 @@
#
##############################################################################
import unittest
from base64 import b64encode, b64decode, b16encode
from lxml import etree
from unittest import expectedFailure
......@@ -44,7 +43,7 @@ from erp5.component.module.SyncMLConstant import MAX_LEN
from erp5.component.document import SyncMLSubscription
from erp5.component.module.testERP5SyncMLMixin import TestERP5SyncMLMixin \
as TestMixin
from six.moves import range
from zExceptions import Forbidden
class TestERP5SyncMLMixin(TestMixin):
......@@ -103,9 +102,9 @@ class TestERP5SyncMLMixin(TestMixin):
self._subscription_url1 = tests_home + '/sync_client1'
self._subscription_url2 = tests_home + '/sync_client2'
self._publication_url = tests_home + '/sync_server'
self.subscription_url1 = 'file:/' + self._subscription_url1
self.subscription_url2 = 'file:/' + self._subscription_url2
self.publication_url = 'file:/' + self._publication_url
self.subscription_url1 = 'file://' + self._subscription_url1
self.subscription_url2 = 'file://' + self._subscription_url2
self.publication_url = 'file://' + self._publication_url
def beforeTearDown(self):
"""Clean up."""
......@@ -196,15 +195,9 @@ class TestERP5SyncMLMixin(TestMixin):
def clearFiles(self):
# reset files, because we do sync by files
file_ = open(self._subscription_url1, 'w')
file_.write('')
file_.close()
file_ = open(self._subscription_url2, 'w')
file_.write('')
file_.close()
file_ = open(self._publication_url, 'w')
file_.write('')
file_.close()
for filename in self._subscription_url1, self._subscription_url2, self._publication_url:
with open(filename, 'w') as f:
f.write('')
def synchronize(self, id): # pylint: disable=redefined-builtin
"""
......@@ -1880,8 +1873,7 @@ return [context[%r]]
self.assertXMLViewIsEqual(self.sub_id1, person_s, person_c1)
self.assertXMLViewIsEqual(self.sub_id1, person_s, person_c2)
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestERP5SyncML))
return suite
\ No newline at end of file
def test_readResponse_file(self):
self.assertRaises(Forbidden,
self.getSynchronizationTool().readResponse,
from_url='file:///etc/hosts')
......@@ -27,6 +27,7 @@
from logging import getLogger
import six
from six.moves.urllib.parse import urlparse
from AccessControl import ClassSecurityInfo
......@@ -39,6 +40,8 @@ from erp5.component.module.SyncMLMessage import SyncMLRequest
from erp5.component.module.SyncMLEngineSynchronous import SyncMLSynchronousEngine
from erp5.component.module.SyncMLEngineAsynchronous import SyncMLAsynchronousEngine
from Products.ERP5.ERP5Site import getSite
from zExceptions import Forbidden
synchronous_engine = SyncMLSynchronousEngine()
asynchronous_engine = SyncMLAsynchronousEngine()
......@@ -237,22 +240,20 @@ class SynchronizationTool(BaseTool):
# we use from only if we have a file
elif isinstance(from_url, six.string_types):
if from_url.startswith('file:'):
filename = from_url[len('file:'):]
xml = None
try:
stream = open(filename, 'rb')
except IOError:
# XXX-Aurel : Why raising here make unit tests to fail ?
# raise ValueError("Impossible to read file %s, error is %s"
# % (filename, msg))
pass
else:
xml = stream.read()
stream.close()
syncml_logger.debug('readResponse xml from file is %s', xml)
if xml:
return xml
parsed_url = urlparse(from_url)
if parsed_url.scheme == 'file':
if (any(
from_url.startswith(pub.getUrlString())
for pub in self.contentValues(portal_type='SyncML Publication'))
or any(
from_url.startswith(sub.getSubscriptionUrlString())
for sub in self.contentValues(portal_type='SyncML Subscription'))):
with open(parsed_url.path, 'rb') as f:
xml = f.read()
syncml_logger.debug('readResponse xml from file is %s', xml)
return xml or None
raise Forbidden
#
# End of part managing protocols
#
......
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