You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
5.4 KiB
165 lines
5.4 KiB
''' Tests for ordr.schemas.helpers ''' |
|
|
|
import pytest |
|
|
|
from pyramid.testing import DummyRequest, DummyResource |
|
|
|
from .. import app_config, dbsession, get_example_user # noqa: F401 |
|
|
|
|
|
def test_deferred_csrf_default(): |
|
''' deferred_csrf_default should return a csrf token ''' |
|
from ordr.schemas.helpers import deferred_csrf_default |
|
from pyramid.csrf import get_csrf_token |
|
|
|
request = DummyRequest() |
|
token = deferred_csrf_default(None, {'request': request}) |
|
|
|
assert token == get_csrf_token(request) |
|
|
|
|
|
def test_deferred_csrf_validator_ok(): |
|
''' test deferred_csrf_validator with valid csrf token ''' |
|
from ordr.schemas.helpers import deferred_csrf_validator |
|
from pyramid.csrf import get_csrf_token |
|
|
|
request = DummyRequest() |
|
token = get_csrf_token(request) |
|
request.POST = {'csrf_token': token} |
|
validation_func = deferred_csrf_validator(None, {'request': request}) |
|
|
|
assert validation_func(None, None) is None |
|
|
|
|
|
@pytest.mark.parametrize('post', [{}, {'csrf_token': 'Albatross!'}]) |
|
def test_deferred_csrf_validator_fails_on_no_csrf_token(post): |
|
''' test deferred_csrf_validator with invalid or missing csrf token ''' |
|
from ordr.schemas.helpers import deferred_csrf_validator |
|
from colander import Invalid |
|
|
|
request = DummyRequest() |
|
request.POST = post |
|
validation_func = deferred_csrf_validator(None, {'request': request}) |
|
|
|
with pytest.raises(Invalid): |
|
assert validation_func(None, None) is None |
|
|
|
|
|
def test_deferred_unique_username_validator_ok(dbsession): # noqa: F811 |
|
''' unknown usernames should not raise an invalidation error ''' |
|
from ordr.schemas.helpers import deferred_unique_username_validator |
|
from ordr.models.account import Role |
|
|
|
request = DummyRequest(dbsession=dbsession) |
|
user = get_example_user(Role.USER) |
|
dbsession.add(user) |
|
validation_func = deferred_unique_username_validator( |
|
None, |
|
{'request': request} |
|
) |
|
|
|
assert validation_func(None, 'AnneElk') is None |
|
|
|
|
|
def test_deferred_unique_username_validator_fails(dbsession): # noqa: F811 |
|
''' known username should raise an invalidation error ''' |
|
from ordr.schemas.helpers import deferred_unique_username_validator |
|
from ordr.models.account import Role |
|
from colander import Invalid |
|
|
|
request = DummyRequest(dbsession=dbsession) |
|
user = get_example_user(Role.USER) |
|
dbsession.add(user) |
|
validation_func = deferred_unique_username_validator( |
|
None, |
|
{'request': request} |
|
) |
|
|
|
with pytest.raises(Invalid): |
|
assert validation_func(None, 'TerryGilliam') is None |
|
|
|
|
|
def test_deferred_unique_email_validator_ok(dbsession): # noqa: F811 |
|
''' unknown emails should not raise an invalidation error ''' |
|
from ordr.schemas.helpers import deferred_unique_email_validator |
|
from ordr.models.account import Role |
|
|
|
context = DummyResource(model=None) |
|
request = DummyRequest(dbsession=dbsession, context=context) |
|
user = get_example_user(Role.USER) |
|
dbsession.add(user) |
|
validation_func = deferred_unique_email_validator( |
|
None, |
|
{'request': request} |
|
) |
|
|
|
assert validation_func(None, 'elk@example.com') is None |
|
|
|
|
|
def test_deferred_unique_email_validator_ok_same_user(dbsession): # noqa: F811 |
|
''' known emails of a user might not raise an error |
|
|
|
if a user is edited and the mail address is not change, no invalidation |
|
error should be raised |
|
''' |
|
from ordr.schemas.helpers import deferred_unique_email_validator |
|
from ordr.models.account import Role |
|
|
|
user = get_example_user(Role.USER) |
|
context = DummyResource(model=user) |
|
request = DummyRequest(dbsession=dbsession, context=context) |
|
dbsession.add(user) |
|
validation_func = deferred_unique_email_validator( |
|
None, |
|
{'request': request} |
|
) |
|
|
|
assert validation_func(None, user.email) is None |
|
|
|
|
|
@pytest.mark.parametrize( # noqa: F811 |
|
'email', ['', 'gilliam@example.com', 'malformed'] |
|
) |
|
def test_deferred_unique_email_validator_fails(dbsession, email): |
|
''' known, empty or malformed emails should raise an invalidation error ''' |
|
from ordr.schemas.helpers import deferred_unique_email_validator |
|
from ordr.models.account import Role |
|
from colander import Invalid |
|
|
|
context = DummyResource(model=None) |
|
request = DummyRequest(dbsession=dbsession, context=context) |
|
user = get_example_user(Role.USER) |
|
dbsession.add(user) |
|
validation_func = deferred_unique_email_validator( |
|
None, |
|
{'request': request} |
|
) |
|
|
|
with pytest.raises(Invalid): |
|
assert validation_func(None, email) is None |
|
|
|
|
|
def test_deferred_password_validator_ok(): |
|
''' correct password should not raise invalidation error ''' |
|
from ordr.schemas.helpers import deferred_password_validator |
|
from ordr.models.account import Role |
|
|
|
user = get_example_user(Role.USER) |
|
request = DummyRequest(user=user) |
|
validation_func = deferred_password_validator(None, {'request': request}) |
|
|
|
assert validation_func(None, 'Terry') is None |
|
|
|
|
|
def test_deferred_password_validator_fails(): |
|
''' incorrect password should raise invalidation error ''' |
|
from ordr.schemas.helpers import deferred_password_validator |
|
from ordr.models.account import Role |
|
from colander import Invalid |
|
|
|
user = get_example_user(Role.USER) |
|
request = DummyRequest(user=user) |
|
validation_func = deferred_password_validator(None, {'request': request}) |
|
|
|
with pytest.raises(Invalid): |
|
assert validation_func(None, 'Wrong Password') is None
|
|
|