Back to index

plone3  3.1.7
test_CatalogTool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 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 CatalogTool module.
00014 
00015 $Id: test_CatalogTool.py 77186 2007-06-28 19:06:19Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from AccessControl.SecurityManagement import newSecurityManager
00022 from DateTime import DateTime
00023 
00024 from Products.CMFCore.tests.base.dummy import DummyContent
00025 from Products.CMFCore.tests.base.dummy import DummySite
00026 from Products.CMFCore.tests.base.security import OmnipotentUser
00027 from Products.CMFCore.tests.base.security import UserWithRoles
00028 from Products.CMFCore.tests.base.testcase import SecurityTest
00029 
00030 
00031 class IndexableObjectWrapperTests(unittest.TestCase):
00032 
00033     def _getTargetClass(self):
00034         from Products.CMFCore.CatalogTool import IndexableObjectWrapper
00035 
00036         return IndexableObjectWrapper
00037 
00038     def _makeOne(self, *args, **kw):
00039         return self._getTargetClass()(*args, **kw)
00040 
00041     def test_z2interfaces(self):
00042         from Interface.Verify import verifyClass
00043         from Products.CMFCore.interfaces.portal_catalog \
00044                 import IndexableObjectWrapper as IIndexableObjectWrapper
00045 
00046         verifyClass(IIndexableObjectWrapper, self._getTargetClass())
00047 
00048     def test_z3interfaces(self):
00049         from zope.interface.verify import verifyClass
00050         from Products.CMFCore.interfaces import IIndexableObjectWrapper
00051 
00052         verifyClass(IIndexableObjectWrapper, self._getTargetClass())
00053 
00054     def test_allowedRolesAndUsers(self):
00055         # XXX This test fails when verbose security is enabled in zope.conf,
00056         # because the roles will then contain '_View_Permission' as well as
00057         # 'Manager'.
00058         obj = DummyContent()
00059         w = self._makeOne({}, obj)
00060         self.assertEqual(w.allowedRolesAndUsers(), ['Manager'])
00061 
00062     def test___str__(self):
00063         obj = DummyContent('foo')
00064         w = self._makeOne({}, obj)
00065         self.assertEqual(str(w), str(obj))
00066 
00067     def test_proxied_attributes(self):
00068         obj = DummyContent('foo')
00069         obj.title = 'Foo'
00070         w = self._makeOne({}, obj)
00071         self.assertEqual(w.getId(), 'foo')
00072         self.assertEqual(w.Title(), 'Foo')
00073 
00074     def test_vars(self):
00075         obj = DummyContent()
00076         w = self._makeOne({'bar': 1, 'baz': 2}, obj)
00077         self.assertEqual(w.bar, 1)
00078         self.assertEqual(w.baz, 2)
00079 
00080     def test_provided(self):
00081         from Products.CMFCore.interfaces import IContentish
00082         from Products.CMFCore.interfaces import IIndexableObjectWrapper
00083 
00084         obj = DummyContent()
00085         w = self._makeOne({}, obj)
00086         self.failUnless(IContentish.providedBy(w))
00087         self.failUnless(IIndexableObjectWrapper.providedBy(w))
00088 
00089 
00090 class CatalogToolTests(SecurityTest):
00091 
00092     def _getTargetClass(self):
00093         from Products.CMFCore.CatalogTool import CatalogTool
00094 
00095         return CatalogTool
00096 
00097     def _makeOne(self, *args, **kw):
00098         return self._getTargetClass()(*args, **kw)
00099 
00100     def test_z2interfaces(self):
00101         from Interface.Verify import verifyClass
00102         from Products.CMFCore.interfaces.portal_actions \
00103                 import ActionProvider as IActionProvider
00104         from Products.CMFCore.interfaces.portal_catalog \
00105                 import portal_catalog as ICatalogTool
00106         from Products.ZCatalog.IZCatalog import IZCatalog
00107 
00108         verifyClass(IActionProvider, self._getTargetClass())
00109         verifyClass(ICatalogTool, self._getTargetClass())
00110         verifyClass(IZCatalog, self._getTargetClass())
00111 
00112     def test_z3interfaces(self):
00113         from zope.interface.verify import verifyClass
00114         from Products.CMFCore.interfaces import IActionProvider
00115         from Products.CMFCore.interfaces import ICatalogTool
00116         from Products.ZCatalog.interfaces import IZCatalog
00117 
00118         verifyClass(IActionProvider, self._getTargetClass())
00119         verifyClass(ICatalogTool, self._getTargetClass())
00120         verifyClass(IZCatalog, self._getTargetClass())
00121 
00122     def loginWithRoles(self, *roles):
00123         user = UserWithRoles(*roles).__of__(self.root)
00124         newSecurityManager(None, user)
00125 
00126     def loginManager(self):
00127         user = OmnipotentUser().__of__(self.root)
00128         newSecurityManager(None, user)
00129 
00130     def setupProxyRoles(self, *proxy_roles):
00131         from AccessControl import getSecurityManager
00132         class FauxExecutable:
00133             _proxy_roles = proxy_roles
00134         sm = getSecurityManager()
00135         sm.addContext(FauxExecutable())
00136 
00137     def test_processActions(self):
00138         """
00139             Tracker #405:  CatalogTool doesn't accept optional third
00140             argument, 'idxs', to 'catalog_object'.
00141         """
00142         tool = self._makeOne()
00143         tool.addIndex('SearchableText', 'KeywordIndex')
00144         dummy = DummyContent(catalog=1)
00145 
00146         tool.catalog_object( dummy, '/dummy' )
00147         tool.catalog_object( dummy, '/dummy', [ 'SearchableText' ] )
00148 
00149     def test_search_anonymous(self):
00150         catalog = self._makeOne()
00151         catalog.addIndex('allowedRolesAndUsers', 'KeywordIndex')
00152         dummy = DummyContent(catalog=1)
00153         catalog.catalog_object(dummy, '/dummy')
00154 
00155         self.assertEqual(1, len(catalog._catalog.searchResults()))
00156         self.assertEqual(0, len(catalog.searchResults()))
00157 
00158     def test_search_member_with_valid_roles(self):
00159         catalog = self._makeOne()
00160         catalog.addIndex('allowedRolesAndUsers', 'KeywordIndex')
00161         dummy = DummyContent(catalog=1)
00162         dummy._View_Permission = ('Blob',)
00163         catalog.catalog_object(dummy, '/dummy')
00164 
00165         self.loginWithRoles('Blob')
00166 
00167         self.assertEqual(1, len(catalog._catalog.searchResults()))
00168         self.assertEqual(1, len(catalog.searchResults()))
00169 
00170     def test_search_member_with_valid_roles_but_proxy_roles_limit(self):
00171         catalog = self._makeOne()
00172         catalog.addIndex('allowedRolesAndUsers', 'KeywordIndex')
00173         dummy = DummyContent(catalog=1)
00174         dummy._View_Permission = ('Blob',)
00175         catalog.catalog_object(dummy, '/dummy')
00176 
00177         self.loginWithRoles('Blob')
00178         self.setupProxyRoles('Waggle')
00179 
00180         self.assertEqual(1, len(catalog._catalog.searchResults()))
00181         self.assertEqual(0, len(catalog.searchResults()))
00182 
00183     def test_search_member_wo_valid_roles(self):
00184         catalog = self._makeOne()
00185         catalog.addIndex('allowedRolesAndUsers', 'KeywordIndex')
00186         dummy = DummyContent(catalog=1)
00187         dummy._View_Permission = ('Blob',)
00188         catalog.catalog_object(dummy, '/dummy')
00189 
00190         self.loginWithRoles('Waggle')
00191 
00192         self.assertEqual(1, len(catalog._catalog.searchResults()))
00193         self.assertEqual(0, len(catalog.searchResults()))
00194 
00195     def test_search_member_wo_valid_roles_but_proxy_roles_allow(self):
00196         catalog = self._makeOne()
00197         catalog.addIndex('allowedRolesAndUsers', 'KeywordIndex')
00198         dummy = DummyContent(catalog=1)
00199         dummy._View_Permission = ('Blob',)
00200         catalog.catalog_object(dummy, '/dummy')
00201 
00202         self.loginWithRoles('Waggle')
00203         self.setupProxyRoles('Blob')
00204 
00205         self.assertEqual(1, len(catalog._catalog.searchResults()))
00206         self.assertEqual(1, len(catalog.searchResults()))
00207 
00208     def test_search_inactive(self):
00209         catalog = self._makeOne()
00210         catalog.addIndex('allowedRolesAndUsers', 'KeywordIndex')
00211         catalog.addIndex('effective', 'DateIndex')
00212         catalog.addIndex('expires', 'DateIndex')
00213         now = DateTime()
00214         dummy = DummyContent(catalog=1)
00215         dummy._View_Permission = ('Blob',)
00216 
00217         self.loginWithRoles('Blob')
00218 
00219         # not yet effective
00220         dummy.effective = now+1
00221         dummy.expires = now+2
00222         catalog.catalog_object(dummy, '/dummy')
00223         self.assertEqual(1, len(catalog._catalog.searchResults()))
00224         self.assertEqual(0, len(catalog.searchResults()))
00225 
00226         # already expired
00227         dummy.effective = now-2
00228         dummy.expires = now-1
00229         catalog.catalog_object(dummy, '/dummy')
00230         self.assertEqual(1, len(catalog._catalog.searchResults()))
00231         self.assertEqual(0, len(catalog.searchResults()))
00232 
00233     def test_search_restrict_manager(self):
00234         catalog = self._makeOne()
00235         catalog.addIndex('allowedRolesAndUsers', 'KeywordIndex')
00236         catalog.addIndex('effective', 'DateIndex')
00237         catalog.addIndex('expires', 'DateIndex')
00238         now = DateTime()
00239         dummy = DummyContent(catalog=1)
00240 
00241         self.loginManager()
00242 
00243         # already expired
00244         dummy.effective = now-4
00245         dummy.expires = now-2
00246         catalog.catalog_object(dummy, '/dummy')
00247         self.assertEqual(1, len(catalog._catalog.searchResults()))
00248         self.assertEqual(1, len(catalog.searchResults()))
00249 
00250         self.assertEqual(1, len(catalog.searchResults(
00251             expires={'query': now-3, 'range': 'min'})))
00252         self.assertEqual(0, len(catalog.searchResults(
00253             expires={'query': now-1, 'range': 'min'})))
00254         self.assertEqual(0, len(catalog.searchResults(
00255             expires={'query': now-3, 'range': 'max'})))
00256         self.assertEqual(1, len(catalog.searchResults(
00257             expires={'query': now-1, 'range': 'max'})))
00258         self.assertEqual(1, len(catalog.searchResults(
00259             expires={'query': (now-3, now-1), 'range': 'min:max'})))
00260         self.assertEqual(1, len(catalog.searchResults(
00261             expires={'query': (now-3, now-1), 'range': 'minmax'})))
00262         self.assertEqual(1, len(catalog.searchResults(
00263             expires={'query': now-2})))
00264         self.assertEqual(1, len(catalog.searchResults(
00265             expires={'query': now-2, 'range': None})))
00266 
00267     def test_search_restrict_inactive(self):
00268         catalog = self._makeOne()
00269         catalog.addIndex('allowedRolesAndUsers', 'KeywordIndex')
00270         catalog.addIndex('effective', 'DateIndex')
00271         catalog.addIndex('expires', 'DateIndex')
00272         now = DateTime()
00273         dummy = DummyContent(catalog=1)
00274         dummy._View_Permission = ('Blob',)
00275 
00276         self.loginWithRoles('Blob')
00277 
00278         # already expired
00279         dummy.effective = now-4
00280         dummy.expires = now-2
00281         catalog.catalog_object(dummy, '/dummy')
00282         self.assertEqual(1, len(catalog._catalog.searchResults()))
00283         self.assertEqual(0, len(catalog.searchResults()))
00284 
00285         self.assertEqual(0, len(catalog.searchResults(
00286             expires={'query': now-3, 'range': 'min'})))
00287         self.assertEqual(0, len(catalog.searchResults(
00288             expires={'query': now-3, 'range': 'max'})))
00289         self.assertEqual(0, len(catalog.searchResults(
00290             expires={'query': now+3, 'range': 'min'})))
00291         self.assertEqual(0, len(catalog.searchResults(
00292             expires={'query': now+3, 'range': 'max'})))
00293         self.assertEqual(0, len(catalog.searchResults(
00294             expires={'query': (now-3, now-1), 'range': 'min:max'})))
00295         self.assertEqual(0, len(catalog.searchResults(
00296             expires={'query': (now-3, now-1), 'range': 'minmax'})))
00297         self.assertEqual(0, len(catalog.searchResults(
00298             expires={'query': now-2, 'range': None})))
00299 
00300     def test_search_restrict_visible(self):
00301         catalog = self._makeOne()
00302         catalog.addIndex('allowedRolesAndUsers', 'KeywordIndex')
00303         catalog.addIndex('effective', 'DateIndex')
00304         catalog.addIndex('expires', 'DateIndex')
00305         now = DateTime()
00306         dummy = DummyContent(catalog=1)
00307         dummy._View_Permission = ('Blob',)
00308 
00309         self.loginWithRoles('Blob')
00310 
00311         # visible
00312         dummy.effective = now-2
00313         dummy.expires = now+2
00314         catalog.catalog_object(dummy, '/dummy')
00315         self.assertEqual(1, len(catalog._catalog.searchResults()))
00316         self.assertEqual(1, len(catalog.searchResults()))
00317 
00318         self.assertEqual(0, len(catalog.searchResults(
00319             effective={'query': now-1, 'range': 'min'})))
00320         self.assertEqual(1, len(catalog.searchResults(
00321             effective={'query': now-1, 'range': 'max'})))
00322         self.assertEqual(0, len(catalog.searchResults(
00323             effective={'query': now+1, 'range': 'min'})))
00324         self.assertEqual(1, len(catalog.searchResults(
00325             effective={'query': now+1, 'range': 'max'})))
00326         self.assertEqual(0, len(catalog.searchResults(
00327             effective={'query': (now-1, now+1), 'range': 'min:max'})))
00328         self.assertEqual(0, len(catalog.searchResults(
00329             effective={'query': (now-1, now+1), 'range': 'minmax'})))
00330         self.assertEqual(1, len(catalog.searchResults(
00331             expires={'query': now-2, 'range': None})))
00332 
00333         self.assertEqual(1, len(catalog.searchResults(
00334             effective={'query': now-3, 'range': 'min'})))
00335         self.assertEqual(0, len(catalog.searchResults(
00336             effective={'query': now-3, 'range': 'max'})))
00337         self.assertEqual(0, len(catalog.searchResults(
00338             effective={'query': now+3, 'range': 'min'})))
00339         self.assertEqual(1, len(catalog.searchResults(
00340             effective={'query': now+3, 'range': 'max'})))
00341         self.assertEqual(1, len(catalog.searchResults(
00342             effective={'query': (now-3, now+3), 'range': 'min:max'})))
00343         self.assertEqual(1, len(catalog.searchResults(
00344             effective={'query': (now-3, now+3), 'range': 'minmax'})))
00345 
00346         self.assertEqual(1, len(catalog.searchResults(
00347             expires={'query': now-1, 'range': 'min'})))
00348         self.assertEqual(0, len(catalog.searchResults(
00349             expires={'query': now-1, 'range': 'max'})))
00350         self.assertEqual(1, len(catalog.searchResults(
00351             expires={'query': now+1, 'range': 'min'})))
00352         self.assertEqual(0, len(catalog.searchResults(
00353             expires={'query': now+1, 'range': 'max'})))
00354         self.assertEqual(0, len(catalog.searchResults(
00355             expires={'query': (now-1, now+1), 'range': 'min:max'})))
00356         self.assertEqual(0, len(catalog.searchResults(
00357             expires={'query': (now-1, now+1), 'range': 'minmax'})))
00358 
00359         self.assertEqual(1, len(catalog.searchResults(
00360             expires={'query': now-3, 'range': 'min'})))
00361         self.assertEqual(0, len(catalog.searchResults(
00362             expires={'query': now-3, 'range': 'max'})))
00363         self.assertEqual(0, len(catalog.searchResults(
00364             expires={'query': now+3, 'range': 'min'})))
00365         self.assertEqual(1, len(catalog.searchResults(
00366             expires={'query': now+3, 'range': 'max'})))
00367         self.assertEqual(1, len(catalog.searchResults(
00368             expires={'query': (now-3, now+3), 'range': 'min:max'})))
00369         self.assertEqual(1, len(catalog.searchResults(
00370             expires={'query': (now-3, now+3), 'range': 'minmax'})))
00371 
00372         self.assertEqual(1, len(catalog.searchResults(
00373             effective={'query': now-1, 'range': 'max'},
00374             expires={'query': now+1, 'range': 'min'})))
00375 
00376         self.assertEqual(0, len(catalog.searchResults(
00377             effective={'query': now+1, 'range': 'max'},
00378             expires={'query': now+3, 'range': 'min'})))
00379 
00380     def test_convertQuery(self):
00381         convert = self._makeOne()._convertQuery
00382 
00383         kw = {}
00384         convert(kw)
00385         self.assertEqual(kw, {})
00386 
00387         kw = {'expires': 5, 'expires_usage': 'brrr:min'}
00388         self.assertRaises(ValueError, convert, kw)
00389 
00390         kw = {'foo': 'bar'}
00391         convert(kw)
00392         self.assertEqual(kw, {'foo': 'bar'})
00393 
00394         kw = {'expires': 5, 'expires_usage': 'range:min'}
00395         convert(kw)
00396         self.assertEqual(kw, {'expires': {'query': 5, 'range': 'min'}})
00397 
00398         kw = {'expires': 5, 'expires_usage': 'range:max'}
00399         convert(kw)
00400         self.assertEqual(kw, {'expires': {'query': 5, 'range': 'max'}})
00401 
00402         kw = {'expires': (5,7), 'expires_usage': 'range:min:max'}
00403         convert(kw)
00404         self.assertEqual(kw, {'expires': {'query': (5,7), 'range': 'min:max'}})
00405 
00406     def test_refreshCatalog(self):
00407         site = DummySite('site').__of__(self.root)
00408         site._setObject('dummy', DummyContent(catalog=1))
00409         site._setObject('portal_catalog', self._makeOne())
00410         ctool = site.portal_catalog
00411         ctool.catalog_object(site.dummy, '/dummy')
00412 
00413         self.assertEqual(1, len(ctool._catalog.searchResults()))
00414         ctool.refreshCatalog(clear=1)
00415         self.assertEqual(1, len(ctool._catalog.searchResults()),
00416                          'CMF Collector issue #379 (\'Update Catalog\' '
00417                          'fails): %s entries after refreshCatalog'
00418                          % (len(ctool._catalog.searchResults()),))
00419 
00420 
00421 def test_suite():
00422     return unittest.TestSuite((
00423         unittest.makeSuite(IndexableObjectWrapperTests),
00424         unittest.makeSuite(CatalogToolTests),
00425         ))
00426 
00427 if __name__ == '__main__':
00428     unittest.main(defaultTest='test_suite')