|
|
|
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):
|
|
|
|
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():
|
|
|
|
from ordr.views.pages import faq
|
|
|
|
result = faq(None, None)
|
|
|
|
assert result == {}
|
|
|
|
|
|
|
|
|
|
|
|
def test_login():
|
|
|
|
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):
|
|
|
|
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):
|
|
|
|
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):
|
|
|
|
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():
|
|
|
|
from ordr.views.pages import logout
|
|
|
|
request = DummyRequest()
|
|
|
|
result = logout(None, request)
|
|
|
|
assert isinstance(result, HTTPFound)
|
|
|
|
assert result.location == 'http://example.com//'
|