''' functional tests for ordr2 ''' import os.path import pytest import transaction import webtest from .. import APP_SETTINGS, create_users, get_user WEBTEST_SETTINGS = APP_SETTINGS.copy() # WEBTEST_SETTINGS.update({ }) class CustomTestApp(webtest.TestApp): ''' adds login and logout methods to webtest.TestApp ''' def reset(self): ''' logs out any user and resets the state of the application ''' self.logout() super().reset() def logout(self): ''' perfomes a logout of a user ''' self.get('/account/logout') def login(self, role_name): ''' perfomes a logs in of a user ''' # do a logout first, testapp fixture scope is on module level self.logout() user = get_user(role_name) response = self.get('/') login_form = response.forms['login-form'] login_form.set('username', user.username) login_form.set('password', user.first_name) login_form.submit() @pytest.fixture(scope='module') def testapp(): ''' fixture for using webtest ''' from ordr2.models.meta import Base from ordr2.models import get_tm_session from ordr2 import main app = main({}, **WEBTEST_SETTINGS) testapp = CustomTestApp(app) session_factory = app.registry['dbsession_factory'] engine = session_factory.kw['bind'] Base.metadata.create_all(engine) with transaction.manager: # set up test data here dbsession = get_tm_session(session_factory, transaction.manager) create_users(dbsession) yield testapp Base.metadata.drop_all(engine)