diff options
| author | Chris McDonough <chrism@plope.com> | 2012-10-07 02:03:47 -0400 |
|---|---|---|
| committer | Chris McDonough <chrism@plope.com> | 2012-10-07 02:03:47 -0400 |
| commit | 1273d56ee5c038f447dce0525844cd3ea6c15e4d (patch) | |
| tree | 11ab8f12072353e7e324e66bda5914c78cd5c1ea | |
| parent | 7d8e08ecff656fd0f525a0fd655cdf20915f5fa8 (diff) | |
| download | pyramid-1273d56ee5c038f447dce0525844cd3ea6c15e4d.tar.gz pyramid-1273d56ee5c038f447dce0525844cd3ea6c15e4d.tar.bz2 pyramid-1273d56ee5c038f447dce0525844cd3ea6c15e4d.zip | |
- 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.
| -rw-r--r-- | CHANGES.txt | 19 | ||||
| -rw-r--r-- | pyramid/config/testing.py | 26 | ||||
| -rw-r--r-- | pyramid/testing.py | 15 | ||||
| -rw-r--r-- | pyramid/tests/test_config/test_testing.py | 24 |
4 files changed, 79 insertions, 5 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 4b3dabbec..df4ada7e9 100644 --- a/CHANGES.txt +++ b/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) ================== diff --git a/pyramid/config/testing.py b/pyramid/config/testing.py index f40cf25a7..abbbffc10 100644 --- a/pyramid/config/testing.py +++ b/pyramid/config/testing.py @@ -19,7 +19,8 @@ from pyramid.config.util import action_method 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 @@ class TestingConfiguratorMixin(object): 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 @@ class TestingConfiguratorMixin(object): :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 diff --git a/pyramid/testing.py b/pyramid/testing.py index 9e8f2bff3..cecf13469 100644 --- a/pyramid/testing.py +++ b/pyramid/testing.py @@ -53,10 +53,17 @@ class DummyRootFactory(object): 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 @@ class DummySecurityPolicy(object): 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 diff --git a/pyramid/tests/test_config/test_testing.py b/pyramid/tests/test_config/test_testing.py index 6c048b46d..1089f09fc 100644 --- a/pyramid/tests/test_config/test_testing.py +++ b/pyramid/tests/test_config/test_testing.py @@ -23,6 +23,30 @@ class TestingConfiguratorMixinTests(unittest.TestCase): 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 |
