-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
test_env.py
446 lines (383 loc) · 14.4 KB
/
test_env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# -*- coding: utf-8 -*-
"""Tests for plone.api.roles."""
from AccessControl import Unauthorized
from OFS.SimpleItem import SimpleItem
from plone import api
from plone.api.tests.base import INTEGRATION_TESTING
from plone.app.testing import TEST_USER_ID
import AccessControl
import unittest
class ExampleException(Exception):
pass
role_mapping = (
('ppp', ('Manager', 'VIP', 'Member')),
('qqq', ('Manager', 'VIP')),
('rrr', ('Manager')),
)
# Version of Zope and Plone should be something like
# 'X.Y' or 'X.Y.Z' or 'X.Y.Z.A'
# It could also include a package status id (Alpha, Beta or RC).
# When run against coredev, we may have a .devN suffix as well.
version_regexp = r'^(\d+(\.\d+){1,3})(a\d+|b\d+|rc\d+)?(\.dev\d)?$'
class HasProtectedMethods(SimpleItem):
security = AccessControl.ClassSecurityInfo()
def __init__(self, id):
self.id = id
@security.public
def public_method(self):
pass
@security.protected('ppp')
def pp_method(self):
pass
@security.protected('qqq')
def qq_method(self):
pass
@security.protected('rrr')
def rr_method(self):
pass
@security.private
def private_method(self):
pass
AccessControl.class_init.InitializeClass(HasProtectedMethods)
class TestPloneApiEnv(unittest.TestCase):
"""Test plone.api.env"""
layer = INTEGRATION_TESTING
def setUp(self):
"""Shared test environment set-up, ran before every test."""
portal = self.portal = self.layer['portal']
portal._setObject('hpm', HasProtectedMethods('hpm'))
# This isn't necessary to the unit tests, it makes debugging them
# easier when they go wrong. Like "verbose-security on" in zope.conf
sm = AccessControl.getSecurityManager()
sm._policy._verbose = 1
# Roles need to be created by name before we can assign permissions
# to them or grant them to users.
# 'Member' and 'Manager' already exist by default, we need to add 'VIP'
portal._addRole('VIP')
for permission, roles in role_mapping:
portal.manage_permission(permission, roles, 1)
api.user.create(
username='worker',
email='ordinary_person@example.com',
password='password1',
roles=('Member',),
)
api.user.create(
username='boss',
email='important_person@example.com',
password='123456',
roles=('Member', 'VIP'),
)
api.user.create(
username='superhuman',
email='xavier@example.com',
password='think_carefully',
roles=('Member', 'Manager'),
)
self._old_sm = AccessControl.SecurityManagement.getSecurityManager()
AccessControl.SecurityManagement.newSecurityManager(
self.portal.REQUEST,
self.portal.acl_users.getUser('boss'),
)
def tearDown(self):
"""Shared test environment clean-up, ran after every test."""
AccessControl.SecurityManagement.setSecurityManager(self._old_sm)
def should_allow(self, names):
for name in names:
self.portal.hpm.restrictedTraverse(name)
def should_forbid(self, names):
for name in names:
with self.assertRaises(Unauthorized):
self.portal.hpm.restrictedTraverse(name)
def test_test_defaults(self):
"""Test that the default set-up does what I expect it to."""
self.should_allow([
'public_method',
'pp_method',
'qq_method',
])
self.should_forbid([
'rr_method',
'private_method',
])
def test_adopt_manager_role(self):
"""Test that we can adopt the Manager role temporarily."""
with api.env.adopt_roles(roles=['Manager']):
self.should_allow([
'public_method',
'pp_method',
'qq_method',
'rr_method',
])
self.should_forbid([
'private_method',
])
self.test_test_defaults()
def test_adopt_manager_string_role(self):
"""Test that we can adopt the Manager role temporarily."""
with api.env.adopt_roles(roles='Manager'):
self.should_allow([
'public_method',
'pp_method',
'qq_method',
'rr_method',
])
self.should_forbid([
'private_method',
])
self.test_test_defaults()
def test_adopt_fewers_role(self):
"""Test that we can adopt a non-Manager role temporarily."""
with api.env.adopt_roles(roles=['Member']):
self.should_allow([
'public_method',
'pp_method',
])
self.should_forbid([
'qq_method',
'rr_method',
'private_method',
])
self.test_test_defaults()
def test_drop_to_anon(self):
"""Test that we can drop roles."""
with api.env.adopt_roles(roles=['Anonymous']):
self.should_allow([
'public_method',
])
self.should_forbid([
'pp_method',
'rr_method',
'qq_method',
'private_method',
])
def test_content_owner_role(self):
"""Tests that adopting a role should not affect content ownership."""
with api.env.adopt_roles(roles=['Manager']):
doc2 = api.content.create(
container=self.portal,
type='Document',
id='doc_2',
)
intended = self.portal.acl_users.getUser(api.user.get_current().id)
actual = doc2.getOwner()
self.assertEqual(actual.getPhysicalPath(), intended.getPhysicalPath())
def test_become_manager_by_name(self):
"""Tests that becoming a manager user works."""
with api.env.adopt_user(username='superhuman'):
self.should_allow([
'public_method',
'pp_method',
'qq_method',
'rr_method',
])
self.should_forbid([
'private_method',
])
self.test_test_defaults()
def test_become_manager_by_obj(self):
"""Tests that becoming a manager with user from api.user works."""
with api.env.adopt_user(user=api.user.get(username='superhuman')):
self.should_allow([
'public_method',
'pp_method',
'qq_method',
'rr_method',
])
self.should_forbid([
'private_method',
])
self.test_test_defaults()
def test_become_manager_by_acl_user(self):
"""Tests that becoming a user with user from acl_users works."""
acl_users = api.portal.get().acl_users
au_ordinary = acl_users.getUser('worker')
with api.env.adopt_user(user=au_ordinary):
self.should_allow([
'public_method',
'pp_method',
])
self.should_forbid([
'private_method',
'qq_method',
'rr_method',
])
au_manager = acl_users.getUser('superhuman')
with api.env.adopt_user(user=au_manager):
self.should_allow([
'public_method',
'pp_method',
'qq_method',
'rr_method',
])
self.should_forbid([
'private_method',
])
def test_become_ordinary(self):
"""Tests that becoming a user with fewer permissions works."""
with api.env.adopt_user(username='worker'):
self.should_allow([
'public_method',
'pp_method',
])
self.should_forbid([
'private_method',
'qq_method',
'rr_method',
])
self.test_test_defaults()
def test_adopted_content_ownership(self):
"""Tests that content created while user-switched is owned."""
with api.env.adopt_user(username='superhuman'):
doc3 = api.content.create(
container=self.portal,
type='Document',
id='doc_3',
)
intended = self.portal.acl_users.getUser('superhuman')
actual = doc3.getOwner()
self.assertEqual(actual.getPhysicalPath(), intended.getPhysicalPath())
def test_adopted_nested_ownership(self):
"""Test deep nesting of adopt_user and adopt_roles blocks."""
with api.env.adopt_user(username='worker'):
self.should_allow([
'public_method',
'pp_method',
])
self.should_forbid([
'private_method',
'qq_method',
'rr_method',
])
with api.env.adopt_roles(['Anonymous']):
self.should_allow([
'public_method',
])
self.should_forbid([
'private_method',
'pp_method',
'qq_method',
'rr_method',
])
with api.env.adopt_user(username='boss'):
self.should_allow([
'public_method',
'pp_method',
'qq_method',
])
self.should_forbid([
'private_method',
'rr_method',
])
with api.env.adopt_roles(['Manager']):
self.should_allow([
'public_method',
'pp_method',
'qq_method',
'rr_method',
])
self.should_forbid([
'private_method',
])
with api.env.adopt_roles(['Anonymous']):
self.should_allow([
'public_method',
])
self.should_forbid([
'private_method',
'pp_method',
'qq_method',
'rr_method',
])
# /roles Anonymous
doc4 = api.content.create(
container=self.portal,
type='Document',
id='doc_ock',
)
intended = self.portal.acl_users.getUser('boss')
intended_pp = intended.getPhysicalPath()
actual = doc4.getOwner()
actual_pp = actual.getPhysicalPath()
self.assertEqual(actual_pp, intended_pp)
# /roles Manager
self.should_allow([
'public_method',
'pp_method',
'qq_method',
])
self.should_forbid([
'private_method',
'rr_method',
])
# /user boss
self.should_allow([
'public_method',
])
self.should_forbid([
'private_method',
'pp_method',
'qq_method',
'rr_method',
])
# /roles Anonymous
self.should_allow([
'public_method',
'pp_method',
])
self.should_forbid([
'private_method',
'qq_method',
'rr_method',
])
# /user worker
def test_adopting_zope_users(self):
api.env.adopt_user(username='admin')
api.env.adopt_user(user=api.user.get(username='admin'))
def test_adopting_anonymous(self):
from AccessControl.users import nobody
self.assertNotEqual(nobody, api.user.get_current())
with api.env.adopt_user(user=nobody):
self.assertEqual(nobody, api.user.get_current())
def test_empty_warning(self):
"""Tests that empty roles lists get warned about."""
from plone.api.exc import InvalidParameterError
with self.assertRaises(InvalidParameterError):
api.env.adopt_roles([])
def test_argument_requirement(self):
"""Tests that missing arguments don't go unnoticed."""
from plone.api.exc import MissingParameterError
with self.assertRaises(MissingParameterError):
api.env.adopt_roles()
def test_debug_mode(self):
"""Tests that the retured value is the same as
getConfiguration.debug_mode."""
from App.config import getConfiguration
from plone.api.env import debug_mode
getConfiguration().debug_mode = True
self.assertEqual(debug_mode(), True)
getConfiguration().debug_mode = False
self.assertEqual(debug_mode(), False)
def test_test_mode(self):
"""Tests that test_mode() returns True as we are in a test runner."""
from plone.api.env import test_mode
self.assertEqual(test_mode(), True)
def test_read_only_mode(self):
"""Test that read_only_mode() returns False
as we have a write enabled ZODB."""
from plone.api.env import read_only_mode
self.assertFalse(read_only_mode())
def test_plone_version(self):
"""Tests that plone_version() returns Plone version."""
from plone.api.env import plone_version
self.assertTrue(isinstance(plone_version(), str))
self.assertRegexpMatches(plone_version(), version_regexp)
def test_zope_version(self):
"""Tests that zope_version() returns Zope version."""
from plone.api.env import zope_version
self.assertTrue(isinstance(zope_version(), str))
self.assertRegexpMatches(zope_version(), version_regexp)
def test_adopt_user_different_username(self):
user = api.user.get(userid=TEST_USER_ID)
with api.env.adopt_user(user=user):
self.assertEqual(api.user.get_current().getId(), TEST_USER_ID)