Commit 8b6848d8 authored by Jérome Perrin's avatar Jérome Perrin Committed by Arnaud Fontaine

tests: makeFileUpload: Use unittest addCleanup() to close files rather than tearDown().

This executes even if setup() encounters an error and the cleanup hook is added
when opening the file, not later on.

Also, unify makeFileUpload() and makeFilePath() to remove duplicated code.
Co-authored-by: Arnaud Fontaine's avatarArnaud Fontaine <arnaud.fontaine@nexedi.com>
parent 40aad952
Pipeline #36133 failed with stage
in 0 seconds
......@@ -33,7 +33,6 @@ import textwrap
from unittest import expectedFailure
from Products.CMFCore.WorkflowCore import WorkflowException
from Products.ERP5Type.tests.utils import FileUpload
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5OOo.tests.testIngestion import FILENAME_REGULAR_EXPRESSION
from Products.ERP5OOo.tests.testIngestion import REFERENCE_REGULAR_EXPRESSION
......@@ -46,14 +45,6 @@ from DateTime import DateTime
import Products.ERP5.tests
def makeFilePath(name):
return os.path.join(os.path.dirname(Products.ERP5.tests.__file__),
'test_data', 'crm_emails', name)
def makeFileUpload(name):
path = makeFilePath(name)
return FileUpload(path, name)
clear_module_name_list = """
campaign_module
event_module
......@@ -80,6 +71,11 @@ class BaseTestCRM(ERP5TypeTestCase):
self.tic()
super(BaseTestCRM, self).beforeTearDown()
def _getTestDataPath(self):
return os.path.join(os.path.dirname(Products.ERP5.tests.__file__),
'test_data',
'crm_emails')
class TestCRM(BaseTestCRM):
def getTitle(self):
return "CRM"
......@@ -709,14 +705,10 @@ class TestCRMMailIngestion(BaseTestCRM):
# make sure customers are available to catalog
self.tic()
def _readTestData(self, filename):
"""read test data from data directory."""
return open(makeFilePath(filename)).read()
def _ingestMail(self, filename=None, data=None):
"""ingest an email from the mail in data dir named `filename`"""
if data is None:
data=self._readTestData(filename)
data = self.makeFileUpload(filename).read()
return self.portal.portal_contributions.newContent(
container_path='event_module',
filename='postfix_mail.eml',
......@@ -887,7 +879,9 @@ class TestCRMMailIngestion(BaseTestCRM):
return object_list[-1]
portal = self.portal
message = message_from_string(self._readTestData('simple'))
message_string = self.makeFileUpload('simple').read()
message = message_from_string(message_string)
message.replace_header('subject', 'Visit:Company A')
data = message.as_string()
self._ingestMail(data=data)
......@@ -895,7 +889,7 @@ class TestCRMMailIngestion(BaseTestCRM):
document = getLastCreatedEvent(portal.event_module)
self.assertEqual(document.getPortalType(), 'Visit')
message = message_from_string(self._readTestData('simple'))
message = message_from_string(message_string)
message.replace_header('subject', 'Fax:Company B')
data = message.as_string()
self._ingestMail(data=data)
......@@ -903,7 +897,7 @@ class TestCRMMailIngestion(BaseTestCRM):
document = getLastCreatedEvent(portal.event_module)
self.assertEqual(document.getPortalType(), 'Fax Message')
message = message_from_string(self._readTestData('simple'))
message = message_from_string(message_string)
message.replace_header('subject', 'TEST:Company B')
data = message.as_string()
self._ingestMail(data=data)
......@@ -911,7 +905,7 @@ class TestCRMMailIngestion(BaseTestCRM):
document = getLastCreatedEvent(portal.event_module)
self.assertEqual(document.getPortalType(), 'Mail Message')
message = message_from_string(self._readTestData('simple'))
message = message_from_string(message_string)
message.replace_header('subject', 'visit:Company A')
data = message.as_string()
self._ingestMail(data=data)
......@@ -919,7 +913,7 @@ class TestCRMMailIngestion(BaseTestCRM):
document = getLastCreatedEvent(portal.event_module)
self.assertEqual(document.getPortalType(), 'Visit')
message = message_from_string(self._readTestData('simple'))
message = message_from_string(message_string)
message.replace_header('subject', 'phone:Company B')
data = message.as_string()
self._ingestMail(data=data)
......@@ -927,7 +921,7 @@ class TestCRMMailIngestion(BaseTestCRM):
document = portal.event_module[portal.event_module.objectIds()[-1]]
self.assertEqual(document.getPortalType(), 'Phone Call')
message = message_from_string(self._readTestData('simple'))
message = message_from_string(message_string)
message.replace_header('subject', 'LETTER:Company C')
data = message.as_string()
self._ingestMail(data=data)
......@@ -935,7 +929,7 @@ class TestCRMMailIngestion(BaseTestCRM):
document = getLastCreatedEvent(portal.event_module)
self.assertEqual(document.getPortalType(), 'Letter')
message = message_from_string(self._readTestData('simple'))
message = message_from_string(message_string)
body = message.get_payload()
message.set_payload('Visit:%s' % body)
data = message.as_string()
......@@ -944,7 +938,7 @@ class TestCRMMailIngestion(BaseTestCRM):
document = getLastCreatedEvent(portal.event_module)
self.assertEqual(document.getPortalType(), 'Visit')
message = message_from_string(self._readTestData('simple'))
message = message_from_string(message_string)
body = message.get_payload()
message.set_payload('PHONE CALL:%s' % body)
data = message.as_string()
......@@ -1279,7 +1273,7 @@ class TestCRMMailSend(BaseTestCRM):
# Add a document which will be attached.
# pdf
filename = 'sample_attachment.pdf'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_object)
self.tic()
......@@ -1326,7 +1320,7 @@ class TestCRMMailSend(BaseTestCRM):
"""
# Add a document which will be attached.
filename = 'sample_attachment.odt'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_object)
self.tic()
......@@ -1373,7 +1367,7 @@ class TestCRMMailSend(BaseTestCRM):
"""
# Add a document which will be attached.
filename = 'sample_attachment.zip'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_object)
self.tic()
......@@ -1418,7 +1412,7 @@ class TestCRMMailSend(BaseTestCRM):
"""
# Add a document which will be attached.
filename = 'sample_attachment.gif'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_object)
self.tic()
......@@ -1515,7 +1509,7 @@ class TestCRMMailSend(BaseTestCRM):
# Add a document which will be attached.
# pdf
filename = 'sample_attachment.pdf'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
# Add a ticket
ticket = self.portal.campaign_module.newContent(portal_type='Campaign',
......@@ -1563,7 +1557,7 @@ class TestCRMMailSend(BaseTestCRM):
"""
# Add a document which will be attached.
filename = 'sample_attachment.zip'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
# Add a ticket
ticket = self.portal.campaign_module.newContent(portal_type='Campaign',
......@@ -1612,7 +1606,7 @@ class TestCRMMailSend(BaseTestCRM):
"""
# Add a document which will be attached.
filename = 'sample_attachment.zip'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
# Add a ticket
ticket = self.portal.campaign_module.newContent(portal_type='Campaign',
......@@ -1689,7 +1683,7 @@ class TestCRMMailSend(BaseTestCRM):
# Add a document on a person which will be attached.
def add_document(filename, container, portal_type):
f = makeFileUpload(filename)
f = self.makeFileUpload(filename)
document = container.newContent(portal_type=portal_type)
document.edit(file=f, reference=filename)
return document
......@@ -1746,7 +1740,7 @@ class TestCRMMailSend(BaseTestCRM):
# Add a document on a person which will be attached.
def add_document(filename, container, portal_type):
f = makeFileUpload(filename)
f = self.makeFileUpload(filename)
document = container.newContent(portal_type=portal_type)
document.edit(file=f, reference=filename)
return document
......
......@@ -32,7 +32,6 @@
import unittest
from AccessControl.SecurityManagement import newSecurityManager
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from erp5.component.test.testDms import makeFileUpload
class TestERP5Discussion(ERP5TypeTestCase):
......@@ -128,7 +127,7 @@ class TestERP5Discussion(ERP5TypeTestCase):
self.assertEqual(0, len(attachment_list))
# check attachment creation
file_ = makeFileUpload('TEST-en-002.doc')
file_ = self.makeFileUpload('TEST-en-002.doc')
web_section1.WebSection_createNewDiscussionThread('test1-new-with-attachment', 'test1 body', file=file_)
discussion_thread, = [x for x in self.portal.discussion_thread_module.objectValues() \
if x.getId() not in discussion_thread_id_set]
......@@ -225,7 +224,7 @@ class TestERP5Discussion(ERP5TypeTestCase):
web_site_value = self.portal.web_site_module.newContent(portal_type='Web Site')
web_section_value = web_site_value.newContent(portal_type='Web Section')
file_ = makeFileUpload('simple.csv')
file_ = self.makeFileUpload('simple.csv')
web_section_value.WebSection_createNewDiscussionThread(
"Thread Title",
"Post Content",
......
......@@ -41,7 +41,7 @@ import transaction
from AccessControl import Unauthorized
from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.utils import FileUpload, createZODBPythonScript
from Products.ERP5Type.tests.utils import createZODBPythonScript
from Products.ERP5Type.Utils import bytes2str, str2bytes
from erp5.component.document.Document import ConversionError
......@@ -67,17 +67,6 @@ Y5pFvE34AgSXzrOPljMwMCgKWeIPJZxG43fR2UfLIRbAbfJjcERTgz1ASE0PcGsIGE1GOsMK0APk
/6e3ek9E9ERmk2rQv49vGHgkcBotISHBQDbglkDTzjjaCKab0QBziJyFukqO6AAAAABJRU5ErkJg
gg==''')
def makeFilePath(name):
from Products.ERP5 import tests
return os.path.join(tests.__path__[0], '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)
def process_image(image, size=(40, 40)):
# open the images to compare, resize them, and convert to grayscale
# get the rgb values of the pixels in the image
......@@ -151,6 +140,10 @@ class TestERP5WebWithDms(ERP5TypeTestCase, ZopeTestCase.Functional):
self.clearModule(self.portal.web_site_module)
self.clearModule(self.portal.web_page_module)
def _getTestDataPath(self):
from Products.ERP5 import tests
return os.path.join(tests.__path__[0], 'test_data')
def setupWebSite(self, **kw):
"""
Setup Web Site
......@@ -472,7 +465,7 @@ class TestERP5WebWithDms(ERP5TypeTestCase, ZopeTestCase.Functional):
self.assertEqual(modification_date, last_modified_header)
# Upload a presentation with 3 pages.
upload_file = makeFileUpload('P-DMS-Presentation.3.Pages-001-en.odp')
upload_file = self.makeFileUpload('P-DMS-Presentation.3.Pages-001-en.odp')
document = document_module.newContent(portal_type='Presentation',
file=upload_file)
reference = 'P-DMS-Presentation.3.Pages'
......@@ -529,7 +522,7 @@ class TestERP5WebWithDms(ERP5TypeTestCase, ZopeTestCase.Functional):
document_reference = 'NXD-Presentation'
document_module = portal.getDefaultModule(portal_type='Presentation')
upload_file = makeFileUpload('P-DMS-Presentation.3.Pages-001-en.odp')
upload_file = self.makeFileUpload('P-DMS-Presentation.3.Pages-001-en.odp')
document = document_module.newContent(portal_type='Presentation',
reference=document_reference,
file=upload_file)
......@@ -537,7 +530,7 @@ class TestERP5WebWithDms(ERP5TypeTestCase, ZopeTestCase.Functional):
image_reference = 'NXD-IMAGE'
image_module = portal.getDefaultModule(portal_type='Image')
upload_file = makeFileUpload('tiolive-ERP5.Freedom.TioLive.Logo-001-en.png')
upload_file = self.makeFileUpload('tiolive-ERP5.Freedom.TioLive.Logo-001-en.png')
image = image_module.newContent(portal_type='Image',
file=upload_file,
reference=image_reference)
......@@ -636,7 +629,7 @@ class TestERP5WebWithDms(ERP5TypeTestCase, ZopeTestCase.Functional):
self.assertEqual(modification_date, last_modified_header)
# Upload a presentation with 3 pages.
upload_file = makeFileUpload('P-DMS-Presentation.3.Pages-001-en.odp')
upload_file = self.makeFileUpload('P-DMS-Presentation.3.Pages-001-en.odp')
document = document_module.newContent(portal_type='Presentation',
file=upload_file)
reference = 'P-DMS-Presentation-001-.3.Pages'
......@@ -711,7 +704,7 @@ return True
website.newContent(portal_type=web_section_portal_type)
document_reference = 'tiolive-ERP5.Freedom.TioLive'
upload_file = makeFileUpload('tiolive-ERP5.Freedom.TioLive-001-en.odp')
upload_file = self.makeFileUpload('tiolive-ERP5.Freedom.TioLive-001-en.odp')
document = self.portal.document_module.newContent(
portal_type='Presentation',
reference=document_reference,
......@@ -809,7 +802,7 @@ return True
image_reference = 'NXD-IMAGE'
module = portal.getDefaultModule(portal_type=image_portal_type)
upload_file = makeFileUpload('tiolive-ERP5.Freedom.TioLive.Logo-001-en.png')
upload_file = self.makeFileUpload('tiolive-ERP5.Freedom.TioLive.Logo-001-en.png')
image = module.newContent(portal_type=image_portal_type,
file=upload_file,
reference=image_reference)
......@@ -879,7 +872,7 @@ return True
"""
portal = self.portal
module = portal.getDefaultModule(portal_type=portal_type)
upload_file = makeFileUpload('%s.svg' % filename)
upload_file = self.makeFileUpload('%s.svg' % filename)
image = module.newContent(portal_type=portal_type,
file=upload_file,
reference="NXD-DOCUMENT")
......@@ -888,7 +881,7 @@ return True
self.assertEqual(image.getContentType(), 'image/svg+xml')
mime, converted_data = image.convert("png")
self.assertEqual(mime, 'image/png')
expected_image = makeFileUpload('%s.png' % filename)
expected_image = self.makeFileUpload('%s.png' % filename)
# Compare images and accept some minimal difference,
difference_value = compare_image(StringIO(converted_data), expected_image)
......@@ -903,7 +896,7 @@ return True
"""
portal = self.portal
module = portal.getDefaultModule(portal_type=portal_type)
upload_file = makeFileUpload('user-TESTSVG-CASE-URL-TEMPLATE.svg')
upload_file = self.makeFileUpload('user-TESTSVG-CASE-URL-TEMPLATE.svg')
svg_content = upload_file.read().replace("REPLACE_THE_URL_HERE", image_url)
# Add image using data instead file this time as it is not the goal of
......@@ -918,7 +911,7 @@ return True
self.assertEqual(image.getContentType(), 'image/svg+xml')
mime, converted_data = image.convert("png")
self.assertEqual(mime, 'image/png')
expected_image = makeFileUpload('user-TESTSVG-CASE-URL.png')
expected_image = self.makeFileUpload('user-TESTSVG-CASE-URL.png')
# Compare images and accept some minimal difference,
difference_value = compare_image(StringIO(converted_data), expected_image)
......@@ -935,7 +928,7 @@ return True
This is not used by ERP5 in production, but this is way that
prooves that conversion from SVG to PNG can use external images.
"""
image_url = "file://" + makeFilePath("user-TESTSVG-BACKGROUND-IMAGE.png")
image_url = "file://" + self.makeFileUploadPath("user-TESTSVG-BACKGROUND-IMAGE.png")
self._testImageConversionFromSVGToPNG_url(image_url, portal_type)
def _testImageConversionFromSVGToPNG_http_url(self, portal_type="Image"):
......@@ -945,7 +938,7 @@ return True
"""
portal = self.portal
module = portal.getDefaultModule(portal_type=portal_type)
upload_file = makeFileUpload('user-TESTSVG-BACKGROUND-IMAGE.png')
upload_file = self.makeFileUpload('user-TESTSVG-BACKGROUND-IMAGE.png')
background_image = module.newContent(portal_type=portal_type,
file=upload_file,
reference="NXD-BACKGROUND")
......@@ -966,11 +959,11 @@ return True
"""
portal = self.portal
module = portal.getDefaultModule(portal_type=portal_type)
upload_file = makeFileUpload('user-TESTSVG-CASE-URL-TEMPLATE.svg')
upload_file = self.makeFileUpload('user-TESTSVG-CASE-URL-TEMPLATE.svg')
svg_content = upload_file.read().replace("REPLACE_THE_URL_HERE",
"http://soidjsoidjqsoijdqsoidjqsdoijsqd.idjsijds/../user-XXX-XXX")
upload_file = makeFileUpload('user-TESTSVG-CASE-URL-TEMPLATE.svg')
upload_file = self.makeFileUpload('user-TESTSVG-CASE-URL-TEMPLATE.svg')
svg2_content = upload_file.read().replace("REPLACE_THE_URL_HERE",
"https://www.erp5.com/usXXX-XXX")
......
......@@ -31,7 +31,7 @@
import unittest
from DateTime import DateTime
from erp5.component.test.testDms import TestDocumentMixin, makeFileUpload
from erp5.component.test.testDms import TestDocumentMixin
try:
import magic
......@@ -59,7 +59,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
def test_image_conversion(self):
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
self.tic()
format_ = 'png'
......@@ -96,7 +96,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
Test Conversion Cache mechanism
"""
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
self.tic()
document_url = document.getRelativeUrl()
......@@ -129,7 +129,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
def test_02_VolatileCacheConversionOfTempObject(self):
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_, temp_object=1)
document.uploadFile()
document.processFile()
......@@ -163,8 +163,8 @@ class TestDocumentConversionCache(TestDocumentMixin):
def test_03_CacheConversionOfTempObjectIsNotMixed(self):
filename1 = 'TEST-en-002.doc'
filename2 = 'TEST-en-002.odt'
file1 = makeFileUpload(filename1)
file2 = makeFileUpload(filename2)
file1 = self.makeFileUpload(filename1)
file2 = self.makeFileUpload(filename2)
document1 = self.portal.portal_contributions.newContent(file=file1, temp_object=1)
document1.uploadFile()
document1.processFile()
......@@ -187,7 +187,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
self.portal.portal_caches.clearAllCache()
self.tic()
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
self.tic()
document_url = document.getRelativeUrl()
......@@ -223,7 +223,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
Test Conversion Cache return expected value with checksum
"""
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
self.tic()
document_url = document.getRelativeUrl()
......@@ -252,7 +252,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
Check that md5 checksum is well updated when upload a file
"""
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
self.tic()
document_url = document.getRelativeUrl()
......@@ -260,7 +260,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
md5sum = document.getContentMd5()
self.assertTrue(md5sum)
filename2 = 'TEST-en-002.odt'
file2 = makeFileUpload(filename2)
file2 = self.makeFileUpload(filename2)
document.edit(file=file2)
self.assertNotEqual(md5sum, document.getContentMd5())
self.tic()
......@@ -275,7 +275,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
self.portal.portal_caches.clearAllCache()
self.tic()
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document_id = 'an id with spaces'
portal_type = 'Text'
module = self.portal.getDefaultModule(portal_type)
......@@ -314,7 +314,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
self.assertEqual(len(portal_type_list), len([pt for pt in portal_type_list if pt in data_mapping]))
for portal_type in portal_type_list:
module = self.portal.getDefaultModule(portal_type=portal_type)
upload_file = makeFileUpload(data_mapping[portal_type])
upload_file = self.makeFileUpload(data_mapping[portal_type])
document = module.newContent(portal_type=portal_type)
document.edit(file=upload_file)
self.tic()
......
......@@ -26,7 +26,6 @@
##############################################################################
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from erp5.component.test.testDms import makeFileUpload
from Products.ERP5Form.PreferenceTool import Priority
......@@ -80,7 +79,7 @@ class TestOOoConversionServerRetry(ERP5TypeTestCase):
self.tic()
filename = 'monochrome_sample.tiff'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.document_module.newContent(portal_type='Text')
document.edit(file = file_)
message = document.Document_tryToConvertToBaseFormat()
......@@ -95,7 +94,7 @@ class TestOOoConversionServerRetry(ERP5TypeTestCase):
system_pref.setPreferredDocumentConversionServerUrlList(['https://broken.url'])
self.tic()
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
message = document.Document_tryToConvertToBaseFormat()
......@@ -110,7 +109,7 @@ class TestOOoConversionServerRetry(ERP5TypeTestCase):
system_pref.setPreferredOoodocServerTimeout(1)
self.tic()
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
message = document.Document_tryToConvertToBaseFormat()
......
......@@ -31,7 +31,6 @@
import unittest
import os
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.utils import FileUpload
from unittest import expectedFailure
import httplib
......@@ -40,20 +39,13 @@ from DateTime import DateTime
from lxml import etree
def makeFilePath(name):
import Products.ERP5.tests
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.
"""
def _getTestDataPath(self):
import Products.ERP5.tests
return os.path.join(os.path.dirname(Products.ERP5.tests.__file__), 'test_data')
def getTitle(self):
return "Test WebDav Support"
......@@ -90,7 +82,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
"""
person = self.portal.person_module.newContent()
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',
request_method='PUT',
stdin=file_object,
......@@ -109,7 +101,7 @@ 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)
file_object = self.makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
......@@ -131,7 +123,7 @@ 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)
file_object = self.makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
......@@ -202,7 +194,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
"""
path = self.portal.portal_contributions.getPath()
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),
request_method='PUT',
stdin=file_object,
......@@ -229,7 +221,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
"""
path = self.portal.portal_contributions.getPath()
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),
request_method='PUT',
stdin=file_object,
......@@ -267,7 +259,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
"""
path = self.portal.portal_contributions.getPath()
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),
request_method='PUT',
stdin=file_object,
......
......@@ -28,7 +28,7 @@
##############################################################################
import unittest
from erp5.component.test.testDms import TestDocument, makeFileUpload
from erp5.component.test.testDms import TestDocument
class TestDocumentWithPreConversion(TestDocument):
"""
......@@ -40,7 +40,7 @@ class TestDocumentWithPreConversion(TestDocument):
def test_preConvertedReferencedImageInWebPageContent(self):
# create an image
upload_file = makeFileUpload('cmyk_sample.jpg')
upload_file = self.makeFileUpload('cmyk_sample.jpg')
image = self.portal.image_module.newContent(portal_type='Image',
reference='Embedded-XXX',
version='001',
......@@ -75,7 +75,7 @@ class TestDocumentWithPreConversion(TestDocument):
language='en')
# draft image is not convertible
upload_file = makeFileUpload('cmyk_sample.jpg')
upload_file = self.makeFileUpload('cmyk_sample.jpg')
image.edit(file=upload_file)
self.tic()
self.assertEqual(False, image.Base_isConvertible())
......
......@@ -30,7 +30,7 @@
import unittest
from unittest import expectedFailure
from Products.ERP5Type.Base import TempBase
from erp5.component.test.testDms import makeFileUpload, TestDocumentMixin
from erp5.component.test.testDms import TestDocumentMixin
def _getGadgetInstanceUrlFromKnowledgePad(knowledge_pad, gadget):
""" Get Knowledge Box's relative URL specialising a gadget in a Knowledge Pad."""
......@@ -998,38 +998,38 @@ class TestKMSearch(TestKMMixIn):
# create docs to be referenced:
# (1) TEST, 002, en
filename = 'TEST-en-002.odt'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
self.portal.portal_contributions.newContent(file=file_)
# (2) TEST, 002, fr
as_name = 'TEST-fr-002.odt'
file_ = makeFileUpload(filename, as_name)
file_ = self.makeFileUpload(filename, as_name)
document2 = self.portal.portal_contributions.newContent(file=file_)
# (3) TEST, 003, en
as_name = 'TEST-en-003.odt'
file_ = makeFileUpload(filename, as_name)
file_ = self.makeFileUpload(filename, as_name)
document3 = self.portal.portal_contributions.newContent(file=file_)
# create docs to contain references in text_content:
# REF, 002, en; "I use reference to look up TEST"
filename = 'REF-en-002.odt'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document5 = self.portal.portal_contributions.newContent(file=file_)
# REFLANG, 001, en: "I use reference and language to look up TEST-fr"
#filename = 'REFLANG-en-001.odt'
#file = makeFileUpload(filename)
#file = self.makeFileUpload(filename)
#document6 = self.portal.portal_contributions.newContent(file=file)
# REFVER, 001, en: "I use reference and version to look up TEST-002"
#filename = 'REFVER-en-001.odt'
#file = makeFileUpload(filename)
#file = self.makeFileUpload(filename)
#document7 = self.portal.portal_contributions.newContent(file=file)
# REFVERLANG, 001, en: "I use reference, version and language to look up TEST-002-en"
#filename = 'REFVERLANG-en-001.odt'
#file = makeFileUpload(filename)
#file = self.makeFileUpload(filename)
#document8 = self.portal.portal_contributions.newContent(file=file)
self.tic()
......
......@@ -27,7 +27,6 @@
import warnings
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from erp5.component.test.testDms import makeFileUpload
original_warnings_showwarnings = warnings.showwarning
......@@ -42,7 +41,7 @@ class TestERP5PDFMerge(ERP5TypeTestCase):
"""
self.assertEqual(warnings.showwarning, original_warnings_showwarnings)
document = self.portal.portal_contributions.newContent(
file=makeFileUpload('REF-en-001.pdf'))
file=self.makeFileUpload('REF-en-001.pdf'))
merged_pdf_data = self.portal.ERP5Site_mergePDFList(
[document.getData(), document.getData()])
self.portal.document_module.newContent(
......@@ -53,7 +52,7 @@ class TestERP5PDFMerge(ERP5TypeTestCase):
def test_erp5_merge_pdf(self):
document = self.portal.portal_contributions.newContent(
file=makeFileUpload('REF-en-001.pdf'))
file=self.makeFileUpload('REF-en-001.pdf'))
merged_pdf_data = self.portal.ERP5Site_mergePDFList(
[document.getData(), document.getData()])
merged_document = self.portal.document_module.newContent(
......@@ -63,7 +62,7 @@ class TestERP5PDFMerge(ERP5TypeTestCase):
def test_erp5_merge_pdf_start_on_recto(self):
document = self.portal.portal_contributions.newContent(
file=makeFileUpload('REF-en-001.pdf'))
file=self.makeFileUpload('REF-en-001.pdf'))
merged_pdf_data = self.portal.ERP5Site_mergePDFList(
[document.getData(), document.getData()], start_on_recto=True)
merged_document = self.portal.document_module.newContent(
......
......@@ -27,10 +27,10 @@
##############################################################################
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from erp5.component.test.testDms import makeFileUpload
from time import time
import base64
class TestRunMyDoc(ERP5TypeTestCase):
"""
Basic Test for internal implementation of RunMyDocs
......@@ -96,7 +96,7 @@ class TestRunMyDoc(ERP5TypeTestCase):
Test Screeshot upload script used by Zelenium to
update screenshots of the documents.
"""
image_upload = makeFileUpload('TEST-en-002.png')
image_upload = self.makeFileUpload('TEST-en-002.png')
self.assertNotEqual(None, image_upload)
# Create a web page, and check if the content is not overwriten
......
......@@ -3,16 +3,7 @@ import PIL.Image as PIL_Image
import os
import transaction
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
class FileUpload(file):
"""Act as an uploaded file.
"""
__allow_access_to_unprotected_subobjects__ = 1
def __init__(self, path, name):
self.filename = name
file.__init__(self, path)
self.headers = {}
from Products.ERP5Type.tests.utils import FileUpload
def makeFilePath(name):
# return os.path.join(os.path.dirname(__file__), 'tmp', name)
......
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
import transaction
from io import FileIO
import os
class FileUpload(FileIO):
"""Act as an uploaded file.
"""
__allow_access_to_unprotected_subobjects__ = 1
def __init__(self, path, name):
self.filename = name
super(FileUpload, self).__init__(path)
self.headers = {}
def makeFilePath(name):
#return os.path.join(os.path.dirname(__file__), 'tmp', name)
return name
def makeFileUpload(name, as_name=None):
if as_name is None:
as_name = name
path = makeFilePath(name)
return FileUpload(path, as_name)
class TestSafeImage(ERP5TypeTestCase):
def afterSetUp(self):
portal = self.getPortalObject()
......@@ -45,7 +23,7 @@ class TestSafeImage(ERP5TypeTestCase):
fd = os.open(path_image, os.O_CREAT | os.O_RDWR)
os.write(fd,str(image.data))
os.close(fd)
_image = makeFileUpload(path_image)
_image = self.makeFileUpload(path_image)
image = self.image_module.newContent(portal_type='Image',title='testImage',
id='testImage',file=_image,filename='testImage')
return image
......@@ -57,7 +35,7 @@ class TestSafeImage(ERP5TypeTestCase):
fd = os.open(path_image, os.O_CREAT | os.O_RDWR)
os.write(fd,str(image.data))
os.close(fd)
tile_image = makeFileUpload(path_image)
tile_image = self.makeFileUpload(path_image)
tile = self.image_module.newContent(portal_type='Image Tile',title='testTile',
id='testTile',file=tile_image,filename='testTile')
return tile
......@@ -69,7 +47,7 @@ class TestSafeImage(ERP5TypeTestCase):
fd = os.open(path_image, os.O_CREAT | os.O_RDWR)
os.write(fd,str(image.data))
os.close(fd)
tile_image_transformed = makeFileUpload(path_image)
tile_image_transformed = self.makeFileUpload(path_image)
tile_transformed = self.image_module.newContent(portal_type='Image Tile Transformed',
title='testTileTransformed',id='testTileTransformed',
file=tile_image_transformed,filename='testTileTransformed')
......
......@@ -35,22 +35,15 @@ import unittest
from AccessControl.SecurityManagement import newSecurityManager
from Products.ERP5Type.tests.runUnitTest import tests_home
from Products.ERP5Type.tests.utils import FileUpload
from erp5.component.tool import SynchronizationTool
from erp5.component.test.testERP5SyncML import TestERP5SyncMLMixin
from erp5.component.document import SyncMLSubscription
import Products.ERP5.tests
test_files = os.path.join(os.path.dirname(Products.ERP5.tests.__file__), 'test_data')
FILENAME_REGULAR_EXPRESSION = "(?P<reference>[A-Z]{3,10})-\
(?P<language>[a-z]{2})-(?P<version>[0-9]{3})"
REFERENCE_REGULAR_EXPRESSION = "(?P<reference>[A-Z]{3,10})(-\
(?P<language>[a-z]{2}))?(-(?P<version>[0-9]{3}))?"
def makeFileUpload(name):
path = os.path.join(test_files, name)
return FileUpload(path, name)
class TestERP5DocumentSyncMLMixin(TestERP5SyncMLMixin):
nb_objects = 10
......@@ -63,13 +56,21 @@ class TestERP5DocumentSyncMLMixin(TestERP5SyncMLMixin):
#for documents (encoding in unicode for utf-8)
#files
filename_text = 'TEST-en-002.txt'
size_filename_text = len(makeFileUpload(filename_text).read())
@property
def size_filename_text(self):
return len(self.makeFileUpload(self.filename_text).read())
filename_odt = 'TEST-en-002.odt'
size_filename_odt = len(makeFileUpload(filename_odt).read())
@property
def size_filename_odt(self):
return len(self.makeFileUpload(self.filename_odt).read())
filename_ppt = 'TEST-en-002.ppt'
size_filename_ppt = len(makeFileUpload(filename_ppt).read())
@property
def size_filename_ppt(self):
return len(self.makeFileUpload(self.filename_ppt).read())
filename_pdf = 'TEST-en-002.pdf'
size_filename_pdf = len(makeFileUpload(filename_pdf).read())
@property
def size_filename_pdf(self):
return len(self.makeFileUpload(self.filename_pdf).read())
#properties
reference1 = 'P-SYNCML.Text'
version1 = '001'
......@@ -135,6 +136,9 @@ class TestERP5DocumentSyncMLMixin(TestERP5SyncMLMixin):
self.clearDocumentModules()
self.clearPublicationsAndSubscriptions()
def _getTestDataPath(self):
import Products.ERP5.tests
return os.path.join(os.path.dirname(Products.ERP5.tests.__file__), 'test_data')
def clearFiles(self):
# reset files, because we do sync by files
......@@ -280,7 +284,7 @@ class TestERP5DocumentSyncMLMixin(TestERP5SyncMLMixin):
kw = {'reference': self.reference1, 'Version': self.version1,
'Language': self.language1, 'Description': self.description1}
document_text.edit(**kw)
file_ = makeFileUpload(self.filename_text)
file_ = self.makeFileUpload(self.filename_text)
document_text.edit(file=file_)
self.tic()
document_pdf = document_server.newContent(id=self.id2,
......@@ -288,7 +292,7 @@ class TestERP5DocumentSyncMLMixin(TestERP5SyncMLMixin):
kw = {'reference': self.reference2, 'Version': self.version2,
'Language': self.language2, 'Description': self.description2}
document_pdf.edit(**kw)
file_ = makeFileUpload(self.filename_pdf)
file_ = self.makeFileUpload(self.filename_pdf)
document_pdf.edit(file=file_)
self.tic()
nb_document = len(document_server)
......@@ -305,7 +309,7 @@ class TestERP5DocumentSyncMLMixin(TestERP5SyncMLMixin):
kw = {'reference': reference, 'version': version, 'language': language}
doc_text.edit(**kw)
if file_name is not None:
file_ = makeFileUpload(file_name)
file_ = self.makeFileUpload(file_name)
doc_text.edit(file=file_)
return doc_text
......@@ -480,7 +484,7 @@ class TestERP5DocumentSyncML(TestERP5DocumentSyncMLMixin):
# Then we do only modification on a client (the gid) of client => add a object
kw = {'reference':self.reference1,'version':self.version3}
document_c.edit(**kw)
file_ = makeFileUpload(self.filename_odt)
file_ = self.makeFileUpload(self.filename_odt)
document_c.edit(file=file_)
self.tic()
self.synchronize(self.sub_id1)
......@@ -591,14 +595,14 @@ class TestERP5DocumentSyncML(TestERP5DocumentSyncMLMixin):
doc_s = document_server._getOb(id_text)
kw = {'description':self.description1}
doc_s.edit(**kw)
file_ = makeFileUpload(self.filename_odt)
file_ = self.makeFileUpload(self.filename_odt)
doc_s.edit(file=file_)
# Side client modification gid of a odt document
id_odt = str(self.ids[self.id_max_text+1])
doc_c = document_client1._getOb(id_odt)
kw = {'description':self.description3}
doc_c.edit(**kw)
file_ = makeFileUpload(self.filename_text)
file_ = self.makeFileUpload(self.filename_text)
doc_c.edit(file=file_)
self.tic()
self.synchronize(self.sub_id1)
......@@ -692,7 +696,7 @@ class TestERP5DocumentSyncML(TestERP5DocumentSyncMLMixin):
document_s = document_server._getOb(self.id1)
kw = {'description': self.description2, 'short_title': self.short_title2 }
document_s.edit(**kw)
file_ = makeFileUpload(self.filename_ppt)
file_ = self.makeFileUpload(self.filename_ppt)
document_s.edit(file=file_)
self.tic()
......@@ -701,7 +705,7 @@ class TestERP5DocumentSyncML(TestERP5DocumentSyncMLMixin):
document_c1 = document_client1._getOb(self.id1)
kw = {'description': self.description3, 'short_title': self.short_title3 }
document_c1.edit(**kw)
file_ = makeFileUpload(self.filename_odt)
file_ = self.makeFileUpload(self.filename_odt)
document_c1.edit(file=file_)
self.tic()
......@@ -822,7 +826,7 @@ class TestERP5DocumentSyncML(TestERP5DocumentSyncMLMixin):
self.assertXMLViewIsEqual(self.sub_id_from_server, document_s, document_c, force=1)
# Then we change things on both sides and we look if there
# is synchronization from only one way
file_ = makeFileUpload(self.filename_odt)
file_ = self.makeFileUpload(self.filename_odt)
document_c.edit(file=file_)
kw = {'short_title' : self.short_title2}
......
This diff is collapsed.
This diff is collapsed.
......@@ -52,6 +52,7 @@ from Products.ERP5Type.tests.backportUnittest import SetupSiteError
from Products.ERP5Type.tests.utils import addUserToDeveloperRole
from Products.ERP5Type.tests.utils import parseListeningAddress
from Products.ERP5Type.tests.utils import timeZoneContext
from Products.ERP5Type.tests.utils import FileUpload
# Quiet messages when installing business templates
install_bt5_quiet = 0
......@@ -928,6 +929,28 @@ class ERP5TypeTestCaseMixin(ProcessingNodeTestCase, PortalTestCase, functional.F
),
)
def _getTestDataPath(self):
"""
Where the tests data resides, to be overriden in children classes. By
default returns the current directory.
"""
return ''
def makeFileUploadPath(self, filename):
return os.path.join(self._getTestDataPath(), filename)
def makeFileUpload(self, filename, as_filename=None, path=None):
"""
Upload the given `filename` as `as_filename
"""
if as_filename is None:
as_filename = filename
if path is None:
path = self._getTestDataPath()
file_upload = FileUpload(os.path.join(path, filename), as_filename)
self.addCleanup(file_upload.close)
return file_upload
class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin):
__original_ZMySQLDA_connect = None
......
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