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 ]
(open(join('data', 'test.ogv')).read(), 'ogv', 'xyz'), # Try convert one video for a invalid format
# Try convert one video to format not possible with open(join('data', 'test.ogv'), 'rb') as f:
(open(join('data', 'test.ogv')).read(), 'ogv', 'moov'), scenario_list.append((f.read(), 'ogv', 'xyz'))
] # Try convert one video to format not possible
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 png for a invalid format
# Try convert one video to format not possible with open(join('data', 'test.png'), 'rb') as f:
(open(join('data', 'test.png')).read(), 'png', '8bim'), 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"""
......
...@@ -100,14 +100,17 @@ class TestConversion(TestCase): ...@@ -100,14 +100,17 @@ class TestConversion(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 document for a invalid format ]
(open(join('data', 'test.doc')).read(), 'doc', 'xyz'), # Try convert one document to an invalid format
# Try convert one document to format not possible with open(join('data', 'test.doc'), 'rb') as f:
(open(join('data', 'test.odp')).read(), 'odp', 'doc'), scenario_list.append((f.read(), 'doc', 'xyz'))
] # Try convert one video to format not possible
with open(join('data', 'test.odp'), 'rb') as f:
scenario_list.append((f.read(), 'odp', 'doc'))
return scenario_list
def testFaultConversion(self): def testFaultConversion(self):
"""Test fail convertion of Invalid OOofiles""" """Test fail convertion of Invalid OOofiles"""
...@@ -128,16 +131,20 @@ class TestConversion(TestCase): ...@@ -128,16 +131,20 @@ class TestConversion(TestCase):
]) ])
def ConvertScenarioList(self): def ConvertScenarioList(self):
return [ scenario_list = []
# Test run_convert method # Test run_convert method
('test.doc', open(join('data', 'test.doc')).read(), 200, '', with open(join('data', 'test.doc'), 'rb') as f:
['data', 'meta', 'mime'], '', 'application/vnd.oasis.opendocument.text' scenario_list.append(
), ('test.doc', f.read(), 200, '',
# Test run_convert method with invalid file ['data', 'meta', 'mime'], '', 'application/vnd.oasis.opendocument.text'
('test.doc', open(join('data', 'test.doc')).read()[:30], 200, '', ))
['data', 'meta', 'mime'], '', 'application/vnd.oasis.opendocument.text' # Test run_convert method with invalid file
), with open(join('data', 'test.doc'), 'rb') as f:
] scenario_list.append(
('test.doc', f.read()[:300], 200, '',
['data', 'meta', 'mime'], '', 'application/vnd.oasis.opendocument.text'
))
return scenario_list
def testRunConvertMethod(self): def testRunConvertMethod(self):
"""Test run_convert method""" """Test run_convert method"""
...@@ -211,7 +218,8 @@ class TestGetMetadata(TestCase): ...@@ -211,7 +218,8 @@ class TestGetMetadata(TestCase):
# and the support to this kind of tests will be dropped. # and the support to this kind of tests will be dropped.
def testRunGenerateMethod(self): def testRunGenerateMethod(self):
"""Test run_generate method""" """Test run_generate method"""
data = open(join('data', 'test.odt'), 'r').read() with open(join('data', 'test.odt'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test.odt', generate_result = self.proxy.run_generate('test.odt',
encodestring(data), encodestring(data),
None, 'pdf', None, 'pdf',
...@@ -229,9 +237,10 @@ class TestGenerate(TestCase): ...@@ -229,9 +237,10 @@ class TestGenerate(TestCase):
def testRunGenerateMethodConvertOdsToHTML(self): def testRunGenerateMethodConvertOdsToHTML(self):
"""Test run_generate method from ods to html. This test is to validate """Test run_generate method from ods to html. This test is to validate
a bug convertions to html""" a bug convertions to html"""
with open(join('data', 'test.ods'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test.ods', generate_result = self.proxy.run_generate('test.ods',
encodestring( encodestring(data),
open(join('data', 'test.ods')).read()),
None, 'html', None, 'html',
"application/vnd.oasis.opendocument.spreadsheet") "application/vnd.oasis.opendocument.spreadsheet")
response_code, response_dict, response_message = generate_result response_code, response_dict, response_message = generate_result
...@@ -240,7 +249,8 @@ class TestGenerate(TestCase): ...@@ -240,7 +249,8 @@ class TestGenerate(TestCase):
self.assertNotEqual(response_dict['data'], '') self.assertNotEqual(response_dict['data'], '')
self.assertEqual(response_dict['mime'], 'application/zip') self.assertEqual(response_dict['mime'], 'application/zip')
output_url = join(self.tmp_url, "zip.zip") output_url = join(self.tmp_url, "zip.zip")
open(output_url, 'w').write(decodestring(response_dict['data'])) with open(output_url, 'wb') as f:
f.write(decodestring(response_dict['data']))
self.assertTrue(is_zipfile(output_url)) self.assertTrue(is_zipfile(output_url))
filename_list = [file.filename for file in ZipFile(output_url).filelist] filename_list = [file.filename for file in ZipFile(output_url).filelist]
for filename in filename_list: for filename in filename_list:
...@@ -254,9 +264,10 @@ class TestGenerate(TestCase): ...@@ -254,9 +264,10 @@ class TestGenerate(TestCase):
def testRunGenerateMethodConvertOdsToMsXslx(self): def testRunGenerateMethodConvertOdsToMsXslx(self):
"""Test run_generate method from ods to ms.xlsx. This test is to validate """Test run_generate method from ods to ms.xlsx. This test is to validate
a bug convertions to html""" a bug convertions to html"""
with open(join('data', 'test.ods'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test.ods', generate_result = self.proxy.run_generate('test.ods',
encodestring( encodestring(data),
open(join('data', 'test.ods')).read()),
None, 'ms.xlsx', None, 'ms.xlsx',
"application/vnd.oasis.opendocument.spreadsheet") "application/vnd.oasis.opendocument.spreadsheet")
response_code, response_dict, response_message = generate_result response_code, response_dict, response_message = generate_result
...@@ -270,9 +281,10 @@ class TestGenerate(TestCase): ...@@ -270,9 +281,10 @@ class TestGenerate(TestCase):
def testPNGFileToConvertOdpToHTML(self): def testPNGFileToConvertOdpToHTML(self):
"""Test run_generate method from odp with png to html. """Test run_generate method from odp with png to html.
This test if returns good png files""" This test if returns good png files"""
with open(join('data', 'test_png.odp'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test_png.odp', generate_result = self.proxy.run_generate('test_png.odp',
encodestring( encodestring(data),
open(join('data', 'test_png.odp')).read()),
None, 'html', None, 'html',
'application/vnd.oasis.opendocument.presentation') 'application/vnd.oasis.opendocument.presentation')
response_code, response_dict, response_message = generate_result response_code, response_dict, response_message = generate_result
...@@ -281,16 +293,18 @@ class TestGenerate(TestCase): ...@@ -281,16 +293,18 @@ class TestGenerate(TestCase):
self.assertNotEqual(response_dict['data'], '') self.assertNotEqual(response_dict['data'], '')
self.assertEqual(response_dict['mime'], 'application/zip') self.assertEqual(response_dict['mime'], 'application/zip')
output_url = join(self.tmp_url, "zip.zip") output_url = join(self.tmp_url, "zip.zip")
open(output_url, 'w').write(decodestring(response_dict['data'])) with open(output_url, 'wb') as f:
f.write(decodestring(response_dict['data']))
self.assertTrue(is_zipfile(output_url)) self.assertTrue(is_zipfile(output_url))
zipfile = ZipFile(output_url) zipfile = ZipFile(output_url)
try: try:
png_path = join(self.tmp_url, "img0.png") png_path = join(self.tmp_url, "img0.png")
zipfile.extractall(self.tmp_url) zipfile.extractall(self.tmp_url)
content_type = self._getFileType(encodestring(open(png_path).read())) with open(png_path, 'rb') as f:
content_type = self._getFileType(encodestring(f.read()))
self.assertEqual(content_type, 'image/png') self.assertEqual(content_type, 'image/png')
m = magic.Magic() m = magic.Magic()
self.assertTrue("8-bit/color RGB" in m.from_file(png_path)) self.assertIn("8-bit/color RGB", m.from_file(png_path))
finally: finally:
zipfile.close() zipfile.close()
if exists(output_url): if exists(output_url):
...@@ -301,9 +315,10 @@ class TestGenerate(TestCase): ...@@ -301,9 +315,10 @@ class TestGenerate(TestCase):
def testRunGenerateMethodConvertOdpToHTML(self): def testRunGenerateMethodConvertOdpToHTML(self):
"""Test run_generate method from odp to html. This test is to validate """Test run_generate method from odp to html. This test is to validate
a bug convertions to html""" a bug convertions to html"""
with open(join('data', 'test.odp'), 'rb') as f:
data = f.read()
generate_result = self.proxy.run_generate('test.odp', generate_result = self.proxy.run_generate('test.odp',
encodestring( encodestring(data),
open(join('data', 'test.odp')).read()),
None, 'html', None, 'html',
'application/vnd.oasis.opendocument.presentation') 'application/vnd.oasis.opendocument.presentation')
response_code, response_dict, response_message = generate_result response_code, response_dict, response_message = generate_result
...@@ -312,7 +327,8 @@ class TestGenerate(TestCase): ...@@ -312,7 +327,8 @@ class TestGenerate(TestCase):
self.assertNotEqual(response_dict['data'], '') self.assertNotEqual(response_dict['data'], '')
self.assertEqual(response_dict['mime'], 'application/zip') self.assertEqual(response_dict['mime'], 'application/zip')
output_url = join(self.tmp_url, "zip.zip") output_url = join(self.tmp_url, "zip.zip")
open(output_url, 'w').write(decodestring(response_dict['data'])) with open(output_url, 'wb') as f:
f.write(decodestring(response_dict['data'].encode()))
self.assertTrue(is_zipfile(output_url)) self.assertTrue(is_zipfile(output_url))
filename_list = [file.filename for file in ZipFile(output_url).filelist] filename_list = [file.filename for file in ZipFile(output_url).filelist]
for filename in filename_list: for filename in filename_list:
...@@ -343,9 +359,10 @@ class TestGenerate(TestCase): ...@@ -343,9 +359,10 @@ class TestGenerate(TestCase):
class TestSetMetadata(TestCase): class TestSetMetadata(TestCase):
def testRunSetMetadata(self): def testRunSetMetadata(self):
"""Test run_setmetadata method, updating the same metadata""" """Test run_setmetadata method, updating the same metadata"""
with open(join('data', 'testMetadata.odt'), 'rb') as f:
data = f.read()
setmetadata_result = self.proxy.run_setmetadata('testMetadata.odt', setmetadata_result = self.proxy.run_setmetadata('testMetadata.odt',
encodestring( encodestring(data),
open(join('data', 'testMetadata.odt')).read()),
{"Title": "testSetMetadata", "Description": "Music"}) {"Title": "testSetMetadata", "Description": "Music"})
response_code, response_dict, response_message = setmetadata_result response_code, response_dict, response_message = setmetadata_result
self.assertEqual(response_code, 200) self.assertEqual(response_code, 200)
...@@ -372,9 +389,10 @@ class TestSetMetadata(TestCase): ...@@ -372,9 +389,10 @@ class TestSetMetadata(TestCase):
def testRunSetMetadataFailResponse(self): def testRunSetMetadataFailResponse(self):
"""Test run_setmetadata method with invalid document""" """Test run_setmetadata method with invalid document"""
with open(join('data', 'testMetadata.odt'), 'rb') as f:
data = f.read()[:100]
setmetadata_result = self.proxy.run_setmetadata('testMetadata.odt', setmetadata_result = self.proxy.run_setmetadata('testMetadata.odt',
encodestring( encodestring(data),
open(join('data', 'testMetadata.odt')).read()[:100]),
{"Title": "testSetMetadata", "Description": "Music"}) {"Title": "testSetMetadata", "Description": "Music"})
response_code, response_dict, response_message = setmetadata_result response_code, response_dict, response_message = setmetadata_result
self.assertEqual(response_code, 402) self.assertEqual(response_code, 402)
...@@ -403,8 +421,10 @@ class TestGetTableItemList(TestCase): ...@@ -403,8 +421,10 @@ class TestGetTableItemList(TestCase):
table_list = [['Developers', ''], table_list = [['Developers', ''],
['Prices', 'Table 1: Prices table from Mon Restaurant'], ['Prices', 'Table 1: Prices table from Mon Restaurant'],
['SoccerTeams', 'Tabela 2: Soccer Teams']] ['SoccerTeams', 'Tabela 2: Soccer Teams']]
with open("data/granulate_table_test.odt", "rb") as f:
data = f.read()
granulated_table = self.proxy.getTableItemList( granulated_table = self.proxy.getTableItemList(
encodestring(open("data/granulate_table_test.odt").read()), encodestring(data),
"odt") "odt")
self.assertEqual(table_list, granulated_table) self.assertEqual(table_list, granulated_table)
...@@ -413,14 +433,17 @@ class TestGetTableItemList(TestCase): ...@@ -413,14 +433,17 @@ class TestGetTableItemList(TestCase):
table_list = [['Table1', ''], table_list = [['Table1', ''],
['Table2', 'Table 1: Prices table from Mon Restaurant'], ['Table2', 'Table 1: Prices table from Mon Restaurant'],
['Table3', 'Tabela 2: Soccer Teams']] ['Table3', 'Tabela 2: Soccer Teams']]
with open("data/granulate_table_test.doc", "rb") as f:
data = f.read()
granulated_table = self.proxy.getTableItemList( granulated_table = self.proxy.getTableItemList(
encodestring(open("data/granulate_table_test.doc").read()), encodestring(data),
"doc") "doc")
self.assertEqual(table_list, granulated_table) self.assertEqual(table_list, granulated_table)
def testGetTableFromOdt(self): def testGetTableFromOdt(self):
"""Test if getTableItemList can get a item of some granulated table from odt file""" """Test if getTableItemList can get a item of some granulated table from odt file"""
data = encodestring(open("./data/granulate_table_test.odt").read()) with open("./data/granulate_table_test.odt", "rb") as f:
data = encodestring(f.read())
granulated_table = self.proxy.getTableItemList(data, "odt") granulated_table = self.proxy.getTableItemList(data, "odt")
table_item = decodestring(self.proxy.getTable(data, granulated_table[1][0], table_item = decodestring(self.proxy.getTable(data, granulated_table[1][0],
"odt")) "odt"))
...@@ -435,7 +458,8 @@ class TestGetTableItemList(TestCase): ...@@ -435,7 +458,8 @@ class TestGetTableItemList(TestCase):
def testGetTableFromDoc(self): def testGetTableFromDoc(self):
"""Test if getTableItemList can get a item of some granulated table from doc file""" """Test if getTableItemList can get a item of some granulated table from doc file"""
data = encodestring(open("./data/granulate_table_test.doc").read()) with open("./data/granulate_table_test.odt", "rb") as f:
data = encodestring(f.read())
granulated_table = self.proxy.getTableItemList(data, "doc") granulated_table = self.proxy.getTableItemList(data, "doc")
self.proxy.getTable(data, granulated_table[1][0], "doc") self.proxy.getTable(data, granulated_table[1][0], "doc")
table_item = decodestring(self.proxy.getTable(data, granulated_table[1][0], table_item = decodestring(self.proxy.getTable(data, granulated_table[1][0],
...@@ -451,24 +475,29 @@ class TestGetTableItemList(TestCase): ...@@ -451,24 +475,29 @@ class TestGetTableItemList(TestCase):
def testGetColumnItemListFromOdt(self): def testGetColumnItemListFromOdt(self):
"""Test if getColumnItemList can get the list of column item from odt file""" """Test if getColumnItemList can get the list of column item from odt file"""
with open("./data/granulate_table_test.odt", "rb") as f:
data = encodestring(f.read())
columns = self.proxy.getColumnItemList( columns = self.proxy.getColumnItemList(
encodestring(open("./data/granulate_table_test.odt").read()), data,
"SoccerTeams", "SoccerTeams",
"odt") "odt")
self.assertEqual([[0, 'Name'], [1, 'Country']], columns) self.assertEqual([[0, 'Name'], [1, 'Country']], columns)
def testGetColumnItemListFromDoc(self): def testGetColumnItemListFromDoc(self):
"""Test if getColumnItemList can get the list of column item from doc file""" """Test if getColumnItemList can get the list of column item from doc file"""
with open("./data/granulate_table_test.doc", "rb") as f:
data = encodestring(f.read())
#in the doc format the tables lose their names #in the doc format the tables lose their names
columns = self.proxy.getColumnItemList( columns = self.proxy.getColumnItemList(
encodestring(open("./data/granulate_table_test.doc").read()), data,
"Table3", "Table3",
"doc") "doc")
self.assertEqual([[0, 'Name'], [1, 'Country']], columns) self.assertEqual([[0, 'Name'], [1, 'Country']], columns)
def testGetLineItemListFromOdt(self): def testGetLineItemListFromOdt(self):
"""Test if getLineItemList can get the list of lines items from odt file""" """Test if getLineItemList can get the list of lines items from odt file"""
data = encodestring(open("./data/granulate_table_test.odt").read()) with open("./data/granulate_table_test.odt", "rb") as f:
data = encodestring(f.read())
line_item_list = self.proxy.getLineItemList(data, "Developers", "odt") line_item_list = self.proxy.getLineItemList(data, "Developers", "odt")
self.assertEqual([['Name', 'Hugo'], ['Phone', '+55 (22) 8888-8888'], self.assertEqual([['Name', 'Hugo'], ['Phone', '+55 (22) 8888-8888'],
['Email', 'hugomaia@tiolive.com'], ['Name', 'Rafael'], ['Email', 'hugomaia@tiolive.com'], ['Name', 'Rafael'],
...@@ -477,7 +506,8 @@ class TestGetTableItemList(TestCase): ...@@ -477,7 +506,8 @@ class TestGetTableItemList(TestCase):
def testGetLineItemListFromDoc(self): def testGetLineItemListFromDoc(self):
"""Test if getLineItemList can get the list of lines items from doc file""" """Test if getLineItemList can get the list of lines items from doc file"""
data = encodestring(open("./data/granulate_table_test.doc").read()) with open("./data/granulate_table_test.doc", "rb") as f:
data = encodestring(f.read())
line_item_list = self.proxy.getLineItemList(data, "Table1", "doc") line_item_list = self.proxy.getLineItemList(data, "Table1", "doc")
self.assertEqual([['Name', 'Hugo'], ['Phone', '+55 (22) 8888-8888'], self.assertEqual([['Name', 'Hugo'], ['Phone', '+55 (22) 8888-8888'],
['Email', 'hugomaia@tiolive.com'], ['Name', 'Rafael'], ['Email', 'hugomaia@tiolive.com'], ['Name', 'Rafael'],
...@@ -488,7 +518,8 @@ class TestGetTableItemList(TestCase): ...@@ -488,7 +518,8 @@ class TestGetTableItemList(TestCase):
class TestImagetItemList(TestCase): class TestImagetItemList(TestCase):
def testGetImageItemListFromOdt(self): def testGetImageItemListFromOdt(self):
"""Test if getImageItemList can get the list of images items from odt file""" """Test if getImageItemList can get the list of images items from odt file"""
data = encodestring(open("./data/granulate_test.odt").read()) with open("./data/granulate_test.odt", "rb") as f:
data = encodestring(f.read())
image_list = self.proxy.getImageItemList(data, "odt") image_list = self.proxy.getImageItemList(data, "odt")
self.assertEqual([['10000000000000C80000009CBF079A6E41EE290C.jpg', ''], self.assertEqual([['10000000000000C80000009CBF079A6E41EE290C.jpg', ''],
['10000201000000C80000004E85B3F70C71E07CE8.png', 'TioLive Logo'], ['10000201000000C80000004E85B3F70C71E07CE8.png', 'TioLive Logo'],
...@@ -499,7 +530,8 @@ class TestImagetItemList(TestCase): ...@@ -499,7 +530,8 @@ class TestImagetItemList(TestCase):
def testGetImageItemListFromDoc(self): def testGetImageItemListFromDoc(self):
"""Test if getImageItemList can get the list of images items from doc file""" """Test if getImageItemList can get the list of images items from doc file"""
data = encodestring(open("./data/granulate_test.doc").read()) with open("./data/granulate_test.doc", "rb") as f:
data = encodestring(f.read())
image_list = self.proxy.getImageItemList(data, "doc") image_list = self.proxy.getImageItemList(data, "doc")
self.assertEqual([['10000000000000C80000009CBF079A6E41EE290C.jpg', ''], self.assertEqual([['10000000000000C80000009CBF079A6E41EE290C.jpg', ''],
['10000201000000C80000004E85B3F70C71E07CE8.png', 'TioLive Logo'], ['10000201000000C80000004E85B3F70C71E07CE8.png', 'TioLive Logo'],
...@@ -510,7 +542,8 @@ class TestImagetItemList(TestCase): ...@@ -510,7 +542,8 @@ class TestImagetItemList(TestCase):
def testGetImageFromOdt(self): def testGetImageFromOdt(self):
"""Test if getImage can get a image from odt file after zip""" """Test if getImage can get a image from odt file after zip"""
data = encodestring(open("./data/granulate_test.odt").read()) with open("./data/granulate_test.odt", "rb") as f:
data = encodestring(f.read())
zip = ZipFile(StringIO(decodestring(data))) zip = ZipFile(StringIO(decodestring(data)))
image_id = '10000201000000C80000004E7B947D46.png' image_id = '10000201000000C80000004E7B947D46.png'
original_image = zip.read('Pictures/%s' % image_id) original_image = zip.read('Pictures/%s' % image_id)
...@@ -519,7 +552,8 @@ class TestImagetItemList(TestCase): ...@@ -519,7 +552,8 @@ class TestImagetItemList(TestCase):
def testGetImageFromDoc(self): def testGetImageFromDoc(self):
"""Test if getImage can get a image from doc file after zip""" """Test if getImage can get a image from doc file after zip"""
data = encodestring(open("./data/granulate_test.doc").read()) with open("./data/granulate_test.doc", "rb") as f:
data = encodestring(f.read())
#This conversion is necessary to catch the image from the doc file; #This conversion is necessary to catch the image from the doc file;
#so compare with the server return. #so compare with the server return.
data_odt = self.proxy.convertFile(data, 'doc', 'odt', False) data_odt = self.proxy.convertFile(data, 'doc', 'odt', False)
...@@ -533,7 +567,8 @@ class TestImagetItemList(TestCase): ...@@ -533,7 +567,8 @@ class TestImagetItemList(TestCase):
class TestParagraphItemList(TestCase): class TestParagraphItemList(TestCase):
def testGetParagraphItemList(self): def testGetParagraphItemList(self):
"""Test if getParagraphItemList can get paragraphs correctly from document""" """Test if getParagraphItemList can get paragraphs correctly from document"""
data = encodestring(open("./data/granulate_test.odt").read()) with open("./data/granulate_test.odt", "rb") as f:
data = encodestring(f.read())
paragraph_list = self.proxy.getParagraphItemList(data, "odt") paragraph_list = self.proxy.getParagraphItemList(data, "odt")
self.assertEqual([[0, 'P3'], [1, 'P1'], [2, 'P12'], [3, 'P6'], [4, 'P7'], self.assertEqual([[0, 'P3'], [1, 'P1'], [2, 'P12'], [3, 'P6'], [4, 'P7'],
[5, 'P8'], [6, 'P6'], [7, 'P6'], [8, 'P13'], [9, 'P9'], [5, 'P8'], [6, 'P6'], [7, 'P6'], [8, 'P13'], [9, 'P9'],
...@@ -545,7 +580,8 @@ class TestParagraphItemList(TestCase): ...@@ -545,7 +580,8 @@ class TestParagraphItemList(TestCase):
def testGetParagraphItem(self): def testGetParagraphItem(self):
"""Test if getParagraph can get a paragraph""" """Test if getParagraph can get a paragraph"""
data = encodestring(open("./data/granulate_test.odt").read()) with open("./data/granulate_test.odt", "rb") as f:
data = encodestring(f.read())
paragraph = self.proxy.getParagraph(data, 1, "odt") paragraph = self.proxy.getParagraph(data, 1, "odt")
self.assertEqual(['', 'P1'], paragraph) self.assertEqual(['', 'P1'], paragraph)
...@@ -553,7 +589,8 @@ class TestParagraphItemList(TestCase): ...@@ -553,7 +589,8 @@ class TestParagraphItemList(TestCase):
class TestChapterItemList(TestCase): class TestChapterItemList(TestCase):
def testGetChapterItemList(self): def testGetChapterItemList(self):
"""Test if getChapterItemList can get the chapters list correctly from document""" """Test if getChapterItemList can get the chapters list correctly from document"""
data = encodestring(open("./data/granulate_chapters_test.odt").read()) with open("./data/granulate_chapters_test.odt", "rb") as f:
data = encodestring(f.read())
chapter_list = self.proxy.getChapterItemList(data, "odt") chapter_list = self.proxy.getChapterItemList(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'],
...@@ -562,7 +599,8 @@ class TestChapterItemList(TestCase): ...@@ -562,7 +599,8 @@ class TestChapterItemList(TestCase):
def testGetChapterItem(self): def testGetChapterItem(self):
"""Test if getChapterItem can get a chapter""" """Test if getChapterItem can get a chapter"""
data = encodestring(open("./data/granulate_chapters_test.odt").read()) with open("./data/granulate_chapters_test.odt", "rb") as f:
data = encodestring(f.read())
chapter = self.proxy.getChapterItem(1, data, "odt") chapter = self.proxy.getChapterItem(1, data, "odt")
self.assertEqual(['Title 1', 1], chapter) self.assertEqual(['Title 1', 1], chapter)
...@@ -573,7 +611,8 @@ class TestCSVEncoding(TestCase): ...@@ -573,7 +611,8 @@ class TestCSVEncoding(TestCase):
* the fields delimiter is guessed by python csv module. * the fields delimiter is guessed by python csv module.
""" """
def test_decode_ascii(self): def test_decode_ascii(self):
data = encodestring(open("./data/csv_ascii.csv").read()) with open("./data/csv_ascii.csv", "rb") as f:
data = encodestring(f.read())
converted = decodestring(self.proxy.convertFile(data, "csv", "html")) converted = decodestring(self.proxy.convertFile(data, "csv", "html"))
parser = etree.HTMLParser() parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser) tree = etree.parse(StringIO(converted), parser)
...@@ -582,7 +621,8 @@ class TestCSVEncoding(TestCase): ...@@ -582,7 +621,8 @@ class TestCSVEncoding(TestCase):
[x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text]) [x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text])
def test_decode_utf8(self): def test_decode_utf8(self):
data = encodestring(open("./data/csv_utf8.csv").read()) with open("./data/csv_utf8.csv", "rb") as f:
data = encodestring(f.read())
converted = decodestring(self.proxy.convertFile(data, "csv", "html")) converted = decodestring(self.proxy.convertFile(data, "csv", "html"))
parser = etree.HTMLParser() parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser) tree = etree.parse(StringIO(converted), parser)
...@@ -594,7 +634,8 @@ class TestCSVEncoding(TestCase): ...@@ -594,7 +634,8 @@ class TestCSVEncoding(TestCase):
[x.text for x in tree.getroot().find('.//tr[2]').iterdescendants() if x.text]) [x.text for x in tree.getroot().find('.//tr[2]').iterdescendants() if x.text])
def test_decode_latin9(self): def test_decode_latin9(self):
data = encodestring(open("./data/csv_latin9.csv").read()) with open("./data/csv_latin9.csv", "rb") as f:
data = encodestring(f.read())
converted = decodestring(self.proxy.convertFile(data, "csv", "html")) converted = decodestring(self.proxy.convertFile(data, "csv", "html"))
parser = etree.HTMLParser() parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser) tree = etree.parse(StringIO(converted), parser)
...@@ -603,7 +644,8 @@ class TestCSVEncoding(TestCase): ...@@ -603,7 +644,8 @@ class TestCSVEncoding(TestCase):
[x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text]) [x.text for x in tree.getroot().find('.//tr[1]').iterdescendants() if x.text])
def test_separator_semicolon(self): def test_separator_semicolon(self):
data = encodestring(open("./data/csv_semicolon.csv").read()) with open("./data/csv_semicolon.csv", "rb") as f:
data = encodestring(f.read())
converted = decodestring(self.proxy.convertFile(data, "csv", "html")) converted = decodestring(self.proxy.convertFile(data, "csv", "html"))
parser = etree.HTMLParser() parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser) tree = etree.parse(StringIO(converted), parser)
...@@ -615,7 +657,8 @@ class TestCSVEncoding(TestCase): ...@@ -615,7 +657,8 @@ class TestCSVEncoding(TestCase):
[x.text for x in tree.getroot().find('.//tr[2]').iterdescendants() if x.text]) [x.text for x in tree.getroot().find('.//tr[2]').iterdescendants() if x.text])
def test_separator_tab(self): def test_separator_tab(self):
data = encodestring(open("./data/tsv.tsv").read()) with open("./data/tsv.tsv", "rb") as f:
data = encodestring(f.read())
converted = decodestring(self.proxy.convertFile(data, "csv", "html")) converted = decodestring(self.proxy.convertFile(data, "csv", "html"))
parser = etree.HTMLParser() parser = etree.HTMLParser()
tree = etree.parse(StringIO(converted), parser) tree = etree.parse(StringIO(converted), parser)
......
...@@ -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 ]
(open(join('data', 'test.pdf')).read(), 'pdf', 'xyz'), # Try convert one video for a invalid format
# Try convert one video to format not possible with open(join('data', 'test.pdf'), 'rb') as f:
(open(join('data', 'test.pdf')).read(), 'pdf', 'ogv'), scenario_list.append((f.read(), 'pdf', 'xyz'))
] # Try convert one video to format not possible
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