Michael Merickel
2018-10-04 052cb72b63b9f5a36f45a4135d1e1c05c10004dd
commit | author | age
2466f6 1 import unittest
CM 2
072a2c 3 from pyramid import testing
86ed40 4
226b49 5 class TestAllPermissionsList(unittest.TestCase):
CM 6     def setUp(self):
072a2c 7         testing.setUp()
226b49 8
CM 9     def tearDown(self):
072a2c 10         testing.tearDown()
226b49 11
CM 12     def _getTargetClass(self):
b60bdb 13         from pyramid.security import AllPermissionsList
226b49 14         return AllPermissionsList
CM 15
16     def _makeOne(self):
17         return self._getTargetClass()()
18
1814cd 19     def test_equality_w_self(self):
226b49 20         thing = self._makeOne()
a1d395 21         self.assertTrue(thing.__eq__(thing))
1814cd 22
TS 23     def test_equality_w_other_instances_of_class(self):
24         thing = self._makeOne()
25         other = self._makeOne()
26         self.assertTrue(thing.__eq__(other))
27
28     def test_equality_miss(self):
29         thing = self._makeOne()
30         other = object()
31         self.assertFalse(thing.__eq__(other))
32
33     def test_contains_w_string(self):
34         thing = self._makeOne()
a1d395 35         self.assertTrue('anything' in thing)
226b49 36
1814cd 37     def test_contains_w_object(self):
TS 38         thing = self._makeOne()
39         self.assertTrue(object() in thing)
40
41     def test_iterable(self):
42         thing = self._makeOne()
43         self.assertEqual(list(thing), [])
44
226b49 45     def test_singleton(self):
b60bdb 46         from pyramid.security import ALL_PERMISSIONS
226b49 47         self.assertEqual(ALL_PERMISSIONS.__class__, self._getTargetClass())
CM 48
17ce57 49 class TestAllowed(unittest.TestCase):
CM 50     def _getTargetClass(self):
b60bdb 51         from pyramid.security import Allowed
17ce57 52         return Allowed
CM 53     
54     def _makeOne(self, *arg, **kw):
55         klass = self._getTargetClass()
56         return klass(*arg, **kw)
57
58     def test_it(self):
59         allowed = self._makeOne('hello')
60         self.assertEqual(allowed.msg, 'hello')
61         self.assertEqual(allowed, True)
a1d395 62         self.assertTrue(allowed)
17ce57 63         self.assertEqual(str(allowed), 'hello')
a1d395 64         self.assertTrue('<Allowed instance at ' in repr(allowed))
CM 65         self.assertTrue("with msg 'hello'>" in repr(allowed))
17ce57 66
CM 67 class TestDenied(unittest.TestCase):
68     def _getTargetClass(self):
b60bdb 69         from pyramid.security import Denied
17ce57 70         return Denied
CM 71     
72     def _makeOne(self, *arg, **kw):
73         klass = self._getTargetClass()
74         return klass(*arg, **kw)
75
76     def test_it(self):
77         denied = self._makeOne('hello')
78         self.assertEqual(denied.msg, 'hello')
79         self.assertEqual(denied, False)
a1d395 80         self.assertFalse(denied)
17ce57 81         self.assertEqual(str(denied), 'hello')
a1d395 82         self.assertTrue('<Denied instance at ' in repr(denied))
CM 83         self.assertTrue("with msg 'hello'>" in repr(denied))
17ce57 84
CM 85 class TestACLAllowed(unittest.TestCase):
86     def _getTargetClass(self):
b60bdb 87         from pyramid.security import ACLAllowed
17ce57 88         return ACLAllowed
CM 89     
90     def _makeOne(self, *arg, **kw):
91         klass = self._getTargetClass()
92         return klass(*arg, **kw)
93
94     def test_it(self):
213001 95         from pyramid.security import Allowed
17ce57 96         msg = ("ACLAllowed permission 'permission' via ACE 'ace' in ACL 'acl' "
CM 97                "on context 'ctx' for principals 'principals'")
98         allowed = self._makeOne('ace', 'acl', 'permission', 'principals', 'ctx')
213001 99         self.assertIsInstance(allowed, Allowed)
a1d395 100         self.assertTrue(msg in allowed.msg)
17ce57 101         self.assertEqual(allowed, True)
a1d395 102         self.assertTrue(allowed)
17ce57 103         self.assertEqual(str(allowed), msg)
a1d395 104         self.assertTrue('<ACLAllowed instance at ' in repr(allowed))
CM 105         self.assertTrue("with msg %r>" % msg in repr(allowed))
17ce57 106
CM 107 class TestACLDenied(unittest.TestCase):
108     def _getTargetClass(self):
b60bdb 109         from pyramid.security import ACLDenied
17ce57 110         return ACLDenied
CM 111     
112     def _makeOne(self, *arg, **kw):
113         klass = self._getTargetClass()
114         return klass(*arg, **kw)
115
116     def test_it(self):
213001 117         from pyramid.security import Denied
17ce57 118         msg = ("ACLDenied permission 'permission' via ACE 'ace' in ACL 'acl' "
CM 119                "on context 'ctx' for principals 'principals'")
120         denied = self._makeOne('ace', 'acl', 'permission', 'principals', 'ctx')
213001 121         self.assertIsInstance(denied, Denied)
a1d395 122         self.assertTrue(msg in denied.msg)
17ce57 123         self.assertEqual(denied, False)
a1d395 124         self.assertFalse(denied)
17ce57 125         self.assertEqual(str(denied), msg)
a1d395 126         self.assertTrue('<ACLDenied instance at ' in repr(denied))
CM 127         self.assertTrue("with msg %r>" % msg in repr(denied))
c8cab3 128
3c2f95 129 class TestPrincipalsAllowedByPermission(unittest.TestCase):
a1a9fb 130     def setUp(self):
072a2c 131         testing.setUp()
a1a9fb 132
CM 133     def tearDown(self):
072a2c 134         testing.tearDown()
a1a9fb 135
3c2f95 136     def _callFUT(self, *arg):
MR 137         from pyramid.security import principals_allowed_by_permission
138         return principals_allowed_by_permission(*arg)
139
140     def test_no_authorization_policy(self):
141         from pyramid.security import Everyone
142         context = DummyContext()
143         result = self._callFUT(context, 'view')
144         self.assertEqual(result, [Everyone])
145
146     def test_with_authorization_policy(self):
147         from pyramid.threadlocal import get_current_registry
148         registry = get_current_registry()
149         _registerAuthorizationPolicy(registry, 'yo')
150         context = DummyContext()
151         result = self._callFUT(context, 'view')
152         self.assertEqual(result, 'yo')
153
0dcd56 154 class TestRemember(unittest.TestCase):
CM 155     def setUp(self):
156         testing.setUp()
157         
158     def tearDown(self):
159         testing.tearDown()
160
7a2b72 161     def _callFUT(self, *arg, **kwarg):
0dcd56 162         from pyramid.security import remember
7a2b72 163         return remember(*arg, **kwarg)
0dcd56 164
CM 165     def test_no_authentication_policy(self):
166         request = _makeRequest()
167         result = self._callFUT(request, 'me')
168         self.assertEqual(result, [])
169
170     def test_with_authentication_policy(self):
171         request = _makeRequest()
172         registry = request.registry
173         _registerAuthenticationPolicy(registry, 'yo')
174         result = self._callFUT(request, 'me')
175         self.assertEqual(result, [('X-Pyramid-Test', 'me')])
176
177     def test_with_authentication_policy_no_reg_on_request(self):
178         from pyramid.threadlocal import get_current_registry
179         registry = get_current_registry()
180         request = _makeRequest()
181         del request.registry
182         _registerAuthenticationPolicy(registry, 'yo')
183         result = self._callFUT(request, 'me')
184         self.assertEqual(result, [('X-Pyramid-Test', 'me')])
185
7a2b72 186     def test_with_missing_arg(self):
MM 187         request = _makeRequest()
188         registry = request.registry
189         _registerAuthenticationPolicy(registry, 'yo')
190         self.assertRaises(TypeError, lambda: self._callFUT(request))
191
0dcd56 192 class TestForget(unittest.TestCase):
CM 193     def setUp(self):
194         testing.setUp()
195         
196     def tearDown(self):
197         testing.tearDown()
198
199     def _callFUT(self, *arg):
200         from pyramid.security import forget
201         return forget(*arg)
202
203     def test_no_authentication_policy(self):
204         request = _makeRequest()
205         result = self._callFUT(request)
206         self.assertEqual(result, [])
207
208     def test_with_authentication_policy(self):
209         request = _makeRequest()
210         _registerAuthenticationPolicy(request.registry, 'yo')
211         result = self._callFUT(request)
212         self.assertEqual(result, [('X-Pyramid-Test', 'logout')])
213
214     def test_with_authentication_policy_no_reg_on_request(self):
215         from pyramid.threadlocal import get_current_registry
216         registry = get_current_registry()
217         request = _makeRequest()
218         del request.registry
219         _registerAuthenticationPolicy(registry, 'yo')
220         result = self._callFUT(request)
221         self.assertEqual(result, [('X-Pyramid-Test', 'logout')])
222         
3c2f95 223 class TestViewExecutionPermitted(unittest.TestCase):
MR 224     def setUp(self):
072a2c 225         testing.setUp()
3c2f95 226
MR 227     def tearDown(self):
072a2c 228         testing.tearDown()
3c2f95 229     
a1a9fb 230     def _callFUT(self, *arg, **kw):
b60bdb 231         from pyramid.security import view_execution_permitted
a1a9fb 232         return view_execution_permitted(*arg, **kw)
CM 233
d66bfb 234     def _registerSecuredView(self, view_name, allow=True):
b60bdb 235         from pyramid.threadlocal import get_current_registry
a1a9fb 236         from zope.interface import Interface
b60bdb 237         from pyramid.interfaces import ISecuredView
CM 238         from pyramid.interfaces import IViewClassifier
a1a9fb 239         class Checker(object):
d66bfb 240             def __permitted__(self, context, request):
a1a9fb 241                 self.context = context
CM 242                 self.request = request
243                 return allow
244         checker = Checker()
b41a3e 245         reg = get_current_registry()
ff1213 246         reg.registerAdapter(checker, (IViewClassifier, Interface, Interface),
b41a3e 247                             ISecuredView, view_name)
a1a9fb 248         return checker
CM 249
250     def test_no_permission(self):
4b552e 251         from zope.interface import Interface
MM 252         from pyramid.threadlocal import get_current_registry
253         from pyramid.interfaces import ISettings
254         from pyramid.interfaces import IView
255         from pyramid.interfaces import IViewClassifier
256         settings = dict(debug_authorization=True)
257         reg = get_current_registry()
258         reg.registerUtility(settings, ISettings)
259         context = DummyContext()
072a2c 260         request = testing.DummyRequest({})
4b552e 261         class DummyView(object):
MM 262             pass
263         view = DummyView()
264         reg.registerAdapter(view, (IViewClassifier, Interface, Interface),
265                             IView, '')
266         result = self._callFUT(context, request, '')
267         msg = result.msg
268         self.assertTrue("Allowed: view name '' in context" in msg)
269         self.assertTrue('(no permission defined)' in msg)
270         self.assertEqual(result, True)
271
272     def test_no_view_registered(self):
b60bdb 273         from pyramid.threadlocal import get_current_registry
CM 274         from pyramid.interfaces import ISettings
567709 275         settings = dict(debug_authorization=True)
b41a3e 276         reg = get_current_registry()
CM 277         reg.registerUtility(settings, ISettings)
a1a9fb 278         context = DummyContext()
072a2c 279         request = testing.DummyRequest({})
4b552e 280         self.assertRaises(TypeError, self._callFUT, context, request, '')
a1a9fb 281
CM 282     def test_with_permission(self):
283         from zope.interface import Interface
284         from zope.interface import directlyProvides
b60bdb 285         from pyramid.interfaces import IRequest
a1a9fb 286         class IContext(Interface):
CM 287             pass
288         context = DummyContext()
289         directlyProvides(context, IContext)
9a038d 290         self._registerSecuredView('', True)
072a2c 291         request = testing.DummyRequest({})
a1a9fb 292         directlyProvides(request, IRequest)
CM 293         result = self._callFUT(context, request, '')
3c2f95 294         self.assertTrue(result)
a1a9fb 295
072a2c 296 class TestAuthenticatedUserId(unittest.TestCase):
3c2f95 297     def setUp(self):
072a2c 298         testing.setUp()
3c2f95 299
MR 300     def tearDown(self):
072a2c 301         testing.tearDown()
CM 302         
3c2f95 303     def test_backward_compat_delegates_to_mixin(self):
0184b5 304         from zope.deprecation import __show__
CM 305         try:
306             __show__.off()
072a2c 307             request = _makeFakeRequest()
0184b5 308             from pyramid.security import authenticated_userid
CM 309             self.assertEqual(
310                 authenticated_userid(request),
311                 'authenticated_userid'
312                 )
313         finally:
314             __show__.on()
3c2f95 315
MR 316     def test_no_authentication_policy(self):
072a2c 317         request = _makeRequest()
3c2f95 318         self.assertEqual(request.authenticated_userid, None)
MR 319
320     def test_with_authentication_policy(self):
072a2c 321         request = _makeRequest()
3c2f95 322         _registerAuthenticationPolicy(request.registry, 'yo')
MR 323         self.assertEqual(request.authenticated_userid, 'yo')
324
325     def test_with_authentication_policy_no_reg_on_request(self):
326         from pyramid.threadlocal import get_current_registry
327         registry = get_current_registry()
072a2c 328         request = _makeRequest()
3c2f95 329         del request.registry
MR 330         _registerAuthenticationPolicy(registry, 'yo')
331         self.assertEqual(request.authenticated_userid, 'yo')
332
072a2c 333 class TestUnAuthenticatedUserId(unittest.TestCase):
CM 334     def setUp(self):
335         testing.setUp()
336
337     def tearDown(self):
338         testing.tearDown()
339         
3c2f95 340     def test_backward_compat_delegates_to_mixin(self):
0184b5 341         from zope.deprecation import __show__
CM 342         try:
343             __show__.off()
072a2c 344             request = _makeFakeRequest()
0184b5 345             from pyramid.security import unauthenticated_userid
CM 346             self.assertEqual(
347                 unauthenticated_userid(request),
348                 'unauthenticated_userid',
349                 )
350         finally:
351             __show__.on()
3c2f95 352
MR 353     def test_no_authentication_policy(self):
072a2c 354         request = _makeRequest()
3c2f95 355         self.assertEqual(request.unauthenticated_userid, None)
MR 356
357     def test_with_authentication_policy(self):
072a2c 358         request = _makeRequest()
3c2f95 359         _registerAuthenticationPolicy(request.registry, 'yo')
MR 360         self.assertEqual(request.unauthenticated_userid, 'yo')
361
362     def test_with_authentication_policy_no_reg_on_request(self):
363         from pyramid.threadlocal import get_current_registry
364         registry = get_current_registry()
072a2c 365         request = _makeRequest()
3c2f95 366         del request.registry
MR 367         _registerAuthenticationPolicy(registry, 'yo')
368         self.assertEqual(request.unauthenticated_userid, 'yo')
369
072a2c 370 class TestEffectivePrincipals(unittest.TestCase):
CM 371     def setUp(self):
372         testing.setUp()
373
374     def tearDown(self):
375         testing.tearDown()
376         
3c2f95 377     def test_backward_compat_delegates_to_mixin(self):
072a2c 378         request = _makeFakeRequest()
0184b5 379         from zope.deprecation import __show__
CM 380         try:
381             __show__.off()
382             from pyramid.security import effective_principals
383             self.assertEqual(
384                 effective_principals(request),
385                 'effective_principals'
386                 )
387         finally:
388             __show__.on()
3c2f95 389
MR 390     def test_no_authentication_policy(self):
391         from pyramid.security import Everyone
072a2c 392         request = _makeRequest()
3c2f95 393         self.assertEqual(request.effective_principals, [Everyone])
MR 394
395     def test_with_authentication_policy(self):
072a2c 396         request = _makeRequest()
3c2f95 397         _registerAuthenticationPolicy(request.registry, 'yo')
MR 398         self.assertEqual(request.effective_principals, 'yo')
399
400     def test_with_authentication_policy_no_reg_on_request(self):
401         from pyramid.threadlocal import get_current_registry
402         registry = get_current_registry()
072a2c 403         request = _makeRequest()
3c2f95 404         del request.registry
MR 405         _registerAuthenticationPolicy(registry, 'yo')
406         self.assertEqual(request.effective_principals, 'yo')
407
a1a9fb 408 class TestHasPermission(unittest.TestCase):
CM 409     def setUp(self):
072a2c 410         testing.setUp()
a1a9fb 411         
CM 412     def tearDown(self):
072a2c 413         testing.tearDown()
a1a9fb 414
3c2f95 415     def _makeOne(self):
MR 416         from pyramid.security import AuthorizationAPIMixin
417         from pyramid.registry import Registry
418         mixin = AuthorizationAPIMixin()
419         mixin.registry = Registry()
420         mixin.context = object()
421         return mixin
422
423     def test_delegates_to_mixin(self):
0184b5 424         from zope.deprecation import __show__
CM 425         try:
426             __show__.off()
427             mixin = self._makeOne()
428             from pyramid.security import has_permission
429             self.called_has_permission = False
3c2f95 430
0184b5 431             def mocked_has_permission(*args, **kw):
CM 432                 self.called_has_permission = True
3c2f95 433
0184b5 434             mixin.has_permission = mocked_has_permission
CM 435             has_permission('view', object(), mixin)
436             self.assertTrue(self.called_has_permission)
437         finally:
438             __show__.on()
a1a9fb 439
CM 440     def test_no_authentication_policy(self):
3c2f95 441         request = self._makeOne()
MR 442         result = request.has_permission('view')
443         self.assertTrue(result)
a1a9fb 444         self.assertEqual(result.msg, 'No authentication policy in use.')
CM 445
3c2f95 446     def test_with_no_authorization_policy(self):
MR 447         request = self._makeOne()
448         _registerAuthenticationPolicy(request.registry, None)
449         self.assertRaises(ValueError,
450                           request.has_permission, 'view', context=None)
451
452     def test_with_authn_and_authz_policies_registered(self):
453         request = self._makeOne()
41723e 454         _registerAuthenticationPolicy(request.registry, None)
CM 455         _registerAuthorizationPolicy(request.registry, 'yo')
3c2f95 456         self.assertEqual(request.has_permission('view', context=None), 'yo')
41723e 457
3c2f95 458     def test_with_no_reg_on_request(self):
b60bdb 459         from pyramid.threadlocal import get_current_registry
41723e 460         registry = get_current_registry()
3c2f95 461         request = self._makeOne()
MR 462         del request.registry
41723e 463         _registerAuthenticationPolicy(registry, None)
CM 464         _registerAuthorizationPolicy(registry, 'yo')
3c2f95 465         self.assertEqual(request.has_permission('view'), 'yo')
a1a9fb 466
3c2f95 467     def test_with_no_context_passed(self):
MR 468         request = self._makeOne()
469         self.assertTrue(request.has_permission('view'))
a1a9fb 470
3c2f95 471     def test_with_no_context_passed_or_on_request(self):
MR 472         request = self._makeOne()
473         del request.context
474         self.assertRaises(AttributeError, request.has_permission, 'view')
a1a9fb 475
072a2c 476 _TEST_HEADER = 'X-Pyramid-Test'
CM 477
2466f6 478 class DummyContext:
226b49 479     def __init__(self, *arg, **kw):
CM 480         self.__dict__.update(kw)
2466f6 481
a1a9fb 482 class DummyAuthenticationPolicy:
2466f6 483     def __init__(self, result):
CM 484         self.result = result
485
7ec9e7 486     def effective_principals(self, request):
2466f6 487         return self.result
CM 488
2526d8 489     def unauthenticated_userid(self, request):
CM 490         return self.result
491
7ec9e7 492     def authenticated_userid(self, request):
a1a9fb 493         return self.result
b54cdb 494
c7afe4 495     def remember(self, request, userid, **kw):
KOP 496         headers = [(_TEST_HEADER, userid)]
3c2f95 497         self._header_remembered = headers[0]
MR 498         return headers
a1a9fb 499
7ec9e7 500     def forget(self, request):
0dcd56 501         headers = [(_TEST_HEADER, 'logout')]
072a2c 502         self._header_forgotten = headers[0]
CM 503         return headers
a1a9fb 504
CM 505 class DummyAuthorizationPolicy:
506     def __init__(self, result):
507         self.result = result
508
509     def permits(self, context, principals, permission):
510         return self.result
b54cdb 511
64ea2e 512     def principals_allowed_by_permission(self, context, permission):
a1a9fb 513         return self.result
226b49 514
41723e 515 def _registerAuthenticationPolicy(reg, result):
b60bdb 516     from pyramid.interfaces import IAuthenticationPolicy
41723e 517     policy = DummyAuthenticationPolicy(result)
CM 518     reg.registerUtility(policy, IAuthenticationPolicy)
519     return policy
520
521 def _registerAuthorizationPolicy(reg, result):
b60bdb 522     from pyramid.interfaces import IAuthorizationPolicy
41723e 523     policy = DummyAuthorizationPolicy(result)
CM 524     reg.registerUtility(policy, IAuthorizationPolicy)
525     return policy
072a2c 526
CM 527 def _makeRequest():
528     from pyramid.registry import Registry
529     request = testing.DummyRequest(environ={})
530     request.registry = Registry()
531     request.context = object()
532     return request
533
534 def _makeFakeRequest():
535     class FakeRequest(testing.DummyRequest):
536         @property
537         def authenticated_userid(req):
538             return 'authenticated_userid'
539
540         @property
541         def unauthenticated_userid(req):
542             return 'unauthenticated_userid'
543
544         @property
545         def effective_principals(req):
546             return 'effective_principals'
547
548     return FakeRequest({})
549