From 25cdbd76b59119bfe4b0b5b8352dc79acfef01d9 Mon Sep 17 00:00:00 2001 From: Theron Luhn Date: Sat, 7 Nov 2020 11:08:31 -0800 Subject: Remove `request.user` from wiki2 authentication tutorial. --- docs/tutorials/wiki2/authentication.rst | 15 ++++++--------- .../wiki2/src/authentication/tutorial/security.py | 2 -- .../src/authentication/tutorial/templates/layout.jinja2 | 4 ++-- .../wiki2/src/authentication/tutorial/views/auth.py | 2 +- .../wiki2/src/authentication/tutorial/views/default.py | 6 +++--- 5 files changed, 12 insertions(+), 17 deletions(-) diff --git a/docs/tutorials/wiki2/authentication.rst b/docs/tutorials/wiki2/authentication.rst index 4d8723176..414d6c879 100644 --- a/docs/tutorials/wiki2/authentication.rst +++ b/docs/tutorials/wiki2/authentication.rst @@ -10,8 +10,7 @@ APIs to add login and logout functionality to our wiki. We will implement authentication with the following steps: -* Add a :term:`security policy` and a ``request.user`` computed property - (``security.py``). +* Add a :term:`security policy` (``security.py``). * Add routes for ``/login`` and ``/logout`` (``routes.py``). * Add login and logout views (``views/auth.py``). * Add a login template (``login.jinja2``). @@ -41,10 +40,8 @@ Update ``tutorial/security.py`` with the following content: :linenos: :language: python -Here we've defined: - -* A new security policy named ``MySecurityPolicy``, which is implementing most of the :class:`pyramid.interfaces.ISecurityPolicy` interface by tracking a :term:`identity` using a signed cookie implemented by :class:`pyramid.authentication.AuthTktCookieHelper` (lines 8-34). -* The ``request.user`` computed property is registered for use throughout our application as the authenticated ``tutorial.models.User`` object for the logged-in user (line 42-44). +Here we've defined a new security policy named ``MySecurityPolicy``, which is implementing most of the :class:`pyramid.interfaces.ISecurityPolicy` interface by tracking a :term:`identity` using a signed cookie implemented by :class:`pyramid.authentication.AuthTktCookieHelper` (lines 8-34). +The security policy outputs the authenticated ``tutorial.models.User`` object for the logged-in user as the :term:`identity`, which is available as ``request.identity``. Our new :term:`security policy` defines how our application will remember, forget, and identify users. It also handles authorization, which we'll cover in the next chapter (if you're wondering why we didn't implement the ``permits`` method yet). @@ -64,7 +61,7 @@ Identifying the current user is done in a few steps: #. The result is stored in the ``identity_cache`` which ensures that subsequent invocations return the same identity object for the request. -Finally, :attr:`pyramid.request.Request.identity` contains either ``None`` or a ``tutorial.models.User`` instance and that value is aliased to ``request.user`` for convenience in our application. +Finally, :attr:`pyramid.request.Request.identity` contains either ``None`` or a ``tutorial.models.User`` instance. Note the usage of the ``identity_cache`` is optional, but it has several advantages in most scenarios: @@ -156,7 +153,7 @@ Only the highlighted lines need to be changed. If the user either is not logged in or is not in the ``basic`` or ``editor`` roles, then we raise ``HTTPForbidden``, which will trigger our forbidden view to compute a response. However, we will hook this later to redirect to the login page. -Also, now that we have ``request.user``, we no longer have to hard-code the creator as the ``editor`` user, so we can finally drop that hack. +Also, now that we have ``request.identity``, we no longer have to hard-code the creator as the ``editor`` user, so we can finally drop that hack. These simple checks should protect our views. @@ -266,7 +263,7 @@ indicated by the highlighted lines. :emphasize-lines: 2-12 :language: html -The ``request.user`` will be ``None`` if the user is not authenticated, or a +The ``request.identity`` will be ``None`` if the user is not authenticated, or a ``tutorial.models.User`` object if the user is authenticated. This check will make the logout link shown only when the user is logged in, and conversely the login link is only shown when the user is logged out. diff --git a/docs/tutorials/wiki2/src/authentication/tutorial/security.py b/docs/tutorials/wiki2/src/authentication/tutorial/security.py index a4843f286..e0d8ed965 100644 --- a/docs/tutorials/wiki2/src/authentication/tutorial/security.py +++ b/docs/tutorials/wiki2/src/authentication/tutorial/security.py @@ -40,5 +40,3 @@ def includeme(config): config.set_default_csrf_options(require_csrf=True) config.set_security_policy(MySecurityPolicy(settings['auth.secret'])) - config.add_request_method( - lambda request: request.identity, 'user', property=True) diff --git a/docs/tutorials/wiki2/src/authentication/tutorial/templates/layout.jinja2 b/docs/tutorials/wiki2/src/authentication/tutorial/templates/layout.jinja2 index 64a1db0c5..5d6a23410 100644 --- a/docs/tutorials/wiki2/src/authentication/tutorial/templates/layout.jinja2 +++ b/docs/tutorials/wiki2/src/authentication/tutorial/templates/layout.jinja2 @@ -33,13 +33,13 @@
- {% if request.user is none %} + {% if request.identity is none %}

Login

{% else %}
- {{request.user.name}} + {{request.identity.name}}
diff --git a/docs/tutorials/wiki2/src/authentication/tutorial/views/auth.py b/docs/tutorials/wiki2/src/authentication/tutorial/views/auth.py index e1a564415..e66c68a34 100644 --- a/docs/tutorials/wiki2/src/authentication/tutorial/views/auth.py +++ b/docs/tutorials/wiki2/src/authentication/tutorial/views/auth.py @@ -53,7 +53,7 @@ def logout(request): @forbidden_view_config(renderer='tutorial:templates/403.jinja2') def forbidden_view(exc, request): - if request.user is None: + if request.identity is None: next_url = request.route_url('login', _query={'next': request.url}) return HTTPSeeOther(location=next_url) diff --git a/docs/tutorials/wiki2/src/authentication/tutorial/views/default.py b/docs/tutorials/wiki2/src/authentication/tutorial/views/default.py index 378ce0ae9..4fb715737 100644 --- a/docs/tutorials/wiki2/src/authentication/tutorial/views/default.py +++ b/docs/tutorials/wiki2/src/authentication/tutorial/views/default.py @@ -45,7 +45,7 @@ def view_page(request): def edit_page(request): pagename = request.matchdict['pagename'] page = request.dbsession.query(models.Page).filter_by(name=pagename).one() - user = request.user + user = request.identity if user is None or (user.role != 'editor' and page.creator != user): raise HTTPForbidden if request.method == 'POST': @@ -60,7 +60,7 @@ def edit_page(request): @view_config(route_name='add_page', renderer='tutorial:templates/edit.jinja2') def add_page(request): - user = request.user + user = request.identity if user is None or user.role not in ('editor', 'basic'): raise HTTPForbidden pagename = request.matchdict['pagename'] @@ -70,7 +70,7 @@ def add_page(request): if request.method == 'POST': body = request.params['body'] page = models.Page(name=pagename, data=body) - page.creator = request.user + page.creator = request.identity request.dbsession.add(page) next_url = request.route_url('view_page', pagename=pagename) return HTTPSeeOther(location=next_url) -- cgit v1.2.3 From b6d38775646efb551f790da948fefb9b25422be8 Mon Sep 17 00:00:00 2001 From: Theron Luhn Date: Sat, 7 Nov 2020 11:26:02 -0800 Subject: Remove `request.user` for wiki2 authorization tutorial. --- docs/tutorials/wiki2/authorization.rst | 4 ++-- docs/tutorials/wiki2/src/authorization/tutorial/security.py | 2 -- .../wiki2/src/authorization/tutorial/templates/layout.jinja2 | 4 ++-- docs/tutorials/wiki2/src/authorization/tutorial/views/auth.py | 2 +- docs/tutorials/wiki2/src/authorization/tutorial/views/default.py | 2 +- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/docs/tutorials/wiki2/authorization.rst b/docs/tutorials/wiki2/authorization.rst index 38b9b7373..be3a09664 100644 --- a/docs/tutorials/wiki2/authorization.rst +++ b/docs/tutorials/wiki2/authorization.rst @@ -5,7 +5,7 @@ Adding authorization ==================== In the last chapter we built :term:`authentication` into our wiki. We also -went one step further and used the ``request.user`` object to perform some +went one step further and used the ``request.identity`` object to perform some explicit :term:`authorization` checks. This is fine for a lot of applications, but :app:`Pyramid` provides some facilities for cleaning this up and decoupling the constraints from the view function itself. @@ -24,7 +24,7 @@ We will implement access control with the following steps: Add ACL support --------------- -A :term:`principal` is a level of abstraction on top of the raw :term:`userid` +A :term:`principal` is a level of abstraction on top of the raw :term:`identity` that describes the user in terms of its capabilities, roles, or other identifiers that are easier to generalize. The permissions are then written against the principals without focusing on the exact user involved. diff --git a/docs/tutorials/wiki2/src/authorization/tutorial/security.py b/docs/tutorials/wiki2/src/authorization/tutorial/security.py index 4f79195ef..18f0bd4c7 100644 --- a/docs/tutorials/wiki2/src/authorization/tutorial/security.py +++ b/docs/tutorials/wiki2/src/authorization/tutorial/security.py @@ -59,5 +59,3 @@ def includeme(config): config.set_default_csrf_options(require_csrf=True) config.set_security_policy(MySecurityPolicy(settings['auth.secret'])) - config.add_request_method( - lambda request: request.identity, 'user', property=True) diff --git a/docs/tutorials/wiki2/src/authorization/tutorial/templates/layout.jinja2 b/docs/tutorials/wiki2/src/authorization/tutorial/templates/layout.jinja2 index 64a1db0c5..5d6a23410 100644 --- a/docs/tutorials/wiki2/src/authorization/tutorial/templates/layout.jinja2 +++ b/docs/tutorials/wiki2/src/authorization/tutorial/templates/layout.jinja2 @@ -33,13 +33,13 @@
- {% if request.user is none %} + {% if request.identity is none %}

Login

{% else %}
- {{request.user.name}} + {{request.identity.name}}
diff --git a/docs/tutorials/wiki2/src/authorization/tutorial/views/auth.py b/docs/tutorials/wiki2/src/authorization/tutorial/views/auth.py index e1a564415..e66c68a34 100644 --- a/docs/tutorials/wiki2/src/authorization/tutorial/views/auth.py +++ b/docs/tutorials/wiki2/src/authorization/tutorial/views/auth.py @@ -53,7 +53,7 @@ def logout(request): @forbidden_view_config(renderer='tutorial:templates/403.jinja2') def forbidden_view(exc, request): - if request.user is None: + if request.identity is None: next_url = request.route_url('login', _query={'next': request.url}) return HTTPSeeOther(location=next_url) diff --git a/docs/tutorials/wiki2/src/authorization/tutorial/views/default.py b/docs/tutorials/wiki2/src/authorization/tutorial/views/default.py index 214788357..4a2a66c84 100644 --- a/docs/tutorials/wiki2/src/authorization/tutorial/views/default.py +++ b/docs/tutorials/wiki2/src/authorization/tutorial/views/default.py @@ -56,7 +56,7 @@ def add_page(request): if request.method == 'POST': body = request.params['body'] page = models.Page(name=pagename, data=body) - page.creator = request.user + page.creator = request.identity request.dbsession.add(page) next_url = request.route_url('view_page', pagename=pagename) return HTTPSeeOther(location=next_url) -- cgit v1.2.3 From f10f81ccdfbf44cfa92ef5fcdcb84e6fed4053fb Mon Sep 17 00:00:00 2001 From: Theron Luhn Date: Sat, 7 Nov 2020 11:28:05 -0800 Subject: Remove request.user from wiki2 testing tutorial. --- docs/tutorials/wiki2/src/tests/tests/test_views.py | 6 +++--- docs/tutorials/wiki2/src/tests/tutorial/security.py | 2 -- docs/tutorials/wiki2/src/tests/tutorial/templates/layout.jinja2 | 4 ++-- docs/tutorials/wiki2/src/tests/tutorial/views/auth.py | 2 +- docs/tutorials/wiki2/src/tests/tutorial/views/default.py | 2 +- docs/tutorials/wiki2/tests.rst | 2 +- 6 files changed, 8 insertions(+), 10 deletions(-) diff --git a/docs/tutorials/wiki2/src/tests/tests/test_views.py b/docs/tutorials/wiki2/src/tests/tests/test_views.py index 007184af8..124782d2c 100644 --- a/docs/tutorials/wiki2/src/tests/tests/test_views.py +++ b/docs/tutorials/wiki2/src/tests/tests/test_views.py @@ -57,7 +57,7 @@ class Test_add_page: return NewPage(pagename) def test_get(self, dummy_request, dbsession): - dummy_request.user = makeUser('foo', 'editor') + dummy_request.identity = makeUser('foo', 'editor') dummy_request.context = self._makeContext('AnotherPage') info = self._callFUT(dummy_request) assert info['pagedata'] == '' @@ -67,7 +67,7 @@ class Test_add_page: dummy_request.method = 'POST' dummy_request.POST['body'] = 'Hello yo!' dummy_request.context = self._makeContext('AnotherPage') - dummy_request.user = makeUser('foo', 'editor') + dummy_request.identity = makeUser('foo', 'editor') self._callFUT(dummy_request) page = ( dbsession.query(models.Page) @@ -102,7 +102,7 @@ class Test_edit_page: dummy_request.method = 'POST' dummy_request.POST['body'] = 'Hello yo!' - dummy_request.user = user + dummy_request.identity = user dummy_request.context = self._makeContext(page) response = self._callFUT(dummy_request) assert response.location == 'http://example.com/abc' diff --git a/docs/tutorials/wiki2/src/tests/tutorial/security.py b/docs/tutorials/wiki2/src/tests/tutorial/security.py index 4f79195ef..18f0bd4c7 100644 --- a/docs/tutorials/wiki2/src/tests/tutorial/security.py +++ b/docs/tutorials/wiki2/src/tests/tutorial/security.py @@ -59,5 +59,3 @@ def includeme(config): config.set_default_csrf_options(require_csrf=True) config.set_security_policy(MySecurityPolicy(settings['auth.secret'])) - config.add_request_method( - lambda request: request.identity, 'user', property=True) diff --git a/docs/tutorials/wiki2/src/tests/tutorial/templates/layout.jinja2 b/docs/tutorials/wiki2/src/tests/tutorial/templates/layout.jinja2 index 64a1db0c5..5d6a23410 100644 --- a/docs/tutorials/wiki2/src/tests/tutorial/templates/layout.jinja2 +++ b/docs/tutorials/wiki2/src/tests/tutorial/templates/layout.jinja2 @@ -33,13 +33,13 @@
- {% if request.user is none %} + {% if request.identity is none %}

Login

{% else %}
- {{request.user.name}} + {{request.identity.name}}
diff --git a/docs/tutorials/wiki2/src/tests/tutorial/views/auth.py b/docs/tutorials/wiki2/src/tests/tutorial/views/auth.py index e1a564415..e66c68a34 100644 --- a/docs/tutorials/wiki2/src/tests/tutorial/views/auth.py +++ b/docs/tutorials/wiki2/src/tests/tutorial/views/auth.py @@ -53,7 +53,7 @@ def logout(request): @forbidden_view_config(renderer='tutorial:templates/403.jinja2') def forbidden_view(exc, request): - if request.user is None: + if request.identity is None: next_url = request.route_url('login', _query={'next': request.url}) return HTTPSeeOther(location=next_url) diff --git a/docs/tutorials/wiki2/src/tests/tutorial/views/default.py b/docs/tutorials/wiki2/src/tests/tutorial/views/default.py index 214788357..4a2a66c84 100644 --- a/docs/tutorials/wiki2/src/tests/tutorial/views/default.py +++ b/docs/tutorials/wiki2/src/tests/tutorial/views/default.py @@ -56,7 +56,7 @@ def add_page(request): if request.method == 'POST': body = request.params['body'] page = models.Page(name=pagename, data=body) - page.creator = request.user + page.creator = request.identity request.dbsession.add(page) next_url = request.route_url('view_page', pagename=pagename) return HTTPSeeOther(location=next_url) diff --git a/docs/tutorials/wiki2/tests.rst b/docs/tutorials/wiki2/tests.rst index 1bf38d988..cee7a809d 100644 --- a/docs/tutorials/wiki2/tests.rst +++ b/docs/tutorials/wiki2/tests.rst @@ -110,7 +110,7 @@ Integration tests We can directly execute the view code, bypassing :app:`Pyramid` and testing just the code that we've written. These tests use dummy requests that we'll prepare appropriately to set the conditions each view expects. -For example, setting ``request.user``, or adding some dummy data to the session. +For example, setting ``request.identity``, or adding some dummy data to the session. Update ``tests/test_views.py`` such that it appears as follows: -- cgit v1.2.3 From ab15b5c8f48ccf0a5f106df7af46e2b9742d0b20 Mon Sep 17 00:00:00 2001 From: Theron Luhn Date: Sun, 8 Nov 2020 12:38:45 -0800 Subject: First attempt at fixing wiki2 tests. --- docs/tutorials/wiki2/src/tests/tests/conftest.py | 13 ++++++++--- docs/tutorials/wiki2/src/tests/tests/test_views.py | 26 ++++++++++++++-------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/docs/tutorials/wiki2/src/tests/tests/conftest.py b/docs/tutorials/wiki2/src/tests/tests/conftest.py index 1c8fb16d0..0fe689697 100644 --- a/docs/tutorials/wiki2/src/tests/tests/conftest.py +++ b/docs/tutorials/wiki2/src/tests/tests/conftest.py @@ -4,7 +4,7 @@ import alembic.command import os from pyramid.paster import get_appsettings from pyramid.scripting import prepare -from pyramid.testing import DummyRequest +from pyramid.testing import DummyRequest, testConfig import pytest import transaction from webob.cookies import Cookie @@ -143,8 +143,9 @@ def app_request(app, tm, dbsession): yield request env['closer']() + @pytest.fixture -def dummy_request(app, tm, dbsession): +def dummy_request(tm, dbsession): """ A lightweight dummy request. @@ -158,9 +159,15 @@ def dummy_request(app, tm, dbsession): """ request = DummyRequest() - request.registry = app.registry request.host = 'example.com' request.dbsession = dbsession request.tm = tm return request + + +@pytest.yield_fixture +def dummy_config(dummy_request): + with testConfig(request=dummy_request) as config: + config.include('tutorial.routes') # Current workaround, feels wrong. + yield config diff --git a/docs/tutorials/wiki2/src/tests/tests/test_views.py b/docs/tutorials/wiki2/src/tests/tests/test_views.py index 124782d2c..4e64dae1f 100644 --- a/docs/tutorials/wiki2/src/tests/tests/test_views.py +++ b/docs/tutorials/wiki2/src/tests/tests/test_views.py @@ -1,9 +1,17 @@ +from pyramid.testing import DummySecurityPolicy + from tutorial import models def makeUser(name, role): return models.User(name=name, role=role) + +def setUser(config, user): + config.set_security_policy( + DummySecurityPolicy(identity=user) + ) + def makePage(name, data, creator): return models.Page(name=name, data=data, creator=creator) @@ -12,7 +20,7 @@ class Test_view_wiki: from tutorial.views.default import view_wiki return view_wiki(request) - def test_it(self, dummy_request): + def test_it(self, dummy_config, dummy_request): response = self._callFUT(dummy_request) assert response.location == 'http://example.com/FrontPage' @@ -25,7 +33,7 @@ class Test_view_page: from tutorial.routes import PageResource return PageResource(page) - def test_it(self, dummy_request, dbsession): + def test_it(self, dummy_config, dummy_request, dbsession): # add a page to the db user = makeUser('foo', 'editor') page = makePage('IDoExist', 'Hello CruelWorld IDoExist', user) @@ -56,18 +64,18 @@ class Test_add_page: from tutorial.routes import NewPage return NewPage(pagename) - def test_get(self, dummy_request, dbsession): - dummy_request.identity = makeUser('foo', 'editor') + def test_get(self, dummy_config, dummy_request, dbsession): + setUser(dummy_config, makeUser('foo', 'editor')) dummy_request.context = self._makeContext('AnotherPage') info = self._callFUT(dummy_request) assert info['pagedata'] == '' assert info['save_url'] == 'http://example.com/add_page/AnotherPage' - def test_submit_works(self, dummy_request, dbsession): + def test_submit_works(self, dummy_config, dummy_request, dbsession): dummy_request.method = 'POST' dummy_request.POST['body'] = 'Hello yo!' dummy_request.context = self._makeContext('AnotherPage') - dummy_request.identity = makeUser('foo', 'editor') + setUser(dummy_config, makeUser('foo', 'editor')) self._callFUT(dummy_request) page = ( dbsession.query(models.Page) @@ -85,7 +93,7 @@ class Test_edit_page: from tutorial.routes import PageResource return PageResource(page) - def test_get(self, dummy_request, dbsession): + def test_get(self, dummy_config, dummy_request, dbsession): user = makeUser('foo', 'editor') page = makePage('abc', 'hello', user) dbsession.add_all([page, user]) @@ -95,14 +103,14 @@ class Test_edit_page: assert info['pagename'] == 'abc' assert info['save_url'] == 'http://example.com/abc/edit_page' - def test_submit_works(self, dummy_request, dbsession): + def test_submit_works(self, dummy_config, dummy_request, dbsession): user = makeUser('foo', 'editor') page = makePage('abc', 'hello', user) dbsession.add_all([page, user]) dummy_request.method = 'POST' dummy_request.POST['body'] = 'Hello yo!' - dummy_request.identity = user + setUser(dummy_config, user) dummy_request.context = self._makeContext(page) response = self._callFUT(dummy_request) assert response.location == 'http://example.com/abc' -- cgit v1.2.3 From ae2e73dd715403ece419b096737ff8e01dc1a32c Mon Sep 17 00:00:00 2001 From: Theron Luhn Date: Mon, 9 Nov 2020 15:44:34 -0800 Subject: Manually set routes for the tests. --- docs/tutorials/wiki2/src/tests/tests/conftest.py | 1 - docs/tutorials/wiki2/src/tests/tests/test_views.py | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/tutorials/wiki2/src/tests/tests/conftest.py b/docs/tutorials/wiki2/src/tests/tests/conftest.py index 0fe689697..8882ea265 100644 --- a/docs/tutorials/wiki2/src/tests/tests/conftest.py +++ b/docs/tutorials/wiki2/src/tests/tests/conftest.py @@ -169,5 +169,4 @@ def dummy_request(tm, dbsession): @pytest.yield_fixture def dummy_config(dummy_request): with testConfig(request=dummy_request) as config: - config.include('tutorial.routes') # Current workaround, feels wrong. yield config diff --git a/docs/tutorials/wiki2/src/tests/tests/test_views.py b/docs/tutorials/wiki2/src/tests/tests/test_views.py index 4e64dae1f..e93b04b3c 100644 --- a/docs/tutorials/wiki2/src/tests/tests/test_views.py +++ b/docs/tutorials/wiki2/src/tests/tests/test_views.py @@ -20,7 +20,11 @@ class Test_view_wiki: from tutorial.views.default import view_wiki return view_wiki(request) + def _addRoutes(self, config): + config.add_route('view_page', '/{pagename}') + def test_it(self, dummy_config, dummy_request): + self._addRoutes(dummy_config) response = self._callFUT(dummy_request) assert response.location == 'http://example.com/FrontPage' @@ -33,6 +37,11 @@ class Test_view_page: from tutorial.routes import PageResource return PageResource(page) + def _addRoutes(self, config): + config.add_route('edit_page', '/{pagename}/edit_page') + config.add_route('add_page', '/add_page/{pagename}') + config.add_route('view_page', '/{pagename}') + def test_it(self, dummy_config, dummy_request, dbsession): # add a page to the db user = makeUser('foo', 'editor') @@ -40,6 +49,7 @@ class Test_view_page: dbsession.add_all([page, user]) # create a request asking for the page we've created + self._addRoutes(dummy_config) dummy_request.context = self._makeContext(page) # call the view we're testing and check its behavior @@ -64,8 +74,13 @@ class Test_add_page: from tutorial.routes import NewPage return NewPage(pagename) + def _addRoutes(self, config): + config.add_route('add_page', '/add_page/{pagename}') + config.add_route('view_page', '/{pagename}') + def test_get(self, dummy_config, dummy_request, dbsession): setUser(dummy_config, makeUser('foo', 'editor')) + self._addRoutes(dummy_config) dummy_request.context = self._makeContext('AnotherPage') info = self._callFUT(dummy_request) assert info['pagedata'] == '' @@ -76,6 +91,7 @@ class Test_add_page: dummy_request.POST['body'] = 'Hello yo!' dummy_request.context = self._makeContext('AnotherPage') setUser(dummy_config, makeUser('foo', 'editor')) + self._addRoutes(dummy_config) self._callFUT(dummy_request) page = ( dbsession.query(models.Page) @@ -93,11 +109,16 @@ class Test_edit_page: from tutorial.routes import PageResource return PageResource(page) + def _addRoutes(self, config): + config.add_route('edit_page', '/{pagename}/edit_page') + config.add_route('view_page', '/{pagename}') + def test_get(self, dummy_config, dummy_request, dbsession): user = makeUser('foo', 'editor') page = makePage('abc', 'hello', user) dbsession.add_all([page, user]) + self._addRoutes(dummy_config) dummy_request.context = self._makeContext(page) info = self._callFUT(dummy_request) assert info['pagename'] == 'abc' @@ -108,6 +129,7 @@ class Test_edit_page: page = makePage('abc', 'hello', user) dbsession.add_all([page, user]) + self._addRoutes(dummy_config) dummy_request.method = 'POST' dummy_request.POST['body'] = 'Hello yo!' setUser(dummy_config, user) -- cgit v1.2.3 From 7933c51abd4b716aea9199acdc8cc7ec9296d07e Mon Sep 17 00:00:00 2001 From: Theron Luhn Date: Wed, 11 Nov 2020 21:44:05 -0800 Subject: Add to test documentation. --- docs/tutorials/wiki2/src/tests/tests/conftest.py | 2 -- docs/tutorials/wiki2/tests.rst | 7 +++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/tutorials/wiki2/src/tests/tests/conftest.py b/docs/tutorials/wiki2/src/tests/tests/conftest.py index 8882ea265..45d33bb92 100644 --- a/docs/tutorials/wiki2/src/tests/tests/conftest.py +++ b/docs/tutorials/wiki2/src/tests/tests/conftest.py @@ -143,7 +143,6 @@ def app_request(app, tm, dbsession): yield request env['closer']() - @pytest.fixture def dummy_request(tm, dbsession): """ @@ -165,7 +164,6 @@ def dummy_request(tm, dbsession): return request - @pytest.yield_fixture def dummy_config(dummy_request): with testConfig(request=dummy_request) as config: diff --git a/docs/tutorials/wiki2/tests.rst b/docs/tutorials/wiki2/tests.rst index cee7a809d..dce14cf9b 100644 --- a/docs/tutorials/wiki2/tests.rst +++ b/docs/tutorials/wiki2/tests.rst @@ -69,6 +69,9 @@ Per-test fixtures - ``dummy_request`` - a :class:`pyramid.testing.DummyRequest` object that is very lightweight. This is a great object to pass to view functions that have minimal side-effects as it'll be fast and simple. +- ``dummy_config`` — a :class:`pyramid.config.Configurator` object used as configuration by ``dummy_request``. + Useful for mocking configuration like routes and security policies. + Modifying the fixtures ---------------------- @@ -109,8 +112,8 @@ Integration tests ================= We can directly execute the view code, bypassing :app:`Pyramid` and testing just the code that we've written. -These tests use dummy requests that we'll prepare appropriately to set the conditions each view expects. -For example, setting ``request.identity``, or adding some dummy data to the session. +These tests use dummy requests that we'll prepare appropriately to set the conditions each view expects, such as adding dummy data to the session. +We'll be using ``dummy_config`` to configure the necessary routes, as well as setting the security policy as :class:`pyramid.testing.DummySecurityPolicy` to mock ``dummy_request.identity``. Update ``tests/test_views.py`` such that it appears as follows: -- cgit v1.2.3 From 042a2b9967f23757393ee099f5c2016d6fb68107 Mon Sep 17 00:00:00 2001 From: Theron Luhn Date: Wed, 11 Nov 2020 21:48:41 -0800 Subject: Backport conftest changes to prior steps. --- docs/tutorials/wiki2/src/authentication/tests/conftest.py | 10 +++++++--- docs/tutorials/wiki2/src/authorization/tests/conftest.py | 10 +++++++--- docs/tutorials/wiki2/src/basiclayout/tests/conftest.py | 10 +++++++--- docs/tutorials/wiki2/src/installation/tests/conftest.py | 10 +++++++--- docs/tutorials/wiki2/src/models/tests/conftest.py | 10 +++++++--- docs/tutorials/wiki2/src/views/tests/conftest.py | 10 +++++++--- 6 files changed, 42 insertions(+), 18 deletions(-) diff --git a/docs/tutorials/wiki2/src/authentication/tests/conftest.py b/docs/tutorials/wiki2/src/authentication/tests/conftest.py index 2db65f887..347180600 100644 --- a/docs/tutorials/wiki2/src/authentication/tests/conftest.py +++ b/docs/tutorials/wiki2/src/authentication/tests/conftest.py @@ -4,7 +4,7 @@ import alembic.command import os from pyramid.paster import get_appsettings from pyramid.scripting import prepare -from pyramid.testing import DummyRequest +from pyramid.testing import DummyRequest, testConfig import pytest import transaction from webob.cookies import Cookie @@ -103,7 +103,7 @@ def app_request(app, tm, dbsession): env['closer']() @pytest.fixture -def dummy_request(app, tm, dbsession): +def dummy_request(tm, dbsession): """ A lightweight dummy request. @@ -117,9 +117,13 @@ def dummy_request(app, tm, dbsession): """ request = DummyRequest() - request.registry = app.registry request.host = 'example.com' request.dbsession = dbsession request.tm = tm return request + +@pytest.yield_fixture +def dummy_config(dummy_request): + with testConfig(request=dummy_request) as config: + yield config diff --git a/docs/tutorials/wiki2/src/authorization/tests/conftest.py b/docs/tutorials/wiki2/src/authorization/tests/conftest.py index 2db65f887..347180600 100644 --- a/docs/tutorials/wiki2/src/authorization/tests/conftest.py +++ b/docs/tutorials/wiki2/src/authorization/tests/conftest.py @@ -4,7 +4,7 @@ import alembic.command import os from pyramid.paster import get_appsettings from pyramid.scripting import prepare -from pyramid.testing import DummyRequest +from pyramid.testing import DummyRequest, testConfig import pytest import transaction from webob.cookies import Cookie @@ -103,7 +103,7 @@ def app_request(app, tm, dbsession): env['closer']() @pytest.fixture -def dummy_request(app, tm, dbsession): +def dummy_request(tm, dbsession): """ A lightweight dummy request. @@ -117,9 +117,13 @@ def dummy_request(app, tm, dbsession): """ request = DummyRequest() - request.registry = app.registry request.host = 'example.com' request.dbsession = dbsession request.tm = tm return request + +@pytest.yield_fixture +def dummy_config(dummy_request): + with testConfig(request=dummy_request) as config: + yield config diff --git a/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py b/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py index 2db65f887..347180600 100644 --- a/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py +++ b/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py @@ -4,7 +4,7 @@ import alembic.command import os from pyramid.paster import get_appsettings from pyramid.scripting import prepare -from pyramid.testing import DummyRequest +from pyramid.testing import DummyRequest, testConfig import pytest import transaction from webob.cookies import Cookie @@ -103,7 +103,7 @@ def app_request(app, tm, dbsession): env['closer']() @pytest.fixture -def dummy_request(app, tm, dbsession): +def dummy_request(tm, dbsession): """ A lightweight dummy request. @@ -117,9 +117,13 @@ def dummy_request(app, tm, dbsession): """ request = DummyRequest() - request.registry = app.registry request.host = 'example.com' request.dbsession = dbsession request.tm = tm return request + +@pytest.yield_fixture +def dummy_config(dummy_request): + with testConfig(request=dummy_request) as config: + yield config diff --git a/docs/tutorials/wiki2/src/installation/tests/conftest.py b/docs/tutorials/wiki2/src/installation/tests/conftest.py index 2db65f887..347180600 100644 --- a/docs/tutorials/wiki2/src/installation/tests/conftest.py +++ b/docs/tutorials/wiki2/src/installation/tests/conftest.py @@ -4,7 +4,7 @@ import alembic.command import os from pyramid.paster import get_appsettings from pyramid.scripting import prepare -from pyramid.testing import DummyRequest +from pyramid.testing import DummyRequest, testConfig import pytest import transaction from webob.cookies import Cookie @@ -103,7 +103,7 @@ def app_request(app, tm, dbsession): env['closer']() @pytest.fixture -def dummy_request(app, tm, dbsession): +def dummy_request(tm, dbsession): """ A lightweight dummy request. @@ -117,9 +117,13 @@ def dummy_request(app, tm, dbsession): """ request = DummyRequest() - request.registry = app.registry request.host = 'example.com' request.dbsession = dbsession request.tm = tm return request + +@pytest.yield_fixture +def dummy_config(dummy_request): + with testConfig(request=dummy_request) as config: + yield config diff --git a/docs/tutorials/wiki2/src/models/tests/conftest.py b/docs/tutorials/wiki2/src/models/tests/conftest.py index 2db65f887..347180600 100644 --- a/docs/tutorials/wiki2/src/models/tests/conftest.py +++ b/docs/tutorials/wiki2/src/models/tests/conftest.py @@ -4,7 +4,7 @@ import alembic.command import os from pyramid.paster import get_appsettings from pyramid.scripting import prepare -from pyramid.testing import DummyRequest +from pyramid.testing import DummyRequest, testConfig import pytest import transaction from webob.cookies import Cookie @@ -103,7 +103,7 @@ def app_request(app, tm, dbsession): env['closer']() @pytest.fixture -def dummy_request(app, tm, dbsession): +def dummy_request(tm, dbsession): """ A lightweight dummy request. @@ -117,9 +117,13 @@ def dummy_request(app, tm, dbsession): """ request = DummyRequest() - request.registry = app.registry request.host = 'example.com' request.dbsession = dbsession request.tm = tm return request + +@pytest.yield_fixture +def dummy_config(dummy_request): + with testConfig(request=dummy_request) as config: + yield config diff --git a/docs/tutorials/wiki2/src/views/tests/conftest.py b/docs/tutorials/wiki2/src/views/tests/conftest.py index 2db65f887..347180600 100644 --- a/docs/tutorials/wiki2/src/views/tests/conftest.py +++ b/docs/tutorials/wiki2/src/views/tests/conftest.py @@ -4,7 +4,7 @@ import alembic.command import os from pyramid.paster import get_appsettings from pyramid.scripting import prepare -from pyramid.testing import DummyRequest +from pyramid.testing import DummyRequest, testConfig import pytest import transaction from webob.cookies import Cookie @@ -103,7 +103,7 @@ def app_request(app, tm, dbsession): env['closer']() @pytest.fixture -def dummy_request(app, tm, dbsession): +def dummy_request(tm, dbsession): """ A lightweight dummy request. @@ -117,9 +117,13 @@ def dummy_request(app, tm, dbsession): """ request = DummyRequest() - request.registry = app.registry request.host = 'example.com' request.dbsession = dbsession request.tm = tm return request + +@pytest.yield_fixture +def dummy_config(dummy_request): + with testConfig(request=dummy_request) as config: + yield config -- cgit v1.2.3 From 86cb46d4d977d3a948308326bee628421cab93ea Mon Sep 17 00:00:00 2001 From: Theron Luhn Date: Wed, 11 Nov 2020 21:53:35 -0800 Subject: Remove `app_request` from wiki2 tests. --- .../wiki2/src/authentication/tests/conftest.py | 23 ---------------------- .../wiki2/src/authorization/tests/conftest.py | 23 ---------------------- .../wiki2/src/basiclayout/tests/conftest.py | 23 ---------------------- .../wiki2/src/installation/tests/conftest.py | 23 ---------------------- docs/tutorials/wiki2/src/models/tests/conftest.py | 23 ---------------------- docs/tutorials/wiki2/src/tests/tests/conftest.py | 23 ---------------------- docs/tutorials/wiki2/src/views/tests/conftest.py | 23 ---------------------- docs/tutorials/wiki2/tests.rst | 3 --- 8 files changed, 164 deletions(-) diff --git a/docs/tutorials/wiki2/src/authentication/tests/conftest.py b/docs/tutorials/wiki2/src/authentication/tests/conftest.py index 347180600..57e86ac1c 100644 --- a/docs/tutorials/wiki2/src/authentication/tests/conftest.py +++ b/docs/tutorials/wiki2/src/authentication/tests/conftest.py @@ -3,7 +3,6 @@ import alembic.config import alembic.command import os from pyramid.paster import get_appsettings -from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction @@ -80,28 +79,6 @@ def testapp(app, tm, dbsession): return testapp -@pytest.fixture -def app_request(app, tm, dbsession): - """ - A real request. - - This request is almost identical to a real request but it has some - drawbacks in tests as it's harder to mock data and is heavier. - - """ - env = prepare(registry=app.registry) - request = env['request'] - request.host = 'example.com' - - # without this, request.dbsession will be joined to the same transaction - # manager but it will be using a different sqlalchemy.orm.Session using - # a separate database transaction - request.dbsession = dbsession - request.tm = tm - - yield request - env['closer']() - @pytest.fixture def dummy_request(tm, dbsession): """ diff --git a/docs/tutorials/wiki2/src/authorization/tests/conftest.py b/docs/tutorials/wiki2/src/authorization/tests/conftest.py index 347180600..57e86ac1c 100644 --- a/docs/tutorials/wiki2/src/authorization/tests/conftest.py +++ b/docs/tutorials/wiki2/src/authorization/tests/conftest.py @@ -3,7 +3,6 @@ import alembic.config import alembic.command import os from pyramid.paster import get_appsettings -from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction @@ -80,28 +79,6 @@ def testapp(app, tm, dbsession): return testapp -@pytest.fixture -def app_request(app, tm, dbsession): - """ - A real request. - - This request is almost identical to a real request but it has some - drawbacks in tests as it's harder to mock data and is heavier. - - """ - env = prepare(registry=app.registry) - request = env['request'] - request.host = 'example.com' - - # without this, request.dbsession will be joined to the same transaction - # manager but it will be using a different sqlalchemy.orm.Session using - # a separate database transaction - request.dbsession = dbsession - request.tm = tm - - yield request - env['closer']() - @pytest.fixture def dummy_request(tm, dbsession): """ diff --git a/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py b/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py index 347180600..57e86ac1c 100644 --- a/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py +++ b/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py @@ -3,7 +3,6 @@ import alembic.config import alembic.command import os from pyramid.paster import get_appsettings -from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction @@ -80,28 +79,6 @@ def testapp(app, tm, dbsession): return testapp -@pytest.fixture -def app_request(app, tm, dbsession): - """ - A real request. - - This request is almost identical to a real request but it has some - drawbacks in tests as it's harder to mock data and is heavier. - - """ - env = prepare(registry=app.registry) - request = env['request'] - request.host = 'example.com' - - # without this, request.dbsession will be joined to the same transaction - # manager but it will be using a different sqlalchemy.orm.Session using - # a separate database transaction - request.dbsession = dbsession - request.tm = tm - - yield request - env['closer']() - @pytest.fixture def dummy_request(tm, dbsession): """ diff --git a/docs/tutorials/wiki2/src/installation/tests/conftest.py b/docs/tutorials/wiki2/src/installation/tests/conftest.py index 347180600..57e86ac1c 100644 --- a/docs/tutorials/wiki2/src/installation/tests/conftest.py +++ b/docs/tutorials/wiki2/src/installation/tests/conftest.py @@ -3,7 +3,6 @@ import alembic.config import alembic.command import os from pyramid.paster import get_appsettings -from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction @@ -80,28 +79,6 @@ def testapp(app, tm, dbsession): return testapp -@pytest.fixture -def app_request(app, tm, dbsession): - """ - A real request. - - This request is almost identical to a real request but it has some - drawbacks in tests as it's harder to mock data and is heavier. - - """ - env = prepare(registry=app.registry) - request = env['request'] - request.host = 'example.com' - - # without this, request.dbsession will be joined to the same transaction - # manager but it will be using a different sqlalchemy.orm.Session using - # a separate database transaction - request.dbsession = dbsession - request.tm = tm - - yield request - env['closer']() - @pytest.fixture def dummy_request(tm, dbsession): """ diff --git a/docs/tutorials/wiki2/src/models/tests/conftest.py b/docs/tutorials/wiki2/src/models/tests/conftest.py index 347180600..57e86ac1c 100644 --- a/docs/tutorials/wiki2/src/models/tests/conftest.py +++ b/docs/tutorials/wiki2/src/models/tests/conftest.py @@ -3,7 +3,6 @@ import alembic.config import alembic.command import os from pyramid.paster import get_appsettings -from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction @@ -80,28 +79,6 @@ def testapp(app, tm, dbsession): return testapp -@pytest.fixture -def app_request(app, tm, dbsession): - """ - A real request. - - This request is almost identical to a real request but it has some - drawbacks in tests as it's harder to mock data and is heavier. - - """ - env = prepare(registry=app.registry) - request = env['request'] - request.host = 'example.com' - - # without this, request.dbsession will be joined to the same transaction - # manager but it will be using a different sqlalchemy.orm.Session using - # a separate database transaction - request.dbsession = dbsession - request.tm = tm - - yield request - env['closer']() - @pytest.fixture def dummy_request(tm, dbsession): """ diff --git a/docs/tutorials/wiki2/src/tests/tests/conftest.py b/docs/tutorials/wiki2/src/tests/tests/conftest.py index 45d33bb92..651643e8c 100644 --- a/docs/tutorials/wiki2/src/tests/tests/conftest.py +++ b/docs/tutorials/wiki2/src/tests/tests/conftest.py @@ -3,7 +3,6 @@ import alembic.config import alembic.command import os from pyramid.paster import get_appsettings -from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction @@ -121,28 +120,6 @@ def testapp(app, tm, dbsession): return testapp -@pytest.fixture -def app_request(app, tm, dbsession): - """ - A real request. - - This request is almost identical to a real request but it has some - drawbacks in tests as it's harder to mock data and is heavier. - - """ - env = prepare(registry=app.registry) - request = env['request'] - request.host = 'example.com' - - # without this, request.dbsession will be joined to the same transaction - # manager but it will be using a different sqlalchemy.orm.Session using - # a separate database transaction - request.dbsession = dbsession - request.tm = tm - - yield request - env['closer']() - @pytest.fixture def dummy_request(tm, dbsession): """ diff --git a/docs/tutorials/wiki2/src/views/tests/conftest.py b/docs/tutorials/wiki2/src/views/tests/conftest.py index 347180600..57e86ac1c 100644 --- a/docs/tutorials/wiki2/src/views/tests/conftest.py +++ b/docs/tutorials/wiki2/src/views/tests/conftest.py @@ -3,7 +3,6 @@ import alembic.config import alembic.command import os from pyramid.paster import get_appsettings -from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction @@ -80,28 +79,6 @@ def testapp(app, tm, dbsession): return testapp -@pytest.fixture -def app_request(app, tm, dbsession): - """ - A real request. - - This request is almost identical to a real request but it has some - drawbacks in tests as it's harder to mock data and is heavier. - - """ - env = prepare(registry=app.registry) - request = env['request'] - request.host = 'example.com' - - # without this, request.dbsession will be joined to the same transaction - # manager but it will be using a different sqlalchemy.orm.Session using - # a separate database transaction - request.dbsession = dbsession - request.tm = tm - - yield request - env['closer']() - @pytest.fixture def dummy_request(tm, dbsession): """ diff --git a/docs/tutorials/wiki2/tests.rst b/docs/tutorials/wiki2/tests.rst index dce14cf9b..4ea2e2e15 100644 --- a/docs/tutorials/wiki2/tests.rst +++ b/docs/tutorials/wiki2/tests.rst @@ -63,9 +63,6 @@ Per-test fixtures The ``testapp`` is able to mutate the request environ such that the ``dbsession`` and ``tm`` fixtures are injected and used by any code that's touching ``request.dbsession`` and ``request.tm``. The ``testapp`` maintains a cookiejar, so it can be used to share state across requests, as well as the transaction database connection. -- ``app_request`` - a :class:`pyramid.request.Request` object that can be used for more lightweight tests versus the full ``testapp``. - The ``app_request`` can be passed to view functions and other code that need a fully functional request object. - - ``dummy_request`` - a :class:`pyramid.testing.DummyRequest` object that is very lightweight. This is a great object to pass to view functions that have minimal side-effects as it'll be fast and simple. -- cgit v1.2.3 From ea25ec5cd5715ab3dcf266a968166425e279927a Mon Sep 17 00:00:00 2001 From: Theron Luhn Date: Fri, 11 Dec 2020 09:22:15 -0800 Subject: Revert "Remove `app_request` from wiki2 tests." This reverts commit 86cb46d4d977d3a948308326bee628421cab93ea. --- .../wiki2/src/authentication/tests/conftest.py | 23 ++++++++++++++++++++++ .../wiki2/src/authorization/tests/conftest.py | 23 ++++++++++++++++++++++ .../wiki2/src/basiclayout/tests/conftest.py | 23 ++++++++++++++++++++++ .../wiki2/src/installation/tests/conftest.py | 23 ++++++++++++++++++++++ docs/tutorials/wiki2/src/models/tests/conftest.py | 23 ++++++++++++++++++++++ docs/tutorials/wiki2/src/tests/tests/conftest.py | 23 ++++++++++++++++++++++ docs/tutorials/wiki2/src/views/tests/conftest.py | 23 ++++++++++++++++++++++ docs/tutorials/wiki2/tests.rst | 3 +++ 8 files changed, 164 insertions(+) diff --git a/docs/tutorials/wiki2/src/authentication/tests/conftest.py b/docs/tutorials/wiki2/src/authentication/tests/conftest.py index 57e86ac1c..347180600 100644 --- a/docs/tutorials/wiki2/src/authentication/tests/conftest.py +++ b/docs/tutorials/wiki2/src/authentication/tests/conftest.py @@ -3,6 +3,7 @@ import alembic.config import alembic.command import os from pyramid.paster import get_appsettings +from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction @@ -79,6 +80,28 @@ def testapp(app, tm, dbsession): return testapp +@pytest.fixture +def app_request(app, tm, dbsession): + """ + A real request. + + This request is almost identical to a real request but it has some + drawbacks in tests as it's harder to mock data and is heavier. + + """ + env = prepare(registry=app.registry) + request = env['request'] + request.host = 'example.com' + + # without this, request.dbsession will be joined to the same transaction + # manager but it will be using a different sqlalchemy.orm.Session using + # a separate database transaction + request.dbsession = dbsession + request.tm = tm + + yield request + env['closer']() + @pytest.fixture def dummy_request(tm, dbsession): """ diff --git a/docs/tutorials/wiki2/src/authorization/tests/conftest.py b/docs/tutorials/wiki2/src/authorization/tests/conftest.py index 57e86ac1c..347180600 100644 --- a/docs/tutorials/wiki2/src/authorization/tests/conftest.py +++ b/docs/tutorials/wiki2/src/authorization/tests/conftest.py @@ -3,6 +3,7 @@ import alembic.config import alembic.command import os from pyramid.paster import get_appsettings +from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction @@ -79,6 +80,28 @@ def testapp(app, tm, dbsession): return testapp +@pytest.fixture +def app_request(app, tm, dbsession): + """ + A real request. + + This request is almost identical to a real request but it has some + drawbacks in tests as it's harder to mock data and is heavier. + + """ + env = prepare(registry=app.registry) + request = env['request'] + request.host = 'example.com' + + # without this, request.dbsession will be joined to the same transaction + # manager but it will be using a different sqlalchemy.orm.Session using + # a separate database transaction + request.dbsession = dbsession + request.tm = tm + + yield request + env['closer']() + @pytest.fixture def dummy_request(tm, dbsession): """ diff --git a/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py b/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py index 57e86ac1c..347180600 100644 --- a/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py +++ b/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py @@ -3,6 +3,7 @@ import alembic.config import alembic.command import os from pyramid.paster import get_appsettings +from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction @@ -79,6 +80,28 @@ def testapp(app, tm, dbsession): return testapp +@pytest.fixture +def app_request(app, tm, dbsession): + """ + A real request. + + This request is almost identical to a real request but it has some + drawbacks in tests as it's harder to mock data and is heavier. + + """ + env = prepare(registry=app.registry) + request = env['request'] + request.host = 'example.com' + + # without this, request.dbsession will be joined to the same transaction + # manager but it will be using a different sqlalchemy.orm.Session using + # a separate database transaction + request.dbsession = dbsession + request.tm = tm + + yield request + env['closer']() + @pytest.fixture def dummy_request(tm, dbsession): """ diff --git a/docs/tutorials/wiki2/src/installation/tests/conftest.py b/docs/tutorials/wiki2/src/installation/tests/conftest.py index 57e86ac1c..347180600 100644 --- a/docs/tutorials/wiki2/src/installation/tests/conftest.py +++ b/docs/tutorials/wiki2/src/installation/tests/conftest.py @@ -3,6 +3,7 @@ import alembic.config import alembic.command import os from pyramid.paster import get_appsettings +from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction @@ -79,6 +80,28 @@ def testapp(app, tm, dbsession): return testapp +@pytest.fixture +def app_request(app, tm, dbsession): + """ + A real request. + + This request is almost identical to a real request but it has some + drawbacks in tests as it's harder to mock data and is heavier. + + """ + env = prepare(registry=app.registry) + request = env['request'] + request.host = 'example.com' + + # without this, request.dbsession will be joined to the same transaction + # manager but it will be using a different sqlalchemy.orm.Session using + # a separate database transaction + request.dbsession = dbsession + request.tm = tm + + yield request + env['closer']() + @pytest.fixture def dummy_request(tm, dbsession): """ diff --git a/docs/tutorials/wiki2/src/models/tests/conftest.py b/docs/tutorials/wiki2/src/models/tests/conftest.py index 57e86ac1c..347180600 100644 --- a/docs/tutorials/wiki2/src/models/tests/conftest.py +++ b/docs/tutorials/wiki2/src/models/tests/conftest.py @@ -3,6 +3,7 @@ import alembic.config import alembic.command import os from pyramid.paster import get_appsettings +from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction @@ -79,6 +80,28 @@ def testapp(app, tm, dbsession): return testapp +@pytest.fixture +def app_request(app, tm, dbsession): + """ + A real request. + + This request is almost identical to a real request but it has some + drawbacks in tests as it's harder to mock data and is heavier. + + """ + env = prepare(registry=app.registry) + request = env['request'] + request.host = 'example.com' + + # without this, request.dbsession will be joined to the same transaction + # manager but it will be using a different sqlalchemy.orm.Session using + # a separate database transaction + request.dbsession = dbsession + request.tm = tm + + yield request + env['closer']() + @pytest.fixture def dummy_request(tm, dbsession): """ diff --git a/docs/tutorials/wiki2/src/tests/tests/conftest.py b/docs/tutorials/wiki2/src/tests/tests/conftest.py index 651643e8c..45d33bb92 100644 --- a/docs/tutorials/wiki2/src/tests/tests/conftest.py +++ b/docs/tutorials/wiki2/src/tests/tests/conftest.py @@ -3,6 +3,7 @@ import alembic.config import alembic.command import os from pyramid.paster import get_appsettings +from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction @@ -120,6 +121,28 @@ def testapp(app, tm, dbsession): return testapp +@pytest.fixture +def app_request(app, tm, dbsession): + """ + A real request. + + This request is almost identical to a real request but it has some + drawbacks in tests as it's harder to mock data and is heavier. + + """ + env = prepare(registry=app.registry) + request = env['request'] + request.host = 'example.com' + + # without this, request.dbsession will be joined to the same transaction + # manager but it will be using a different sqlalchemy.orm.Session using + # a separate database transaction + request.dbsession = dbsession + request.tm = tm + + yield request + env['closer']() + @pytest.fixture def dummy_request(tm, dbsession): """ diff --git a/docs/tutorials/wiki2/src/views/tests/conftest.py b/docs/tutorials/wiki2/src/views/tests/conftest.py index 57e86ac1c..347180600 100644 --- a/docs/tutorials/wiki2/src/views/tests/conftest.py +++ b/docs/tutorials/wiki2/src/views/tests/conftest.py @@ -3,6 +3,7 @@ import alembic.config import alembic.command import os from pyramid.paster import get_appsettings +from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction @@ -79,6 +80,28 @@ def testapp(app, tm, dbsession): return testapp +@pytest.fixture +def app_request(app, tm, dbsession): + """ + A real request. + + This request is almost identical to a real request but it has some + drawbacks in tests as it's harder to mock data and is heavier. + + """ + env = prepare(registry=app.registry) + request = env['request'] + request.host = 'example.com' + + # without this, request.dbsession will be joined to the same transaction + # manager but it will be using a different sqlalchemy.orm.Session using + # a separate database transaction + request.dbsession = dbsession + request.tm = tm + + yield request + env['closer']() + @pytest.fixture def dummy_request(tm, dbsession): """ diff --git a/docs/tutorials/wiki2/tests.rst b/docs/tutorials/wiki2/tests.rst index 4ea2e2e15..dce14cf9b 100644 --- a/docs/tutorials/wiki2/tests.rst +++ b/docs/tutorials/wiki2/tests.rst @@ -63,6 +63,9 @@ Per-test fixtures The ``testapp`` is able to mutate the request environ such that the ``dbsession`` and ``tm`` fixtures are injected and used by any code that's touching ``request.dbsession`` and ``request.tm``. The ``testapp`` maintains a cookiejar, so it can be used to share state across requests, as well as the transaction database connection. +- ``app_request`` - a :class:`pyramid.request.Request` object that can be used for more lightweight tests versus the full ``testapp``. + The ``app_request`` can be passed to view functions and other code that need a fully functional request object. + - ``dummy_request`` - a :class:`pyramid.testing.DummyRequest` object that is very lightweight. This is a great object to pass to view functions that have minimal side-effects as it'll be fast and simple. -- cgit v1.2.3 From 0416521c1c95cb8739b65906c1c3cced1b9163dc Mon Sep 17 00:00:00 2001 From: Theron Luhn Date: Fri, 11 Dec 2020 09:41:05 -0800 Subject: Install new cookiecutter conftests. --- .../wiki2/src/authentication/tests/conftest.py | 35 ++++++++++++---------- .../wiki2/src/authorization/tests/conftest.py | 35 ++++++++++++---------- .../wiki2/src/basiclayout/tests/conftest.py | 35 ++++++++++++---------- .../wiki2/src/installation/tests/conftest.py | 35 ++++++++++++---------- docs/tutorials/wiki2/src/models/tests/conftest.py | 35 ++++++++++++---------- docs/tutorials/wiki2/src/tests/tests/conftest.py | 34 +++++++++++---------- docs/tutorials/wiki2/src/views/tests/conftest.py | 35 ++++++++++++---------- 7 files changed, 133 insertions(+), 111 deletions(-) diff --git a/docs/tutorials/wiki2/src/authentication/tests/conftest.py b/docs/tutorials/wiki2/src/authentication/tests/conftest.py index 347180600..4ac4c60a8 100644 --- a/docs/tutorials/wiki2/src/authentication/tests/conftest.py +++ b/docs/tutorials/wiki2/src/authentication/tests/conftest.py @@ -7,7 +7,6 @@ from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction -from webob.cookies import Cookie import webtest from tutorial import main @@ -89,28 +88,26 @@ def app_request(app, tm, dbsession): drawbacks in tests as it's harder to mock data and is heavier. """ - env = prepare(registry=app.registry) - request = env['request'] - request.host = 'example.com' + with prepare(registry=app.registry) as env: + request = env['request'] + request.host = 'example.com' - # without this, request.dbsession will be joined to the same transaction - # manager but it will be using a different sqlalchemy.orm.Session using - # a separate database transaction - request.dbsession = dbsession - request.tm = tm + # without this, request.dbsession will be joined to the same transaction + # manager but it will be using a different sqlalchemy.orm.Session using + # a separate database transaction + request.dbsession = dbsession + request.tm = tm - yield request - env['closer']() + yield request @pytest.fixture def dummy_request(tm, dbsession): """ A lightweight dummy request. - This request is ultra-lightweight and should be used only when the - request itself is not a large focus in the call-stack. - - It is way easier to mock and control side-effects using this object. + This request is ultra-lightweight and should be used only when the request + itself is not a large focus in the call-stack. It is much easier to mock + and control side-effects using this object, however: - It does not have request extensions applied. - Threadlocals are not properly pushed. @@ -123,7 +120,13 @@ def dummy_request(tm, dbsession): return request -@pytest.yield_fixture +@pytest.fixture def dummy_config(dummy_request): + """ + A dummy :class:`pyramid.config.Configurator` object. This allows for + mock configuration, including configuration for ``dummy_request``, as well + as pushing the appropriate threadlocals. + + """ with testConfig(request=dummy_request) as config: yield config diff --git a/docs/tutorials/wiki2/src/authorization/tests/conftest.py b/docs/tutorials/wiki2/src/authorization/tests/conftest.py index 347180600..4ac4c60a8 100644 --- a/docs/tutorials/wiki2/src/authorization/tests/conftest.py +++ b/docs/tutorials/wiki2/src/authorization/tests/conftest.py @@ -7,7 +7,6 @@ from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction -from webob.cookies import Cookie import webtest from tutorial import main @@ -89,28 +88,26 @@ def app_request(app, tm, dbsession): drawbacks in tests as it's harder to mock data and is heavier. """ - env = prepare(registry=app.registry) - request = env['request'] - request.host = 'example.com' + with prepare(registry=app.registry) as env: + request = env['request'] + request.host = 'example.com' - # without this, request.dbsession will be joined to the same transaction - # manager but it will be using a different sqlalchemy.orm.Session using - # a separate database transaction - request.dbsession = dbsession - request.tm = tm + # without this, request.dbsession will be joined to the same transaction + # manager but it will be using a different sqlalchemy.orm.Session using + # a separate database transaction + request.dbsession = dbsession + request.tm = tm - yield request - env['closer']() + yield request @pytest.fixture def dummy_request(tm, dbsession): """ A lightweight dummy request. - This request is ultra-lightweight and should be used only when the - request itself is not a large focus in the call-stack. - - It is way easier to mock and control side-effects using this object. + This request is ultra-lightweight and should be used only when the request + itself is not a large focus in the call-stack. It is much easier to mock + and control side-effects using this object, however: - It does not have request extensions applied. - Threadlocals are not properly pushed. @@ -123,7 +120,13 @@ def dummy_request(tm, dbsession): return request -@pytest.yield_fixture +@pytest.fixture def dummy_config(dummy_request): + """ + A dummy :class:`pyramid.config.Configurator` object. This allows for + mock configuration, including configuration for ``dummy_request``, as well + as pushing the appropriate threadlocals. + + """ with testConfig(request=dummy_request) as config: yield config diff --git a/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py b/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py index 347180600..4ac4c60a8 100644 --- a/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py +++ b/docs/tutorials/wiki2/src/basiclayout/tests/conftest.py @@ -7,7 +7,6 @@ from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction -from webob.cookies import Cookie import webtest from tutorial import main @@ -89,28 +88,26 @@ def app_request(app, tm, dbsession): drawbacks in tests as it's harder to mock data and is heavier. """ - env = prepare(registry=app.registry) - request = env['request'] - request.host = 'example.com' + with prepare(registry=app.registry) as env: + request = env['request'] + request.host = 'example.com' - # without this, request.dbsession will be joined to the same transaction - # manager but it will be using a different sqlalchemy.orm.Session using - # a separate database transaction - request.dbsession = dbsession - request.tm = tm + # without this, request.dbsession will be joined to the same transaction + # manager but it will be using a different sqlalchemy.orm.Session using + # a separate database transaction + request.dbsession = dbsession + request.tm = tm - yield request - env['closer']() + yield request @pytest.fixture def dummy_request(tm, dbsession): """ A lightweight dummy request. - This request is ultra-lightweight and should be used only when the - request itself is not a large focus in the call-stack. - - It is way easier to mock and control side-effects using this object. + This request is ultra-lightweight and should be used only when the request + itself is not a large focus in the call-stack. It is much easier to mock + and control side-effects using this object, however: - It does not have request extensions applied. - Threadlocals are not properly pushed. @@ -123,7 +120,13 @@ def dummy_request(tm, dbsession): return request -@pytest.yield_fixture +@pytest.fixture def dummy_config(dummy_request): + """ + A dummy :class:`pyramid.config.Configurator` object. This allows for + mock configuration, including configuration for ``dummy_request``, as well + as pushing the appropriate threadlocals. + + """ with testConfig(request=dummy_request) as config: yield config diff --git a/docs/tutorials/wiki2/src/installation/tests/conftest.py b/docs/tutorials/wiki2/src/installation/tests/conftest.py index 347180600..4ac4c60a8 100644 --- a/docs/tutorials/wiki2/src/installation/tests/conftest.py +++ b/docs/tutorials/wiki2/src/installation/tests/conftest.py @@ -7,7 +7,6 @@ from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction -from webob.cookies import Cookie import webtest from tutorial import main @@ -89,28 +88,26 @@ def app_request(app, tm, dbsession): drawbacks in tests as it's harder to mock data and is heavier. """ - env = prepare(registry=app.registry) - request = env['request'] - request.host = 'example.com' + with prepare(registry=app.registry) as env: + request = env['request'] + request.host = 'example.com' - # without this, request.dbsession will be joined to the same transaction - # manager but it will be using a different sqlalchemy.orm.Session using - # a separate database transaction - request.dbsession = dbsession - request.tm = tm + # without this, request.dbsession will be joined to the same transaction + # manager but it will be using a different sqlalchemy.orm.Session using + # a separate database transaction + request.dbsession = dbsession + request.tm = tm - yield request - env['closer']() + yield request @pytest.fixture def dummy_request(tm, dbsession): """ A lightweight dummy request. - This request is ultra-lightweight and should be used only when the - request itself is not a large focus in the call-stack. - - It is way easier to mock and control side-effects using this object. + This request is ultra-lightweight and should be used only when the request + itself is not a large focus in the call-stack. It is much easier to mock + and control side-effects using this object, however: - It does not have request extensions applied. - Threadlocals are not properly pushed. @@ -123,7 +120,13 @@ def dummy_request(tm, dbsession): return request -@pytest.yield_fixture +@pytest.fixture def dummy_config(dummy_request): + """ + A dummy :class:`pyramid.config.Configurator` object. This allows for + mock configuration, including configuration for ``dummy_request``, as well + as pushing the appropriate threadlocals. + + """ with testConfig(request=dummy_request) as config: yield config diff --git a/docs/tutorials/wiki2/src/models/tests/conftest.py b/docs/tutorials/wiki2/src/models/tests/conftest.py index 347180600..4ac4c60a8 100644 --- a/docs/tutorials/wiki2/src/models/tests/conftest.py +++ b/docs/tutorials/wiki2/src/models/tests/conftest.py @@ -7,7 +7,6 @@ from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction -from webob.cookies import Cookie import webtest from tutorial import main @@ -89,28 +88,26 @@ def app_request(app, tm, dbsession): drawbacks in tests as it's harder to mock data and is heavier. """ - env = prepare(registry=app.registry) - request = env['request'] - request.host = 'example.com' + with prepare(registry=app.registry) as env: + request = env['request'] + request.host = 'example.com' - # without this, request.dbsession will be joined to the same transaction - # manager but it will be using a different sqlalchemy.orm.Session using - # a separate database transaction - request.dbsession = dbsession - request.tm = tm + # without this, request.dbsession will be joined to the same transaction + # manager but it will be using a different sqlalchemy.orm.Session using + # a separate database transaction + request.dbsession = dbsession + request.tm = tm - yield request - env['closer']() + yield request @pytest.fixture def dummy_request(tm, dbsession): """ A lightweight dummy request. - This request is ultra-lightweight and should be used only when the - request itself is not a large focus in the call-stack. - - It is way easier to mock and control side-effects using this object. + This request is ultra-lightweight and should be used only when the request + itself is not a large focus in the call-stack. It is much easier to mock + and control side-effects using this object, however: - It does not have request extensions applied. - Threadlocals are not properly pushed. @@ -123,7 +120,13 @@ def dummy_request(tm, dbsession): return request -@pytest.yield_fixture +@pytest.fixture def dummy_config(dummy_request): + """ + A dummy :class:`pyramid.config.Configurator` object. This allows for + mock configuration, including configuration for ``dummy_request``, as well + as pushing the appropriate threadlocals. + + """ with testConfig(request=dummy_request) as config: yield config diff --git a/docs/tutorials/wiki2/src/tests/tests/conftest.py b/docs/tutorials/wiki2/src/tests/tests/conftest.py index 45d33bb92..5ef28acd1 100644 --- a/docs/tutorials/wiki2/src/tests/tests/conftest.py +++ b/docs/tutorials/wiki2/src/tests/tests/conftest.py @@ -130,28 +130,26 @@ def app_request(app, tm, dbsession): drawbacks in tests as it's harder to mock data and is heavier. """ - env = prepare(registry=app.registry) - request = env['request'] - request.host = 'example.com' + with prepare(registry=app.registry) as env: + request = env['request'] + request.host = 'example.com' - # without this, request.dbsession will be joined to the same transaction - # manager but it will be using a different sqlalchemy.orm.Session using - # a separate database transaction - request.dbsession = dbsession - request.tm = tm + # without this, request.dbsession will be joined to the same transaction + # manager but it will be using a different sqlalchemy.orm.Session using + # a separate database transaction + request.dbsession = dbsession + request.tm = tm - yield request - env['closer']() + yield request @pytest.fixture def dummy_request(tm, dbsession): """ A lightweight dummy request. - This request is ultra-lightweight and should be used only when the - request itself is not a large focus in the call-stack. - - It is way easier to mock and control side-effects using this object. + This request is ultra-lightweight and should be used only when the request + itself is not a large focus in the call-stack. It is much easier to mock + and control side-effects using this object, however: - It does not have request extensions applied. - Threadlocals are not properly pushed. @@ -164,7 +162,13 @@ def dummy_request(tm, dbsession): return request -@pytest.yield_fixture +@pytest.fixture def dummy_config(dummy_request): + """ + A dummy :class:`pyramid.config.Configurator` object. This allows for + mock configuration, including configuration for ``dummy_request``, as well + as pushing the appropriate threadlocals. + + """ with testConfig(request=dummy_request) as config: yield config diff --git a/docs/tutorials/wiki2/src/views/tests/conftest.py b/docs/tutorials/wiki2/src/views/tests/conftest.py index 347180600..4ac4c60a8 100644 --- a/docs/tutorials/wiki2/src/views/tests/conftest.py +++ b/docs/tutorials/wiki2/src/views/tests/conftest.py @@ -7,7 +7,6 @@ from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig import pytest import transaction -from webob.cookies import Cookie import webtest from tutorial import main @@ -89,28 +88,26 @@ def app_request(app, tm, dbsession): drawbacks in tests as it's harder to mock data and is heavier. """ - env = prepare(registry=app.registry) - request = env['request'] - request.host = 'example.com' + with prepare(registry=app.registry) as env: + request = env['request'] + request.host = 'example.com' - # without this, request.dbsession will be joined to the same transaction - # manager but it will be using a different sqlalchemy.orm.Session using - # a separate database transaction - request.dbsession = dbsession - request.tm = tm + # without this, request.dbsession will be joined to the same transaction + # manager but it will be using a different sqlalchemy.orm.Session using + # a separate database transaction + request.dbsession = dbsession + request.tm = tm - yield request - env['closer']() + yield request @pytest.fixture def dummy_request(tm, dbsession): """ A lightweight dummy request. - This request is ultra-lightweight and should be used only when the - request itself is not a large focus in the call-stack. - - It is way easier to mock and control side-effects using this object. + This request is ultra-lightweight and should be used only when the request + itself is not a large focus in the call-stack. It is much easier to mock + and control side-effects using this object, however: - It does not have request extensions applied. - Threadlocals are not properly pushed. @@ -123,7 +120,13 @@ def dummy_request(tm, dbsession): return request -@pytest.yield_fixture +@pytest.fixture def dummy_config(dummy_request): + """ + A dummy :class:`pyramid.config.Configurator` object. This allows for + mock configuration, including configuration for ``dummy_request``, as well + as pushing the appropriate threadlocals. + + """ with testConfig(request=dummy_request) as config: yield config -- cgit v1.2.3 From 2ce552b73fc40bd35565be0e4599bf2189ea6e09 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Sat, 12 Dec 2020 00:21:14 -0600 Subject: change identity checks to use request.is_authenticated --- .../tutorials/wiki2/src/authentication/tutorial/templates/layout.jinja2 | 2 +- docs/tutorials/wiki2/src/authentication/tutorial/views/auth.py | 2 +- docs/tutorials/wiki2/src/authorization/tutorial/templates/layout.jinja2 | 2 +- docs/tutorials/wiki2/src/authorization/tutorial/views/auth.py | 2 +- docs/tutorials/wiki2/src/tests/tutorial/templates/layout.jinja2 | 2 +- docs/tutorials/wiki2/src/tests/tutorial/views/auth.py | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/tutorials/wiki2/src/authentication/tutorial/templates/layout.jinja2 b/docs/tutorials/wiki2/src/authentication/tutorial/templates/layout.jinja2 index 5d6a23410..55f4a85dc 100644 --- a/docs/tutorials/wiki2/src/authentication/tutorial/templates/layout.jinja2 +++ b/docs/tutorials/wiki2/src/authentication/tutorial/templates/layout.jinja2 @@ -33,7 +33,7 @@
- {% if request.identity is none %} + {% if not request.is_authenticated %}

Login

diff --git a/docs/tutorials/wiki2/src/authentication/tutorial/views/auth.py b/docs/tutorials/wiki2/src/authentication/tutorial/views/auth.py index e66c68a34..807ff3464 100644 --- a/docs/tutorials/wiki2/src/authentication/tutorial/views/auth.py +++ b/docs/tutorials/wiki2/src/authentication/tutorial/views/auth.py @@ -53,7 +53,7 @@ def logout(request): @forbidden_view_config(renderer='tutorial:templates/403.jinja2') def forbidden_view(exc, request): - if request.identity is None: + if not request.is_authenticated: next_url = request.route_url('login', _query={'next': request.url}) return HTTPSeeOther(location=next_url) diff --git a/docs/tutorials/wiki2/src/authorization/tutorial/templates/layout.jinja2 b/docs/tutorials/wiki2/src/authorization/tutorial/templates/layout.jinja2 index 5d6a23410..55f4a85dc 100644 --- a/docs/tutorials/wiki2/src/authorization/tutorial/templates/layout.jinja2 +++ b/docs/tutorials/wiki2/src/authorization/tutorial/templates/layout.jinja2 @@ -33,7 +33,7 @@
- {% if request.identity is none %} + {% if not request.is_authenticated %}

Login

diff --git a/docs/tutorials/wiki2/src/authorization/tutorial/views/auth.py b/docs/tutorials/wiki2/src/authorization/tutorial/views/auth.py index e66c68a34..807ff3464 100644 --- a/docs/tutorials/wiki2/src/authorization/tutorial/views/auth.py +++ b/docs/tutorials/wiki2/src/authorization/tutorial/views/auth.py @@ -53,7 +53,7 @@ def logout(request): @forbidden_view_config(renderer='tutorial:templates/403.jinja2') def forbidden_view(exc, request): - if request.identity is None: + if not request.is_authenticated: next_url = request.route_url('login', _query={'next': request.url}) return HTTPSeeOther(location=next_url) diff --git a/docs/tutorials/wiki2/src/tests/tutorial/templates/layout.jinja2 b/docs/tutorials/wiki2/src/tests/tutorial/templates/layout.jinja2 index 5d6a23410..55f4a85dc 100644 --- a/docs/tutorials/wiki2/src/tests/tutorial/templates/layout.jinja2 +++ b/docs/tutorials/wiki2/src/tests/tutorial/templates/layout.jinja2 @@ -33,7 +33,7 @@
- {% if request.identity is none %} + {% if not request.is_authenticated %}

Login

diff --git a/docs/tutorials/wiki2/src/tests/tutorial/views/auth.py b/docs/tutorials/wiki2/src/tests/tutorial/views/auth.py index e66c68a34..807ff3464 100644 --- a/docs/tutorials/wiki2/src/tests/tutorial/views/auth.py +++ b/docs/tutorials/wiki2/src/tests/tutorial/views/auth.py @@ -53,7 +53,7 @@ def logout(request): @forbidden_view_config(renderer='tutorial:templates/403.jinja2') def forbidden_view(exc, request): - if request.identity is None: + if not request.is_authenticated: next_url = request.route_url('login', _query={'next': request.url}) return HTTPSeeOther(location=next_url) -- cgit v1.2.3