Back to index

plone3  3.1.7
test_FSReSTMethod.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2006 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 FSReSTMethod module.
00014 
00015 $Id: test_FSReSTMethod.py 77113 2007-06-26 20:36:26Z yuppie $
00016 """
00017 import unittest
00018 import Testing
00019 
00020 import os
00021 import re
00022 
00023 from Acquisition import aq_base
00024 from zope.testing.cleanup import cleanUp
00025 
00026 from Products.CMFCore.testing import TraversingZCMLLayer
00027 from Products.CMFCore.tests.base.testcase import FSDVTest
00028 from Products.CMFCore.tests.base.testcase import RequestTest
00029 from Products.CMFCore.tests.base.testcase import SecurityTest
00030 
00031 
00032 class FSReSTMaker(FSDVTest):
00033 
00034     def setUp(self):
00035         from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
00036         FSDVTest.setUp(self)
00037         main = ZopePageTemplate('main_template', _TEST_MAIN_TEMPLATE)
00038         self.root._setOb('main_template', main)
00039 
00040     def _makeOne( self, id, filename ):
00041         from Products.CMFCore.FSMetadata import FSMetadata
00042         from Products.CMFCore.FSReSTMethod import FSReSTMethod
00043         path = os.path.join(self.skin_path_name, filename)
00044         metadata = FSMetadata(path)
00045         metadata.read()
00046         return FSReSTMethod( id, path, properties=metadata.getProperties() )
00047 
00048 _EXPECTED_HTML = """\
00049 <html>
00050 <body>
00051 
00052 <div class="document" id="title-goes-here">
00053 <h1 class="title">Title Goes Here</h1>
00054 <h2 class="subtitle" id="subhead-here">Subhead Here</h2>
00055 <p>And this is a paragraph,
00056     broken across lines.</p>
00057 
00058 </div>
00059 
00060 </body>
00061 </html>
00062 """
00063 
00064 _TEST_MAIN_TEMPLATE = """\
00065 <html metal:define-macro="main">
00066 <body>
00067 
00068 <metal:block define-slot="body">
00069 BODY GOES HERE
00070 </metal:block>
00071 </body>
00072 </html>
00073 """
00074 
00075 WS = re.compile(r'\s+')
00076 
00077 def _normalize_whitespace(text):
00078     return ' '.join(WS.split(text.rstrip()))
00079 
00080 
00081 class FSReSTMethodTests(RequestTest, FSReSTMaker):
00082 
00083     layer = TraversingZCMLLayer
00084 
00085     def setUp(self):
00086         RequestTest.setUp(self)
00087         FSReSTMaker.setUp(self)
00088 
00089     def tearDown(self):
00090         FSReSTMaker.tearDown(self)
00091         RequestTest.tearDown(self)
00092 
00093     def test___call__( self ):
00094         script = self._makeOne( 'testReST', 'testReST.rst' )
00095         script = script.__of__(self.app)
00096         self.assertEqual(_normalize_whitespace(script(self.REQUEST)),
00097                          _normalize_whitespace(_EXPECTED_HTML))
00098 
00099     def test_caching( self ):
00100         #   Test HTTP caching headers.
00101         from Products.CMFCore.tests.base.dummy import DummyCachingManager
00102         self.root.caching_policy_manager = DummyCachingManager()
00103         original_len = len( self.RESPONSE.headers )
00104         script = self._makeOne('testReST', 'testReST.rst')
00105         script = script.__of__(self.root)
00106         script(self.REQUEST, self.RESPONSE)
00107         self.failUnless( len( self.RESPONSE.headers ) >= original_len + 2 )
00108         self.failUnless( 'foo' in self.RESPONSE.headers.keys() )
00109         self.failUnless( 'bar' in self.RESPONSE.headers.keys() )
00110 
00111     def test_ownership( self ):
00112         script = self._makeOne( 'testReST', 'testReST.rst' )
00113         script = script.__of__(self.root)
00114         # FSReSTMethod has no owner
00115         owner_tuple = script.getOwnerTuple()
00116         self.assertEqual(owner_tuple, None)
00117 
00118         # and ownership is not acquired [CMF/450]
00119         self.root._owner= ('/foobar', 'baz')
00120         owner_tuple = script.getOwnerTuple()
00121         self.assertEqual(owner_tuple, None)
00122 
00123     def test_304_response_from_cpm( self ):
00124         # test that we get a 304 response from the cpm via this template
00125         from DateTime import DateTime
00126         from webdav.common import rfc1123_date
00127         from Products.CMFCore.tests.base.dummy \
00128             import DummyCachingManagerWithPolicy
00129 
00130         mod_time = DateTime()
00131         self.root.caching_policy_manager = DummyCachingManagerWithPolicy()
00132         script = self._makeOne('testReST', 'testReST.rst')
00133         script = script.__of__(self.root)
00134         self.REQUEST.environ[ 'IF_MODIFIED_SINCE'
00135                             ] = '%s;' % rfc1123_date( mod_time+3600 )
00136         data = script(self.REQUEST, self.RESPONSE)
00137 
00138         self.assertEqual( data, '' )
00139         self.assertEqual( self.RESPONSE.getStatus(), 304 )
00140 
00141 
00142 ADD_ZPT = 'Add page templates'
00143 ZPT_META_TYPES = ( { 'name'        : 'Page Template'
00144                    , 'action'      : 'manage_addPageTemplate'
00145                    , 'permission'  : ADD_ZPT
00146                    }
00147                  ,
00148                  )
00149 
00150 
00151 class FSReSTMethodCustomizationTests(SecurityTest, FSReSTMaker):
00152 
00153     def setUp(self):
00154         from OFS.Folder import Folder
00155 
00156         SecurityTest.setUp(self)
00157         FSReSTMaker.setUp(self)
00158 
00159         self.root._setObject( 'portal_skins', Folder( 'portal_skins' ) )
00160         self.skins = self.root.portal_skins
00161 
00162         self.skins._setObject( 'custom', Folder( 'custom' ) )
00163         self.custom = self.skins.custom
00164 
00165         self.skins._setObject( 'fsdir', Folder( 'fsdir' ) )
00166         self.fsdir = self.skins.fsdir
00167 
00168         self.fsdir._setObject( 'testReST'
00169                              , self._makeOne( 'testReST', 'testReST.rst' ) )
00170 
00171         self.fsReST = self.fsdir.testReST
00172 
00173     def tearDown(self):
00174         cleanUp()
00175         FSReSTMaker.tearDown(self)
00176         SecurityTest.tearDown(self)
00177 
00178     def test_customize( self ):
00179         from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
00180         from Products.CMFCore.FSReSTMethod import _CUSTOMIZED_TEMPLATE_ZPT
00181 
00182         self.custom.all_meta_types = ZPT_META_TYPES
00183 
00184         self.fsReST.manage_doCustomize(folder_path='custom')
00185 
00186         self.assertEqual(len(self.custom.objectIds()), 1)
00187         self.failUnless('testReST' in self.custom.objectIds())
00188         target = self.custom._getOb('testReST')
00189 
00190         self.failUnless(isinstance(target, ZopePageTemplate))
00191 
00192         propinfo = target.propdict()['rest']
00193         self.assertEqual(propinfo['type'], 'text')
00194         self.assertEqual(target.rest, self.fsReST.raw)
00195 
00196         self.assertEqual(_normalize_whitespace(target.document_src()),
00197                          _normalize_whitespace(_CUSTOMIZED_TEMPLATE_ZPT))
00198 
00199     def test_customize_alternate_root( self ):
00200         from OFS.Folder import Folder
00201 
00202         self.root.other = Folder('other')
00203 
00204         self.fsReST.manage_doCustomize(folder_path='other', root=self.root)
00205 
00206         self.failIf('testReST' in self.custom.objectIds())
00207         self.failUnless('testReST' in self.root.other.objectIds())
00208 
00209     def test_customize_fpath_as_dot( self ):
00210 
00211         self.fsReST.manage_doCustomize(folder_path='.')
00212 
00213         self.failIf('testReST' in self.custom.objectIds())
00214         self.failUnless('testReST' in self.skins.objectIds())
00215 
00216     def test_customize_manual_clone( self ):
00217         from OFS.Folder import Folder
00218 
00219         clone = Folder('testReST')
00220 
00221         self.fsReST.manage_doCustomize(folder_path='custom', obj=clone)
00222 
00223         self.failUnless('testReST' in self.custom.objectIds())
00224         self.failUnless(aq_base(self.custom._getOb('testReST')) is clone)
00225 
00226     def test_customize_caching(self):
00227         # Test to ensure that cache manager associations survive customizing
00228         from Products.StandardCacheManagers import RAMCacheManager
00229         cache_id = 'gofast'
00230         self.custom.all_meta_types = ZPT_META_TYPES
00231         RAMCacheManager.manage_addRAMCacheManager( self.root
00232                                                  , cache_id
00233                                                  , REQUEST=None
00234                                                  )
00235         self.fsReST.ZCacheable_setManagerId(cache_id, REQUEST=None)
00236 
00237         self.assertEqual(self.fsReST.ZCacheable_getManagerId(), cache_id)
00238 
00239         self.fsReST.manage_doCustomize(folder_path='custom')
00240         custom_pt = self.custom.testReST
00241 
00242         self.assertEqual(custom_pt.ZCacheable_getManagerId(), cache_id)
00243 
00244 
00245 def test_suite():
00246     return unittest.TestSuite((
00247         unittest.makeSuite(FSReSTMethodTests),
00248         unittest.makeSuite(FSReSTMethodCustomizationTests),
00249         ))
00250 
00251 if __name__ == '__main__':
00252     from Products.CMFCore.testing import run
00253     run(test_suite())