Holger Frey
7 years ago
3 changed files with 195 additions and 55 deletions
@ -0,0 +1,85 @@
@@ -0,0 +1,85 @@
|
||||
''' Tests for ordr2.views.account ''' |
||||
|
||||
import pytest |
||||
|
||||
from pyramid.httpexceptions import HTTPFound |
||||
from pyramid.testing import DummyRequest, DummyResource |
||||
|
||||
|
||||
from .. import app_config, dbsession, get_user, create_users |
||||
|
||||
|
||||
@pytest.mark.parametrize('rolename', ['user', 'purchaser', 'admin']) |
||||
def test_account_login_active_users(dbsession, rolename): |
||||
''' login ok for active users ''' |
||||
from ordr2.views.account import login |
||||
create_users(dbsession) |
||||
|
||||
user = get_user(rolename) |
||||
request = DummyRequest( |
||||
dbsession=dbsession, |
||||
post={ |
||||
'username': user.username, |
||||
'password': user.first_name |
||||
} |
||||
) |
||||
result = login(None, request) |
||||
|
||||
assert isinstance(result, HTTPFound) |
||||
assert result.location == '//orders' |
||||
|
||||
|
||||
@pytest.mark.parametrize('rolename', ['unvalidated', 'new', 'inactive']) |
||||
def test_account_login_fails_inactive_users(dbsession, rolename): |
||||
''' login fails for inactive users ''' |
||||
from ordr2.views.account import login |
||||
create_users(dbsession) |
||||
|
||||
user = get_user(rolename) |
||||
request = DummyRequest( |
||||
dbsession=dbsession, |
||||
post={ |
||||
'username': user.username, |
||||
'password': user.first_name |
||||
} |
||||
) |
||||
result = login(None, request) |
||||
|
||||
assert result == {} |
||||
|
||||
|
||||
@pytest.mark.parametrize( |
||||
'username, password', [ |
||||
('EricIdle', 'wrong password'), |
||||
('unknown user', 'Eric'), |
||||
('unknown user', 'wrong password'), |
||||
('', '') |
||||
] |
||||
) |
||||
def test_account_login_fails_wrong_credentials(dbsession, username, password): |
||||
''' login fails for unknown or wrong credentials ''' |
||||
from ordr2.views.account import login |
||||
create_users(dbsession) |
||||
|
||||
request = DummyRequest( |
||||
dbsession=dbsession, |
||||
post={ |
||||
'username': username, |
||||
'password': password |
||||
} |
||||
) |
||||
result = login(None, request) |
||||
|
||||
assert result == {} |
||||
|
||||
|
||||
def test_logout(app_config): |
||||
''' logout works ''' |
||||
from ordr2.views.account import logout |
||||
user = get_user('admin') |
||||
|
||||
request = DummyRequest(user=user) |
||||
result = logout(None, request) |
||||
|
||||
assert isinstance(result, HTTPFound) |
||||
assert result.location == 'http://example.com//' |
Reference in new issue