Browse Source

webroot redirects to login or orders if user is logged in

rework
Holger Frey 7 years ago
parent
commit
65ad7738ff
  1. 2
      ordr/templates/layout.jinja2
  2. 13
      ordr/views/pages.py
  3. 11
      tests/_functional/pages.py
  4. 16
      tests/views/pages.py

2
ordr/templates/layout.jinja2

@ -24,7 +24,7 @@ @@ -24,7 +24,7 @@
<a class="navbar-brand text-primary" href="/"><strong>ordr</strong></a>
{% if not request.user %}
<ul class="navbar-nav mr-auto">
<li class="nav-item {% if context.nav_active=='welcome' and not request.view_name %}active{% endif %}">
<li class="nav-item {% if context.nav_active=='welcome' and request.view_name=='login' %}active{% endif %}">
<a href="/" class="nav-link">Welcome</a>
</li>
<li class="nav-item {% if context.nav_active=='welcome' and request.view_name=='faq' %}active{% endif %}">

13
ordr/views/pages.py

@ -1,3 +1,4 @@ @@ -1,3 +1,4 @@
from pyramid.httpexceptions import HTTPFound
from pyramid.view import view_config
@ -7,6 +8,18 @@ from pyramid.view import view_config @@ -7,6 +8,18 @@ from pyramid.view import view_config
renderer='ordr:templates/pages/welcome.jinja2',
)
def welcome(context, request):
next = 'orders' if request.user else 'login'
redirect_to = request.resource_url(context, next)
return HTTPFound(redirect_to)
@view_config(
context='ordr.resources.RootResource',
name='login',
permission='view',
renderer='ordr:templates/pages/welcome.jinja2',
)
def login(context, request):
return {}

11
tests/_functional/pages.py

@ -1,10 +1,21 @@ @@ -1,10 +1,21 @@
''' functional tests for ordr2.views.pages '''
import pytest
from . import testapp # noqa: F401
@pytest.mark.xfail
def test_welcome(testapp): # noqa: F811
testapp.logout()
result = testapp.get('/')
assert result.location == 'http://localhost/login'
testapp.login('user', 'password')
assert result.location == 'http://localhost/orders'
def test_login_get(testapp): # noqa: F811
result = testapp.get('/login')
active = result.html.find('li', class_='active')
assert active.a['href'] == '/'
expected = {'/', '/faq', '/register', '/forgot', '/register'}

16
tests/views/pages.py

@ -1,9 +1,19 @@ @@ -1,9 +1,19 @@
import pytest
from pyramid.httpexceptions import HTTPFound
from pyramid.testing import DummyRequest
def test_welcome():
@pytest.mark.parametrize(
'user,location',
[(None, '/login'), ('someone', '/orders')]
)
def test_welcome(user, location):
from ordr.views.pages import welcome
result = welcome(None, None)
assert result == {}
request = DummyRequest(user=user)
result = welcome(None, request)
assert isinstance(result, HTTPFound)
assert result.location == f'http://example.com/{location}'
def test_faq():