Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- The Configurator ``testing_securitypolicy`` method now returns the …

…policy

  object it creates.

- The Configurator ``testing_securitypolicy`` method accepts two new
  arguments: ``remember_result`` and ``forget_result``.  If supplied, these
  values influence the result of the policy's ``remember`` and ``forget``
  methods, respectively.

- The DummySecurityPolicy created by ``testing_securitypolicy`` now sets a
  ``forgotten`` value on the policy (the value ``True``) when its ``forget``
  method is called.

- The DummySecurityPolicy created by ``testing_securitypolicy`` now sets a
  ``remembered`` value on the policy, which is the value of the ``principal``
  argument it's called with when its ``remember`` method is called.
  • Loading branch information...
commit 1273d56ee5c038f447dce0525844cd3ea6c15e4d 1 parent 7d8e08e
@mcdonc mcdonc authored
View
19 CHANGES.txt
@@ -21,6 +21,25 @@ Bug Fixes
confuse the two, and your code might have rendered one instead of the
other.
+Features
+--------
+
+- The Configurator ``testing_securitypolicy`` method now returns the policy
+ object it creates.
+
+- The Configurator ``testing_securitypolicy`` method accepts two new
+ arguments: ``remember_result`` and ``forget_result``. If supplied, these
+ values influence the result of the policy's ``remember`` and ``forget``
+ methods, respectively.
+
+- The DummySecurityPolicy created by ``testing_securitypolicy`` now sets a
+ ``forgotten`` value on the policy (the value ``True``) when its ``forget``
+ method is called.
+
+- The DummySecurityPolicy created by ``testing_securitypolicy`` now sets a
+ ``remembered`` value on the policy, which is the value of the ``principal``
+ argument it's called with when its ``remember`` method is called.
+
1.4a2 (2012-09-27)
==================
View
26 pyramid/config/testing.py
@@ -19,7 +19,8 @@
class TestingConfiguratorMixin(object):
# testing API
def testing_securitypolicy(self, userid=None, groupids=(),
- permissive=True):
+ permissive=True, remember_result=None,
+ forget_result=None):
"""Unit/integration testing helper: Registers a pair of faux
:app:`Pyramid` security policies: a :term:`authentication
policy` and a :term:`authorization policy`.
@@ -31,6 +32,24 @@ def testing_securitypolicy(self, userid=None, groupids=(),
nonpermissive :term:`authorization policy` is registered; this
policy denies all access.
+ ``remember_result``, if provided, should be the result returned by
+ the ``remember`` method of the faux authentication policy. If it is
+ not provided (or it is provided, and is ``None``), the default value
+ ``[]`` (the empty list) will be returned by ``remember``.
+
+ .. note::
+
+ ``remember_result`` is new as of Pyramid 1.4.
+
+ ``forget_result``, if provided, should be the result returned by
+ the ``forget`` method of the faux authentication policy. If it is
+ not provided (or it is provided, and is ``None``), the default value
+ ``[]`` (the empty list) will be returned by ``forget``.
+
+ .. note::
+
+ ``forget_result`` is new as of Pyramid 1.4.
+
The behavior of the registered :term:`authentication policy`
depends on the values provided for the ``userid`` and
``groupids`` argument. The authentication policy will return
@@ -47,9 +66,12 @@ def testing_securitypolicy(self, userid=None, groupids=(),
:func:`pyramid.security.principals_allowed_by_permission`.
"""
from pyramid.testing import DummySecurityPolicy
- policy = DummySecurityPolicy(userid, groupids, permissive)
+ policy = DummySecurityPolicy(
+ userid, groupids, permissive, remember_result, forget_result
+ )
self.registry.registerUtility(policy, IAuthorizationPolicy)
self.registry.registerUtility(policy, IAuthenticationPolicy)
+ return policy
def testing_resources(self, resources):
"""Unit/integration testing helper: registers a dictionary of
View
15 pyramid/testing.py
@@ -53,10 +53,17 @@ def __init__(self, request):
class DummySecurityPolicy(object):
""" A standin for both an IAuthentication and IAuthorization policy """
- def __init__(self, userid=None, groupids=(), permissive=True):
+ def __init__(self, userid=None, groupids=(), permissive=True,
+ remember_result=None, forget_result=None):
self.userid = userid
self.groupids = groupids
self.permissive = permissive
+ if remember_result is None:
+ remember_result = []
+ if forget_result is None:
+ forget_result = []
+ self.remember_result = remember_result
+ self.forget_result = forget_result
def authenticated_userid(self, request):
return self.userid
@@ -73,10 +80,12 @@ def effective_principals(self, request):
return effective_principals
def remember(self, request, principal, **kw):
- return []
+ self.remembered = principal
+ return self.remember_result
def forget(self, request):
- return []
+ self.forgotten = True
+ return self.forget_result
def permits(self, context, principals, permission):
return self.permissive
View
24 pyramid/tests/test_config/test_testing.py
@@ -23,6 +23,30 @@ def test_testing_securitypolicy(self):
self.assertEqual(ut.groupids, ('group1', 'group2'))
self.assertEqual(ut.permissive, False)
+ def test_testing_securitypolicy_remember_result(self):
+ from pyramid.security import remember
+ config = self._makeOne(autocommit=True)
+ pol = config.testing_securitypolicy(
+ 'user', ('group1', 'group2'),
+ permissive=False, remember_result=True)
+ request = DummyRequest()
+ request.registry = config.registry
+ val = remember(request, 'fred')
+ self.assertEqual(pol.remembered, 'fred')
+ self.assertEqual(val, True)
+
+ def test_testing_securitypolicy_forget_result(self):
+ from pyramid.security import forget
+ config = self._makeOne(autocommit=True)
+ pol = config.testing_securitypolicy(
+ 'user', ('group1', 'group2'),
+ permissive=False, forget_result=True)
+ request = DummyRequest()
+ request.registry = config.registry
+ val = forget(request)
+ self.assertEqual(pol.forgotten, True)
+ self.assertEqual(val, True)
+
def test_testing_resources(self):
from pyramid.traversal import find_resource
from pyramid.interfaces import ITraverser
Please sign in to comment.
Something went wrong with that request. Please try again.