Back to index

plone3  3.1.7
test_constraintypes.py
Go to the documentation of this file.
00001 #  ATContentTypes http://plone.org/products/atcontenttypes/
00002 #  Archetypes reimplementation of the CMF core types
00003 #  Copyright (c) 2003-2006 AT Content Types development team
00004 #
00005 #  This program is free software; you can redistribute it and/or modify
00006 #  it under the terms of the GNU General Public License as published by
00007 #  the Free Software Foundation; either version 2 of the License, or
00008 #  (at your option) any later version.
00009 #
00010 #  This program is distributed in the hope that it will be useful,
00011 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 #  GNU General Public License for more details.
00014 #
00015 #  You should have received a copy of the GNU General Public License
00016 #  along with this program; if not, write to the Free Software
00017 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 #
00019 """
00020 """
00021 
00022 __author__ = 'Leonardo Almeida and Martin Aspeli <optilude@gmx.net>'
00023 __docformat__ = 'restructuredtext'
00024 
00025 from Testing import ZopeTestCase # side effect import. leave it here.
00026 from Products.ATContentTypes.tests import atcttestcase
00027 
00028 from AccessControl import Unauthorized
00029 from AccessControl.SecurityManagement import newSecurityManager
00030 
00031 from Products.ATContentTypes.lib import constraintypes
00032 from Products.CMFPlone.interfaces.ConstrainTypes import ISelectableConstrainTypes as ZopeTwoISelectableConstrainTypes
00033 from Products.CMFPlone.interfaces import ISelectableConstrainTypes
00034 
00035 tests = []
00036 
00037 class TestConstrainTypes(atcttestcase.ATCTSiteTestCase):
00038     folder_type = 'Folder'
00039     image_type = 'Image'
00040     document_type = 'Document'
00041     file_type = 'File'
00042 
00043     def afterSetUp(self):
00044         atcttestcase.ATCTSiteTestCase.afterSetUp(self)
00045         self.folder.invokeFactory(self.folder_type, id='af')
00046         self.tt = self.portal.portal_types
00047         # an ATCT folder
00048         self.af = self.folder.af
00049         # portal_types object for ATCT folder
00050         self.at = self.tt.getTypeInfo(self.af)
00051 
00052     def test_isMixedIn(self):
00053         self.failUnless(isinstance(self.af,
00054                                    constraintypes.ConstrainTypesMixin),
00055                         "ConstrainTypesMixin was not mixed in to ATFolder")
00056         self.failUnless(ZopeTwoISelectableConstrainTypes.isImplementedBy(self.af),
00057                         "ISelectableConstrainTypes not implemented by ATFolder instance")
00058         self.failUnless(ISelectableConstrainTypes.providedBy(self.af),
00059                         "ISelectableConstrainTypes not implemented by ATFolder instance")
00060 
00061     def test_enabled(self):
00062         self.af.setConstrainTypesMode(constraintypes.ENABLED)
00063         self.af.setLocallyAllowedTypes(['Folder', 'Image'])
00064         self.af.setImmediatelyAddableTypes(['Folder'])
00065         
00066         self.failUnlessEqual(self.af.getLocallyAllowedTypes(), 
00067                                 ('Folder', 'Image',))
00068         self.failUnlessEqual(self.af.getImmediatelyAddableTypes(),
00069                                 ('Folder',))
00070         
00071         self.assertRaises(Unauthorized, self.af.invokeFactory, 'Document', 'a')
00072         try:
00073             self.af.invokeFactory('Image', 'image', title="death")
00074         except Unauthorized:
00075             self.fail()
00076         
00077     def test_disabled(self):
00078         self.af.setConstrainTypesMode(constraintypes.DISABLED)
00079         self.af.setLocallyAllowedTypes(['Folder', 'Image'])
00080         self.af.setImmediatelyAddableTypes(['Folder'])
00081         
00082         # We can still set and persist, even though it is disabled - must
00083         # remember!
00084         self.failUnlessEqual(self.af.getRawLocallyAllowedTypes(), 
00085                                 ('Folder', 'Image',))
00086         self.failUnlessEqual(self.af.getRawImmediatelyAddableTypes(), 
00087                                 ('Folder',))
00088         
00089         try:
00090             self.af.invokeFactory('Document', 'whatever', title='life')
00091             self.af.invokeFactory('Image', 'image', title="more life")
00092         except Unauthorized:
00093             self.fail()
00094             
00095         # Make sure immediately-addable are all types if we are disabled
00096         allowedIds = [ctype.getId() for ctype in self.af.allowedContentTypes()]
00097         self.failUnlessEqual(allowedIds, self.af.getImmediatelyAddableTypes())
00098         
00099     def test_acquireFromHomogenousParent(self):
00100         # Set up outer folder with restrictions enabled
00101         self.af.setConstrainTypesMode(constraintypes.ENABLED)
00102         self.af.setLocallyAllowedTypes(['Folder', 'Image'])
00103         self.af.setImmediatelyAddableTypes(['Folder'])
00104         
00105         # Create inner type to acquire (default)
00106         self.af.invokeFactory('Folder', 'inner', title='inner')
00107         inner = self.af.inner
00108         
00109         inner.setConstrainTypesMode(constraintypes.ACQUIRE)
00110         
00111         # Test persistence
00112         inner.setLocallyAllowedTypes(['Document', 'Event'])
00113         inner.setImmediatelyAddableTypes(['Document'])
00114         
00115         self.failUnlessEqual(inner.getRawLocallyAllowedTypes(), 
00116                                 ('Document', 'Event',))
00117         self.failUnlessEqual(inner.getRawImmediatelyAddableTypes(),
00118                                 ('Document',))
00119         
00120         self.assertRaises(Unauthorized, inner.invokeFactory, 'Event', 'a')
00121         try:
00122             inner.invokeFactory('Image', 'whatever', title='life')
00123         except Unauthorized:
00124             self.fail()
00125         
00126         # Make sure immediately-addable are inherited
00127         self.failUnlessEqual(inner.getImmediatelyAddableTypes(), 
00128                                 self.af.getImmediatelyAddableTypes())
00129 
00130         
00131         # Create a new unprivileged user who can only access the inner folder
00132         self.portal.acl_users._doAddUser('restricted', 'secret', ['Member'], [])
00133         inner.manage_addLocalRoles('restricted', ('Manager',))
00134         # Login the new user
00135         user = self.portal.acl_users.getUserById('restricted')
00136         newSecurityManager(None, user)
00137         self.failUnlessEqual(inner.getLocallyAllowedTypes(),
00138                         ('Folder', 'Image'))
00139 
00140         
00141     def test_acquireFromHetereogenousParent(self):
00142     
00143         # Let folder use a restricted set of types
00144         self.portal.portal_types.Folder.filter_content_types = 1
00145         self.portal.portal_types.Folder.allowed_content_types = \
00146             ('Document', 'Image', 'News Item', 'Topic', 'Folder')
00147         
00148         # Set up outer folder with restrictions enabled
00149         self.af.setConstrainTypesMode(constraintypes.ENABLED)
00150         self.af.setLocallyAllowedTypes(['Folder', 'Image'])
00151         self.af.setImmediatelyAddableTypes(['Folder', 'Image'])
00152   
00153         # Create inner type to acquire (default)
00154         self.af.invokeFactory('Folder', 'outer', title='outer')
00155         outer = self.af.outer
00156         
00157         outer.invokeFactory('Folder', 'inner', title='inner')
00158         inner = outer.inner 
00159         
00160         inner.setConstrainTypesMode(constraintypes.ACQUIRE)
00161         
00162         # Test persistence
00163         inner.setLocallyAllowedTypes(['Document', 'Event'])
00164         inner.setImmediatelyAddableTypes(['Document'])
00165         
00166         self.failUnlessEqual(inner.getRawLocallyAllowedTypes(), 
00167                                 ('Document', 'Event',))
00168         self.failUnlessEqual(inner.getRawImmediatelyAddableTypes(),
00169                                 ('Document',))
00170         
00171         # Fail - we didn't acquire this, really, since we can't acquire
00172         # from parent folder of different type
00173         self.assertRaises((Unauthorized, ValueError), inner.invokeFactory, 'Topic', 'a')
00174         self.failIf('News Item' in inner.getLocallyAllowedTypes())
00175         
00176         # Make sure immediately-addable are set to default
00177         self.failUnlessEqual(inner.getImmediatelyAddableTypes(), 
00178                                 inner.getLocallyAllowedTypes())
00179 
00180 
00181 tests.append(TestConstrainTypes)
00182 
00183 import unittest
00184 def test_suite():
00185     suite = unittest.TestSuite()
00186     for test in tests:
00187         suite.addTest(unittest.makeSuite(test))
00188     return suite