Skip to content

Commit 0d62399

Browse files
committed
tests: test C and PYTHON implementations in testPermissionRole
1 parent 94282bd commit 0d62399

File tree

1 file changed

+59
-47
lines changed

1 file changed

+59
-47
lines changed

src/AccessControl/tests/testPermissionRole.py

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from Acquisition import Implicit
2020
from Acquisition import aq_base
2121

22-
from AccessControl.PermissionRole import PermissionRole
22+
from ..Implementation import PURE_PYTHON
2323

2424

2525
ViewPermission = 'View'
@@ -50,40 +50,39 @@ class PermissiveObject(Explicit):
5050
_Edit_Things__Permission = ['Anonymous']
5151

5252

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}')
8786

8887
def testRestrictive(self, explicit=0):
8988
app = AppRoot()
@@ -93,9 +92,9 @@ def testRestrictive(self, explicit=0):
9392
app.c = ImplicitContainer()
9493
app.c.o = RestrictiveObject()
9594
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, ())
9998

10099
def testPermissive(self, explicit=0):
101100
app = AppRoot()
@@ -105,25 +104,38 @@ def testPermissive(self, explicit=0):
105104
app.c = ImplicitContainer()
106105
app.c.o = PermissiveObject()
107106
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', ))
113112

114113
def testExplicit(self):
115114
self.testRestrictive(1)
116115
self.testPermissive(1)
117116

118117
def testAppDefaults(self):
119118
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', ))
123122

124123
def testPermissionRoleSupportsGetattr(self):
125-
a = PermissionRole('a')
124+
a = self._getTargetClass()('a')
126125
self.assertEqual(getattr(a, '__roles__'), ('Manager', ))
127126
self.assertEqual(getattr(a, '_d'), ('Manager', ))
128127
self.assertEqual(getattr(a, '__name__'), 'a')
129128
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

Comments
 (0)