19
19
from Acquisition import Implicit
20
20
from Acquisition import aq_base
21
21
22
- from AccessControl . PermissionRole import PermissionRole
22
+ from .. Implementation import PURE_PYTHON
23
23
24
24
25
25
ViewPermission = 'View'
@@ -50,40 +50,39 @@ class PermissiveObject(Explicit):
50
50
_Edit_Things__Permission = ['Anonymous' ]
51
51
52
52
53
- def assertPRoles (ob , permission , expect ):
54
- """
55
- Asserts that in the context of ob, the given permission maps to
56
- the given roles.
57
- """
58
- pr = PermissionRole (permission )
59
- roles = pr .__of__ (ob )
60
- roles2 = aq_base (pr ).__of__ (ob )
61
- assert roles == roles2 or tuple (roles ) == tuple (roles2 ), (
62
- 'Different methods of checking roles computed unequal results' )
63
- same = 0
64
- if roles :
65
- # When verbose security is enabled, permission names are
66
- # embedded in the computed roles. Remove the permission
67
- # names.
68
- roles = [r for r in roles if not r .endswith ('_Permission' )]
69
-
70
- if roles is None or expect is None :
71
- if (roles is None or tuple (roles ) == ('Anonymous' , )) and \
72
- (expect is None or tuple (expect ) == ('Anonymous' , )):
73
- same = 1
74
- else :
75
- got = {}
76
- for r in roles :
77
- got [r ] = 1
78
- expected = {}
79
- for r in expect :
80
- expected [r ] = 1
81
- if got == expected : # Dict compare does the Right Thing.
82
- same = 1
83
- assert same , f'Expected roles: { expect !r} , got: { roles !r} '
84
-
85
-
86
- class PermissionRoleTests (unittest .TestCase ):
53
+ class PermissionRoleTestBase :
54
+
55
+ def assertPRoles (self , ob , permission , expect ):
56
+ """
57
+ Asserts that in the context of ob, the given permission maps to
58
+ the given roles.
59
+ """
60
+ pr = self ._getTargetClass ()(permission )
61
+ roles = pr .__of__ (ob )
62
+ roles2 = aq_base (pr ).__of__ (ob )
63
+ assert roles == roles2 or tuple (roles ) == tuple (roles2 ), (
64
+ 'Different methods of checking roles computed unequal results' )
65
+ same = 0
66
+ if roles :
67
+ # When verbose security is enabled, permission names are
68
+ # embedded in the computed roles. Remove the permission
69
+ # names.
70
+ roles = [r for r in roles if not r .endswith ('_Permission' )]
71
+
72
+ if roles is None or expect is None :
73
+ if (roles is None or tuple (roles ) == ('Anonymous' , )) and \
74
+ (expect is None or tuple (expect ) == ('Anonymous' , )):
75
+ same = 1
76
+ else :
77
+ got = {}
78
+ for r in roles :
79
+ got [r ] = 1
80
+ expected = {}
81
+ for r in expect :
82
+ expected [r ] = 1
83
+ if got == expected : # Dict compare does the Right Thing.
84
+ same = 1
85
+ self .assertTrue (same , f'Expected roles: { expect !r} , got: { roles !r} ' )
87
86
88
87
def testRestrictive (self , explicit = 0 ):
89
88
app = AppRoot ()
@@ -93,9 +92,9 @@ def testRestrictive(self, explicit=0):
93
92
app .c = ImplicitContainer ()
94
93
app .c .o = RestrictiveObject ()
95
94
o = app .c .o
96
- assertPRoles (o , ViewPermission , ('Manager' , ))
97
- assertPRoles (o , EditThingsPermission , ('Manager' , 'Owner' ))
98
- assertPRoles (o , DeletePermission , ())
95
+ self . assertPRoles (o , ViewPermission , ('Manager' , ))
96
+ self . assertPRoles (o , EditThingsPermission , ('Manager' , 'Owner' ))
97
+ self . assertPRoles (o , DeletePermission , ())
99
98
100
99
def testPermissive (self , explicit = 0 ):
101
100
app = AppRoot ()
@@ -105,25 +104,38 @@ def testPermissive(self, explicit=0):
105
104
app .c = ImplicitContainer ()
106
105
app .c .o = PermissiveObject ()
107
106
o = app .c .o
108
- assertPRoles (o , ViewPermission , ('Anonymous' , ))
109
- assertPRoles (o , EditThingsPermission , ('Anonymous' ,
110
- 'Manager' ,
111
- 'Owner' ))
112
- assertPRoles (o , DeletePermission , ('Manager' , ))
107
+ self . assertPRoles (o , ViewPermission , ('Anonymous' , ))
108
+ self . assertPRoles (o , EditThingsPermission , ('Anonymous' ,
109
+ 'Manager' ,
110
+ 'Owner' ))
111
+ self . assertPRoles (o , DeletePermission , ('Manager' , ))
113
112
114
113
def testExplicit (self ):
115
114
self .testRestrictive (1 )
116
115
self .testPermissive (1 )
117
116
118
117
def testAppDefaults (self ):
119
118
o = AppRoot ()
120
- assertPRoles (o , ViewPermission , ('Anonymous' , ))
121
- assertPRoles (o , EditThingsPermission , ('Manager' , 'Owner' ))
122
- assertPRoles (o , DeletePermission , ('Manager' , ))
119
+ self . assertPRoles (o , ViewPermission , ('Anonymous' , ))
120
+ self . assertPRoles (o , EditThingsPermission , ('Manager' , 'Owner' ))
121
+ self . assertPRoles (o , DeletePermission , ('Manager' , ))
123
122
124
123
def testPermissionRoleSupportsGetattr (self ):
125
- a = PermissionRole ('a' )
124
+ a = self . _getTargetClass () ('a' )
126
125
self .assertEqual (getattr (a , '__roles__' ), ('Manager' , ))
127
126
self .assertEqual (getattr (a , '_d' ), ('Manager' , ))
128
127
self .assertEqual (getattr (a , '__name__' ), 'a' )
129
128
self .assertEqual (getattr (a , '_p' ), '_a_Permission' )
129
+
130
+
131
+ class Python_PermissionRoleTests (PermissionRoleTestBase , unittest .TestCase ):
132
+ def _getTargetClass (self ):
133
+ from AccessControl .ImplPython import PermissionRole
134
+ return PermissionRole
135
+
136
+
137
+ @unittest .skipIf (PURE_PYTHON , reason = "Test expects C impl." )
138
+ class C__PermissionRoleTests (PermissionRoleTestBase , unittest .TestCase ):
139
+ def _getTargetClass (self ):
140
+ from AccessControl .ImplC import PermissionRole
141
+ return PermissionRole
0 commit comments