''' functional tests for ordr2 ''' import pytest import re import transaction import webtest from bs4 import BeautifulSoup from .. import APP_SETTINGS, get_example_user WEBTEST_SETTINGS = APP_SETTINGS.copy() WEBTEST_SETTINGS['pyramid.includes'] = [ 'pyramid_mailer.testing' ] class CustomTestApp(webtest.TestApp): ''' might add custom functionality to webtest.TestApp ''' pass def login(self, username, password): ''' login ''' self.logout() result = self.get('/login') login_form = result.forms[0] login_form['username'] = username login_form['password'] = password login_form.submit() def logout(self): ''' logout ''' self.get('/logout') def reset(self): ''' reset the webapp ''' self.logout() super().reset() def create_users(dbsession): ''' create example users ''' from ordr.models.account import Role for role in Role: user = get_example_user(role) dbsession.add(user) def get_token_url(email, prefix='/'): ''' extracts an account token url from an email ''' soup = BeautifulSoup(email.html, 'html.parser') for link in soup.find_all('a'): if re.search(prefix + '[a-f0-9]{32}', link['href']): return link['href'] @pytest.fixture(scope='module') def testappsetup(): ''' fixture for using webtest this fixture just sets up the testapp. please use the testapp() fixture below for real tests. ''' from ordr.models.meta import Base from ordr.models import get_tm_session from ordr 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) @pytest.fixture(scope='function') def testapp(testappsetup): ''' fixture using webtests, resets the logged every time ''' testappsetup.reset() yield testappsetup