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.
159 lines
5.1 KiB
159 lines
5.1 KiB
7 years ago
|
''' Tests for ordr2.schemas.helpers '''
|
||
|
|
||
|
import pytest
|
||
|
|
||
|
from pyramid.testing import DummyRequest, DummyResource
|
||
|
|
||
|
from .. import app_config, dbsession, get_user
|
||
|
|
||
|
|
||
|
def test_deferred_csrf_default(app_config):
|
||
|
''' deferred_csrf_default should return a csrf token '''
|
||
|
from ordr2.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(app_config):
|
||
|
''' test deferred_csrf_validator with valid csrf token '''
|
||
|
from ordr2.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(app_config, post):
|
||
|
''' test deferred_csrf_validator with invalid or missing csrf token '''
|
||
|
from ordr2.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):
|
||
|
''' unknown usernames should not raise an invalidation error '''
|
||
|
from ordr2.schemas.helpers import deferred_unique_username_validator
|
||
|
|
||
|
request = DummyRequest(dbsession=dbsession)
|
||
|
user = get_user('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):
|
||
|
''' known username should raise an invalidation error '''
|
||
|
from ordr2.schemas.helpers import deferred_unique_username_validator
|
||
|
from colander import Invalid
|
||
|
|
||
|
request = DummyRequest(dbsession=dbsession)
|
||
|
user = get_user('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):
|
||
|
''' unknown emails should not raise an invalidation error '''
|
||
|
from ordr2.schemas.helpers import deferred_unique_email_validator
|
||
|
|
||
|
context = DummyResource(model=None)
|
||
|
request = DummyRequest(dbsession=dbsession, context=context)
|
||
|
user = get_user('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_belongs_to_same_user(dbsession):
|
||
|
''' 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 ordr2.schemas.helpers import deferred_unique_email_validator
|
||
|
|
||
|
user = get_user('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('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 ordr2.schemas.helpers import deferred_unique_email_validator
|
||
|
from colander import Invalid
|
||
|
|
||
|
context = DummyResource(model=None)
|
||
|
request = DummyRequest(dbsession=dbsession, context=context)
|
||
|
user = get_user('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(app_config):
|
||
|
''' correct password should not raise invalidation error '''
|
||
|
from ordr2.schemas.helpers import deferred_password_validator
|
||
|
from pyramid.csrf import get_csrf_token
|
||
|
|
||
|
user = get_user('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(app_config):
|
||
|
''' incorrect password should raise invalidation error '''
|
||
|
from ordr2.schemas.helpers import deferred_password_validator
|
||
|
from colander import Invalid
|
||
|
|
||
|
user = get_user('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
|
||
|
|