Back to index

plone3  3.1.7
testJSRegistry.py
Go to the documentation of this file.
00001 #
00002 # JSRegistry Tests
00003 #
00004 from zope.component import getUtility, getMultiAdapter
00005 from zope.contentprovider.interfaces import IContentProvider
00006 
00007 from App.Common import rfc1123_date
00008 from DateTime import DateTime
00009 from AccessControl import Unauthorized
00010 from zope.interface.verify import verifyObject
00011 
00012 from Products.CMFCore.utils import getToolByName
00013 
00014 from Products.PloneTestCase.PloneTestCase import PLONE21, portal_owner, default_password
00015 
00016 from Products.ResourceRegistries.config import JSTOOLNAME
00017 from Products.ResourceRegistries.interfaces import IJSRegistry
00018 from Products.ResourceRegistries.tests.RegistryTestCase import RegistryTestCase
00019 from Products.ResourceRegistries.tests.RegistryTestCase import FunctionalRegistryTestCase
00020 from Products.ResourceRegistries.tools.packer import test_suite as packer_tests
00021 
00022 class TestJSImplementation(RegistryTestCase):
00023 
00024     def test_interfaces(self):
00025         tool = getattr(self.portal, JSTOOLNAME)
00026         self.failUnless(IJSRegistry.providedBy(tool))
00027         self.failUnless(verifyObject(IJSRegistry, tool))
00028 
00029 
00030 class TestJSTool(RegistryTestCase):
00031 
00032     def testToolExists(self):
00033         self.failUnless(JSTOOLNAME in self.portal.objectIds())
00034 
00035     def testZMIForm(self):
00036         tool = getattr(self.portal, JSTOOLNAME)
00037         self.setRoles(['Manager'])
00038         self.failUnless(tool.manage_jsForm())
00039         self.failUnless(tool.manage_jsComposition())
00040 
00041 
00042 class TestJSSkin(RegistryTestCase):
00043 
00044     def testSkins(self):
00045         skins = self.portal.portal_skins.objectIds()
00046         self.failUnless('ResourceRegistries' in skins)
00047 
00048     def testSkinExists(self):
00049         self.failUnless(getattr(self.portal, 'test_rr_1.js'))
00050 
00051 
00052 class testJSZMIMethods(RegistryTestCase):
00053 
00054     def afterSetUp(self):
00055         self.tool = getattr(self.portal, JSTOOLNAME)
00056         self.tool.clearResources()
00057 
00058     def testAdd(self):
00059         self.tool.manage_addScript(id='joe')
00060         self.assertEqual(len(self.tool.getResources()), 1)
00061         self.failUnless(self.tool.getResources())
00062 
00063 
00064 class TestJSScriptRegistration(RegistryTestCase):
00065 
00066     def afterSetUp(self):
00067         self.tool = getattr(self.portal, JSTOOLNAME)
00068         self.tool.clearResources()
00069 
00070     def testStoringScript(self):
00071         self.tool.registerScript('foo')
00072         self.assertEqual(len(self.tool.getResources()), 1)
00073         script = self.tool.getResources()[0]
00074         self.assertEqual(script.getId(), 'foo')
00075         self.assertEqual(script.getExpression(), '')
00076         self.assertEqual(script.getInline(), False)
00077         self.assertEqual(script.getEnabled(), True)
00078 
00079     def testDisallowingDuplicateIds(self):
00080         self.tool.registerScript('foo')
00081         self.assertRaises(ValueError, self.tool.registerScript, 'foo')
00082 
00083     def testUnregisterScript(self):
00084         self.tool.registerScript('foo')
00085         self.assertEqual(len(self.tool.getResources()), 1)
00086         self.assertEqual(self.tool.getResources()[0].getId(), 'foo')
00087         self.tool.unregisterResource('foo')
00088         self.assertEqual(len(self.tool.getResources()), 0)
00089 
00090 
00091 class TestJSScriptRenaming(RegistryTestCase):
00092 
00093     def afterSetUp(self):
00094         self.tool = getattr(self.portal, JSTOOLNAME)
00095         self.tool.clearResources()
00096 
00097     def testRenaming(self):
00098         self.tool.registerScript('ham')
00099         self.tool.registerScript('spam')
00100         self.tool.registerScript('eggs')
00101         self.assertEqual(self.tool.concatenatedresources[
00102                          self.tool.cookedresources[0].getId()],
00103                          ['ham', 'spam', 'eggs'])
00104         self.tool.renameResource('spam', 'bacon')
00105         self.assertEqual(self.tool.concatenatedresources[
00106                          self.tool.cookedresources[0].getId()],
00107                          ['ham', 'bacon', 'eggs'])
00108 
00109     def testRenamingIdClash(self):
00110         self.tool.registerScript('ham')
00111         self.tool.registerScript('spam')
00112         self.tool.registerScript('eggs')
00113         self.assertRaises(ValueError, self.tool.renameResource, 'spam', 'eggs')
00114 
00115     def testDoubleRenaming(self):
00116         self.tool.registerScript('ham')
00117         self.tool.registerScript('spam')
00118         self.tool.registerScript('eggs')
00119         self.tool.renameResource('spam', 'bacon')
00120         self.assertRaises(ValueError, self.tool.renameResource, 'spam', 'bacon')
00121 
00122 
00123 class TestJSToolSecurity(RegistryTestCase):
00124 
00125     def afterSetUp(self):
00126         self.tool = getattr(self.portal, JSTOOLNAME)
00127         self.tool.clearResources()
00128 
00129     def testRegistrationSecurity(self):
00130         self.assertRaises(Unauthorized, self.tool.restrictedTraverse,
00131                           'registerScript')
00132         self.assertRaises(Unauthorized, self.tool.restrictedTraverse,
00133                           'unregisterResource')
00134         self.setRoles(['Manager'])
00135         try:
00136             self.tool.restrictedTraverse('registerScript')
00137             self.tool.restrictedTraverse('unregisterResource')
00138         except Unauthorized:
00139             self.fail()
00140 
00141 
00142 class TestJSToolExpression(RegistryTestCase):
00143 
00144     def afterSetUp(self):
00145         self.tool = getattr(self.portal, JSTOOLNAME)
00146         self.tool.clearResources()
00147 
00148     def testSimplestExpression(self):
00149         context = self.portal
00150         self.failUnless(self.tool.evaluateExpression('python:1', context))
00151         self.failIf(self.tool.evaluateExpression('python:0', context))
00152         self.failUnless(self.tool.evaluateExpression('python:0+1', context))
00153 
00154     def testNormalExpression(self):
00155         context = self.portal
00156         self.failUnless(self.tool.evaluateExpression('object/absolute_url',
00157                                                      context))
00158 
00159     def testExpressionInFolder(self):
00160         self.folder.invokeFactory('Document', 'eggs')
00161         context = self.folder
00162         self.failUnless(self.tool.evaluateExpression(
00163                         'python:"eggs" in object.objectIds()', context))
00164 
00165 
00166 class TestJSScriptCooking(RegistryTestCase):
00167 
00168     def afterSetUp(self):
00169         self.tool = getattr(self.portal, JSTOOLNAME)
00170         self.tool.clearResources()
00171 
00172     def testScriptCooking(self):
00173         self.tool.registerScript('ham')
00174         self.tool.registerScript('spam')
00175         self.tool.registerScript('eggs')
00176         self.assertEqual(len(self.tool.getResources()), 3)
00177         self.assertEqual(len(self.tool.cookedresources), 1)
00178         self.assertEqual(len(self.tool.concatenatedresources.keys()), 4)
00179         self.tool.setAutoGroupingMode(True)
00180         self.assertEqual(len(self.tool.getResources()), 3)
00181         self.assertEqual(len(self.tool.cookedresources), 1)
00182         self.assertEqual(len(self.tool.concatenatedresources.keys()), 4)
00183 
00184     def testScriptCookingValues(self):
00185         self.tool.registerScript('ham')
00186         self.tool.registerScript('spam')
00187         self.tool.registerScript('eggs')
00188         self.assertEqual(self.tool.concatenatedresources[
00189                          self.tool.cookedresources[0].getId()],
00190                          ['ham', 'spam', 'eggs'])
00191 
00192     def testGetEvaluatedScriptsCollapsing(self):
00193         self.tool.registerScript('ham')
00194         self.tool.registerScript('spam')
00195         self.tool.registerScript('eggs')
00196         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 1)
00197 
00198     def testMoreComplexScriptsCollapsing(self):
00199         self.tool.registerScript('ham')
00200         self.tool.registerScript('spam', expression='string:spam')
00201         self.tool.registerScript('spam spam', expression='string:spam')
00202         self.tool.registerScript('spam spam spam', expression='string:spam')
00203         self.tool.registerScript('eggs')
00204         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 3)
00205         magic_ids = [item.getId() for item in self.tool.getEvaluatedResources(self.folder)]
00206         self.failUnless('ham' in self.tool.concatenatedresources[magic_ids[0]])
00207         self.failUnless('eggs' in self.tool.concatenatedresources[magic_ids[2]])
00208         self.failUnless('spam' in self.tool.concatenatedresources[magic_ids[1]])
00209         self.failUnless('spam spam' in self.tool.concatenatedresources[magic_ids[1]])
00210         self.failUnless('spam spam spam' in self.tool.concatenatedresources[magic_ids[1]])
00211 
00212         self.tool.setAutoGroupingMode(True)
00213         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 2)
00214         magic_ids = [item.getId() for item in self.tool.getEvaluatedResources(self.folder)]
00215         self.failUnless('ham' in self.tool.concatenatedresources[magic_ids[0]])
00216         self.failUnless('eggs' in self.tool.concatenatedresources[magic_ids[0]])
00217         self.failUnless('spam' in self.tool.concatenatedresources[magic_ids[1]])
00218         self.failUnless('spam spam' in self.tool.concatenatedresources[magic_ids[1]])
00219         self.failUnless('spam spam spam' in self.tool.concatenatedresources[magic_ids[1]])
00220 
00221     def testGetEvaluatedScriptsWithExpression(self):
00222         self.tool.registerScript('ham')
00223         self.tool.registerScript('spam', expression='python:1')
00224         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 2)
00225         self.tool.setAutoGroupingMode(True)
00226         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 2)
00227 
00228     def testGetEvaluatedScriptsWithFailingExpression(self):
00229         self.tool.registerScript('ham')
00230         self.tool.registerScript('spam', expression='python:0')
00231         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 1)
00232         self.tool.setAutoGroupingMode(True)
00233         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 1)
00234 
00235     def testGetEvaluatedScriptsWithContextualExpression(self):
00236         self.folder.invokeFactory('Document', 'eggs')
00237         self.tool.registerScript('spam', expression='python:"eggs" in object.objectIds()')
00238         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 1)
00239         self.tool.setAutoGroupingMode(True)
00240         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 1)
00241 
00242     def testCollapsingScriptsLookup(self):
00243         self.tool.registerScript('ham')
00244         self.tool.registerScript('spam', expression='string:spam')
00245         self.tool.registerScript('spam spam', expression='string:spam')
00246         evaluated = self.tool.getEvaluatedResources(self.folder)
00247         self.assertEqual(len(evaluated), 2)
00248         self.tool.setAutoGroupingMode(True)
00249         evaluated = self.tool.getEvaluatedResources(self.folder)
00250         self.assertEqual(len(evaluated), 2)
00251 
00252     def testRenderingIsInTheRightOrder(self):
00253         self.tool.registerScript('ham', expression='string:ham')
00254         self.tool.registerScript('spam', expression='string:spam')
00255         evaluated = self.tool.getEvaluatedResources(self.folder)
00256         magic_ids = [item.getId() for item in evaluated]
00257         ids = []
00258         for magic_id in magic_ids:
00259             self.assertEqual(len(self.tool.concatenatedresources[magic_id]), 1)
00260             ids.append(self.tool.concatenatedresources[magic_id][0])
00261         self.failUnless(ids[0] == 'ham')
00262         self.failUnless(ids[1] == 'spam')
00263 
00264     def testRenderingScriptLinks(self):
00265         self.tool.registerScript('ham')
00266         self.tool.registerScript('ham2merge')
00267         self.tool.registerScript('spam', expression='string:spam')
00268         self.tool.registerScript('test_rr_1.css', inline='1')
00269         view = self.portal.restrictedTraverse('@@plone')
00270         viewletmanager = getMultiAdapter((self.portal, self.portal.REQUEST, view), IContentProvider, name = u'plone.resourceregistries.scripts')
00271         viewletmanager.update()
00272         all = viewletmanager.render()
00273         evaluated = self.tool.getEvaluatedResources(self.folder)
00274         magic_ids = [item.getId() for item in evaluated]
00275         self.failUnless('background-color' in all)
00276         self.failUnless('<script' in all)
00277         self.failUnless('/%s' %(magic_ids[1],) in all)
00278 
00279     def testReenderingConcatenatesInline(self):
00280         self.tool.registerScript('test_rr_1.css', inline='1')
00281         self.tool.registerScript('test_rr_2.css', inline='1')
00282         view = self.portal.restrictedTraverse('@@plone')
00283         viewletmanager = getMultiAdapter((self.portal, self.portal.REQUEST, view), IContentProvider, name = u'plone.resourceregistries.scripts')
00284         viewletmanager.update()
00285         all = viewletmanager.render()
00286         self.failUnless('background-color' in all)
00287         self.failUnless('blue' in all)
00288 
00289     def testRenderingWorksInMainTemplate(self):
00290         renderedpage = getattr(self.portal, 'index_html')()
00291         self.failIf('background-color' in renderedpage)
00292         self.tool.registerScript('test_rr_1.css', inline=1)
00293         renderedpage = getattr(self.portal, 'index_html')()
00294         self.failUnless('background-color' in renderedpage)
00295 
00296 
00297 class TestScriptMoving(RegistryTestCase):
00298 
00299     def afterSetUp(self):
00300         self.tool = getattr(self.portal, JSTOOLNAME)
00301         self.tool.clearResources()
00302 
00303     def testScriptMoveDown(self):
00304         self.tool.registerScript('ham')
00305         self.tool.registerScript('spam')
00306         self.tool.registerScript('eggs')
00307         self.assertEqual(self.tool.getResourceIds(),
00308                          ('ham', 'spam', 'eggs'))
00309         self.tool.moveResourceDown('ham')
00310         self.assertEqual(self.tool.getResourceIds(),
00311                          ('spam', 'ham', 'eggs'))
00312 
00313     def testScriptMoveDownAtEnd(self):
00314         self.tool.registerScript('ham')
00315         self.tool.registerScript('spam')
00316         self.tool.registerScript('eggs')
00317         self.assertEqual(self.tool.getResourceIds(),
00318                          ('ham', 'spam', 'eggs'))
00319         self.tool.moveResourceDown('eggs')
00320         self.assertEqual(self.tool.getResourceIds(),
00321                          ('ham', 'spam', 'eggs'))
00322 
00323     def testScriptMoveUp(self):
00324         self.tool.registerScript('ham')
00325         self.tool.registerScript('spam')
00326         self.tool.registerScript('eggs')
00327         self.assertEqual(self.tool.getResourceIds(),
00328                          ('ham', 'spam', 'eggs'))
00329         self.tool.moveResourceUp('spam')
00330         self.assertEqual(self.tool.getResourceIds(),
00331                          ('spam', 'ham', 'eggs'))
00332 
00333     def testScriptMoveUpAtStart(self):
00334         self.tool.registerScript('ham')
00335         self.tool.registerScript('spam')
00336         self.tool.registerScript('eggs')
00337         self.assertEqual(self.tool.getResourceIds(),
00338                          ('ham', 'spam', 'eggs'))
00339         self.tool.moveResourceUp('ham')
00340         self.assertEqual(self.tool.getResourceIds(),
00341                          ('ham', 'spam', 'eggs'))
00342 
00343     def testScriptMoveIllegalId(self):
00344         self.tool.registerScript('ham')
00345         self.tool.registerScript('spam')
00346         self.tool.registerScript('eggs')
00347         self.assertEqual(self.tool.getResourceIds(),
00348                          ('ham', 'spam', 'eggs'))
00349         self.assertRaises(ValueError, self.tool.moveResourceUp, 'foo')
00350 
00351     def testScriptMoveToBottom(self):
00352         self.tool.registerScript('ham')
00353         self.tool.registerScript('spam')
00354         self.tool.registerScript('eggs')
00355         self.assertEqual(self.tool.getResourceIds(),
00356                          ('ham', 'spam', 'eggs'))
00357         self.tool.moveResourceToBottom('ham')
00358         self.assertEqual(self.tool.getResourceIds(),
00359                          ('spam', 'eggs', 'ham'))
00360 
00361     def testScriptMoveToTop(self):
00362         self.tool.registerScript('ham')
00363         self.tool.registerScript('spam')
00364         self.tool.registerScript('eggs')
00365         self.assertEqual(self.tool.getResourceIds(),
00366                          ('ham', 'spam', 'eggs'))
00367         self.tool.moveResourceToTop('eggs')
00368         self.assertEqual(self.tool.getResourceIds(),
00369                          ('eggs', 'ham', 'spam'))
00370 
00371     def testScriptMoveBefore(self):
00372         self.tool.registerScript('ham')
00373         self.tool.registerScript('spam')
00374         self.tool.registerScript('eggs')
00375         self.tool.registerScript('bacon')
00376         self.assertEqual(self.tool.getResourceIds(),
00377                          ('ham', 'spam', 'eggs', 'bacon'))
00378         self.tool.moveResourceBefore('bacon', 'ham')
00379         self.assertEqual(self.tool.getResourceIds(),
00380                          ('bacon', 'ham', 'spam', 'eggs'))
00381 
00382     def testScriptMoveAfter(self):
00383         self.tool.registerScript('ham')
00384         self.tool.registerScript('spam')
00385         self.tool.registerScript('eggs')
00386         self.tool.registerScript('bacon')
00387         self.assertEqual(self.tool.getResourceIds(),
00388                          ('ham', 'spam', 'eggs', 'bacon'))
00389         self.tool.moveResourceAfter('bacon', 'ham')
00390         self.assertEqual(self.tool.getResourceIds(),
00391                          ('ham', 'bacon', 'spam', 'eggs'))
00392 
00393     def testScriptMove(self):
00394         self.tool.registerScript('ham')
00395         self.tool.registerScript('spam')
00396         self.tool.registerScript('eggs')
00397         self.tool.registerScript('bacon')
00398         self.assertEqual(self.tool.getResourceIds(),
00399                          ('ham', 'spam', 'eggs', 'bacon'))
00400         self.tool.moveResource('ham', 2)
00401         self.assertEqual(self.tool.getResourceIds(),
00402                          ('spam', 'eggs', 'ham', 'bacon'))
00403         self.tool.moveResource('bacon', 0)
00404         self.assertEqual(self.tool.getResourceIds(),
00405                          ('bacon', 'spam', 'eggs', 'ham'))
00406 
00407 class TestJSTraversal(RegistryTestCase):
00408 
00409     def afterSetUp(self):
00410         self.tool = getattr(self.portal, JSTOOLNAME)
00411         self.tool.clearResources()
00412         self.tool.registerScript('test_rr_1.js')
00413 
00414     def testGetItemTraversal(self):
00415         self.failUnless(self.tool['test_rr_1.js'])
00416 
00417     def testGetItemTraversalContent(self):
00418         self.failUnless('running' in str(
00419                         self.tool['test_rr_1.js']))
00420 
00421     def testRestrictedTraverseContent(self):
00422         self.failUnless('running' in str(
00423                         self.portal.restrictedTraverse(
00424                             'portal_javascripts/test_rr_1.js')))
00425 
00426     def testRestrictedTraverseComposition(self):
00427         self.tool.registerScript('test_rr_2.css')
00428         scripts = self.tool.getEvaluatedResources(self.portal)
00429         self.assertEqual(len(scripts), 1)
00430         magicId = scripts[0].getId()
00431         content = str(self.portal.restrictedTraverse('portal_javascripts/%s' % magicId))
00432         # XXX: Review
00433         #self.failUnless('test_rr_1.js' in content)
00434         #self.failUnless('registerPloneFunction' in content)
00435 
00436     def testCompositesWithBrokenId(self):
00437         self.tool.registerScript('nonexistant.js')
00438         scripts = self.tool.getEvaluatedResources(self.portal)
00439         self.assertEqual(len(scripts), 1)
00440         magicId = scripts[0].getId()
00441         content = str(self.portal.restrictedTraverse('portal_javascripts/%s' % magicId))
00442 
00443 
00444 class TestPublishing(FunctionalRegistryTestCase):
00445 
00446     def afterSetUp(self):
00447         self.tool = getattr(self.portal, JSTOOLNAME)
00448         self.tool.clearResources()
00449         self.toolpath = '/' + self.tool.absolute_url(1)
00450         self.folderpath = '/' + self.folder.absolute_url(1)
00451         self.tool.registerScript('test_rr_1.js')
00452         self.folder.invokeFactory('Document', 'index_html')
00453 
00454     def testPublishJSThroughTool(self):
00455         response = self.publish(self.toolpath + '/test_rr_1.js')
00456         self.assertEqual(response.getStatus(), 200)
00457         self.assertEqual(response.getHeader('Content-Type'), 'application/x-javascript;charset=utf-8')
00458 
00459     def testPublishNonMagicJSThroughTool(self):
00460         self.setRoles(['Manager'])
00461         body = """<dtml-var "'joined' + 'string'">"""
00462         self.folder.addDTMLMethod('testmethod', file=body)
00463         self.tool.registerScript('testmethod')
00464         response = self.publish(self.toolpath + '/testmethod')
00465         self.assertEqual(response.getStatus(), 200)
00466         self.assertEqual(response.getHeader('Content-Type'), 'application/x-javascript;charset=utf-8')
00467 
00468     def testPublishPageWithInlineJS(self):
00469         # This one fails from string/utf-8 concatenation
00470         response = self.publish(self.folderpath)
00471         self.assertEqual(response.getStatus(), 200)
00472         self.assertEqual(response.getHeader('Content-Type'),
00473                          'text/html;charset=utf-8')
00474         self.tool.clearResources()
00475         self.tool.registerScript('test_rr_1.js', inline=True)
00476         # Test that the main page retains its content-type
00477         response = self.publish(self.folderpath)
00478         self.assertEqual(response.getHeader('Content-Type'),
00479                          'text/html;charset=utf-8')
00480         self.assertEqual(response.getStatus(), 200)
00481 
00482     def testPublishPageWithInlineJS2(self):
00483         self.tool.clearResources()
00484         # Test that the main page retains its content-type
00485         self.setRoles(['Manager'])
00486         body = """<dtml-call "REQUEST.RESPONSE.setHeader('Content-Type', 'text/javascript')">/*and some js comments too*/ """
00487         self.folder.addDTMLMethod('testmethod', file=body)
00488         self.tool.registerScript('testmethod', inline=True)
00489         response = self.publish(self.folderpath)
00490         self.assertEqual(response.getHeader('Content-Type'),
00491                          'text/html;charset=utf-8')
00492         self.assertEqual(response.getStatus(), 200)
00493 
00494 class TestFivePublishing(FunctionalRegistryTestCase):
00495     'Publishing with Five'
00496 
00497     def afterSetUp(self):
00498         # Define some resource
00499         from Products.Five.zcml import load_config
00500         import Products.ResourceRegistries.tests
00501         load_config('test.zcml', Products.ResourceRegistries.tests)
00502         self.tool = getattr(self.portal, JSTOOLNAME)
00503         self.tool.clearResources()
00504         self.tool.registerScript('++resource++test_rr_1.js')
00505         self.toolpath = '/' + self.tool.absolute_url(1)
00506         self.portalpath = '/' + getToolByName(self.portal, 'portal_url')(1)
00507         self.setRoles(['Manager'])
00508         self.portal.invokeFactory('Document', 'index_html')
00509         self.setRoles(['Member'])
00510 
00511     def testPublishFiveResource(self):
00512         response = self.publish(self.toolpath + '/++resource++test_rr_1.js')
00513         self.assertEqual(response.getStatus(), 200)
00514         self.assertEqual(response.getHeader('Content-Type')[:24], 'application/x-javascript')
00515         self.assertEqual("window.alert('running')" in response.getBody(), True)
00516 
00517 class TestDebugMode(FunctionalRegistryTestCase):
00518 
00519     def afterSetUp(self):
00520         self.tool = getattr(self.portal, JSTOOLNAME)
00521         self.tool.clearResources()
00522         self.portalpath = '/' + getToolByName(self.portal, 'portal_url')(1)
00523         self.toolpath = '/' + self.tool.absolute_url(1)
00524 
00525     def testDebugModeSplitting(self):
00526         self.tool.registerScript('ham')
00527         self.tool.registerScript('spam')
00528         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 1)
00529         self.tool.setDebugMode(True)
00530         self.tool.cookResources()
00531         self.assertEqual(len(self.tool.getEvaluatedResources(self.folder)), 2)
00532 
00533     def testDebugModeSplitting2(self):
00534         self.tool.registerScript('ham')
00535         # Publish in normal mode
00536         response = self.publish(self.toolpath+'/ham')
00537         now = DateTime()
00538         days = 7
00539         soon = now + days
00540         self.assertEqual(response.getStatus(), 200)
00541         self.assertEqual(response.getHeader('Expires'), rfc1123_date(soon.timeTime()))
00542         self.assertEqual(response.getHeader('Cache-Control'), 'max-age=%d' % int(days*24*3600))
00543 
00544         # Set debug mode
00545         self.tool.setDebugMode(True)
00546         self.tool.cookResources()
00547         # Publish in debug mode
00548         response = self.publish(self.toolpath+'/ham')
00549         self.failIfEqual(response.getHeader('Expires'), rfc1123_date(soon.timeTime()))
00550         self.assertEqual(response.getHeader('Expires'), rfc1123_date(now.timeTime()))
00551         self.assertEqual(response.getHeader('Cache-Control'), 'max-age=0')
00552 
00553 
00554 class TestZODBTraversal(RegistryTestCase):
00555 
00556     def afterSetUp(self):
00557         self.tool = getattr(self.portal, JSTOOLNAME)
00558         self.tool.clearResources()
00559         self.setRoles(['Manager'])
00560         self.portal.invokeFactory('File',
00561                                    id='testroot.js',
00562                                    format='application/x-javascript',
00563                                    content_type='application/x-javascript',
00564                                    file="window.alert('red')")
00565         self.portal.invokeFactory('Folder', 'subfolder')
00566         self.portal.subfolder.invokeFactory('File',
00567                                    id='testsubfolder.js',
00568                                    format='application/x-javascript',
00569                                    content_type='application/x-javascript',
00570                                    file="window.alert('blue')")
00571 
00572         self.tool.registerScript('testroot.js')
00573         self.tool.registerScript('subfolder/testsubfolder.js')
00574         self.setRoles(['Member'])
00575 
00576     def testGetItemTraversal(self):
00577         self.failUnless(self.tool['testroot.js'])
00578         self.failUnless(self.tool['subfolder/testsubfolder.js'])
00579 
00580     def testGetItemTraversalContent(self):
00581         self.failUnless('red' in str(self.tool['testroot.js']))
00582         self.failUnless('blue' in str(self.tool['subfolder/testsubfolder.js']))
00583         self.failIf('blue' in str(self.tool['testroot.js']))
00584         self.failIf('red' in str(self.tool['subfolder/testsubfolder.js']))
00585 
00586 
00587     def testRestrictedTraverseContent(self):
00588         self.failUnless('red' in str(
00589                         self.portal.restrictedTraverse('portal_javascripts/testroot.js')))
00590         self.failUnless('blue' in str(
00591                         self.portal.restrictedTraverse('portal_javascripts/subfolder/testsubfolder.js')))
00592         self.failIf('blue' in str(
00593                         self.portal.restrictedTraverse('portal_javascripts/testroot.js')))
00594         self.failIf('red' in str(
00595                         self.portal.restrictedTraverse('portal_javascripts/subfolder/testsubfolder.js')))
00596 
00597     def testRestrictedTraverseComposition(self):
00598         scripts = self.tool.getEvaluatedResources(self.portal)
00599         self.assertEqual(len(scripts), 1)
00600         magicId = scripts[0].getId()
00601         content = str(self.portal.restrictedTraverse('portal_javascripts/%s' % magicId))
00602         self.failUnless('red' in content)
00603         self.failUnless('blue' in content)
00604 
00605     def testContextDependantInlineJS(self):
00606         self.tool.clearResources()
00607         self.setRoles(['Manager'])
00608         self.portal.invokeFactory('Folder', 'folder1')
00609         self.portal.invokeFactory('Folder', 'folder2')
00610         self.portal.folder1.invokeFactory('File',
00611                                    id='context.js',
00612                                    format='application/x-javascript',
00613                                    content_type='application/x-javascript',
00614                                    file="window.alert('pink')")
00615         self.portal.folder2.invokeFactory('File',
00616                                    id='context.js',
00617                                    format='application/x-javascript',
00618                                    content_type='application/x-javascript',
00619                                    file="window.alert('purple')")
00620         self.tool.registerScript('context.js', inline=True)
00621         self.setRoles(['Member'])
00622         view = self.portal.restrictedTraverse('@@plone')
00623         viewletmanager = getMultiAdapter((self.portal.folder1, self.portal.folder1.REQUEST, view), IContentProvider, name = u'plone.resourceregistries.scripts')
00624         viewletmanager.update()
00625         content = viewletmanager.render()
00626         self.failUnless('pink' in content)
00627         self.failIf('purple' in content)
00628         view = self.portal.restrictedTraverse('@@plone')
00629         viewletmanager = getMultiAdapter((self.portal.folder2, self.portal.folder2.REQUEST, view), IContentProvider, name = u'plone.resourceregistries.scripts')
00630         viewletmanager.update()
00631         content = viewletmanager.render()
00632         self.failUnless('purple' in content)
00633         self.failIf('pink' in content)
00634 
00635 class TestResourcePermissions(FunctionalRegistryTestCase):
00636 
00637     def afterSetUp(self):
00638         self.tool = getattr(self.portal, JSTOOLNAME)
00639         self.toolpath = '/' + self.tool.absolute_url(1)
00640         self.tool.clearResources()
00641         self.tool.registerScript('testroot.js', cookable=False)
00642         self.tool.registerScript('test_rr_1.js')
00643         self.setRoles(['Manager'])
00644         self.portal.invokeFactory('File',
00645                                    id='testroot.js',
00646                                    format='application/x-javascript',
00647                                    content_type='application/x-javascript',
00648                                    file="window.alert('red')")
00649 
00650         script = self.portal.restrictedTraverse('testroot.js')
00651 
00652         script.manage_permission('View',['Manager'], acquire=0)
00653         script.manage_permission('Access contents information',['Manager'], acquire=0)
00654         self.setRoles(['Member'])
00655 
00656     def testUnauthorizedGetItem(self):
00657         try:
00658             content = str(self.tool['testroot.js'])
00659         except Unauthorized:
00660             return
00661 
00662         self.fail()
00663 
00664     def testUnauthorizedTraversal(self):
00665         try:
00666             content = str(self.portal.restrictedTraverse('portal_javascripts/testroot.js'))
00667         except Unauthorized:
00668             return
00669 
00670         self.fail()
00671 
00672     def testTestUnauthorizedTraversal(self):
00673         try:
00674             content = str(self.portal.restrictedTraverse('portal_javascripts/testroot.js'))
00675         except Unauthorized:
00676             return
00677 
00678         self.fail()
00679 
00680     def testRaiseUnauthorizedOnPublish(self):
00681         response = self.publish(self.toolpath + '/testroot.js')
00682         #Will be 302 if CookieCrumbler is enabled
00683         self.failUnless(response.getStatus() in [302, 403, 401])
00684 
00685     def testRemovedFromResources(self):
00686         # This test assumes that content is not merged or cached
00687         self.tool.unregisterResource('test_rr_1.js')
00688         self.tool.registerResource('test_rr_1.js', cookable=False, cacheable=False)
00689         scripts = self.tool.getEvaluatedResources(self.portal)
00690         ids = [item.getId() for item in scripts]
00691         self.failIf('testroot.js' in ids)
00692         self.failUnless('test_rr_1.js' in ids)
00693         # Return resources to normal (not sure if this is needed)
00694         self.tool.unregisterResource('test_rr_1.js')
00695         self.tool.registerScript('test_rr_1.js')
00696 
00697     def testRemovedFromMergedResources(self):
00698         self.tool.unregisterResource('testroot.js')
00699         self.tool.registerScript('testroot.js')
00700         scripts = self.tool.getEvaluatedResources(self.portal)
00701         magicId = None
00702         for script in scripts:
00703             id = script.getId()
00704             if '-cachekey' in id:
00705                 magicId = id
00706         self.failUnless(magicId)
00707         content = str(self.portal.restrictedTraverse('portal_javascripts/%s' % magicId))
00708         self.failIf('red' in content)
00709         self.failUnless('not authorized' in content)
00710         self.failUnless('running' in content)
00711 
00712     def testAuthorizedGetItem(self):
00713         self.setRoles(['Manager'])
00714         try:
00715             content = str(self.tool['testroot.js'])
00716         except Unauthorized:
00717             self.fail()
00718 
00719     def testAuthorizedTraversal(self):
00720         self.setRoles(['Manager'])
00721         try:
00722             content = str(self.portal.restrictedTraverse('portal_css/testroot.js'))
00723         except Unauthorized:
00724             self.fail()
00725 
00726     def testAuthorizedOnPublish(self):
00727         authstr = "%s:%s" % (portal_owner, default_password)
00728         response = self.publish(self.toolpath + '/testroot.js', basic=authstr)
00729         self.failUnlessEqual(response.getStatus(), 200)
00730 
00731 class TestMergingDisabled(RegistryTestCase):
00732 
00733     def afterSetUp(self):
00734         self.tool = getattr(self.portal, JSTOOLNAME)
00735         self.tool.clearResources()
00736         self.tool.registerScript('testroot.js')
00737         self.tool.registerScript('test_rr_1.js')
00738         self.tool.registerScript('simple2.js', cookable=False)
00739         self.setRoles(['Manager'])
00740         self.portal.invokeFactory('File',
00741                                    id='testroot.js',
00742                                    format='application/x-javascript',
00743                                    content_type='application/x-javascript',
00744                                    file="window.alert('green')")
00745         self.portal.invokeFactory('File',
00746                                    id='simple2.js',
00747                                    format='application/x-javascript',
00748                                    content_type='application/x-javascript',
00749                                    file="window.alert('blue')")
00750         self.setRoles(['Member'])
00751 
00752     def testDefaultStylesheetCookableAttribute(self):
00753         self.failUnless(self.tool.getResources()[self.tool.getResourcePosition('test_rr_1.js')].getCookable())
00754         self.failUnless(self.tool.getResources()[self.tool.getResourcePosition('testroot.js')].getCookable())
00755 
00756     def testStylesheetCookableAttribute(self):
00757         self.failIf(self.tool.getResources()[self.tool.getResourcePosition('simple2.js')].getCookable())
00758 
00759     def testNumberOfResources(self):
00760         self.assertEqual(len(self.tool.getResources()), 3)
00761         self.assertEqual(len(self.tool.cookedresources), 2)
00762         self.assertEqual(len(self.tool.concatenatedresources), 5)
00763         scripts = self.tool.getEvaluatedResources(self.portal)
00764         self.assertEqual(len(scripts), 2)
00765 
00766     def testCompositionWithLastUncooked(self):
00767         self.tool.moveResourceToBottom('simple2.js')
00768         self.assertEqual(len(self.tool.getResources()), 3)
00769         self.assertEqual(len(self.tool.cookedresources), 2)
00770         self.assertEqual(len(self.tool.concatenatedresources), 5)
00771         scripts = self.tool.getEvaluatedResources(self.portal)
00772         self.assertEqual(len(scripts), 2)
00773         magicIds = []
00774         for script in scripts:
00775             id = script.getId()
00776             if '-cachekey' in id:
00777                 magicIds.append(id)
00778         self.failUnless(magicIds[-1].startswith('simple2'))
00779         content = str(self.portal.restrictedTraverse('portal_javascripts/%s' % magicIds[-2]))
00780         self.failUnless('running' in content)
00781         self.failUnless('green' in content)
00782         self.failIf('blue' in content)
00783         content = str(self.portal.restrictedTraverse('portal_javascripts/%s' % magicIds[-1]))
00784         self.failIf('running' in content)
00785         self.failIf('green' in content)
00786         self.failUnless('blue' in content)
00787         content = str(self.portal.restrictedTraverse('portal_javascripts/simple2.js'))
00788         self.failUnless('blue' in content)
00789 
00790     def testCompositionWithFirstUncooked(self):
00791         self.tool.moveResourceToTop('simple2.js')
00792         self.assertEqual(len(self.tool.getResources()), 3)
00793         self.assertEqual(len(self.tool.cookedresources), 2)
00794         self.assertEqual(len(self.tool.concatenatedresources), 5)
00795         scripts = self.tool.getEvaluatedResources(self.portal)
00796         self.assertEqual(len(scripts), 2)
00797         magicId = None
00798         for script in scripts:
00799             id = script.getId()
00800             if '-cachekey' in id:
00801                 magicId = id
00802         self.failUnless(magicId)
00803         content = str(self.portal.restrictedTraverse('portal_javascripts/%s' % magicId))
00804         self.failUnless('running' in content)
00805         self.failUnless('green' in content)
00806         self.failIf('blue' in content)
00807         content = str(self.portal.restrictedTraverse('portal_javascripts/simple2.js'))
00808         self.failUnless('blue' in content)
00809 
00810     def testCompositionWithMiddleUncooked(self):
00811         self.tool.moveResourceToTop('simple2.js')
00812         self.tool.moveResourceDown('simple2.js')
00813         self.assertEqual(len(self.tool.getResources()), 3)
00814         self.assertEqual(len(self.tool.cookedresources), 3)
00815         self.assertEqual(len(self.tool.concatenatedresources), 6)
00816         scripts = self.tool.getEvaluatedResources(self.portal)
00817         self.assertEqual(len(scripts), 3)
00818         content = str(self.portal.restrictedTraverse('portal_javascripts/simple2.js'))
00819         self.failUnless('blue' in content)
00820         content = str(self.portal.restrictedTraverse('portal_javascripts/test_rr_1.js'))
00821         self.failUnless('running' in content)
00822         content = str(self.portal.restrictedTraverse('portal_javascripts/testroot.js'))
00823         self.failUnless('green' in content)
00824 
00825     def testLargerCompositionWithMiddleUncooked(self):
00826         self.setRoles(['Manager'])
00827         self.portal.invokeFactory('File',
00828                                    id='testpurple.js',
00829                                    format='application/x-javascript',
00830                                    content_type='application/x-javascript',
00831                                    file="window.alert('purple')")
00832         self.portal.invokeFactory('File',
00833                                    id='testpink.js',
00834                                    format='application/x-javascript',
00835                                    content_type='application/x-javascript',
00836                                    file="window.alert('pink')")
00837         self.setRoles(['Member'])
00838         self.tool.registerScript('testpurple.js')
00839         self.tool.registerScript('testpink.js')
00840         self.tool.moveResourceToTop('simple2.js')
00841         self.tool.moveResourceDown('simple2.js', 2)
00842         #Now have [[green,running],blue,[purple,pink]]
00843         self.assertEqual(len(self.tool.getResources()), 5)
00844         self.assertEqual(len(self.tool.cookedresources), 3)
00845         self.assertEqual(len(self.tool.concatenatedresources), 8)
00846         scripts = self.tool.getEvaluatedResources(self.portal)
00847         self.assertEqual(len(scripts), 3)
00848         magicIds = []
00849         for script in scripts:
00850             id = script.getId()
00851             if '-cachekey' in id:
00852                 magicIds.append(id)
00853         self.assertEqual(len(magicIds), 3)
00854         content = str(self.portal.restrictedTraverse('portal_javascripts/%s' % magicIds[0]))
00855         self.failUnless('running' in content)
00856         self.failUnless('green' in content)
00857         self.failIf('pink' in content)
00858         self.failIf('purple' in content)
00859         content = str(self.portal.restrictedTraverse('portal_javascripts/%s' % magicIds[2]))
00860         self.failUnless('pink' in content)
00861         self.failUnless('purple' in content)
00862         self.failIf('running' in content)
00863         self.failIf('green' in content)
00864         content = str(self.portal.restrictedTraverse('portal_javascripts/simple2.js'))
00865         self.failUnless('blue' in content)
00866 
00867 
00868 class TestUnicodeAwareness(RegistryTestCase):
00869 
00870     def afterSetUp(self):
00871         from Products.Five.zcml import load_config
00872         import Products.ResourceRegistries.tests
00873         load_config('test.zcml', Products.ResourceRegistries.tests)
00874         self.tool = getattr(self.portal, JSTOOLNAME)
00875         self.tool.clearResources()
00876         body = "/* add a comment with unicode\n   \xc3\x9bercool! */\nwindow.alert('running')\n"
00877         self.setRoles(['Manager'])
00878         self.portal.addDTMLMethod('testmethod.js', file=body)
00879         self.portal.invokeFactory('File',
00880                                    id='testfile.js',
00881                                    format='application/x-javascript',
00882                                    content_type='application/x-javascript;charset=utf-8',
00883                                    file=body)
00884         self.setRoles(['Member'])
00885 
00886     def testGetOriginal(self):
00887         # this needs to be first because it's a zpt returning unicode
00888         self.tool.registerScript('test_rr_1.js')
00889         self.tool.registerScript('++resource++test_rr_1.js')
00890         self.tool.registerScript('test_rr_2.js')
00891         self.tool.registerScript('test_rr_3.js')
00892         self.tool.registerScript('testmethod.js')
00893         self.tool.registerScript('testfile.js')
00894         scripts = self.tool.getEvaluatedResources(self.portal)
00895         magicId = None
00896         for script in scripts:
00897             id = script.getId()
00898             if '-cachekey' in id:
00899                 magicId = id
00900         self.failUnless(magicId)
00901         content = self.tool.getResourceContent(magicId, self.portal, original=True)
00902 
00903 
00904 class TestCachingHeaders(FunctionalRegistryTestCase):
00905     def afterSetUp(self):
00906         self.tool = getattr(self.portal, JSTOOLNAME)
00907         self.skins_tool = getToolByName(self.portal, 'portal_skins')
00908         self.tool.clearResources()
00909         self.portalpath = '/' + getToolByName(self.portal, 'portal_url')(1)
00910         self.toolpath = '/' + self.tool.absolute_url(1)
00911 
00912     def testCachingHeadersFromTool(self):
00913         self.tool.registerScript('ham')
00914         # Publish
00915         response = self.publish(self.toolpath+'/ham')
00916         now = DateTime()
00917         days = 7
00918         soon = now + days
00919         self.assertEqual(response.getStatus(), 200)
00920         self.assertEqual(response.getHeader('Expires'), rfc1123_date(soon.timeTime()))
00921         self.assertEqual(response.getHeader('Cache-Control'), 'max-age=%d' % int(days*24*3600))
00922 
00923         # Publish again
00924         response = self.publish(self.toolpath+'/ham')
00925         now = DateTime()
00926         days = 7
00927         soon = now + days
00928         self.assertEqual(response.getStatus(), 200)
00929         self.assertEqual(response.getHeader('Expires'), rfc1123_date(soon.timeTime()))
00930         self.assertEqual(response.getHeader('Cache-Control'), 'max-age=%d' % int(days*24*3600))
00931 
00932     def testCachingHeadersFromSkin(self):
00933         self.tool.registerScript('ham')
00934         # Publish in normal mode
00935         skinpath = self.skins_tool.getDefaultSkin()
00936         url = '%s/%s/ham' % (self.toolpath, skinpath)
00937         response = self.publish(url)
00938         now = DateTime()
00939         days = 7
00940         soon = now + days
00941         self.assertEqual(response.getStatus(), 200)
00942         self.assertEqual(response.getHeader('Expires'), rfc1123_date(soon.timeTime()))
00943         self.assertEqual(response.getHeader('Cache-Control'), 'max-age=%d' % int(days*24*3600))
00944 
00945         # Publish again
00946         response = self.publish(url)
00947         now = DateTime()
00948         days = 7
00949         soon = now + days
00950         self.assertEqual(response.getStatus(), 200)
00951         self.assertEqual(response.getHeader('Expires'), rfc1123_date(soon.timeTime()))
00952         self.assertEqual(response.getHeader('Cache-Control'), 'max-age=%d' % int(days*24*3600))
00953 
00954     def testCachingHeadersFromToolWithRAMCache(self):
00955         ram_cache_id = 'RAMCache'
00956         self.tool.ZCacheable_setManagerId(ram_cache_id)
00957         self.tool.ZCacheable_setEnabled(1)
00958         self.tool.registerScript('ham')
00959         # Publish
00960         response = self.publish(self.toolpath+'/ham')
00961         now = DateTime()
00962         days = 7
00963         soon = now + days
00964         self.assertEqual(response.getStatus(), 200)
00965         self.assertEqual(response.getHeader('Expires'), rfc1123_date(soon.timeTime()))
00966         self.assertEqual(response.getHeader('Cache-Control'), 'max-age=%d' % int(days*24*3600))
00967 
00968         # Publish again
00969         response = self.publish(self.toolpath+'/ham')
00970         now = DateTime()
00971         days = 7
00972         soon = now + days
00973         self.assertEqual(response.getStatus(), 200)
00974         self.assertEqual(response.getHeader('Expires'), rfc1123_date(soon.timeTime()))
00975         self.assertEqual(response.getHeader('Cache-Control'), 'max-age=%d' % int(days*24*3600))
00976 
00977     def testCachingHeadersFromSkinWithRAMCache(self):
00978         ram_cache_id = 'RAMCache'
00979         self.tool.ZCacheable_setManagerId(ram_cache_id)
00980         self.tool.ZCacheable_setEnabled(1)
00981         self.tool.registerScript('ham')
00982         # Publish in normal mode
00983         skinpath = self.skins_tool.getDefaultSkin()
00984         url = '%s/%s/ham' % (self.toolpath, skinpath)
00985         response = self.publish(url)
00986         now = DateTime()
00987         days = 7
00988         soon = now + days
00989         self.assertEqual(response.getStatus(), 200)
00990         self.assertEqual(response.getHeader('Expires'), rfc1123_date(soon.timeTime()))
00991         self.assertEqual(response.getHeader('Cache-Control'), 'max-age=%d' % int(days*24*3600))
00992 
00993         # Publish again
00994         response = self.publish(url)
00995         now = DateTime()
00996         days = 7
00997         soon = now + days
00998         self.assertEqual(response.getStatus(), 200)
00999         self.assertEqual(response.getHeader('Expires'), rfc1123_date(soon.timeTime()))
01000         self.assertEqual(response.getHeader('Cache-Control'), 'max-age=%d' % int(days*24*3600))
01001 
01002 
01003 def test_suite():
01004     from unittest import TestSuite, makeSuite
01005     suite = TestSuite()
01006     suite.addTest(makeSuite(TestJSImplementation))
01007     suite.addTest(makeSuite(TestJSTool))
01008     suite.addTest(makeSuite(TestJSSkin))
01009     suite.addTest(makeSuite(testJSZMIMethods))
01010     suite.addTest(makeSuite(TestJSScriptRegistration))
01011     suite.addTest(makeSuite(TestJSScriptRenaming))
01012     suite.addTest(makeSuite(TestJSToolSecurity))
01013     suite.addTest(makeSuite(TestJSToolExpression))
01014     suite.addTest(makeSuite(TestJSScriptCooking))
01015     suite.addTest(makeSuite(TestScriptMoving))
01016     suite.addTest(makeSuite(TestJSTraversal))
01017     suite.addTest(makeSuite(TestPublishing))
01018     suite.addTest(makeSuite(TestFivePublishing))
01019     suite.addTest(makeSuite(TestDebugMode))
01020     suite.addTest(makeSuite(TestZODBTraversal))
01021     suite.addTest(makeSuite(TestMergingDisabled))
01022     suite.addTest(makeSuite(TestResourcePermissions))
01023     suite.addTest(makeSuite(TestUnicodeAwareness))
01024     suite.addTest(makeSuite(TestCachingHeaders))
01025 
01026     return suite