Browse Source

linting applied

rework
Holger Frey 7 years ago
parent
commit
20032eede1
  1. 4
      ordr/views/pages.py
  2. 21
      tests/_functional/layout.py
  3. 42
      tests/_functional/login_logout.py
  4. 39
      tests/_functional/pages.py
  5. 16
      tests/views/pages.py

4
ordr/views/pages.py

@ -33,7 +33,7 @@ def faq(context, request): @@ -33,7 +33,7 @@ def faq(context, request):
renderer='ordr:templates/pages/login.jinja2',
)
def login(context, request):
return { 'loginerror': False }
return {'loginerror': False}
@view_config(
@ -55,7 +55,7 @@ def check_login(context, request): @@ -55,7 +55,7 @@ def check_login(context, request):
if user and user.is_active and user.check_password(password):
headers = remember(request, user.id)
return HTTPFound(request.resource_url(request.root), headers=headers)
return { 'loginerror': True }
return {'loginerror': True}
@view_config(

21
tests/_functional/layout.py

@ -19,20 +19,23 @@ def test_navbar_no_user(testapp): # noqa: F811 @@ -19,20 +19,23 @@ def test_navbar_no_user(testapp): # noqa: F811
assert 'nav-item dropdown' not in result
@pytest.mark.xfail # noqa: F811
@pytest.mark.parametrize(
'username,password,expected', [
('user', 'password', ['/', '/orders', '/logout', '/account']),
('purchaser', 'password', ['/', '/orders', '/logout', '/account']),
('admin', 'password', [
'/', '/orders', '/admin', '/logout', '/account'
]),
@pytest.mark.parametrize( # noqa: F811
'username,password,extras', [
('TerryGilliam', 'Terry', []),
('EricIdle', 'Eric', []),
('TerryJones', 'Terry', ['/admin']),
]
)
def test_navbar_with_user(testapp, username, password, expected):
def test_navbar_with_user(testapp, username, password, extras):
testapp.login(username, password)
result = testapp.get('/faq')
navbar = result.html.find('nav', class_='navbar-dark')
hrefs = [a['href'] for a in navbar.find_all('a')]
expected = ['/', '/orders', '/faq']
expected.extend(extras)
expected.extend(['#', '/logout', '/account'])
print('expected', expected)
print('found ', hrefs)
assert expected == hrefs
assert 'nav-item dropdown' in result
assert username in result

42
tests/_functional/login_logout.py

@ -0,0 +1,42 @@ @@ -0,0 +1,42 @@
''' functional tests for ordr2.views.pages '''
import pytest
from . import testappsetup, testapp # noqa: F401
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'}
hrefs = {a['href'] for a in result.html.find_all('a')}
assert expected == hrefs
forms = result.html.find_all('form')
assert len(forms) == 1
login_form = forms[0]
assert login_form['action'] == '/login'
assert login_form['method'] == 'POST'
assert 'account is not activated' not in result
def test_login_ok(testapp): # noqa: F811
result = testapp.get('/login')
login_form = result.forms[0]
login_form['username'] = 'TerryGilliam'
login_form['password'] = 'Terry'
result = login_form.submit()
assert result.location == 'http://localhost/'
@pytest.mark.parametrize( # noqa: F811
'username,password',
[('John', 'Cleese'), ('unknown user', 'wrong password')]
)
def test_login_denied(testapp, username, password):
result = testapp.get('/login')
login_form = result.forms[0]
login_form['username'] = 'John'
login_form['password'] = 'Cleese'
result = login_form.submit()
assert 'account is not activated' in result

39
tests/_functional/pages.py

@ -1,7 +1,5 @@ @@ -1,7 +1,5 @@
''' functional tests for ordr2.views.pages '''
import pytest
from . import testappsetup, testapp # noqa: F401
@ -17,40 +15,3 @@ def test_faq(testapp): # noqa: F811 @@ -17,40 +15,3 @@ def test_faq(testapp): # noqa: F811
result = testapp.get('/faq')
active = result.html.find('li', class_='active')
assert active.a['href'] == '/faq'
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'}
hrefs = {a['href'] for a in result.html.find_all('a')}
assert expected == hrefs
forms = result.html.find_all('form')
assert len(forms) == 1
login_form = forms[0]
assert login_form['action'] == '/login'
assert login_form['method'] == 'POST'
assert 'account is not activated' not in result
def test_login_ok(testapp): # noqa: F811
result = testapp.get('/login')
login_form = result.forms[0]
login_form['username'] = 'TerryGilliam'
login_form['password'] = 'Terry'
result = login_form.submit()
assert result.location == 'http://localhost/'
@pytest.mark.parametrize(
'username,password',
[('John', 'Cleese'), ('unknown user', 'wrong password')]
)
def test_login_denied(testapp, username, password): # noqa: F811
result = testapp.get('/login')
login_form = result.forms[0]
login_form['username'] = 'John'
login_form['password'] = 'Cleese'
result = login_form.submit()
assert 'account is not activated' in result

16
tests/views/pages.py

@ -29,10 +29,12 @@ def test_faq(): @@ -29,10 +29,12 @@ def test_faq():
def test_login():
from ordr.views.pages import login
result = login(None, None)
assert result == { 'loginerror': False }
assert result == {'loginerror': False}
@pytest.mark.parametrize('role', [Role.USER, Role.PURCHASER, Role.ADMIN])
@pytest.mark.parametrize( # noqa: F811
'role', [Role.USER, Role.PURCHASER, Role.ADMIN]
)
def test_check_login_ok(dbsession, role):
from ordr.views.pages import check_login
user = get_example_user(role)
@ -44,7 +46,9 @@ def test_check_login_ok(dbsession, role): @@ -44,7 +46,9 @@ def test_check_login_ok(dbsession, role):
assert result.location == 'http://example.com//'
@pytest.mark.parametrize('role', [Role.UNVALIDATED, Role.NEW, Role.INACTIVE])
@pytest.mark.parametrize( # noqa: F811
'role', [Role.UNVALIDATED, Role.NEW, Role.INACTIVE]
)
def test_check_login_not_activated(dbsession, role):
from ordr.views.pages import check_login
user = get_example_user(role)
@ -52,10 +56,10 @@ def test_check_login_not_activated(dbsession, role): @@ -52,10 +56,10 @@ def test_check_login_not_activated(dbsession, role):
post_data = {'username': user.username, 'password': user.first_name}
request = DummyRequest(dbsession=dbsession, POST=post_data)
result = check_login(None, request)
assert result == { 'loginerror': True }
assert result == {'loginerror': True}
@pytest.mark.parametrize(
@pytest.mark.parametrize( # noqa: F811
'username,password', [
('', ''),
('TerryGilliam', ''),
@ -71,7 +75,7 @@ def test_check_login_invalid_credentials(dbsession, username, password): @@ -71,7 +75,7 @@ def test_check_login_invalid_credentials(dbsession, username, password):
post_data = {'username': username, 'password': password}
request = DummyRequest(dbsession=dbsession, POST=post_data)
result = check_login(None, request)
assert result == { 'loginerror': True }
assert result == {'loginerror': True}
def test_logout():