Back to index

plone3  3.1.7
test_rolemap.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2004 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 """ GenericSetup rolemap export / import unit tests
00014 
00015 $Id: test_rolemap.py 82202 2007-12-08 19:35:54Z tseaver $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from OFS.Folder import Folder
00022 
00023 from Products.GenericSetup.testing import ExportImportZCMLLayer
00024 from common import BaseRegistryTests
00025 from common import DummyExportContext
00026 from common import DummyImportContext
00027 
00028 
00029 class RolemapExportConfiguratorTests(BaseRegistryTests):
00030 
00031     layer = ExportImportZCMLLayer
00032 
00033     def _getTargetClass( self ):
00034 
00035         from Products.GenericSetup.rolemap import RolemapExportConfigurator
00036         return RolemapExportConfigurator
00037 
00038     def test_listRoles_normal( self ):
00039 
00040         EXPECTED = [ 'Anonymous', 'Authenticated', 'Manager', 'Owner' ]
00041         self.app.site = Folder(id='site')
00042         site = self.app.site
00043         configurator = self._makeOne( site )
00044 
00045         roles = list( configurator.listRoles() )
00046         self.assertEqual( len( roles ), len( EXPECTED ) )
00047 
00048         roles.sort()
00049 
00050         for found, expected in zip( roles, EXPECTED ):
00051             self.assertEqual( found, expected )
00052 
00053     def test_listRoles_added( self ):
00054 
00055         EXPECTED = [ 'Anonymous', 'Authenticated', 'Manager', 'Owner', 'ZZZ' ]
00056         self.app.site = Folder(id='site')
00057         site = self.app.site
00058         existing_roles = list( getattr( site, '__ac_roles__', [] ) )[:]
00059         existing_roles.append( 'ZZZ' )
00060         site.__ac_roles__ = existing_roles
00061 
00062         configurator = self._makeOne( site )
00063 
00064         roles = list( configurator.listRoles() )
00065         self.assertEqual( len( roles ), len( EXPECTED ) )
00066 
00067         roles.sort()
00068 
00069         for found, expected in zip( roles, EXPECTED ):
00070             self.assertEqual( found, expected )
00071 
00072     def test_listPermissions_nooverrides( self ):
00073 
00074         site = Folder(id='site').__of__(self.app)
00075         configurator = self._makeOne( site )
00076 
00077         self.assertEqual( len( configurator.listPermissions() ), 0 )
00078 
00079     def test_listPermissions_acquire( self ):
00080 
00081         ACI = 'Access contents information'
00082         ROLES = [ 'Manager', 'Owner' ]
00083 
00084         site = Folder(id='site').__of__(self.app)
00085         site.manage_permission( ACI, ROLES, acquire=1 )
00086         configurator = self._makeOne( site )
00087 
00088         self.assertEqual( len( configurator.listPermissions() ), 1 )
00089         info = configurator.listPermissions()[ 0 ]
00090         self.assertEqual( info[ 'name' ], ACI )
00091         self.assertEqual( info[ 'roles' ], ROLES )
00092         self.failUnless( info[ 'acquire' ] )
00093 
00094     def test_listPermissions_no_acquire( self ):
00095 
00096         ACI = 'Access contents information'
00097         ROLES = [ 'Manager', 'Owner' ]
00098 
00099         site = Folder(id='site').__of__(self.app)
00100         site.manage_permission( ACI, ROLES )
00101         configurator = self._makeOne( site )
00102 
00103         self.assertEqual( len( configurator.listPermissions() ), 1 )
00104         info = configurator.listPermissions()[ 0 ]
00105         self.assertEqual( info[ 'name' ], ACI )
00106         self.assertEqual( info[ 'roles' ], ROLES )
00107         self.failIf( info[ 'acquire' ] )
00108 
00109     def test_generateXML_empty( self ):
00110 
00111         self.app.site = Folder(id='site')
00112         site = self.app.site
00113         configurator = self._makeOne( site ).__of__( site )
00114 
00115         self._compareDOM( configurator.generateXML(), _EMPTY_EXPORT )
00116 
00117     def test_generateXML_added_role( self ):
00118 
00119         self.app.site = Folder(id='site')
00120         site = self.app.site
00121         existing_roles = list( getattr( site, '__ac_roles__', [] ) )[:]
00122         existing_roles.append( 'ZZZ' )
00123         site.__ac_roles__ = existing_roles
00124         configurator = self._makeOne( site ).__of__( site )
00125 
00126         self._compareDOM( configurator.generateXML(), _ADDED_ROLE_EXPORT )
00127 
00128     def test_generateEXML_acquired_perm( self ):
00129 
00130         ACI = 'Access contents information'
00131         ROLES = [ 'Manager', 'Owner' ]
00132 
00133         site = Folder(id='site').__of__(self.app)
00134         site.manage_permission( ACI, ROLES, acquire=1 )
00135         configurator = self._makeOne( site ).__of__( site )
00136 
00137         self._compareDOM( configurator.generateXML(), _ACQUIRED_EXPORT )
00138 
00139     def test_generateEXML_unacquired_perm( self ):
00140 
00141         ACI = 'Access contents information'
00142         ROLES = [ 'Manager', 'Owner', 'ZZZ' ]
00143 
00144         site = Folder(id='site').__of__(self.app)
00145         existing_roles = list( getattr( site, '__ac_roles__', [] ) )[:]
00146         existing_roles.append( 'ZZZ' )
00147         site.__ac_roles__ = existing_roles
00148         site.manage_permission( ACI, ROLES )
00149         configurator = self._makeOne( site ).__of__( site )
00150 
00151         self._compareDOM( configurator.generateXML(), _COMBINED_EXPORT )
00152 
00153     def test_generateEXML_unacquired_perm_added_role( self ):
00154 
00155         ACI = 'Access contents information'
00156         ROLES = [ 'Manager', 'Owner' ]
00157 
00158         site = Folder(id='site').__of__(self.app)
00159         site.manage_permission( ACI, ROLES )
00160         configurator = self._makeOne( site ).__of__( site )
00161 
00162         self._compareDOM( configurator.generateXML(), _UNACQUIRED_EXPORT )
00163 
00164 
00165 class RolemapImportConfiguratorTests(BaseRegistryTests):
00166 
00167     layer = ExportImportZCMLLayer
00168 
00169     def _getTargetClass( self ):
00170 
00171         from Products.GenericSetup.rolemap import RolemapImportConfigurator
00172         return RolemapImportConfigurator
00173 
00174     def test_parseXML_empty( self ):
00175 
00176         self.app.site = Folder(id='site')
00177         site = self.app.site
00178         existing_roles = list( getattr( site, '__ac_roles__', [] ) )[:]
00179         configurator = self._makeOne( site )
00180 
00181         rolemap_info = configurator.parseXML( _EMPTY_EXPORT )
00182 
00183         self.assertEqual( len( rolemap_info[ 'roles' ] ), 4 )
00184         self.assertEqual( len( rolemap_info[ 'permissions' ] ), 0 )
00185 
00186     def test_parseXML_added_role( self ):
00187 
00188         self.app.site = Folder(id='site')
00189         site = self.app.site
00190         configurator = self._makeOne( site )
00191 
00192         rolemap_info = configurator.parseXML( _ADDED_ROLE_EXPORT )
00193         roles = rolemap_info[ 'roles' ]
00194 
00195         self.assertEqual( len( roles ), 5 )
00196         self.failUnless( 'Anonymous' in roles )
00197         self.failUnless( 'Authenticated' in roles )
00198         self.failUnless( 'Manager' in roles )
00199         self.failUnless( 'Owner' in roles )
00200         self.failUnless( 'ZZZ' in roles )
00201 
00202     def test_parseXML_acquired_permission( self ):
00203 
00204         ACI = 'Access contents information'
00205 
00206         self.app.site = Folder(id='site')
00207         site = self.app.site
00208         configurator = self._makeOne( site )
00209 
00210         rolemap_info = configurator.parseXML( _ACQUIRED_EXPORT )
00211 
00212         self.assertEqual( len( rolemap_info[ 'permissions' ] ), 1 )
00213         permission = rolemap_info[ 'permissions' ][ 0 ]
00214 
00215         self.assertEqual( permission[ 'name' ], ACI )
00216         self.failUnless( permission[ 'acquire' ] )
00217 
00218         p_roles = permission[ 'roles' ]
00219         self.assertEqual( len( p_roles ), 2 )
00220         self.failUnless( 'Manager' in p_roles )
00221         self.failUnless( 'Owner' in p_roles )
00222 
00223     def test_parseXML_unacquired_permission( self ):
00224 
00225         ACI = 'Access contents information'
00226 
00227         self.app.site = Folder(id='site')
00228         site = self.app.site
00229         configurator = self._makeOne( site )
00230 
00231         rolemap_info = configurator.parseXML( _UNACQUIRED_EXPORT )
00232 
00233         self.assertEqual( len( rolemap_info[ 'permissions' ] ), 1 )
00234         permission = rolemap_info[ 'permissions' ][ 0 ]
00235 
00236         self.assertEqual( permission[ 'name' ], ACI )
00237         self.failIf( permission[ 'acquire' ] )
00238 
00239         p_roles = permission[ 'roles' ]
00240         self.assertEqual( len( p_roles ), 2 )
00241         self.failUnless( 'Manager' in p_roles )
00242         self.failUnless( 'Owner' in p_roles )
00243 
00244     def test_parseXML_unacquired_permission_added_role( self ):
00245 
00246         ACI = 'Access contents information'
00247 
00248         self.app.site = Folder(id='site')
00249         site = self.app.site
00250         configurator = self._makeOne( site )
00251 
00252         rolemap_info = configurator.parseXML( _COMBINED_EXPORT )
00253         roles = rolemap_info[ 'roles' ]
00254 
00255         self.assertEqual( len( roles ), 5 )
00256         self.failUnless( 'Anonymous' in roles )
00257         self.failUnless( 'Authenticated' in roles )
00258         self.failUnless( 'Manager' in roles )
00259         self.failUnless( 'Owner' in roles )
00260         self.failUnless( 'ZZZ' in roles )
00261 
00262         self.assertEqual( len( rolemap_info[ 'permissions' ] ), 1 )
00263         permission = rolemap_info[ 'permissions' ][ 0 ]
00264 
00265         self.assertEqual( permission[ 'name' ], ACI )
00266         self.failIf( permission[ 'acquire' ] )
00267 
00268         p_roles = permission[ 'roles' ]
00269         self.assertEqual( len( p_roles ), 3 )
00270         self.failUnless( 'Manager' in p_roles )
00271         self.failUnless( 'Owner' in p_roles )
00272         self.failUnless( 'ZZZ' in p_roles )
00273 
00274 
00275 
00276 _EMPTY_EXPORT = """\
00277 <?xml version="1.0"?>
00278 <rolemap>
00279   <roles>
00280     <role name="Anonymous"/>
00281     <role name="Authenticated"/>
00282     <role name="Manager"/>
00283     <role name="Owner"/>
00284   </roles>
00285   <permissions>
00286   </permissions>
00287 </rolemap>
00288 """
00289 
00290 _ADDED_ROLE_EXPORT = """\
00291 <?xml version="1.0"?>
00292 <rolemap>
00293   <roles>
00294     <role name="Anonymous"/>
00295     <role name="Authenticated"/>
00296     <role name="Manager"/>
00297     <role name="Owner"/>
00298     <role name="ZZZ"/>
00299   </roles>
00300   <permissions>
00301   </permissions>
00302 </rolemap>
00303 """
00304 
00305 _ACQUIRED_EXPORT = """\
00306 <?xml version="1.0"?>
00307 <rolemap>
00308   <roles>
00309     <role name="Anonymous"/>
00310     <role name="Authenticated"/>
00311     <role name="Manager"/>
00312     <role name="Owner"/>
00313   </roles>
00314   <permissions>
00315     <permission name="Access contents information"
00316                 acquire="True">
00317       <role name="Manager"/>
00318       <role name="Owner"/>
00319     </permission>
00320   </permissions>
00321 </rolemap>
00322 """
00323 
00324 _UNACQUIRED_EXPORT = """\
00325 <?xml version="1.0"?>
00326 <rolemap>
00327   <roles>
00328     <role name="Anonymous"/>
00329     <role name="Authenticated"/>
00330     <role name="Manager"/>
00331     <role name="Owner"/>
00332   </roles>
00333   <permissions>
00334     <permission name="Access contents information"
00335                 acquire="False">
00336       <role name="Manager"/>
00337       <role name="Owner"/>
00338     </permission>
00339   </permissions>
00340 </rolemap>
00341 """
00342 
00343 _COMBINED_EXPORT = """\
00344 <?xml version="1.0"?>
00345 <rolemap>
00346   <roles>
00347     <role name="Anonymous"/>
00348     <role name="Authenticated"/>
00349     <role name="Manager"/>
00350     <role name="Owner"/>
00351     <role name="ZZZ"/>
00352   </roles>
00353   <permissions>
00354     <permission name="Access contents information"
00355                 acquire="False">
00356       <role name="Manager"/>
00357       <role name="Owner"/>
00358       <role name="ZZZ"/>
00359     </permission>
00360   </permissions>
00361 </rolemap>
00362 """
00363 
00364 class Test_exportRolemap( BaseRegistryTests ):
00365 
00366     layer = ExportImportZCMLLayer
00367 
00368     def test_unchanged( self ):
00369 
00370         self.app.site = Folder('site')
00371         site = self.app.site
00372 
00373         context = DummyExportContext( site )
00374 
00375         from Products.GenericSetup.rolemap import exportRolemap
00376         exportRolemap( context )
00377 
00378         self.assertEqual( len( context._wrote ), 1 )
00379         filename, text, content_type = context._wrote[ 0 ]
00380         self.assertEqual( filename, 'rolemap.xml' )
00381         self._compareDOM( text, _EMPTY_EXPORT )
00382         self.assertEqual( content_type, 'text/xml' )
00383 
00384     def test_added_role( self ):
00385 
00386         self.app.site = Folder('site')
00387         site = self.app.site
00388         existing_roles = list( getattr( site, '__ac_roles__', [] ) )[:]
00389         existing_roles.append( 'ZZZ' )
00390         site.__ac_roles__ = existing_roles
00391 
00392         context = DummyExportContext( site )
00393 
00394         from Products.GenericSetup.rolemap import exportRolemap
00395         exportRolemap( context )
00396 
00397         self.assertEqual( len( context._wrote ), 1 )
00398         filename, text, content_type = context._wrote[ 0 ]
00399         self.assertEqual( filename, 'rolemap.xml' )
00400         self._compareDOM( text, _ADDED_ROLE_EXPORT )
00401         self.assertEqual( content_type, 'text/xml' )
00402 
00403 
00404     def test_acquired_perm( self ):
00405 
00406         ACI = 'Access contents information'
00407         ROLES = [ 'Manager', 'Owner' ]
00408 
00409         self.app.site = Folder('site')
00410         site = self.app.site
00411         site.manage_permission( ACI, ROLES, acquire=1 )
00412 
00413         context = DummyExportContext( site )
00414 
00415         from Products.GenericSetup.rolemap import exportRolemap
00416         exportRolemap( context )
00417 
00418         self.assertEqual( len( context._wrote ), 1 )
00419         filename, text, content_type = context._wrote[ 0 ]
00420         self.assertEqual( filename, 'rolemap.xml' )
00421         self._compareDOM( text, _ACQUIRED_EXPORT )
00422         self.assertEqual( content_type, 'text/xml' )
00423 
00424     def test_unacquired_perm( self ):
00425 
00426         ACI = 'Access contents information'
00427         ROLES = [ 'Manager', 'Owner', 'ZZZ' ]
00428 
00429         self.app.site = Folder('site')
00430         site = self.app.site
00431         existing_roles = list( getattr( site, '__ac_roles__', [] ) )[:]
00432         existing_roles.append( 'ZZZ' )
00433         site.__ac_roles__ = existing_roles
00434         site.manage_permission( ACI, ROLES )
00435 
00436         context = DummyExportContext( site )
00437 
00438         from Products.GenericSetup.rolemap import exportRolemap
00439         exportRolemap( context )
00440 
00441         self.assertEqual( len( context._wrote ), 1 )
00442         filename, text, content_type = context._wrote[ 0 ]
00443         self.assertEqual( filename, 'rolemap.xml' )
00444         self._compareDOM( text, _COMBINED_EXPORT )
00445         self.assertEqual( content_type, 'text/xml' )
00446 
00447     def test_unacquired_perm_added_role( self ):
00448 
00449         ACI = 'Access contents information'
00450         ROLES = [ 'Manager', 'Owner' ]
00451 
00452         self.app.site = Folder('site')
00453         site = self.app.site
00454         site.manage_permission( ACI, ROLES )
00455 
00456         context = DummyExportContext( site )
00457 
00458         from Products.GenericSetup.rolemap import exportRolemap
00459         exportRolemap( context )
00460 
00461         self.assertEqual( len( context._wrote ), 1 )
00462         filename, text, content_type = context._wrote[ 0 ]
00463         self.assertEqual( filename, 'rolemap.xml' )
00464         self._compareDOM( text, _UNACQUIRED_EXPORT )
00465         self.assertEqual( content_type, 'text/xml' )
00466 
00467 
00468 class Test_importRolemap( BaseRegistryTests ):
00469 
00470     layer = ExportImportZCMLLayer
00471 
00472     def test_empty_default_purge( self ):
00473 
00474         self.app.site = Folder(id='site')
00475         site = self.app.site
00476         original_roles = list( getattr( site, '__ac_roles__', [] ) )[:]
00477         modified_roles = original_roles[:]
00478         modified_roles.append( 'ZZZ' )
00479         site.__ac_roles__ = modified_roles
00480 
00481         context = DummyImportContext( site )
00482         context._files[ 'rolemap.xml' ] = _EMPTY_EXPORT
00483 
00484         from Products.GenericSetup.rolemap import importRolemap
00485         importRolemap( context )
00486 
00487         new_roles = list( getattr( site, '__ac_roles__', [] ) )[:]
00488 
00489         original_roles.sort()
00490         new_roles.sort()
00491 
00492         self.assertEqual( original_roles, new_roles )
00493 
00494     def test_empty_explicit_purge( self ):
00495 
00496         self.app.site = Folder(id='site')
00497         site = self.app.site
00498         original_roles = list( getattr( site, '__ac_roles__', [] ) )[:]
00499         modified_roles = original_roles[:]
00500         modified_roles.append( 'ZZZ' )
00501         site.__ac_roles__ = modified_roles
00502 
00503         context = DummyImportContext( site, True )
00504         context._files[ 'rolemap.xml' ] = _EMPTY_EXPORT
00505 
00506         from Products.GenericSetup.rolemap import importRolemap
00507         importRolemap( context )
00508 
00509         new_roles = list( getattr( site, '__ac_roles__', [] ) )[:]
00510 
00511         original_roles.sort()
00512         new_roles.sort()
00513 
00514         self.assertEqual( original_roles, new_roles )
00515 
00516     def test_empty_skip_purge( self ):
00517 
00518         self.app.site = Folder(id='site')
00519         site = self.app.site
00520         original_roles = list( getattr( site, '__ac_roles__', [] ) )[:]
00521         modified_roles = original_roles[:]
00522         modified_roles.append( 'ZZZ' )
00523         site.__ac_roles__ = modified_roles
00524 
00525         context = DummyImportContext( site, False )
00526         context._files[ 'rolemap.xml' ] = _EMPTY_EXPORT
00527 
00528         from Products.GenericSetup.rolemap import importRolemap
00529         importRolemap( context )
00530 
00531         new_roles = list( getattr( site, '__ac_roles__', [] ) )[:]
00532 
00533         modified_roles.sort()
00534         new_roles.sort()
00535 
00536         self.assertEqual( modified_roles, new_roles )
00537 
00538     def test_acquired_permission_explicit_purge( self ):
00539 
00540         ACI = 'Access contents information'
00541         VIEW = 'View'
00542 
00543         self.app.site = Folder(id='site')
00544         site = self.app.site
00545         site.manage_permission( ACI, () )
00546         site.manage_permission( VIEW, () )
00547 
00548         existing_allowed = [ x[ 'name' ]
00549                                 for x in site.rolesOfPermission( ACI )
00550                                 if x[ 'selected' ] ]
00551 
00552         self.assertEqual( existing_allowed, [] )
00553 
00554         self.failIf( site.acquiredRolesAreUsedBy( ACI ) )
00555         self.failIf( site.acquiredRolesAreUsedBy( VIEW ) )
00556 
00557         context = DummyImportContext( site, True )
00558         context._files[ 'rolemap.xml' ] = _ACQUIRED_EXPORT
00559 
00560         from Products.GenericSetup.rolemap import importRolemap
00561         importRolemap( context )
00562 
00563         new_allowed = [ x[ 'name' ]
00564                            for x in site.rolesOfPermission( ACI )
00565                            if x[ 'selected' ] ]
00566 
00567         self.assertEqual( new_allowed, [ 'Manager', 'Owner' ] )
00568 
00569         # ACI is overwritten by XML, but VIEW was purged
00570         self.failUnless( site.acquiredRolesAreUsedBy( ACI ) )
00571         self.failUnless( site.acquiredRolesAreUsedBy( VIEW ) )
00572 
00573     def test_acquired_permission_no_purge( self ):
00574 
00575         ACI = 'Access contents information'
00576         VIEW = 'View'
00577 
00578         self.app.site = Folder(id='site')
00579         site = self.app.site
00580         site.manage_permission( ACI, () )
00581         site.manage_permission( VIEW, () )
00582 
00583         existing_allowed = [ x[ 'name' ]
00584                                 for x in site.rolesOfPermission( ACI )
00585                                 if x[ 'selected' ] ]
00586 
00587         self.assertEqual( existing_allowed, [] )
00588 
00589         self.failIf( site.acquiredRolesAreUsedBy( ACI ) )
00590 
00591         context = DummyImportContext( site, False )
00592         context._files[ 'rolemap.xml' ] = _ACQUIRED_EXPORT
00593 
00594         from Products.GenericSetup.rolemap import importRolemap
00595         importRolemap( context )
00596 
00597         new_allowed = [ x[ 'name' ]
00598                            for x in site.rolesOfPermission( ACI )
00599                            if x[ 'selected' ] ]
00600 
00601         self.assertEqual( new_allowed, [ 'Manager', 'Owner' ] )
00602 
00603         # ACI is overwritten by XML, but VIEW is not
00604         self.failUnless( site.acquiredRolesAreUsedBy( ACI ) )
00605         self.failIf( site.acquiredRolesAreUsedBy( VIEW ) )
00606 
00607     def test_unacquired_permission_explicit_purge( self ):
00608 
00609         ACI = 'Access contents information'
00610         VIEW = 'View'
00611 
00612         self.app.site = Folder(id='site')
00613         site = self.app.site
00614         site.manage_permission( VIEW, () )
00615 
00616         existing_allowed = [ x[ 'name' ]
00617                                 for x in site.rolesOfPermission( ACI )
00618                                 if x[ 'selected' ] ]
00619 
00620         self.assertEqual( existing_allowed, [ 'Manager' ] )
00621 
00622         self.failUnless( site.acquiredRolesAreUsedBy( ACI ) )
00623         self.failIf( site.acquiredRolesAreUsedBy( VIEW ) )
00624 
00625         context = DummyImportContext( site, True )
00626         context._files[ 'rolemap.xml' ] = _UNACQUIRED_EXPORT
00627 
00628         from Products.GenericSetup.rolemap import importRolemap
00629         importRolemap( context )
00630 
00631         new_allowed = [ x[ 'name' ]
00632                            for x in site.rolesOfPermission( ACI )
00633                            if x[ 'selected' ] ]
00634 
00635         self.assertEqual( new_allowed, [ 'Manager', 'Owner' ] )
00636 
00637         self.failIf( site.acquiredRolesAreUsedBy( ACI ) )
00638         self.failUnless( site.acquiredRolesAreUsedBy( VIEW ) )
00639 
00640     def test_unacquired_permission_skip_purge( self ):
00641 
00642         ACI = 'Access contents information'
00643         VIEW = 'View'
00644 
00645         self.app.site = Folder(id='site')
00646         site = self.app.site
00647         site.manage_permission( VIEW, () )
00648 
00649         existing_allowed = [ x[ 'name' ]
00650                                 for x in site.rolesOfPermission( ACI )
00651                                 if x[ 'selected' ] ]
00652 
00653         self.assertEqual( existing_allowed, [ 'Manager' ] )
00654 
00655         self.failUnless( site.acquiredRolesAreUsedBy( ACI ) )
00656         self.failIf( site.acquiredRolesAreUsedBy( VIEW ) )
00657 
00658         context = DummyImportContext( site, False )
00659         context._files[ 'rolemap.xml' ] = _UNACQUIRED_EXPORT
00660 
00661         from Products.GenericSetup.rolemap import importRolemap
00662         importRolemap( context )
00663 
00664         new_allowed = [ x[ 'name' ]
00665                            for x in site.rolesOfPermission( ACI )
00666                            if x[ 'selected' ] ]
00667 
00668         self.assertEqual( new_allowed, [ 'Manager', 'Owner' ] )
00669 
00670         self.failIf( site.acquiredRolesAreUsedBy( ACI ) )
00671         self.failIf( site.acquiredRolesAreUsedBy( VIEW ) )
00672 
00673     def test_unacquired_permission_added_role_explicit_purge( self ):
00674 
00675         ACI = 'Access contents information'
00676         VIEW = 'View'
00677 
00678         self.app.site = Folder(id='site')
00679         site = self.app.site
00680         site.manage_permission( VIEW, () )
00681 
00682         existing_allowed = [ x[ 'name' ]
00683                                 for x in site.rolesOfPermission( ACI )
00684                                 if x[ 'selected' ] ]
00685 
00686         self.assertEqual( existing_allowed, [ 'Manager' ] )
00687 
00688         self.failUnless( site.acquiredRolesAreUsedBy( ACI ) )
00689         self.failIf( site.acquiredRolesAreUsedBy( VIEW ) )
00690 
00691         self.failIf( site._has_user_defined_role( 'ZZZ' ) )
00692 
00693         context = DummyImportContext( site, True )
00694         context._files[ 'rolemap.xml' ] = _COMBINED_EXPORT
00695 
00696         from Products.GenericSetup.rolemap import importRolemap
00697         importRolemap( context )
00698 
00699         self.failUnless( site._has_user_defined_role( 'ZZZ' ) )
00700 
00701         new_allowed = [ x[ 'name' ]
00702                            for x in site.rolesOfPermission( ACI )
00703                            if x[ 'selected' ] ]
00704 
00705         self.assertEqual( new_allowed, [ 'Manager', 'Owner', 'ZZZ' ] )
00706 
00707         self.failIf( site.acquiredRolesAreUsedBy( ACI ) )
00708         self.failUnless( site.acquiredRolesAreUsedBy( VIEW ) )
00709 
00710     def test_unacquired_permission_added_role_skip_purge( self ):
00711 
00712         ACI = 'Access contents information'
00713         VIEW = 'View'
00714 
00715         self.app.site = Folder(id='site')
00716         site = self.app.site
00717         site.manage_permission( VIEW, () )
00718 
00719         existing_allowed = [ x[ 'name' ]
00720                                 for x in site.rolesOfPermission( ACI )
00721                                 if x[ 'selected' ] ]
00722 
00723         self.assertEqual( existing_allowed, [ 'Manager' ] )
00724 
00725         self.failUnless( site.acquiredRolesAreUsedBy( ACI ) )
00726         self.failIf( site.acquiredRolesAreUsedBy( VIEW ) )
00727 
00728         self.failIf( site._has_user_defined_role( 'ZZZ' ) )
00729 
00730         context = DummyImportContext( site, False )
00731         context._files[ 'rolemap.xml' ] = _COMBINED_EXPORT
00732 
00733         from Products.GenericSetup.rolemap import importRolemap
00734         importRolemap( context )
00735 
00736         self.failUnless( site._has_user_defined_role( 'ZZZ' ) )
00737 
00738         new_allowed = [ x[ 'name' ]
00739                            for x in site.rolesOfPermission( ACI )
00740                            if x[ 'selected' ] ]
00741 
00742         self.assertEqual( new_allowed, [ 'Manager', 'Owner', 'ZZZ' ] )
00743 
00744         self.failIf( site.acquiredRolesAreUsedBy( ACI ) )
00745         self.failIf( site.acquiredRolesAreUsedBy( VIEW ) )
00746 
00747     def test_unacquired_permission_added_role_skip_purge_encode_ascii( self ):
00748 
00749         ACI = 'Access contents information'
00750         VIEW = 'View'
00751 
00752         self.app.site = Folder(id='site')
00753         site = self.app.site
00754         site.manage_permission( VIEW, () )
00755 
00756         existing_allowed = [ x[ 'name' ]
00757                                 for x in site.rolesOfPermission( ACI )
00758                                 if x[ 'selected' ] ]
00759 
00760         self.assertEqual( existing_allowed, [ 'Manager' ] )
00761 
00762         self.failUnless( site.acquiredRolesAreUsedBy( ACI ) )
00763         self.failIf( site.acquiredRolesAreUsedBy( VIEW ) )
00764 
00765         self.failIf( site._has_user_defined_role( 'ZZZ' ) )
00766 
00767         context = DummyImportContext( site, False, encoding='ascii' )
00768         context._files[ 'rolemap.xml' ] = _COMBINED_EXPORT
00769 
00770         from Products.GenericSetup.rolemap import importRolemap
00771         importRolemap( context )
00772 
00773         self.failUnless( site._has_user_defined_role( 'ZZZ' ) )
00774 
00775         new_allowed = [ x[ 'name' ]
00776                            for x in site.rolesOfPermission( ACI )
00777                            if x[ 'selected' ] ]
00778 
00779         self.assertEqual( new_allowed, [ 'Manager', 'Owner', 'ZZZ' ] )
00780 
00781         self.failIf( site.acquiredRolesAreUsedBy( ACI ) )
00782         self.failIf( site.acquiredRolesAreUsedBy( VIEW ) )
00783 
00784 
00785 def test_suite():
00786     return unittest.TestSuite((
00787         unittest.makeSuite(RolemapExportConfiguratorTests),
00788         unittest.makeSuite(RolemapImportConfiguratorTests),
00789         unittest.makeSuite(Test_exportRolemap),
00790         unittest.makeSuite(Test_importRolemap),
00791         ))
00792 
00793 if __name__ == '__main__':
00794     from Products.GenericSetup.testing import run
00795     run(test_suite())