|
|
|
import pytest
|
|
|
|
|
|
|
|
from pyramid.httpexceptions import HTTPFound
|
|
|
|
from pyramid.testing import DummyRequest
|
|
|
|
|
|
|
|
from ordr.models.account import Role
|
|
|
|
|
|
|
|
from .. import app_config, dbsession, get_example_user # noqa: F401
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'user,location',
|
|
|
|
[(None, '/login'), ('someone', '/orders')]
|
|
|
|
)
|
|
|
|
def test_welcome(user, location):
|
|
|
|
''' test redirects on web root '''
|
|
|
|
from ordr.views.pages import welcome
|
|
|
|
|
|
|
|
request = DummyRequest(user=user)
|
|
|
|
result = welcome(None, request)
|
|
|
|
|
|
|
|
assert isinstance(result, HTTPFound)
|
|
|
|
assert result.location == f'http://example.com/{location}'
|
|
|
|
|
|
|
|
|
|
|
|
def test_faq():
|
|
|
|
''' test the view for the faq page '''
|
|
|
|
from ordr.views.pages import faq
|
|
|
|
result = faq(None, None)
|
|
|
|
assert result == {}
|
|
|
|
|
|
|
|
|
|
|
|
def test_login():
|
|
|
|
''' test the view for the login form '''
|
|
|
|
from ordr.views.pages import login
|
|
|
|
result = login(None, None)
|
|
|
|
assert result == {'loginerror': False}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize( # noqa: F811
|
|
|
|
'role', [Role.USER, Role.PURCHASER, Role.ADMIN]
|
|
|
|
)
|
|
|
|
def test_check_login_ok(dbsession, role):
|
|
|
|
''' test the processing of the login form with valid credentials '''
|
|
|
|
from ordr.views.pages import check_login
|
|
|
|
|
|
|
|
user = get_example_user(role)
|
|
|
|
dbsession.add(user)
|
|
|
|
post_data = {'username': user.username, 'password': user.first_name}
|
|
|
|
request = DummyRequest(dbsession=dbsession, POST=post_data)
|
|
|
|
result = check_login(None, request)
|
|
|
|
|
|
|
|
assert isinstance(result, HTTPFound)
|
|
|
|
assert result.location == 'http://example.com//'
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize( # noqa: F811
|
|
|
|
'role', [Role.UNVALIDATED, Role.NEW, Role.INACTIVE]
|
|
|
|
)
|
|
|
|
def test_check_login_not_activated(dbsession, role):
|
|
|
|
''' test the processing of the login form with an inactive user '''
|
|
|
|
from ordr.views.pages import check_login
|
|
|
|
|
|
|
|
user = get_example_user(role)
|
|
|
|
dbsession.add(user)
|
|
|
|
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}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize( # noqa: F811
|
|
|
|
'username,password', [
|
|
|
|
('', ''),
|
|
|
|
('TerryGilliam', ''),
|
|
|
|
('', 'Terry'),
|
|
|
|
('TerryGilliam', 'wrong password'),
|
|
|
|
('wrong username', 'Terry'),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_check_login_invalid_credentials(dbsession, username, password):
|
|
|
|
''' test the processing of the login form with invalid credentials '''
|
|
|
|
from ordr.views.pages import check_login
|
|
|
|
|
|
|
|
user = get_example_user(Role.USER)
|
|
|
|
dbsession.add(user)
|
|
|
|
post_data = {'username': username, 'password': password}
|
|
|
|
request = DummyRequest(dbsession=dbsession, POST=post_data)
|
|
|
|
result = check_login(None, request)
|
|
|
|
|
|
|
|
assert result == {'loginerror': True}
|
|
|
|
|
|
|
|
|
|
|
|
def test_logout():
|
|
|
|
''' test the logout view '''
|
|
|
|
from ordr.views.pages import logout
|
|
|
|
|
|
|
|
request = DummyRequest()
|
|
|
|
result = logout(None, request)
|
|
|
|
|
|
|
|
assert isinstance(result, HTTPFound)
|
|
|
|
assert result.location == 'http://example.com//'
|