|
|
@ -3,13 +3,14 @@ import pytest |
|
|
|
from datetime import datetime, timedelta |
|
|
|
from datetime import datetime, timedelta |
|
|
|
from pyramid.testing import DummyRequest |
|
|
|
from pyramid.testing import DummyRequest |
|
|
|
|
|
|
|
|
|
|
|
from .. import app_config # noqa: F401 |
|
|
|
from .. import app_config, dbsession, get_example_user # noqa: F401 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize( |
|
|
|
@pytest.mark.parametrize( |
|
|
|
'key,result', [('NEW', 'role:new'), ('USER', 'role:user')] |
|
|
|
'key,result', [('NEW', 'role:new'), ('USER', 'role:user')] |
|
|
|
) |
|
|
|
) |
|
|
|
def test_role_principal(key, result): |
|
|
|
def test_role_principal(key, result): |
|
|
|
|
|
|
|
''' test the principal representation of a role ''' |
|
|
|
from ordr.models.account import Role |
|
|
|
from ordr.models.account import Role |
|
|
|
subject = Role[key] |
|
|
|
subject = Role[key] |
|
|
|
assert subject.principal == result |
|
|
|
assert subject.principal == result |
|
|
@ -19,6 +20,7 @@ def test_role_principal(key, result): |
|
|
|
'key,result', [('NEW', 'New'), ('USER', 'User')] |
|
|
|
'key,result', [('NEW', 'New'), ('USER', 'User')] |
|
|
|
) |
|
|
|
) |
|
|
|
def test_role__str__(key, result): |
|
|
|
def test_role__str__(key, result): |
|
|
|
|
|
|
|
''' test the string representation of a role ''' |
|
|
|
from ordr.models.account import Role |
|
|
|
from ordr.models.account import Role |
|
|
|
subject = Role[key] |
|
|
|
subject = Role[key] |
|
|
|
assert str(subject) == result |
|
|
|
assert str(subject) == result |
|
|
@ -26,6 +28,7 @@ def test_role__str__(key, result): |
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('id_', [1, 2, 5, 123]) |
|
|
|
@pytest.mark.parametrize('id_', [1, 2, 5, 123]) |
|
|
|
def test_user_principal(id_): |
|
|
|
def test_user_principal(id_): |
|
|
|
|
|
|
|
''' test the principal representation of a user ''' |
|
|
|
from ordr.models.account import User |
|
|
|
from ordr.models.account import User |
|
|
|
user = User(id=id_) |
|
|
|
user = User(id=id_) |
|
|
|
assert user.principal == f'user:{id_}' |
|
|
|
assert user.principal == f'user:{id_}' |
|
|
@ -42,6 +45,7 @@ def test_user_principal(id_): |
|
|
|
] |
|
|
|
] |
|
|
|
) |
|
|
|
) |
|
|
|
def test_user_principals(name, principals): |
|
|
|
def test_user_principals(name, principals): |
|
|
|
|
|
|
|
''' test all principals of a user ''' |
|
|
|
from ordr.models.account import User, Role |
|
|
|
from ordr.models.account import User, Role |
|
|
|
|
|
|
|
|
|
|
|
user = User(id=1, role=Role[name]) |
|
|
|
user = User(id=1, role=Role[name]) |
|
|
@ -62,12 +66,14 @@ def test_user_principals(name, principals): |
|
|
|
] |
|
|
|
] |
|
|
|
) |
|
|
|
) |
|
|
|
def test_user_is_active(name, expected): |
|
|
|
def test_user_is_active(name, expected): |
|
|
|
|
|
|
|
''' test the calculated property 'active' of a user ''' |
|
|
|
from ordr.models.account import User, Role |
|
|
|
from ordr.models.account import User, Role |
|
|
|
user = User(id=1, role=Role[name]) |
|
|
|
user = User(id=1, role=Role[name]) |
|
|
|
assert expected == user.is_active |
|
|
|
assert expected == user.is_active |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_user_set_password(): |
|
|
|
def test_user_set_password(): |
|
|
|
|
|
|
|
''' test 'set_password()' method of a user ''' |
|
|
|
from ordr.models.account import User |
|
|
|
from ordr.models.account import User |
|
|
|
from ordr.security import password_context |
|
|
|
from ordr.security import password_context |
|
|
|
|
|
|
|
|
|
|
@ -87,6 +93,7 @@ def test_user_set_password(): |
|
|
|
] |
|
|
|
] |
|
|
|
) |
|
|
|
) |
|
|
|
def test_user_check_password(password, expected): |
|
|
|
def test_user_check_password(password, expected): |
|
|
|
|
|
|
|
''' test the 'check_password()' method of a user ''' |
|
|
|
from ordr.models.account import User |
|
|
|
from ordr.models.account import User |
|
|
|
from ordr.security import password_context |
|
|
|
from ordr.security import password_context |
|
|
|
|
|
|
|
|
|
|
@ -100,6 +107,7 @@ def test_user_check_password(password, expected): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_user_check_password_updates_old_sheme(): |
|
|
|
def test_user_check_password_updates_old_sheme(): |
|
|
|
|
|
|
|
''' test that 'check_password()' updates the hash off an old scheme ''' |
|
|
|
from ordr.models.account import User |
|
|
|
from ordr.models.account import User |
|
|
|
from ordr.security import password_context |
|
|
|
from ordr.security import password_context |
|
|
|
|
|
|
|
|
|
|
@ -117,12 +125,14 @@ def test_user_check_password_updates_old_sheme(): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_user__str__(): |
|
|
|
def test_user__str__(): |
|
|
|
|
|
|
|
''' test the string representation of a user ''' |
|
|
|
from ordr.models.account import User |
|
|
|
from ordr.models.account import User |
|
|
|
user = User(username='Eric Idle') |
|
|
|
user = User(username='Eric Idle') |
|
|
|
assert str(user) == 'Eric Idle' |
|
|
|
assert str(user) == 'Eric Idle' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_user_issue_token(app_config): # noqa: F811 |
|
|
|
def test_user_issue_token(app_config): # noqa: F811 |
|
|
|
|
|
|
|
''' test the 'issue_token()' method of a user ''' |
|
|
|
from ordr.models.account import User, Token, TokenSubject |
|
|
|
from ordr.models.account import User, Token, TokenSubject |
|
|
|
|
|
|
|
|
|
|
|
request = DummyRequest() |
|
|
|
request = DummyRequest() |
|
|
@ -137,6 +147,7 @@ def test_user_issue_token(app_config): # noqa: F811 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_token_issue_token(app_config): # noqa: F811 |
|
|
|
def test_token_issue_token(app_config): # noqa: F811 |
|
|
|
|
|
|
|
''' test the 'issue()' class method of the token class ''' |
|
|
|
from ordr.models.account import User, Token, TokenSubject |
|
|
|
from ordr.models.account import User, Token, TokenSubject |
|
|
|
|
|
|
|
|
|
|
|
request = DummyRequest() |
|
|
|
request = DummyRequest() |
|
|
@ -159,6 +170,7 @@ def test_token_issue_token(app_config): # noqa: F811 |
|
|
|
'subject,delta', [('REGISTRATION', 5), ('RESET_PASSWORD', 10)] |
|
|
|
'subject,delta', [('REGISTRATION', 5), ('RESET_PASSWORD', 10)] |
|
|
|
) |
|
|
|
) |
|
|
|
def test_token_issue_token_time_from_settings(app_config, subject, delta): |
|
|
|
def test_token_issue_token_time_from_settings(app_config, subject, delta): |
|
|
|
|
|
|
|
''' test that 'issue()' uses the exiration time from setting ''' |
|
|
|
from ordr.models.account import User, Token, TokenSubject |
|
|
|
from ordr.models.account import User, Token, TokenSubject |
|
|
|
|
|
|
|
|
|
|
|
request = DummyRequest() |
|
|
|
request = DummyRequest() |
|
|
@ -172,3 +184,72 @@ def test_token_issue_token_time_from_settings(app_config, subject, delta): |
|
|
|
expected_expires.timestamp(), |
|
|
|
expected_expires.timestamp(), |
|
|
|
abs=1 |
|
|
|
abs=1 |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('use_subject', [True, False]) # noqa: F811 |
|
|
|
|
|
|
|
def test_registration_token_retrieve_ok(dbsession, use_subject): |
|
|
|
|
|
|
|
''' test 'retrieve()' class method returns token instance ''' |
|
|
|
|
|
|
|
from ordr.models.account import Role, Token, TokenSubject |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
request = DummyRequest(dbsession=dbsession) |
|
|
|
|
|
|
|
user = get_example_user(Role.NEW) |
|
|
|
|
|
|
|
token = user.issue_token(request, TokenSubject.REGISTRATION) |
|
|
|
|
|
|
|
dbsession.add(user) |
|
|
|
|
|
|
|
dbsession.flush() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
subject = TokenSubject.REGISTRATION if use_subject else None |
|
|
|
|
|
|
|
result = Token.retrieve(request, token.hash, subject=subject) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert result == token |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_registration_token_retrieve_not_found(dbsession): # noqa: F811 |
|
|
|
|
|
|
|
''' test 'retrieve()' class method returns None if token not found ''' |
|
|
|
|
|
|
|
from ordr.models.account import Role, Token, TokenSubject |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
request = DummyRequest(dbsession=dbsession) |
|
|
|
|
|
|
|
user = get_example_user(Role.NEW) |
|
|
|
|
|
|
|
user.issue_token(request, TokenSubject.REGISTRATION) |
|
|
|
|
|
|
|
dbsession.add(user) |
|
|
|
|
|
|
|
dbsession.flush() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result = Token.retrieve(request, 'unknown hash') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert result is None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_registration_token_retrieve_wrong_subject(dbsession): # noqa: F811 |
|
|
|
|
|
|
|
''' test 'retrieve()' class method returns None if wrong subject used ''' |
|
|
|
|
|
|
|
from ordr.models.account import Role, Token, TokenSubject |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
request = DummyRequest(dbsession=dbsession) |
|
|
|
|
|
|
|
user = get_example_user(Role.NEW) |
|
|
|
|
|
|
|
token = user.issue_token(request, TokenSubject.REGISTRATION) |
|
|
|
|
|
|
|
dbsession.add(user) |
|
|
|
|
|
|
|
dbsession.flush() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result = Token.retrieve( |
|
|
|
|
|
|
|
request, |
|
|
|
|
|
|
|
token.hash, |
|
|
|
|
|
|
|
subject=TokenSubject.RESET_PASSWORD |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert result is None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_registration_token_expired_raises_exception(dbsession): # noqa: F811 |
|
|
|
|
|
|
|
''' test 'retrieve()' class method raises exception if token is expired ''' |
|
|
|
|
|
|
|
from ordr.models.account import Role, Token, TokenSubject, TokenExpired |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
request = DummyRequest(dbsession=dbsession) |
|
|
|
|
|
|
|
user = get_example_user(Role.NEW) |
|
|
|
|
|
|
|
token = user.issue_token(request, TokenSubject.REGISTRATION) |
|
|
|
|
|
|
|
token.expires = datetime.utcnow() - timedelta(weeks=1) |
|
|
|
|
|
|
|
dbsession.add(user) |
|
|
|
|
|
|
|
dbsession.flush() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with pytest.raises(TokenExpired): |
|
|
|
|
|
|
|
Token.retrieve(request, token.hash) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dbsession.flush() |
|
|
|
|
|
|
|
assert dbsession.query(Token).count() == 0 |
|
|
|