Commit 615dbcc6 authored by Nicolas Delaby's avatar Nicolas Delaby

Use python-magic to guess which type is with mimetype property.

output message will not OS configuration dependant


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@42085 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 27021265
...@@ -35,6 +35,7 @@ from base64 import encodestring, decodestring ...@@ -35,6 +35,7 @@ from base64 import encodestring, decodestring
from cloudoooTestCase import CloudoooTestCase, make_suite from cloudoooTestCase import CloudoooTestCase, make_suite
from zipfile import ZipFile, is_zipfile from zipfile import ZipFile, is_zipfile
from types import DictType from types import DictType
import magic
DAEMON = True DAEMON = True
...@@ -109,14 +110,13 @@ class TestServer(CloudoooTestCase): ...@@ -109,14 +110,13 @@ class TestServer(CloudoooTestCase):
source_format, source_format,
destination_format, zip) destination_format, zip)
open(output_url, 'w').write(decodestring(output_data)) open(output_url, 'w').write(decodestring(output_data))
stdout, stderr = self._getFileType(output_url) content_type = self._getFileType(output_url)
self.assertEquals(stdout, stdout_msg) self.assertEquals(content_type, stdout_msg)
self.assertEquals(stderr, None)
def _getFileType(self, output_url): def _getFileType(self, output_url):
return Popen(["file", "-b", output_url], mime = magic.Magic(mime=True)
stdout=PIPE, return mime.from_file(output_url)
stderr=PIPE).communicate()
def testGetAllowedExtensionListByType(self): def testGetAllowedExtensionListByType(self):
"""Call getAllowedExtensionList and verify if the returns is a list with """Call getAllowedExtensionList and verify if the returns is a list with
...@@ -159,7 +159,7 @@ class TestServer(CloudoooTestCase): ...@@ -159,7 +159,7 @@ class TestServer(CloudoooTestCase):
join(self.tmp_url, "document_output.odt"), join(self.tmp_url, "document_output.odt"),
'doc', 'doc',
'odt', 'odt',
'OpenDocument Text\n') 'application/vnd.oasis.opendocument.text')
def testconvertDocToPdf(self): def testconvertDocToPdf(self):
"""Test Convert Doc -> Pdf""" """Test Convert Doc -> Pdf"""
...@@ -167,7 +167,7 @@ class TestServer(CloudoooTestCase): ...@@ -167,7 +167,7 @@ class TestServer(CloudoooTestCase):
join(self.tmp_url, "document_output.pdf"), join(self.tmp_url, "document_output.pdf"),
'doc', 'doc',
'pdf', 'pdf',
'PDF document, version 1.4\n') 'application/pdf')
def testgetFileMetadataItemListWithoutData(self): def testgetFileMetadataItemListWithoutData(self):
"""Test server using method getFileMetadataItemList. Without data """Test server using method getFileMetadataItemList. Without data
...@@ -192,8 +192,8 @@ class TestServer(CloudoooTestCase): ...@@ -192,8 +192,8 @@ class TestServer(CloudoooTestCase):
True) True)
self.assertNotEquals(metadata_dict.get("Data"), None) self.assertNotEquals(metadata_dict.get("Data"), None)
open(document_output_url, 'w').write(decodestring(metadata_dict["Data"])) open(document_output_url, 'w').write(decodestring(metadata_dict["Data"]))
stdout, stderr = self._getFileType(document_output_url) content_type = self._getFileType(document_output_url)
self.assertEquals(stdout, 'OpenDocument Text\n') self.assertEquals(content_type, 'application/vnd.oasis.opendocument.text')
self.assertEquals(metadata_dict.get("Title"), "cloudooo Test") self.assertEquals(metadata_dict.get("Title"), "cloudooo Test")
self.assertEquals(metadata_dict.get("Subject"), "Subject Test") self.assertEquals(metadata_dict.get("Subject"), "Subject Test")
self.assertEquals(metadata_dict.get("Description"), "cloudooo Comments") self.assertEquals(metadata_dict.get("Description"), "cloudooo Comments")
...@@ -208,8 +208,8 @@ class TestServer(CloudoooTestCase): ...@@ -208,8 +208,8 @@ class TestServer(CloudoooTestCase):
odf_data = self.proxy.updateFileMetadata(encodestring(data), 'odt', odf_data = self.proxy.updateFileMetadata(encodestring(data), 'odt',
{"Title":"testSetMetadata"}) {"Title":"testSetMetadata"})
open(document_output_url, 'w').write(decodestring(odf_data)) open(document_output_url, 'w').write(decodestring(odf_data))
stdout, stderr = self._getFileType(document_output_url) content_type = self._getFileType(document_output_url)
self.assertEquals(stdout, 'OpenDocument Text\n') self.assertEquals(content_type, 'application/vnd.oasis.opendocument.text')
metadata_dict = self.proxy.getFileMetadataItemList(odf_data, 'odt') metadata_dict = self.proxy.getFileMetadataItemList(odf_data, 'odt')
self.assertEquals(metadata_dict.get("Title"), "testSetMetadata") self.assertEquals(metadata_dict.get("Title"), "testSetMetadata")
self.assertEquals(metadata_dict.get("CreationDate"), "9/7/2009 17:38:25") self.assertEquals(metadata_dict.get("CreationDate"), "9/7/2009 17:38:25")
...@@ -224,8 +224,8 @@ class TestServer(CloudoooTestCase): ...@@ -224,8 +224,8 @@ class TestServer(CloudoooTestCase):
'odt', 'odt',
{"Reference":"testSetMetadata"}) {"Reference":"testSetMetadata"})
open(document_output_url, 'w').write(decodestring(odf_data)) open(document_output_url, 'w').write(decodestring(odf_data))
stdout, stderr = self._getFileType(document_output_url) content_type = self._getFileType(document_output_url)
self.assertEquals(stdout, 'OpenDocument Text\n') self.assertEquals(content_type, 'application/vnd.oasis.opendocument.text')
metadata_dict = self.proxy.getFileMetadataItemList(odf_data, 'odt') metadata_dict = self.proxy.getFileMetadataItemList(odf_data, 'odt')
self.assertEquals(metadata_dict.get("Reference"), "testSetMetadata") self.assertEquals(metadata_dict.get("Reference"), "testSetMetadata")
...@@ -239,8 +239,8 @@ class TestServer(CloudoooTestCase): ...@@ -239,8 +239,8 @@ class TestServer(CloudoooTestCase):
new_odf_data = self.proxy.updateFileMetadata(odf_data, 'odt', new_odf_data = self.proxy.updateFileMetadata(odf_data, 'odt',
{"Reference":"new value", "Something": "ABC"}) {"Reference":"new value", "Something": "ABC"})
open(document_output_url, 'w').write(decodestring(new_odf_data)) open(document_output_url, 'w').write(decodestring(new_odf_data))
stdout, stderr = self._getFileType(document_output_url) content_type = self._getFileType(document_output_url)
self.assertEquals(stdout, 'OpenDocument Text\n') self.assertEquals(content_type, 'application/vnd.oasis.opendocument.text')
metadata_dict = self.proxy.getFileMetadataItemList(new_odf_data, 'odt') metadata_dict = self.proxy.getFileMetadataItemList(new_odf_data, 'odt')
self.assertEquals(metadata_dict.get("Reference"), "new value") self.assertEquals(metadata_dict.get("Reference"), "new value")
self.assertEquals(metadata_dict.get("Something"), "ABC") self.assertEquals(metadata_dict.get("Something"), "ABC")
...@@ -262,12 +262,11 @@ class TestServer(CloudoooTestCase): ...@@ -262,12 +262,11 @@ class TestServer(CloudoooTestCase):
def testWithZipFile(self): def testWithZipFile(self):
"""Test if send a zipfile returns a document correctly""" """Test if send a zipfile returns a document correctly"""
output_msg = 'Zip archive data, at least v2.0 to extract\n'
self._testConvertFile("data/test.zip", self._testConvertFile("data/test.zip",
join(self.tmp_url, "output_zipfile.zip"), join(self.tmp_url, "output_zipfile.zip"),
'zip', 'zip',
'txt', 'txt',
output_msg, 'application/zip',
True) True)
def testSendZipAndReturnTxt(self): def testSendZipAndReturnTxt(self):
...@@ -277,8 +276,7 @@ class TestServer(CloudoooTestCase): ...@@ -277,8 +276,7 @@ class TestServer(CloudoooTestCase):
output_url, output_url,
'zip', 'zip',
'txt', 'txt',
'ASCII text\n') 'text/plain')
self.assertTrue(open(output_url).read().endswith('cloudooo Test\n \n')) self.assertTrue(open(output_url).read().endswith('cloudooo Test\n \n'))
def testConvertPNGToSVG(self): def testConvertPNGToSVG(self):
...@@ -288,7 +286,7 @@ class TestServer(CloudoooTestCase): ...@@ -288,7 +286,7 @@ class TestServer(CloudoooTestCase):
output_url, output_url,
'png', 'png',
'svg', 'svg',
'SVG Scalable Vector Graphics image\n') 'image/svg+xml')
def testConvertPPTXToODP(self): def testConvertPPTXToODP(self):
"""Test export pptx to odp""" """Test export pptx to odp"""
...@@ -296,7 +294,7 @@ class TestServer(CloudoooTestCase): ...@@ -296,7 +294,7 @@ class TestServer(CloudoooTestCase):
join(self.tmp_url, "output.pptx"), join(self.tmp_url, "output.pptx"),
'pptx', 'pptx',
'odp', 'odp',
'OpenDocument Presentation\n') 'application/vnd.oasis.opendocument.presentation')
def testConvertDocxToODT(self): def testConvertDocxToODT(self):
"""Test export docx to odt""" """Test export docx to odt"""
...@@ -304,7 +302,7 @@ class TestServer(CloudoooTestCase): ...@@ -304,7 +302,7 @@ class TestServer(CloudoooTestCase):
join(self.tmp_url, "output_docx.odt"), join(self.tmp_url, "output_docx.odt"),
'docx', 'docx',
'odt', 'odt',
'OpenDocument Text\n') 'application/vnd.oasis.opendocument.text')
def testConvertPyToPDF(self): def testConvertPyToPDF(self):
"""Test export python to pdf""" """Test export python to pdf"""
...@@ -312,7 +310,7 @@ class TestServer(CloudoooTestCase): ...@@ -312,7 +310,7 @@ class TestServer(CloudoooTestCase):
join(self.tmp_url, "cloudoooTestCase.py"), join(self.tmp_url, "cloudoooTestCase.py"),
'py', 'py',
'pdf', 'pdf',
'PDF document, version 1.4\n') 'application/pdf')
def testSendEmptyRequest(self): def testSendEmptyRequest(self):
"""Test to verify if the behavior of server is normal when a empty string """Test to verify if the behavior of server is normal when a empty string
...@@ -438,9 +436,10 @@ class TestServer(CloudoooTestCase): ...@@ -438,9 +436,10 @@ class TestServer(CloudoooTestCase):
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)
stdout, stderr = self._getFileType(png_path) content_type = self._getFileType(png_path)
self.assertTrue(stdout.startswith('PNG image data'), stdout) self.assertEquals(content_type, 'image/png')
self.assertTrue("8-bit/color RGB" in stdout, stdout) m = magic.Magic()
self.assertTrue("8-bit/color RGB" in m.from_file(png_path))
finally: finally:
zipfile.close() zipfile.close()
if exists(output_url): if exists(output_url):
......
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