Back to index

plone3  3.1.7
test_File.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Unit tests for File module.
00014 
00015 $Id: test_File.py 77186 2007-06-28 19:06:19Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from os.path import join as path_join
00022 
00023 from zope.testing.cleanup import cleanUp
00024 
00025 from Products.CMFCore.testing import ConformsToContent
00026 from Products.CMFCore.tests.base.dummy import DummyCachingManager
00027 from Products.CMFCore.tests.base.dummy import DummyCachingManagerWithPolicy
00028 from Products.CMFCore.tests.base.testcase import RequestTest
00029 from Products.CMFDefault import tests
00030 
00031 TESTS_HOME = tests.__path__[0]
00032 TEST_JPG = path_join(TESTS_HOME, 'TestImage.jpg')
00033 TEST_SWF = path_join(TESTS_HOME, 'TestFile.swf')
00034 
00035 
00036 class FileTests(ConformsToContent, unittest.TestCase):
00037 
00038     def _getTargetClass(self):
00039         from Products.CMFDefault.File import File
00040 
00041         return File
00042 
00043     def _makeOne(self, *args, **kw):
00044         return self._getTargetClass()(*args, **kw)
00045 
00046     def test_z3interfaces(self):
00047         from zope.interface.verify import verifyClass
00048         from Products.CMFDefault.interfaces import IFile
00049         from Products.CMFDefault.interfaces import IMutableFile
00050 
00051         verifyClass(IFile, self._getTargetClass())
00052         verifyClass(IMutableFile, self._getTargetClass())
00053 
00054     def test_getId_on_old_File_instance(self):
00055         file = self._makeOne('testfile')
00056         self.assertEqual(file.getId(), 'testfile')
00057         self.assertEqual(file.id, 'testfile')
00058         # Mimick old instance when base classes had OFS.Image.File first
00059         file.__name__ = 'testfile'
00060         delattr(file, 'id')
00061         self.assertEqual(file.getId(), 'testfile')
00062         self.assertEqual(file.id(), 'testfile')
00063 
00064     def test_File_setFormat(self):
00065         # Setting the DC.format must also set the content_type property
00066         file = self._makeOne('testfile', format='image/jpeg')
00067         self.assertEqual(file.Format(), 'image/jpeg')
00068         self.assertEqual(file.content_type, 'image/jpeg')
00069         file.setFormat('image/gif')
00070         self.assertEqual(file.Format(), 'image/gif')
00071         self.assertEqual(file.content_type, 'image/gif')
00072 
00073     def test_FileContentTypeUponConstruction(self):
00074         # Test the content type after calling the constructor with the
00075         # file object being passed in (http://www.zope.org/Collectors/CMF/370)
00076         testfile = open(TEST_JPG, 'rb')
00077         # Notice the cheat? File objects lack the extra intelligence that
00078         # picks content types from the actual file data, so it needs to be
00079         # helped along with a file extension...
00080         file = self._makeOne('testfile.jpg', file=testfile)
00081         testfile.close()
00082         self.assertEqual(file.Format(), 'image/jpeg')
00083         self.assertEqual(file.content_type, 'image/jpeg')
00084 
00085 
00086 class CachingTests(RequestTest):
00087 
00088     def _getTargetClass(self):
00089         from Products.CMFDefault.File import File
00090 
00091         return File
00092 
00093     def _makeOne(self, *args, **kw):
00094         return self._getTargetClass()(*args, **kw)
00095 
00096     def _extractFile( self ):
00097 
00098         f = open( TEST_SWF, 'rb' )
00099         try:
00100             data = f.read()
00101         finally:
00102             f.close()
00103 
00104         return TEST_SWF, data
00105 
00106     def _setupCachingPolicyManager(self, cpm_object):
00107         self.root.caching_policy_manager = cpm_object
00108 
00109     def tearDown(self):
00110         cleanUp()
00111         RequestTest.tearDown(self)
00112 
00113     def test_index_html_with_304_from_cpm( self ):
00114         self._setupCachingPolicyManager(DummyCachingManagerWithPolicy())
00115         path, ref = self._extractFile()
00116 
00117         from webdav.common import rfc1123_date
00118         from Products.CMFCore.tests.base.dummy import FAKE_ETAG
00119 
00120         file = self._makeOne( 'test_file', 'test_file.swf', file=ref )
00121         file = file.__of__( self.root )
00122 
00123         mod_time = file.modified().timeTime()
00124 
00125         self.REQUEST.environ[ 'IF_MODIFIED_SINCE'
00126                             ] = '%s;' % rfc1123_date( mod_time )
00127         self.REQUEST.environ[ 'IF_NONE_MATCH'
00128                             ] = '%s;' % FAKE_ETAG
00129 
00130         data = file.index_html( self.REQUEST, self.RESPONSE )
00131         self.assertEqual( len(data), 0 )
00132         self.assertEqual( self.RESPONSE.getStatus(), 304 )
00133 
00134     def test_index_html_200_with_cpm( self ):
00135         # should behave the same as without cpm installed
00136         self._setupCachingPolicyManager(DummyCachingManager())
00137         path, ref = self._extractFile()
00138 
00139         from webdav.common import rfc1123_date
00140 
00141         file = self._makeOne( 'test_file', 'test_file.swf', file=ref )
00142         file = file.__of__( self.root )
00143 
00144         mod_time = file.modified().timeTime()
00145 
00146         data = file.index_html( self.REQUEST, self.RESPONSE )
00147 
00148         self.assertEqual( len( data ), len( ref ) )
00149         self.assertEqual( data, ref )
00150         # ICK!  'HTTPResponse.getHeader' doesn't case-flatten the key!
00151         self.assertEqual( self.RESPONSE.getHeader( 'Content-Length'.lower() )
00152                         , str(len(ref)) )
00153         self.assertEqual( self.RESPONSE.getHeader( 'Content-Type'.lower() )
00154                         , 'application/octet-stream' )
00155         self.assertEqual( self.RESPONSE.getHeader( 'Last-Modified'.lower() )
00156                         , rfc1123_date( mod_time ) )
00157 
00158     def test_caching( self ):
00159         self._setupCachingPolicyManager(DummyCachingManager())
00160         original_len = len(self.RESPONSE.headers)
00161         file = self._makeOne('test_file', 'test_file.swf')
00162         file = file.__of__(self.root)
00163         file.index_html(self.REQUEST, self.RESPONSE)
00164         headers = self.RESPONSE.headers
00165         self.failUnless(len(headers) >= original_len + 3)
00166         self.failUnless('foo' in headers.keys())
00167         self.failUnless('bar' in headers.keys())
00168         self.assertEqual(headers['test_path'], '/test_file')
00169 
00170 
00171 def test_suite():
00172     return unittest.TestSuite((
00173         unittest.makeSuite(FileTests),
00174         unittest.makeSuite(CachingTests),
00175         ))
00176 
00177 if __name__ == '__main__':
00178     unittest.main(defaultTest='test_suite')