Skip to content

Commit

Permalink
add tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
merwok committed Jul 1, 2020
1 parent baa5928 commit 28b4471
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 12 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ Features
- ``pyramid.config.Configurator.set_security_policy``.
- ``pyramid.interfaces.ISecurityPolicy``
- ``pyramid.request.Request.authenticated_identity``.
- ``pyramid.request.Request.authenticated_userid``
- ``pyramid.request.Request.is_authenticated``
- ``pyramid.authentication.SessionAuthenticationHelper``
- ``pyramid.authorization.ACLHelper``
- config predicate ``is_authenticated=True/False``

See https://github.com/Pylons/pyramid/pull/3465
See https://github.com/Pylons/pyramid/pull/3465 and
https://github.com/Pylons/pyramid/pull/3598

- Changed the default ``serializer`` on
``pyramid.session.SignedCookieSessionFactory`` to use
Expand Down
6 changes: 5 additions & 1 deletion docs/narr/viewconfig.rst
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,11 @@ configured view.

``is_authenticated``

XXX doc doc
This value, if specified, should be either ``True`` or ``False``. If it is
specified and is ``True``, the request must be for an authenticated user,
as determined by the :term:`security policy` in use. If it is specified and
``False``, the associated view callable will match only if the request does
not have an authenticated user.

.. versionadded:: 2.0

Expand Down
7 changes: 6 additions & 1 deletion src/pyramid/config/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,12 @@ def add_route(
is_authenticated
XXX doc doc
This value, if specified, should be either ``True`` or ``False``.
If it is specified and is ``True``, the route will only match if
the request has an authenticated user, as determined by the
:term:`security policy` in use. If it is specified and ``False``,
the route will only match if the request does not have an
authenticated user.
.. versionadded:: 2.0
Expand Down
6 changes: 5 additions & 1 deletion src/pyramid/config/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,11 @@ def wrapper(context, request):
is_authenticated
XXX doc doc
This value, if specified, should be either ``True`` or ``False``.
If it is specified and is ``True``, the request must be for an
authenticated user, as determined by the :term:`security policy` in
use. If it is specified and ``False``, the associated view callable
will match only if the request does not have an authenticated user.
..versionadded:: 2.0
Expand Down
9 changes: 6 additions & 3 deletions src/pyramid/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ def app_iter_range(start, stop):
serves up only the given start:stop range. """

authenticated_identity = Attribute(
"""XXX Doc doc"""
"""An object representing the authenticated user, as determined by
the security policy in use. The object's class and meaning is defined
by the security policy. Will be None for unauthenticated requests."""
)

authenticated_userid = Attribute(
"""XXX Doc doc"""
"""A string to identify the authenticated user, or None."""
)

body = Attribute(
Expand Down Expand Up @@ -242,7 +244,8 @@ def encode_content(encoding='gzip', lazy=False):
headers = Attribute(""" The headers in a dictionary-like object """)

is_authenticated = Attribute(
"""XXX doc doc"""
"""A boolean indicated whether the request has an authenticated
user (determined by the security policy in use)."""
)

last_modified = Attribute(
Expand Down
2 changes: 1 addition & 1 deletion src/pyramid/predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def text(self):
phash = text

def __call__(self, context, request):
return request.is_authenticated == self.val
return bool(request.is_authenticated) is self.val


class EffectivePrincipalsPredicate:
Expand Down
23 changes: 19 additions & 4 deletions tests/test_config/test_predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,28 @@ def test_header_multiple_mixed_fails(self):
self.assertFalse(predicates[0](Dummy(), request))

def test_is_authenticated_true_matches(self):
...
_, predicates, _ = self._callFUT(is_authenticated=True)
request = DummyRequest()
request.is_authenticated = True
self.assertTrue(predicates[0](Dummy(), request))

def test_is_authenticated_true_fails(self):
...
_, predicates, _ = self._callFUT(is_authenticated=True)
request = DummyRequest()
request.is_authenticated = False
self.assertFalse(predicates[0](Dummy(), request))

def test_is_authenticated_false_matches(self):
...
_, predicates, _ = self._callFUT(is_authenticated=False)
request = DummyRequest()
request.is_authenticated = False
self.assertTrue(predicates[0](Dummy(), request))

def test_is_authenticated_false_fails(self):
...
_, predicates, _ = self._callFUT(is_authenticated=False)
request = DummyRequest()
request.is_authenticated = True
self.assertFalse(predicates[0](Dummy(), request))

def test_unknown_predicate(self):
from pyramid.exceptions import ConfigurationError
Expand Down
23 changes: 23 additions & 0 deletions tests/test_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,29 @@ def test_security_policy_trumps_authentication_policy(self):
self.assertEqual(request.unauthenticated_userid, 'wat')


class TestIsAuthenticated(unittest.TestCase):
def setUp(self):
testing.setUp()

def tearDown(self):
testing.tearDown()

def test_no_security_policy(self):
request = _makeRequest()
self.assertIs(request.is_authenticated, False)

def test_with_security_policy(self):
request = _makeRequest()
_registerSecurityPolicy(request.registry, '123')
self.assertIs(request.is_authenticated, True)

def test_with_legacy_security_policy(self):
request = _makeRequest()
_registerAuthenticationPolicy(request.registry, 'yo')
_registerLegacySecurityPolicy(request.registry)
self.assertEqual(request.authenticated_userid, 'yo')


class TestEffectivePrincipals(unittest.TestCase):
def setUp(self):
testing.setUp()
Expand Down

0 comments on commit 28b4471

Please sign in to comment.