|
|
|
''' Test package for ordr2. '''
|
|
|
|
|
|
|
|
import os.path
|
|
|
|
import pytest
|
|
|
|
import transaction
|
|
|
|
|
|
|
|
from pyramid import testing
|
|
|
|
from pyramid.csrf import get_csrf_token
|
|
|
|
from webob.multidict import MultiDict
|
|
|
|
|
|
|
|
|
|
|
|
# some path mangling to get the path to passlib.ini
|
|
|
|
currrent_dir = os.path.dirname(__file__)
|
|
|
|
ordr2_dir = os.path.dirname(currrent_dir)
|
|
|
|
passlib_config_path = os.path.join(ordr2_dir, 'passlib.ini')
|
|
|
|
assert os.path.isfile(passlib_config_path)
|
|
|
|
|
|
|
|
APP_SETTINGS = {
|
|
|
|
'sqlalchemy.url': 'sqlite:///:memory:',
|
|
|
|
'auth.secret': 'not-very-secure',
|
|
|
|
'session.secret':
|
|
|
|
'4d72ee16df8cf1238048ade32e3ce4d51757af8ada4a962cfae5cea5c08421a0',
|
|
|
|
'session.auto_csrf': True,
|
|
|
|
'passlib.config': passlib_config_path,
|
|
|
|
'pyramid.includes': [
|
|
|
|
'pyramid_jinja2',
|
|
|
|
],
|
|
|
|
'mail.default_sender': 'ordr@example.com'
|
|
|
|
}
|
|
|
|
|
|
|
|
EXAMPLE_USER_DATA = {
|
|
|
|
'unvalidated': (1, 'Graham', 'Chapman'),
|
|
|
|
'new': (2, 'John', 'Cleese'),
|
|
|
|
'user': (3, 'Terry', 'Gilliam'),
|
|
|
|
'purchaser': (4, 'Eric', 'Idle'),
|
|
|
|
'admin': (5, 'Terry', 'Jones'),
|
|
|
|
'inactive': (6, 'Michael', 'Palin'),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# fixtures
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def app_config():
|
|
|
|
''' fixture for tests requiring a pyramid.testing setup '''
|
|
|
|
with testing.testConfig(settings=APP_SETTINGS) as config:
|
|
|
|
config.include('pyramid_mailer.testing')
|
|
|
|
config.include('pyramid_jinja2')
|
|
|
|
|
|
|
|
from ordr2.models.account import passlib_context
|
|
|
|
passlib_context.update(schemes=['argon2'])
|
|
|
|
|
|
|
|
yield config
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def dbsession(app_config):
|
|
|
|
''' fixture for testing with database connection '''
|
|
|
|
from ordr2.models.meta import Base
|
|
|
|
from ordr2.models import (
|
|
|
|
get_engine,
|
|
|
|
get_session_factory,
|
|
|
|
get_tm_session
|
|
|
|
)
|
|
|
|
|
|
|
|
settings = app_config.get_settings()
|
|
|
|
engine = get_engine(settings)
|
|
|
|
session_factory = get_session_factory(engine)
|
|
|
|
session = get_tm_session(session_factory, transaction.manager)
|
|
|
|
Base.metadata.create_all(engine)
|
|
|
|
|
|
|
|
yield session
|
|
|
|
|
|
|
|
transaction.abort()
|
|
|
|
Base.metadata.drop_all(engine)
|
|
|
|
|
|
|
|
|
|
|
|
# helpers
|
|
|
|
|
|
|
|
def get_user(role_name):
|
|
|
|
''' get the user model for one well known user '''
|
|
|
|
from ordr2.models import Role, User
|
|
|
|
id_, first_name, last_name = EXAMPLE_USER_DATA[role_name]
|
|
|
|
user = User(
|
|
|
|
id=id_,
|
|
|
|
username=first_name + last_name,
|
|
|
|
first_name = first_name,
|
|
|
|
last_name = last_name,
|
|
|
|
email = last_name.lower() + '@example.com',
|
|
|
|
role=Role(role_name)
|
|
|
|
)
|
|
|
|
user.set_password(first_name)
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
def create_users(db):
|
|
|
|
''' set up all well known example users '''
|
|
|
|
from ordr2.models import Role
|
|
|
|
for role in Role:
|
|
|
|
user = get_user(role.value)
|
|
|
|
db.add(user)
|
|
|
|
|
|
|
|
|
|
|
|
def set_deform_data(request, form_data, modifier=None):
|
|
|
|
''' augments the request to include post data as provided by deform '''
|
|
|
|
post_dict = MultiDict([
|
|
|
|
('__formid__', 'deform'),
|
|
|
|
('_charset_', 'UTF-8'),
|
|
|
|
('csrf_token', get_csrf_token(request))
|
|
|
|
])
|
|
|
|
for key, value in form_data.items():
|
|
|
|
post_dict.add(key, value)
|
|
|
|
if modifier:
|
|
|
|
# the update() or __setitem__ function of multidict will append
|
|
|
|
# items with the same key instead of replacing them
|
|
|
|
items = list(post_dict.items())
|
|
|
|
modified = [(key, modifier.pop(key, value)) for key, value in items]
|
|
|
|
post_dict = MultiDict(modified)
|
|
|
|
post_dict.update(modifier)
|
|
|
|
request.POST = post_dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|