Back to index

plone3  3.1.7
testCheckId.py
Go to the documentation of this file.
00001 #
00002 # Test id autogeneration related scripts
00003 #
00004 
00005 from Products.CMFPlone.tests import PloneTestCase
00006 from Products.CMFPlone.tests import dummy
00007 
00008 from AccessControl import Unauthorized
00009 from Products.CMFCore.utils import getToolByName
00010 from ZODB.POSException import ConflictError
00011 
00012 
00013 class TestIsIDAutoGenerated(PloneTestCase.PloneTestCase):
00014     '''Tests the isIDAutoGenerated script'''
00015 
00016     def testAutoGeneratedId(self):
00017         plone_utils = getToolByName(self.portal, 'plone_utils')
00018         r = plone_utils.isIDAutoGenerated('document.2004-11-09.0123456789')
00019         self.assertEqual(r, True)
00020 
00021     def testAutoGeneratedIdWithUnderScores(self):
00022         plone_utils = getToolByName(self.portal, 'plone_utils')
00023         portal_types = getToolByName(self.portal, 'portal_types')
00024         portal_types.test_type=self.portal.portal_types.Event
00025         portal_types.test_type.id="test_type"
00026 
00027         r = plone_utils.isIDAutoGenerated('test_type.2004-11-09.0123456789')
00028 
00029         del portal_types.test_type
00030 
00031         self.assertEqual(r, True)
00032 
00033     def testEmptyId(self):
00034         plone_utils = getToolByName(self.portal, 'plone_utils')
00035         r = plone_utils.isIDAutoGenerated('')
00036         self.assertEqual(r, False)
00037 
00038     def testValidPortalTypeNameButNotAutoGeneratedId(self):
00039         plone_utils = getToolByName(self.portal, 'plone_utils')
00040         # This was raising an IndexError exception for
00041         # Zope < 2.7.3 (DateTime.py < 1.85.12.11) and a
00042         # SyntaxError for Zope >= 2.7.3 (DateTime.py >= 1.85.12.11)
00043         r = plone_utils.isIDAutoGenerated('document.tar.gz')
00044         self.assertEqual(r, False)
00045         # check DateError
00046         r = plone_utils.isIDAutoGenerated('document.tar.12/32/2004')
00047         self.assertEqual(r, False)
00048         # check TimeError
00049         r = plone_utils.isIDAutoGenerated('document.tar.12/31/2004 12:62')
00050         self.assertEqual(r, False)
00051 
00052 
00053 class TestCheckId(PloneTestCase.PloneTestCase):
00054     '''Tests the check_id script'''
00055 
00056     def testGoodId(self):
00057         r = self.folder.check_id('foo')
00058         self.assertEqual(r, None)   # success
00059 
00060     def testEmptyId(self):
00061         r = self.folder.check_id('')
00062         self.assertEqual(r, None)   # success
00063 
00064     def testRequiredId(self):
00065         r = self.folder.check_id('', required=1)
00066         self.assertEqual(r, u'Please enter a name.')
00067 
00068     def testAlternativeId(self):
00069         r = self.folder.check_id('', alternative_id='foo')
00070         self.assertEqual(r, None)   # success
00071 
00072     def testBadId(self):
00073         r = self.folder.check_id('=')
00074         #self.assertEqual(r, "'=' is not a legal name.")
00075         self.assertEqual(r, u'${name} is not a legal name. The following characters are invalid: ${characters}')
00076         self.assertEqual(r.mapping[u'name'], '=')
00077         self.assertEqual(r.mapping[u'characters'], '=')
00078 
00079     def testCatalogIndex(self):
00080         # TODO: Tripwire
00081         portal_membership = getToolByName(self.portal, 'portal_membership')
00082         have_permission = portal_membership.checkPermission
00083         self.failUnless(have_permission('Search ZCatalog', self.portal.portal_catalog),
00084                         'Expected permission "Search ZCatalog"')
00085 
00086         r = self.folder.check_id('created')
00087         self.assertEqual(r, u'${name} is reserved.')
00088         self.assertEqual(r.mapping[u'name'], 'created')
00089 
00090     def testCatalogMetadata(self):
00091         portal_catalog = getToolByName(self.portal, 'portal_catalog')
00092         portal_catalog.addColumn('new_metadata')
00093         self.failUnless('new_metadata' in portal_catalog.schema())
00094         self.failIf('new_metadata' in portal_catalog.indexes())
00095         r = self.folder.check_id('new_metadata')
00096         self.assertEqual(r, u'${name} is reserved.')
00097         self.assertEqual(r.mapping[u'name'], 'new_metadata')
00098 
00099     def testCollision(self):
00100         self.folder.invokeFactory('Document', id='foo')
00101         self.folder.invokeFactory('Document', id='bar')
00102         r = self.folder.foo.check_id('bar')
00103         self.assertEqual(r, u'There is already an item named ${name} in this folder.')
00104         self.assertEqual(r.mapping[u'name'], 'bar')
00105 
00106     def testTempObjectCollision(self):
00107         foo = self.folder.restrictedTraverse('portal_factory/Document/foo')
00108         self.folder._setObject('bar', dummy.Item('bar'))
00109         r = foo.check_id('bar')
00110         self.assertEqual(r, u'${name} is reserved.')
00111         self.assertEqual(r.mapping[u'name'], 'bar')
00112 
00113     def testReservedId(self):
00114         self.folder._setObject('foo', dummy.Item('foo'))
00115         r = self.folder.foo.check_id('portal_catalog')
00116         self.assertEqual(r, u'${name} is reserved.')
00117         self.assertEqual(r.mapping[u'name'], 'portal_catalog')
00118 
00119     def testHiddenObjectId(self):
00120         # If a parallel object is not in content-space, should get 'reserved'
00121         # instead of 'taken'
00122         r = self.folder.check_id('portal_skins')
00123         self.assertEqual(r, u'${name} is reserved.')
00124         self.assertEqual(r.mapping[u'name'], 'portal_skins')
00125 
00126     def testCanOverrideParentNames(self):
00127         self.folder.invokeFactory('Document', id='item1')
00128         self.folder.invokeFactory('Folder', id='folder1')
00129         self.folder.invokeFactory('Document', id='foo')
00130         r = self.folder.folder1.foo.check_id('item1')
00131         self.assertEqual(r, None)
00132 
00133     def testInvalidId(self):
00134         self.folder._setObject('foo', dummy.Item('foo'))
00135         r = self.folder.foo.check_id('_foo')
00136         self.assertEqual(r, u'${name} is reserved.')
00137         self.assertEqual(r.mapping[u'name'], '_foo')
00138 
00139     def testContainerHook(self):
00140         # Container may have a checkValidId method; make sure it is called
00141         self.folder._setObject('checkValidId', dummy.Raiser(dummy.Error))
00142         self.folder._setObject('foo', dummy.Item('foo'))
00143         r = self.folder.foo.check_id('whatever')
00144         self.assertEqual(r, u'${name} is reserved.')
00145         self.assertEqual(r.mapping[u'name'], 'whatever')
00146 
00147     def testContainerHookRaisesUnauthorized(self):
00148         # check_id should not swallow Unauthorized errors raised by hook
00149         self.folder._setObject('checkValidId', dummy.Raiser(Unauthorized))
00150         self.folder._setObject('foo', dummy.Item('foo'))
00151         self.assertRaises(Unauthorized, self.folder.foo.check_id, 'whatever')
00152 
00153     def testContainerHookRaisesConflictError(self):
00154         # check_id should not swallow ConflictErrors raised by hook
00155         self.folder._setObject('checkValidId', dummy.Raiser(ConflictError))
00156         self.folder._setObject('foo', dummy.Item('foo'))
00157         self.assertRaises(ConflictError, self.folder.foo.check_id, 'whatever')
00158 
00159     def testMissingUtils(self):
00160         # check_id should not bomb out if the plone_utils tool is missing
00161         self.portal._delObject('plone_utils')
00162         r = self.folder.check_id('foo')
00163         self.assertEqual(r, None)   # success
00164 
00165     def testMissingCatalog(self):
00166         # check_id should not bomb out if the portal_catalog tool is missing
00167         self.portal._delObject('portal_catalog')
00168         r = self.folder.check_id('foo')
00169         self.assertEqual(r, None)   # success
00170 
00171     def testMissingFactory(self):
00172         # check_id should not bomb out if the portal_factory tool is missing
00173         self.portal._delObject('portal_factory')
00174         r = self.folder.check_id('foo')
00175         self.assertEqual(r, None)   # success
00176 
00177     def testCatalogIndexSkipped(self):
00178         # Note that the check is skipped when we don't have
00179         # the "Search ZCatalogs" permission.
00180         self.portal.manage_permission('Search ZCatalog', ['Manager'], acquire=0)
00181 
00182         r = self.folder.check_id('created')
00183         # But now the final hasattr check picks this up
00184         self.assertEqual(r, u'${name} is reserved.')
00185         self.assertEqual(r.mapping[u'name'], 'created')
00186 
00187     def testCollisionSkipped(self):
00188         # Note that check is skipped when we don't have
00189         # the "Access contents information" permission.
00190         self.folder.manage_permission('Access contents information', [], acquire=0)
00191 
00192         self.folder._setObject('foo', dummy.Item('foo'))
00193         self.folder._setObject('bar', dummy.Item('bar'))
00194         r = self.folder.foo.check_id('bar')
00195         self.assertEqual(r, None)   # success
00196 
00197     def testReservedIdSkipped(self):
00198         # This check is picked up by the checkIdAvailable, unless we don't have
00199         # the "Add portal content" permission, in which case it is picked up by
00200         # the final hasattr check.
00201         self.folder.manage_permission('Add portal content', [], acquire=0)
00202 
00203         self.folder._setObject('foo', dummy.Item('foo'))
00204         r = self.folder.foo.check_id('portal_catalog')
00205         self.assertEqual(r, u'${name} is reserved.')
00206         self.assertEqual(r.mapping[u'name'], 'portal_catalog')
00207 
00208     def testInvalidIdSkipped(self):
00209         # Note that the check is skipped when we don't have
00210         # the "Add portal content" permission.
00211         self.folder.manage_permission('Add portal content', [], acquire=0)
00212 
00213         self.folder._setObject('foo', dummy.Item('foo'))
00214         r = self.folder.foo.check_id('_foo')
00215         self.assertEqual(r, None)   # success
00216 
00217     def testParentMethodAliasDisallowed(self):
00218         # Note that the check is skipped when we don't have
00219         # the "Add portal content" permission.
00220         self.folder.manage_permission('Add portal content', ['Manager'], acquire=0)
00221 
00222         self.folder._setObject('foo', dummy.Item('foo'))
00223         for alias in self.folder.getTypeInfo().getMethodAliases().keys():
00224             r = self.folder.foo.check_id(alias)
00225             self.assertEqual(r, u'${name} is reserved.')
00226             self.assertEqual(r.mapping[u'name'], alias)
00227 
00228     def testCheckingMethodAliasesOnPortalRoot(self):
00229         # Test for bug http://dev.plone.org/plone/ticket/4351
00230         self.setRoles(['Manager'])
00231         self.portal.manage_permission('Add portal content', ['Manager'], acquire=0)
00232 
00233         # Should not raise: Before we were using obj.getTypeInfo(), which is
00234         # not defined on the portal root.
00235         try:
00236             self.portal.check_id('foo')
00237         except AttributeError, e:
00238             self.fail(e)
00239 
00240     def testProxyRoles(self):
00241         # Proxy roles should cover missing view permission for all but the
00242         # most unusual workflows.
00243         proxy_roles = self.folder.check_id._proxy_roles
00244         self.failUnless('Manager' in proxy_roles)
00245         self.failUnless('Owner' in proxy_roles)
00246         self.failUnless('Authenticated' in proxy_roles)
00247         self.failUnless('Anonymous' in proxy_roles)
00248 
00249 class TestVisibleIdsEnabled(PloneTestCase.PloneTestCase):
00250     '''Tests the visibleIdsEnabled script'''
00251 
00252     def afterSetUp(self):
00253         portal_membership = getToolByName(self.portal, 'portal_membership')
00254         portal_properties = getToolByName(self.portal, 'portal_properties')
00255         self.member = portal_membership.getAuthenticatedMember()
00256         self.props = portal_properties.site_properties
00257 
00258     def testFailsWithSitePropertyDisabled(self):
00259         # Set baseline
00260         self.member.setProperties(visible_ids=False)
00261         self.props.manage_changeProperties(visible_ids=False)
00262         # Should fail when site property is set false
00263         self.failIf(self.portal.visibleIdsEnabled())
00264         self.member.setProperties(visible_ids=True)
00265         self.failIf(self.portal.visibleIdsEnabled())
00266 
00267     def testFailsWithMemberPropertyDisabled(self):
00268         # Should fail when member property is false
00269         self.member.setProperties(visible_ids=False)
00270         self.props.manage_changeProperties(visible_ids=True)
00271         self.failIf(self.portal.visibleIdsEnabled())
00272 
00273     def testSucceedsWithMemberAndSitePropertyEnabled(self):
00274         # Should succeed only when site property and member property are true
00275         self.props.manage_changeProperties(visible_ids=True)
00276         self.member.setProperties(visible_ids=True)
00277         self.failUnless(self.portal.visibleIdsEnabled())
00278 
00279 def test_suite():
00280     from unittest import TestSuite, makeSuite
00281     suite = TestSuite()
00282     suite.addTest(makeSuite(TestCheckId))
00283     suite.addTest(makeSuite(TestIsIDAutoGenerated))
00284     suite.addTest(makeSuite(TestVisibleIdsEnabled))
00285     return suite