diff --git a/ordr/views/pages.py b/ordr/views/pages.py index c9163a6..1b4f5b8 100644 --- a/ordr/views/pages.py +++ b/ordr/views/pages.py @@ -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): 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( diff --git a/tests/_functional/layout.py b/tests/_functional/layout.py index a761712..c16fc41 100644 --- a/tests/_functional/layout.py +++ b/tests/_functional/layout.py @@ -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 diff --git a/tests/_functional/login_logout.py b/tests/_functional/login_logout.py new file mode 100644 index 0000000..7c6d02b --- /dev/null +++ b/tests/_functional/login_logout.py @@ -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 diff --git a/tests/_functional/pages.py b/tests/_functional/pages.py index 3ca29da..fd154ad 100644 --- a/tests/_functional/pages.py +++ b/tests/_functional/pages.py @@ -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 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 diff --git a/tests/views/pages.py b/tests/views/pages.py index d7a49fd..d9980f4 100644 --- a/tests/views/pages.py +++ b/tests/views/pages.py @@ -9,7 +9,7 @@ from .. import app_config, dbsession, get_example_user # noqa: F401 @pytest.mark.parametrize( - 'user,location', + 'user,location', [(None, '/login'), ('someone', '/orders')] ) def test_welcome(user, location): @@ -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): 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,16 +56,16 @@ 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', ''), - ('', 'Terry'), - ('TerryGilliam', 'wrong password'), - ('wrong username', 'Terry'), + ('', ''), + ('TerryGilliam', ''), + ('', 'Terry'), + ('TerryGilliam', 'wrong password'), + ('wrong username', 'Terry'), ] ) 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():