Back to index

plone3  3.1.7
test_file_upload.py
Go to the documentation of this file.
00001 ################################################################################
00002 #
00003 # Copyright (c) 2002-2005, Benjamin Saller <bcsaller@ideasuite.com>, and
00004 #                              the respective authors. All rights reserved.
00005 # For a list of Archetypes contributors see docs/CREDITS.txt.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions are met:
00009 #
00010 # * Redistributions of source code must retain the above copyright notice, this
00011 #   list of conditions and the following disclaimer.
00012 # * Redistributions in binary form must reproduce the above copyright notice,
00013 #   this list of conditions and the following disclaimer in the documentation
00014 #   and/or other materials provided with the distribution.
00015 # * Neither the name of the author nor the names of its contributors may be used
00016 #   to endorse or promote products derived from this software without specific
00017 #   prior written permission.
00018 #
00019 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00020 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00022 # FOR A PARTICULAR PURPOSE.
00023 #
00024 ################################################################################
00025 
00026 # Load fixture
00027 import os
00028 import unittest
00029 from Testing import ZopeTestCase
00030 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00031 from OFS.Image import File
00032 
00033 from Products.Archetypes.atapi import MetadataStorage, BaseContent
00034 from Products.Archetypes.tests.utils import PACKAGE_HOME
00035 
00036 class FileLike:
00037 
00038     def __init__(self, data):
00039         self.data = data
00040         self.pos = 0
00041 
00042     def read(self, chunk=None):
00043         if chunk == None:
00044             return self.data
00045         cur = self.pos
00046         next = cur + chunk
00047         self.pos = next
00048         return self.data[cur:next]
00049 
00050     def seek(self, pos, start=0):
00051         if start == 0:
00052             self.pos = pos
00053         if start == 1:
00054             self.pos += pos
00055         if start == 2:
00056             self.pos = len(self.data) - pos
00057 
00058     def tell(self):
00059         return self.pos
00060 
00061 
00062 class FileFieldTest(ATSiteTestCase):
00063 
00064     def afterSetUp(self):
00065         from Products.Archetypes import Field
00066         from Products.MimetypesRegistry.MimeTypesRegistry import MimeTypesRegistry
00067         self.folder.mimetypes_registry = MimeTypesRegistry()
00068         self.folder._setOb('test_object_', BaseContent('test_object_'))
00069         self.instance = self.folder._getOb('test_object_')
00070         self.field = Field.FileField('file')
00071         self.factory = self.field.content_class
00072 
00073     def test_stringio_text(self):
00074         from cStringIO import StringIO
00075         f = StringIO('x' * (1 << 19))
00076         f.seek(0)
00077         v, m, f = self.field._process_input(f, instance=self.instance)
00078         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00079         self.assertEquals(m, 'text/plain')
00080         self.failIf(f)
00081 
00082     def test_stringio_binary(self):
00083         from cStringIO import StringIO
00084         f = StringIO('\x00' + 'x' * (1 << 19))
00085         f.seek(0)
00086         v, m, f = self.field._process_input(f, instance=self.instance)
00087         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00088         self.assertEquals(m, 'application/octet-stream')
00089         self.failIf(f)
00090 
00091     def test_file_like_text(self):
00092         f = FileLike('x' * (1 << 19))
00093         f.seek(0)
00094         v, m, f = self.field._process_input(f, instance=self.instance)
00095         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00096         self.assertEquals(m, 'text/plain')
00097         self.failIf(f)
00098 
00099     def test_file_like_binary(self):
00100         f = FileLike('\x00' + 'x' * (1 << 19))
00101         f.seek(0)
00102         v, m, f = self.field._process_input(f, instance=self.instance)
00103         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00104         self.assertEquals(m, 'application/octet-stream')
00105         self.failIf(f)
00106 
00107     def test_file_upload_text(self):
00108         from cgi import FieldStorage
00109         from ZPublisher.HTTPRequest import FileUpload
00110         from tempfile import TemporaryFile
00111         fp = TemporaryFile('w+b')
00112         fp.write('x' * (1 << 19))
00113         fp.seek(0)
00114         env = {'REQUEST_METHOD':'PUT'}
00115         headers = {'content-type':'text/plain',
00116                    'content-length': 1 << 19,
00117                    'content-disposition':'attachment; filename=test.txt'}
00118         fs = FieldStorage(fp=fp, environ=env, headers=headers)
00119         f = FileUpload(fs)
00120         v, m, f = self.field._process_input(f, instance=self.instance)
00121         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00122         self.assertEquals(m, 'text/plain')
00123         self.assertEquals(f, 'test.txt')
00124 
00125     def test_file_upload_binary(self):
00126         from cgi import FieldStorage
00127         from ZPublisher.HTTPRequest import FileUpload
00128         from tempfile import TemporaryFile
00129         fp = TemporaryFile('w+b')
00130         fp.write('\x00' + 'x' * (1 << 19))
00131         fp.seek(0)
00132         env = {'REQUEST_METHOD':'PUT'}
00133         headers = {'content-type':'text/plain',
00134                    'content-length': 1 << 19,
00135                    'content-disposition':'attachment; filename=test.bin'}
00136         fs = FieldStorage(fp=fp, environ=env, headers=headers)
00137         f = FileUpload(fs)
00138         v, m, f = self.field._process_input(f, instance=self.instance)
00139         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00140         self.assertEquals(m, 'application/octet-stream')
00141         self.assertEquals(f, 'test.bin')
00142 
00143     def test_real_file_text(self):
00144         from tempfile import TemporaryFile
00145         fd = TemporaryFile('w+b')
00146         fd.write('x' * (1 << 19))
00147         fd.seek(0)
00148         v, m, f = self.field._process_input(fd, instance=self.instance)
00149         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00150         self.assertEquals(m, 'text/plain')
00151         self.failIf(f, f)
00152 
00153     def test_real_file_binary(self):
00154         from tempfile import TemporaryFile
00155         fd = TemporaryFile('w+b')
00156         fd.write('\x00' + 'x' * (1 << 19))
00157         fd.seek(0)
00158         v, m, f = self.field._process_input(fd, instance=self.instance)
00159         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00160         self.assertEquals(m, 'application/octet-stream')
00161         self.failIf(f, f)
00162 
00163     def test_real_file_force_filename_detect_mime_pdf(self):
00164         from tempfile import TemporaryFile
00165         fd = TemporaryFile('w+b')
00166         fd.write('\x00' + 'x' * (1 << 19))
00167         fd.seek(0)
00168         v, m, f = self.field._process_input(fd, instance=self.instance,
00169                                             filename='file.pdf')
00170         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00171         self.assertEquals(m, 'application/pdf')
00172         self.assertEquals(f, 'file.pdf')
00173 
00174     def test_real_file_force_filename_detect_mime_xml(self):
00175         from tempfile import TemporaryFile
00176         fd = TemporaryFile('w+b')
00177         fd.write('\x00' + 'x' * (1 << 19))
00178         fd.seek(0)
00179         v, m, f = self.field._process_input(fd, instance=self.instance,
00180                                             filename='file.xml')
00181         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00182         self.assertEquals(m, 'text/xml')
00183         self.assertEquals(f, 'file.xml')
00184 
00185     def test_real_file_force_filename_detect_faq(self):
00186         from tempfile import TemporaryFile
00187         fd = TemporaryFile('w+b')
00188         fd.write('x' * (1 << 19))
00189         fd.seek(0)
00190         v, m, f = self.field._process_input(fd, instance=self.instance,
00191                                             filename='file.faq')
00192         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00193         self.assertEquals(m, 'application/octet-stream')
00194         self.assertEquals(f, 'file.faq')
00195 
00196     def test_real_file_force_mimetype(self):
00197         from tempfile import TemporaryFile
00198         fd = TemporaryFile('w+b')
00199         fd.write('\x00' + 'x' * (1 << 19))
00200         fd.seek(0)
00201         v, m, f = self.field._process_input(fd, instance=self.instance,
00202                                             mimetype='text/xml')
00203         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00204         self.assertEquals(m, 'text/xml')
00205         self.failIf(f, f)
00206 
00207     def test_ofs_file_text(self):
00208         from tempfile import TemporaryFile
00209         fd = TemporaryFile('w+b')
00210         fd.write('x' * (1 << 19))
00211         fd.seek(0)
00212         f = File('f', '', '')
00213         self.folder._setObject('f', f)
00214         self.folder.f.manage_upload(fd)
00215         self.folder.f.content_type = 'text/plain'
00216         v, m, f = self.field._process_input(self.folder.f,
00217                                             instance=self.instance)
00218         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00219         # Should retain content type.
00220         self.assertEquals(m, 'text/plain')
00221         self.assertEquals(f, self.folder.f.getId())
00222 
00223     def test_ofs_file_binary(self):
00224         from tempfile import TemporaryFile
00225         fd = TemporaryFile('w+b')
00226         fd.write('\x00' + 'x' * (1 << 19))
00227         fd.seek(0)
00228         f = File('f', '', '')
00229         self.folder._setObject('f', f)
00230         self.folder.f.manage_upload(fd)
00231         v, m, f = self.field._process_input(self.folder.f,
00232                                             instance=self.instance)
00233         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00234         self.assertEquals(m, 'application/octet-stream')
00235         self.assertEquals(f, self.folder.f.getId())
00236 
00237     def test_pdata_text(self):
00238         from tempfile import TemporaryFile
00239         fd = TemporaryFile('w+b')
00240         fd.write('x' * (1 << 19))
00241         fd.seek(0)
00242         f = File('f', '', '')
00243         self.folder._setObject('f', f)
00244         self.folder.f.manage_upload(fd)
00245         v, m, f = self.field._process_input(self.folder.f.data,
00246                                             instance=self.instance)
00247         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00248         self.assertEquals(m, 'text/plain')
00249         self.failIf(f)
00250 
00251     def test_pdata_binary(self):
00252         from tempfile import TemporaryFile
00253         fd = TemporaryFile('w+b')
00254         fd.write('\x00' + 'x' * (1 << 19))
00255         fd.seek(0)
00256         f = File('f', '', '')
00257         self.folder._setObject('f', f)
00258         self.folder.f.manage_upload(fd)
00259         v, m, f = self.field._process_input(self.folder.f.data,
00260                                             instance=self.instance)
00261         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00262         self.assertEquals(m, 'application/octet-stream')
00263         self.failIf(f)
00264 
00265     def test_string_text(self):
00266         f = 'x' * (1 << 19)
00267         v, m, f = self.field._process_input(f, instance=self.instance)
00268         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00269         self.assertEquals(m, 'text/plain')
00270         self.failIf(f)
00271 
00272     def test_string_binary(self):
00273         f = '\x00' + 'x' * (1 << 19)
00274         v, m, f = self.field._process_input(f, instance=self.instance)
00275         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00276         self.assertEquals(m, 'application/octet-stream')
00277         self.failIf(f)
00278 
00279     def test_string_pdf(self):
00280         f = open(os.path.join(PACKAGE_HOME, 'input', 'webdav.pdf')).read()
00281         v, m, f = self.field._process_input(f, instance=self.instance)
00282         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00283         self.assertEquals(m, 'application/pdf')
00284         self.failIf(f)
00285 
00286     def test_base_unit_text(self):
00287         from Products.Archetypes.BaseUnit import BaseUnit
00288         f = BaseUnit('f', 'x' * (1 << 19), instance=self.instance)
00289         v, m, f = self.field._process_input(f, instance=self.instance)
00290         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00291         self.assertEquals(m, 'text/plain')
00292         self.failIf(f)
00293 
00294     def test_base_unit_binary(self):
00295         from Products.Archetypes.BaseUnit import BaseUnit
00296         f = BaseUnit('f', '\x00' + 'x' * (1 << 19), instance=self.instance)
00297         v, m, f = self.field._process_input(f, instance=self.instance)
00298         self.failUnless(isinstance(v, self.factory), (type(v), self.factory))
00299         self.assertEquals(m, 'application/octet-stream')
00300         self.failIf(f)
00301 
00302     def test_get(self):
00303         text = 'x' * (1 << 19)
00304         self.field.set(instance=self.instance, value=text)
00305         result = self.field.get(instance=self.instance)
00306         # For FileField, we should return a File for backwards
00307         # compatibility.
00308         self.failUnless(isinstance(result, self.factory),
00309                         (type(result), self.factory))
00310 
00311     def test_get_metadata_storage(self):
00312         text = 'x' * (1 << 19)
00313         self.field.storage = MetadataStorage()
00314         self.field.set(instance=self.instance, value=text)
00315         result = self.field.get(instance=self.instance)
00316         # For FileField, we should return a File for backwards
00317         # compatibility.
00318         self.failUnless(isinstance(result, self.factory),
00319                         (type(result), self.factory))
00320                         
00321     def test_delete_file_via_set(self):
00322         sample = 'a sample text file to be deleted ............................'
00323         self.field.set(self.instance, sample)
00324         samplesize = self.field.get_size(self.instance)
00325         self.failUnless(samplesize > 0)
00326         deletefile = 'DELETE_FILE'
00327         self.field.set(self.instance, deletefile)
00328         samplesize = self.field.get_size(self.instance)
00329         self.failUnless(samplesize == 0)
00330 
00331 
00332 class TextFieldTest(FileFieldTest):
00333 
00334     def afterSetUp(self):
00335         from Products.Archetypes import Field
00336         from Products.MimetypesRegistry.MimeTypesRegistry import MimeTypesRegistry
00337         from Products.PortalTransforms.TransformTool import TransformTool
00338         self.folder.mimetypes_registry = MimeTypesRegistry()
00339         self.folder.portal_transforms = TransformTool()
00340         self.folder._setOb('test_object_', BaseContent('test_object_'))
00341         self.instance = self.folder._getOb('test_object_')
00342         self.field = Field.TextField('file')
00343         self.factory = self.field.content_class
00344 
00345     def test_factory(self):
00346         from Products.Archetypes.BaseUnit import BaseUnit
00347         self.failUnless(self.factory is BaseUnit)
00348 
00349     def test_field(self):
00350         from Products.Archetypes import Field
00351         self.failUnless(isinstance(self.field, Field.TextField))
00352 
00353     def test_get(self):
00354         text = 'x' * (1 << 19)
00355         self.field.set(instance=self.instance, value=text)
00356         result = self.field.get(instance=self.instance)
00357         # For TextField, we should really return a string for
00358         # backwards compatibility.
00359         self.failUnless(isinstance(result, str), type(result))
00360 
00361     def test_get_metadata_storage(self):
00362         text = 'x' * (1 << 19)
00363         self.field.storage = MetadataStorage()
00364         self.field.set(instance=self.instance, value=text)
00365         result = self.field.get(instance=self.instance)
00366         # For TextField, we should really return a string for
00367         # backwards compatibility.
00368         self.failUnless(isinstance(result, str), type(result))
00369 
00370 def test_suite():
00371     suite = unittest.TestSuite()
00372     tests = [
00373         unittest.makeSuite(FileFieldTest),
00374         unittest.makeSuite(TextFieldTest),
00375         ]
00376     for t in tests:
00377         suite.addTest(t)
00378     return suite