''' functional tests for ordr2 ''' import os.path import pytest import re import transaction import webtest from bs4 import BeautifulSoup from .. import APP_SETTINGS, create_users, get_user WEBTEST_SETTINGS = APP_SETTINGS.copy() WEBTEST_SETTINGS['pyramid.includes'].append('pyramid_mailer.testing') def get_token_url(email): ''' extracts an account token url from an email ''' soup = BeautifulSoup(email.html, 'html.parser') for link in soup.find_all('a'): if re.search('/account/[a-f0-9]{32}', link['href']): return link['href'] 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)