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

*: use open() as context manager to ensure files are closed

parent f741c9fb
...@@ -68,7 +68,8 @@ class File(object): ...@@ -68,7 +68,8 @@ class File(object):
file_path = tempfile.mktemp(suffix=".%s" % self.source_format, file_path = tempfile.mktemp(suffix=".%s" % self.source_format,
dir=self.directory_name) dir=self.directory_name)
# stores the data in temporary file # stores the data in temporary file
open(file_path, 'wb').write(self.original_data) with open(file_path, 'wb') as f:
f.write(self.original_data)
# If is a zipfile is need extract all files from whitin the compressed file # If is a zipfile is need extract all files from whitin the compressed file
if is_zipfile(file_path): if is_zipfile(file_path):
zipfile = ZipFile(file_path) zipfile = ZipFile(file_path)
...@@ -106,12 +107,14 @@ class File(object): ...@@ -106,12 +107,14 @@ class File(object):
zipfile.write(file) zipfile.write(file)
finally: finally:
zipfile.close() zipfile.close()
opened_zip = open(zip_path, 'r').read() with open(zip_path, 'rb') as f:
opened_zip = f.read()
remove(zip_path) remove(zip_path)
chdir(current_dir_url) chdir(current_dir_url)
return opened_zip return opened_zip
else: else:
return open(self.url, 'r').read() with open(self.url, 'rb') as f:
return f.read()
def getUrl(self): def getUrl(self):
"""Returns full path.""" """Returns full path."""
......
...@@ -44,14 +44,17 @@ class TestServer(TestCase): ...@@ -44,14 +44,17 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList()) self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self): def FaultConversionScenarioList(self):
return [ scenario_list = [
# Test to verify if server fail when a empty string is sent # Test to verify if server fail when a empty file is sent
('', '', ''), (b'', '', ''),
]
# Try convert one video for a invalid format # Try convert one video for a invalid format
(open(join('data', 'test.ogv')).read(), 'ogv', 'xyz'), with open(join('data', 'test.ogv'), 'rb') as f:
scenario_list.append((f.read(), 'ogv', 'xyz'))
# Try convert one video to format not possible # Try convert one video to format not possible
(open(join('data', 'test.ogv')).read(), 'ogv', 'moov'), with open(join('data', 'test.ogv'), 'rb') as f:
] scenario_list.append((f.read(), 'ogv', 'moov'))
return scenario_list
def testFaultConversion(self): def testFaultConversion(self):
"""Test fail convertion of Invalid video files""" """Test fail convertion of Invalid video files"""
......
...@@ -41,7 +41,8 @@ class TestHandler(HandlerTestCase): ...@@ -41,7 +41,8 @@ class TestHandler(HandlerTestCase):
def testConvertPNGtoJPG(self): def testConvertPNGtoJPG(self):
"""Test conversion of png to jpg""" """Test conversion of png to jpg"""
png_file = open("data/test.png").read() with open("data/test.png", "rb") as f:
png_file = f.read()
handler = Handler(self.tmp_url, png_file, "png", **self.kw) handler = Handler(self.tmp_url, png_file, "png", **self.kw)
jpg_file = handler.convert("jpg") jpg_file = handler.convert("jpg")
mime = magic.Magic(mime=True) mime = magic.Magic(mime=True)
...@@ -50,7 +51,8 @@ class TestHandler(HandlerTestCase): ...@@ -50,7 +51,8 @@ class TestHandler(HandlerTestCase):
def testgetMetadataFromImage(self): def testgetMetadataFromImage(self):
"""Test if metadata is extracted from image correctly""" """Test if metadata is extracted from image correctly"""
png_file = open("data/test.png").read() with open("data/test.png", "rb") as f:
png_file = f.read()
handler = Handler(self.tmp_url, png_file, "png", **self.kw) handler = Handler(self.tmp_url, png_file, "png", **self.kw)
metadata = handler.getMetadata() metadata = handler.getMetadata()
self.assertEqual(metadata.get("Compression"), "Zip") self.assertEqual(metadata.get("Compression"), "Zip")
......
...@@ -44,14 +44,17 @@ class TestServer(TestCase): ...@@ -44,14 +44,17 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList()) self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self): def FaultConversionScenarioList(self):
return [ scenario_list = [
# Test to verify if server fail when a empty string is sent # Test to verify if server fail when a empty file is sent
('', '', ''), (b'', '', ''),
# Try convert one video for a invalid format
(open(join('data', 'test.png')).read(), 'png', 'xyz'),
# Try convert one video to format not possible
(open(join('data', 'test.png')).read(), 'png', '8bim'),
] ]
# Try convert one png for a invalid format
with open(join('data', 'test.png'), 'rb') as f:
scenario_list.append((f.read(), 'png', 'xyz'))
# Try convert one png to format not possible
with open(join('data', 'test.png'), 'rb') as f:
scenario_list.append((f.read(), 'png', '8bim'))
return scenario_list
def testFaultConversion(self): def testFaultConversion(self):
"""Test fail convertion of Invalid image files""" """Test fail convertion of Invalid image files"""
......
...@@ -58,7 +58,8 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -58,7 +58,8 @@ class TestFileSystemDocument(unittest.TestCase):
document_filename = "document" document_filename = "document"
document_test_url = path.join(self.fsdocument.directory_name, document_test_url = path.join(self.fsdocument.directory_name,
document_filename) document_filename)
open(document_test_url, 'wb').write(decodestring("Test Document")) with open(document_test_url, 'wb') as f:
f.write(decodestring(b"Test Document"))
self.fsdocument.reload(document_test_url) self.fsdocument.reload(document_test_url)
self.assertEqual(path.exists(old_document_url), False) self.assertEqual(path.exists(old_document_url), False)
self.assertNotEqual(self.fsdocument.original_data, self.assertNotEqual(self.fsdocument.original_data,
...@@ -82,7 +83,8 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -82,7 +83,8 @@ class TestFileSystemDocument(unittest.TestCase):
def testLoadDocumentFile(self): def testLoadDocumentFile(self):
"""Test if the document is created correctly""" """Test if the document is created correctly"""
url = self.fsdocument.getUrl() url = self.fsdocument.getUrl()
tmp_document = open(url, 'r').read() with open(url, 'rb') as f:
tmp_document = f.read()
self.assertEqual(self.data, tmp_document) self.assertEqual(self.data, tmp_document)
self.fsdocument.trash() self.fsdocument.trash()
self.assertEqual(path.exists(url), False) self.assertEqual(path.exists(url), False)
...@@ -93,7 +95,8 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -93,7 +95,8 @@ class TestFileSystemDocument(unittest.TestCase):
document_filename = "document" document_filename = "document"
document_test_url = path.join(self.fsdocument.directory_name, document_test_url = path.join(self.fsdocument.directory_name,
document_filename) document_filename)
open(document_test_url, 'wb').write(self.data) with open(document_test_url, 'wb') as f:
f.write(self.data)
self.fsdocument.reload(document_test_url) self.fsdocument.reload(document_test_url)
url = self.fsdocument.getUrl() url = self.fsdocument.getUrl()
self.assertEqual(path.exists(old_document_url), False) self.assertEqual(path.exists(old_document_url), False)
...@@ -103,7 +106,8 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -103,7 +106,8 @@ class TestFileSystemDocument(unittest.TestCase):
def testZipDocumentList(self): def testZipDocumentList(self):
"""Tests if the zip file is returned correctly""" """Tests if the zip file is returned correctly"""
open(path.join(self.fsdocument.directory_name, 'document2'), 'w').write('test') with open(path.join(self.fsdocument.directory_name, 'document2'), 'w') as f:
f.write('test')
zip_file = self.fsdocument.getContent(True) zip_file = self.fsdocument.getContent(True)
mime = magic.Magic(mime=True) mime = magic.Magic(mime=True)
mimetype = mime.from_buffer(zip_file) mimetype = mime.from_buffer(zip_file)
...@@ -118,8 +122,8 @@ class TestFileSystemDocument(unittest.TestCase): ...@@ -118,8 +122,8 @@ class TestFileSystemDocument(unittest.TestCase):
def testSendZipFile(self): def testSendZipFile(self):
"""Tests if the htm is extrated from zipfile""" """Tests if the htm is extrated from zipfile"""
zip_input_url = 'data/test.zip' with open('./data/test.zip', 'rb') as f:
data = open(zip_input_url).read() data = f.read()
zipdocument = FileSystemDocument(self.tmp_url, data, 'zip') zipdocument = FileSystemDocument(self.tmp_url, data, 'zip')
mime = magic.Magic(mime=True) mime = magic.Magic(mime=True)
mimetype = mime.from_buffer(zipdocument.getContent(True)) mimetype = mime.from_buffer(zipdocument.getContent(True))
......
...@@ -39,7 +39,8 @@ from cloudooo.handler.ooo.granulator import OOGranulator ...@@ -39,7 +39,8 @@ from cloudooo.handler.ooo.granulator import OOGranulator
class TestOOGranulator(HandlerTestCase): class TestOOGranulator(HandlerTestCase):
def setUp(self): def setUp(self):
data = open('./data/granulate_test.odt').read() with open('./data/granulate_test.odt', 'rb') as f:
data = f.read()
self.oogranulator = OOGranulator(data, 'odt') self.oogranulator = OOGranulator(data, 'odt')
def testOdfWithoutContentXml(self): def testOdfWithoutContentXml(self):
...@@ -56,7 +57,8 @@ class TestOOGranulator(HandlerTestCase): ...@@ -56,7 +57,8 @@ class TestOOGranulator(HandlerTestCase):
def testgetTableItemList(self): def testgetTableItemList(self):
"""Test if getTableItemList() returns the right tables list""" """Test if getTableItemList() returns the right tables list"""
data = open('./data/granulate_table_test.odt').read() with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
table_list = [('Developers', ''), table_list = [('Developers', ''),
('Prices', 'Table 1: Prices table from Mon Restaurant'), ('Prices', 'Table 1: Prices table from Mon Restaurant'),
...@@ -65,7 +67,8 @@ class TestOOGranulator(HandlerTestCase): ...@@ -65,7 +67,8 @@ class TestOOGranulator(HandlerTestCase):
def testGetTable(self): def testGetTable(self):
"""Test if getTable() returns on odf file with the right table""" """Test if getTable() returns on odf file with the right table"""
data = open('./data/granulate_table_test.odt').read() with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
table_data_doc = oogranulator.getTable('Developers') table_data_doc = oogranulator.getTable('Developers')
content_xml_str = ZipFile(StringIO(table_data_doc)).read('content.xml') content_xml_str = ZipFile(StringIO(table_data_doc)).read('content.xml')
...@@ -79,21 +82,24 @@ class TestOOGranulator(HandlerTestCase): ...@@ -79,21 +82,24 @@ class TestOOGranulator(HandlerTestCase):
def testGetTableItemWithoutSuccess(self): def testGetTableItemWithoutSuccess(self):
"""Test if getTable() returns None for an non existent table name""" """Test if getTable() returns None for an non existent table name"""
data = open('./data/granulate_table_test.odt').read() with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
table_data = oogranulator.getTable('NonExistentTable') table_data = oogranulator.getTable('NonExistentTable')
self.assertEqual(table_data, None) self.assertEqual(table_data, None)
def testGetColumnItemList(self): def testGetColumnItemList(self):
"""Test if getColumnItemList() returns the right table columns list""" """Test if getColumnItemList() returns the right table columns list"""
data = open('./data/granulate_table_test.odt').read() with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
self.assertEqual([[0, 'Name'], [1, 'Country']], self.assertEqual([[0, 'Name'], [1, 'Country']],
oogranulator.getColumnItemList('SoccerTeams')) oogranulator.getColumnItemList('SoccerTeams'))
def testGetLineItemList(self): def testGetLineItemList(self):
"""Test if getLineItemList() returns the right table lines list""" """Test if getLineItemList() returns the right table lines list"""
data = open('./data/granulate_table_test.odt').read() with open('./data/granulate_table_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
matrix = [['Name', 'Phone', 'Email'], matrix = [['Name', 'Phone', 'Email'],
['Hugo', '+55 (22) 8888-8888', 'hugomaia@tiolive.com'], ['Hugo', '+55 (22) 8888-8888', 'hugomaia@tiolive.com'],
...@@ -121,7 +127,8 @@ class TestOOGranulator(HandlerTestCase): ...@@ -121,7 +127,8 @@ class TestOOGranulator(HandlerTestCase):
def testGetImageSuccessfully(self): def testGetImageSuccessfully(self):
"""Test if getImage() returns the right image file successfully""" """Test if getImage() returns the right image file successfully"""
data = open('./data/granulate_test.odt').read() with open('./data/granulate_test.odt', 'rb') as f:
data = f.read()
zip = ZipFile(StringIO(data)) zip = ZipFile(StringIO(data))
image_id = '10000000000000C80000009C38276C51.jpg' image_id = '10000000000000C80000009C38276C51.jpg'
original_image = zip.read('Pictures/%s' % image_id) original_image = zip.read('Pictures/%s' % image_id)
...@@ -131,13 +138,14 @@ class TestOOGranulator(HandlerTestCase): ...@@ -131,13 +138,14 @@ class TestOOGranulator(HandlerTestCase):
def testGetImageWithoutSuccess(self): def testGetImageWithoutSuccess(self):
"""Test if getImage() returns an empty string for not existent id""" """Test if getImage() returns an empty string for not existent id"""
obtained_image = self.oogranulator.getImage('anything.png') obtained_image = self.oogranulator.getImage('anything.png')
self.assertEqual('', obtained_image) self.assertEqual(b'', obtained_image)
def testGetParagraphItemList(self): def testGetParagraphItemList(self):
"""Test if getParagraphItemList() returns the right paragraphs list, with """Test if getParagraphItemList() returns the right paragraphs list, with
the ids always in the same order""" the ids always in the same order"""
for i in range(5): for _ in range(5):
data = open('./data/granulate_test.odt').read() with open('./data/granulate_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
paragraph_list = oogranulator.getParagraphItemList() paragraph_list = oogranulator.getParagraphItemList()
self.assertEqual((0, 'P3'), paragraph_list[0]) self.assertEqual((0, 'P3'), paragraph_list[0])
...@@ -162,7 +170,8 @@ class TestOOGranulator(HandlerTestCase): ...@@ -162,7 +170,8 @@ class TestOOGranulator(HandlerTestCase):
def testGetChapterItemList(self): def testGetChapterItemList(self):
"""Test if getChapterItemList() returns the right chapters list""" """Test if getChapterItemList() returns the right chapters list"""
data = open('./data/granulate_chapters_test.odt').read() with open('./data/granulate_chapters_test.odt', 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
self.assertEqual([(0, 'Title 0'), (1, 'Title 1'), (2, 'Title 2'), self.assertEqual([(0, 'Title 0'), (1, 'Title 1'), (2, 'Title 2'),
(3, 'Title 3'), (4, 'Title 4'), (5, 'Title 5'), (3, 'Title 3'), (4, 'Title 4'), (5, 'Title 5'),
...@@ -172,7 +181,8 @@ class TestOOGranulator(HandlerTestCase): ...@@ -172,7 +181,8 @@ class TestOOGranulator(HandlerTestCase):
def testGetChapterItem(self): def testGetChapterItem(self):
"""Test if getChapterItem() returns the right chapter""" """Test if getChapterItem() returns the right chapter"""
data = open("./data/granulate_chapters_test.odt").read() with open("./data/granulate_chapters_test.odt", 'rb') as f:
data = f.read()
oogranulator = OOGranulator(data, 'odt') oogranulator = OOGranulator(data, 'odt')
self.assertEqual(['Title 1', 1], oogranulator.getChapterItem(1)) self.assertEqual(['Title 1', 1], oogranulator.getChapterItem(1))
...@@ -48,9 +48,8 @@ class TestHandler(HandlerTestCase): ...@@ -48,9 +48,8 @@ class TestHandler(HandlerTestCase):
def _save_document(self, document_output_url, data): def _save_document(self, document_output_url, data):
"""Create document in file system""" """Create document in file system"""
new_file = open(document_output_url, "w") with open(document_output_url, "wb") as f:
new_file.write(data) f.write(data)
new_file.close()
self._file_path_list.append(document_output_url) self._file_path_list.append(document_output_url)
def _assert_document_output(self, document, expected_mimetype): def _assert_document_output(self, document, expected_mimetype):
...@@ -70,18 +69,20 @@ class TestHandler(HandlerTestCase): ...@@ -70,18 +69,20 @@ class TestHandler(HandlerTestCase):
def testConvertOdtToDoc(self): def testConvertOdtToDoc(self):
"""Test convert ODT to DOC""" """Test convert ODT to DOC"""
data = encodestring(open("data/test.odt").read()) with open("data/test.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'odt') 'odt')
doc_exported = handler.convert("doc") doc_exported = handler.convert("doc")
self._assert_document_output(doc_exported, "application/msword") self._assert_document_output(doc_exported, "application/msword")
def testConvertDocToOdt(self): def testConvertDocToOdt(self):
"""Test convert DOC to ODT""" """Test convert DOC to ODT"""
data = encodestring(open("data/test.doc").read()) with open("data/test.doc", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'doc') 'doc')
doc_exported = handler.convert("odt") doc_exported = handler.convert("odt")
self._assert_document_output(doc_exported, self._assert_document_output(doc_exported,
...@@ -89,9 +90,10 @@ class TestHandler(HandlerTestCase): ...@@ -89,9 +90,10 @@ class TestHandler(HandlerTestCase):
def testGetMetadata(self): def testGetMetadata(self):
"""Test getMetadata""" """Test getMetadata"""
data = encodestring(open("data/test.odt").read()) with open("data/test.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'odt') 'odt')
metadata = handler.getMetadata() metadata = handler.getMetadata()
self.assertEqual(metadata.get('MIMEType'), self.assertEqual(metadata.get('MIMEType'),
...@@ -102,9 +104,10 @@ class TestHandler(HandlerTestCase): ...@@ -102,9 +104,10 @@ class TestHandler(HandlerTestCase):
def testSetMetadata(self): def testSetMetadata(self):
"""Test setMetadata""" """Test setMetadata"""
data = encodestring(open("data/test.odt").read()) with open("data/test.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'odt') 'odt')
new_data = handler.setMetadata({"Title": "cloudooo Test -"}) new_data = handler.setMetadata({"Title": "cloudooo Test -"})
new_handler = Handler(self.tmp_url, new_handler = Handler(self.tmp_url,
...@@ -113,7 +116,7 @@ class TestHandler(HandlerTestCase): ...@@ -113,7 +116,7 @@ class TestHandler(HandlerTestCase):
metadata = new_handler.getMetadata() metadata = new_handler.getMetadata()
self.assertEqual(metadata.get('Title'), "cloudooo Test -") self.assertEqual(metadata.get('Title'), "cloudooo Test -")
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'odt') 'odt')
new_data = handler.setMetadata({"Title": "Namie's working record"}) new_data = handler.setMetadata({"Title": "Namie's working record"})
new_handler = Handler(self.tmp_url, new_handler = Handler(self.tmp_url,
...@@ -125,9 +128,10 @@ class TestHandler(HandlerTestCase): ...@@ -125,9 +128,10 @@ class TestHandler(HandlerTestCase):
def testConvertWithOpenOfficeStopped(self): def testConvertWithOpenOfficeStopped(self):
"""Test convert with openoffice stopped""" """Test convert with openoffice stopped"""
openoffice.stop() openoffice.stop()
data = encodestring(open("data/test.doc").read()) with open("data/test.doc", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'doc') 'doc')
doc_exported = handler.convert("odt") doc_exported = handler.convert("odt")
self._assert_document_output(doc_exported, self._assert_document_output(doc_exported,
...@@ -136,9 +140,10 @@ class TestHandler(HandlerTestCase): ...@@ -136,9 +140,10 @@ class TestHandler(HandlerTestCase):
def testGetMetadataWithOpenOfficeStopped(self): def testGetMetadataWithOpenOfficeStopped(self):
"""Test getMetadata with openoffice stopped""" """Test getMetadata with openoffice stopped"""
openoffice.stop() openoffice.stop()
data = encodestring(open("data/test.odt").read()) with open("data/test.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'odt') 'odt')
metadata = handler.getMetadata() metadata = handler.getMetadata()
self.assertEqual(metadata.get('Title'), 'title') self.assertEqual(metadata.get('Title'), 'title')
...@@ -148,9 +153,10 @@ class TestHandler(HandlerTestCase): ...@@ -148,9 +153,10 @@ class TestHandler(HandlerTestCase):
def testSetMetadataWithOpenOfficeStopped(self): def testSetMetadataWithOpenOfficeStopped(self):
"""Test setMetadata with openoffice stopped""" """Test setMetadata with openoffice stopped"""
openoffice.stop() openoffice.stop()
data = encodestring(open("data/test.doc").read()) with open("data/test.doc", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'doc') 'doc')
new_data = handler.setMetadata({"Title": "cloudooo Test -"}) new_data = handler.setMetadata({"Title": "cloudooo Test -"})
new_handler = Handler(self.tmp_url, new_handler = Handler(self.tmp_url,
...@@ -162,9 +168,10 @@ class TestHandler(HandlerTestCase): ...@@ -162,9 +168,10 @@ class TestHandler(HandlerTestCase):
def testRefreshOdt(self): def testRefreshOdt(self):
"""Test refresh argument""" """Test refresh argument"""
# Check when refreshing is disabled # Check when refreshing is disabled
data = encodestring(open("data/test_fields.odt").read()) with open("data/test_fields.odt", "rb") as f:
data = f.read()
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'odt', 'odt',
refresh=False) refresh=False)
doc_exported = handler.convert("odt") doc_exported = handler.convert("odt")
...@@ -176,9 +183,8 @@ class TestHandler(HandlerTestCase): ...@@ -176,9 +183,8 @@ class TestHandler(HandlerTestCase):
namespaces=content_tree.nsmap)) namespaces=content_tree.nsmap))
# Check when refreshing is enabled # Check when refreshing is enabled
data = encodestring(open("data/test_fields.odt").read())
handler = Handler(self.tmp_url, handler = Handler(self.tmp_url,
decodestring(data), data,
'odt', 'odt',
refresh=True) refresh=True)
doc_exported = handler.convert("odt") doc_exported = handler.convert("odt")
......
...@@ -37,7 +37,8 @@ from cloudooo.handler.ooo.document import OdfDocument ...@@ -37,7 +37,8 @@ from cloudooo.handler.ooo.document import OdfDocument
class TestOdfDocument(HandlerTestCase): class TestOdfDocument(HandlerTestCase):
def setUp(self): def setUp(self):
data = open('./data/granulate_test.odt').read() with open('./data/granulate_test.odt', 'rb') as f:
data = f.read()
self.oodocument = OdfDocument(data, 'odt') self.oodocument = OdfDocument(data, 'odt')
def testReceivedGoodFile(self): def testReceivedGoodFile(self):
...@@ -47,7 +48,7 @@ class TestOdfDocument(HandlerTestCase): ...@@ -47,7 +48,7 @@ class TestOdfDocument(HandlerTestCase):
def testGetContentXml(self): def testGetContentXml(self):
"""Test if the getContentXml method returns the content.xml file""" """Test if the getContentXml method returns the content.xml file"""
content_xml = self.oodocument.getContentXml() content_xml = self.oodocument.getContentXml()
self.assertTrue('The content of this file is just' in content_xml) self.assertIn(b'The content of this file is just', content_xml)
def testGetExistentFile(self): def testGetExistentFile(self):
"""Test if the getFile method returns the requested file""" """Test if the getFile method returns the requested file"""
......
This diff is collapsed.
...@@ -50,7 +50,8 @@ class TestUnoConverter(HandlerTestCase): ...@@ -50,7 +50,8 @@ class TestUnoConverter(HandlerTestCase):
""" """ """ """
openoffice.acquire() openoffice.acquire()
self.hostname, self.port = openoffice.getAddress() self.hostname, self.port = openoffice.getAddress()
data = open("data/test.odt", 'r').read() with open("data/test.odt", "rb") as f:
data = f.read()
self.document = FileSystemDocument(self.tmp_url, data, 'odt') self.document = FileSystemDocument(self.tmp_url, data, 'odt')
def tearDown(self): def tearDown(self):
......
...@@ -41,14 +41,16 @@ class TestHandler(HandlerTestCase): ...@@ -41,14 +41,16 @@ class TestHandler(HandlerTestCase):
def testConvertPDFtoText(self): def testConvertPDFtoText(self):
"""Test conversion of pdf to txt""" """Test conversion of pdf to txt"""
pdf_document = open("data/test.pdf").read() with open("data/test.pdf", "rb") as f:
pdf_document = f.read()
handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw) handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw)
txt_document = handler.convert("txt") txt_document = handler.convert("txt")
self.assertTrue(txt_document.startswith("UNG Docs Architecture")) self.assertTrue(txt_document.startswith(b"UNG Docs Architecture"))
def testgetMetadata(self): def testgetMetadata(self):
"""Test if the metadata are extracted correctly""" """Test if the metadata are extracted correctly"""
pdf_document = open("data/test.pdf").read() with open("data/test.pdf", "rb") as f:
pdf_document = f.read()
handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw) handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw)
metadata = handler.getMetadata() metadata = handler.getMetadata()
self.assertEqual(type(metadata), DictType) self.assertEqual(type(metadata), DictType)
...@@ -57,7 +59,8 @@ class TestHandler(HandlerTestCase): ...@@ -57,7 +59,8 @@ class TestHandler(HandlerTestCase):
def testsetMetadata(self): def testsetMetadata(self):
"""Test if the metadata is inserted correctly""" """Test if the metadata is inserted correctly"""
pdf_document = open("data/test.pdf").read() with open("data/test.pdf", "rb") as f:
pdf_document = f.read()
handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw) handler = Handler(self.tmp_url, pdf_document, "pdf", **self.kw)
metadata_dict = {"title": "Set Metadata Test", "creator": "gabriel\'@"} metadata_dict = {"title": "Set Metadata Test", "creator": "gabriel\'@"}
new_document = handler.setMetadata(metadata_dict) new_document = handler.setMetadata(metadata_dict)
......
...@@ -45,14 +45,17 @@ class TestServer(TestCase): ...@@ -45,14 +45,17 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList()) self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self): def FaultConversionScenarioList(self):
return [ scenario_list = [
# Test to verify if server fail when a empty string is sent # Test to verify if server fail when a empty file is sent
('', '', ''), (b'', '', ''),
]
# Try convert one video for a invalid format # Try convert one video for a invalid format
(open(join('data', 'test.pdf')).read(), 'pdf', 'xyz'), with open(join('data', 'test.pdf'), 'rb') as f:
scenario_list.append((f.read(), 'pdf', 'xyz'))
# Try convert one video to format not possible # Try convert one video to format not possible
(open(join('data', 'test.pdf')).read(), 'pdf', 'ogv'), with open(join('data', 'test.pdf'), 'rb') as f:
] scenario_list.append((f.read(), 'pdf', 'ogv'))
return scenario_list
def testFaultConversion(self): def testFaultConversion(self):
"""Test fail convertion of Invalid pdf files""" """Test fail convertion of Invalid pdf files"""
......
...@@ -40,7 +40,8 @@ class TestHandler(HandlerTestCase): ...@@ -40,7 +40,8 @@ class TestHandler(HandlerTestCase):
self.kw = dict(env=dict(PATH=self.env_path)) self.kw = dict(env=dict(PATH=self.env_path))
def _testBase(self, html_path, **conversion_kw): def _testBase(self, html_path, **conversion_kw):
html_file = open(html_path).read() with open(html_path, 'rb') as f:
html_file = f.read()
handler = Handler(self.tmp_url, html_file, "html", **self.kw) handler = Handler(self.tmp_url, html_file, "html", **self.kw)
pdf_file = handler.convert("pdf", **conversion_kw) pdf_file = handler.convert("pdf", **conversion_kw)
mime = magic.Magic(mime=True) mime = magic.Magic(mime=True)
...@@ -67,16 +68,18 @@ class TestHandler(HandlerTestCase): ...@@ -67,16 +68,18 @@ class TestHandler(HandlerTestCase):
def testConvertHtmlWithTableOfContent(self): def testConvertHtmlWithTableOfContent(self):
"""Test conversion of html with an additional table of content""" """Test conversion of html with an additional table of content"""
with open("data/test_toc.xsl", 'rb') as f:
xsl_style_sheet_data = f.read()
self._testBase( self._testBase(
"data/test_with_toc.html", "data/test_with_toc.html",
toc=True, toc=True,
xsl_style_sheet_data=b64encode(open("data/test_toc.xsl").read()), xsl_style_sheet_data=b64encode(xsl_style_sheet_data),
) )
# XXX how to check for table of content presence ? # XXX how to check for table of content presence ?
def testsetMetadata(self): def testsetMetadata(self):
""" Test if metadata are inserted correclty """ """ Test if metadata are inserted correclty """
handler = Handler(self.tmp_url, "", "png", **self.kw) handler = Handler(self.tmp_url, b"", "png", **self.kw)
self.assertRaises(NotImplementedError, handler.setMetadata) self.assertRaises(NotImplementedError, handler.setMetadata)
def testGetAllowedConversionFormatList(self): def testGetAllowedConversionFormatList(self):
......
...@@ -44,10 +44,11 @@ class TestServer(TestCase): ...@@ -44,10 +44,11 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList()) self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self): def FaultConversionScenarioList(self):
return [ scenario_list = [
# Test to verify if server fail when a empty string is sent # Test to verify if server fail when a empty file is sent
('', '', ''), (b'', '', ''),
# Try convert one html for a invalid format
(open(join('data', 'test_with_png_dataurl.html')).read(), 'html', 'xyz'),
] ]
# Try convert one html for a invalid format
with open(join('data', 'test_with_png_dataurl.html'), 'rb') as f:
scenario_list.append((f.read(), 'html', 'xyz'))
return scenario_list
...@@ -321,7 +321,8 @@ class Handler(object): ...@@ -321,7 +321,8 @@ class Handler(object):
absolute_path = os.path.join(root, file_name) absolute_path = os.path.join(root, file_name)
file_name = os.path.join(relative_root, file_name) file_name = os.path.join(relative_root, file_name)
zipfile.write(absolute_path, file_name) zipfile.write(absolute_path, file_name)
output_data = open(output_file_name).read() with open(output_file_name, 'rb') as f:
output_data = f.read()
finally: finally:
os.unlink(output_file_name) os.unlink(output_file_name)
return output_data return output_data
......
...@@ -52,12 +52,14 @@ class TestServer(TestCase): ...@@ -52,12 +52,14 @@ class TestServer(TestCase):
self.runConversionList(self.ConversionScenarioList()) self.runConversionList(self.ConversionScenarioList())
def FaultConversionScenarioList(self): def FaultConversionScenarioList(self):
return [ scenario_list = [
# Test to verify if server fail when a empty string is sent # Test to verify if server fail when a empty file is sent
('', '', ''), (b'', '', ''),
# Try convert one xlsx for a invalid format
(open(join('data', 'test.xlsx')).read(), 'xlsx', 'xyz'),
] ]
# Try convert one xlsx for a invalid format
with open(join('data', 'test.xlsx'), 'rb') as f:
scenario_list.append((f.read(), 'xlsx', 'xyz'))
return scenario_list
def test_xlsx_to_xlsy(self): def test_xlsx_to_xlsy(self):
with open(join('data', 'test.xlsx')) as f: with open(join('data', 'test.xlsx')) as f:
......
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