62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
import os
|
|
import tempfile
|
|
|
|
from webtest import TestApp
|
|
from quixote.wsgi import QWIP
|
|
|
|
import wcs
|
|
from wcs import publisher
|
|
from wcs.qommon.http_request import HTTPRequest
|
|
|
|
QWIP.request_class = HTTPRequest
|
|
|
|
def create_temporary_pub():
|
|
APP_DIR = tempfile.mkdtemp()
|
|
publisher.WcsPublisher.APP_DIR = APP_DIR
|
|
publisher.WcsPublisher.DATA_DIR = os.path.abspath(
|
|
os.path.join(os.path.dirname(wcs.__file__), '..', 'data'))
|
|
pub = publisher.WcsPublisher.create_publisher()
|
|
# allow saving the user
|
|
pub.app_dir = os.path.join(APP_DIR, 'example.net')
|
|
os.mkdir(pub.app_dir)
|
|
return pub
|
|
|
|
def get_app(pub):
|
|
return TestApp(QWIP(pub), extra_environ={'HTTP_HOST': 'example.net'})
|
|
|
|
def login(app, username='admin', password='admin'):
|
|
login_page = app.get('/login/')
|
|
login_form = login_page.forms['login-form']
|
|
login_form['username'] = username
|
|
login_form['password'] = password
|
|
resp = login_form.submit()
|
|
assert resp.status_int == 302
|
|
return app
|
|
|
|
class EmailsMocking(object):
|
|
def __init__(self):
|
|
self.emails = {}
|
|
import wcs.qommon.emails
|
|
import qommon.emails
|
|
wcs.qommon.emails.email = self.sendmail
|
|
qommon.emails.email = self.sendmail
|
|
|
|
def sendmail(self, subject, *args, **kwargs):
|
|
self.emails[subject] = {'args': args, 'kwargs': kwargs}
|
|
|
|
def get(self, subject):
|
|
return self.emails.get(subject)
|
|
|
|
def empty(self):
|
|
self.emails = {}
|
|
|
|
def count(self):
|
|
return len(self.emails)
|
|
|
|
emails = EmailsMocking()
|
|
|
|
|
|
class MockSubstitutionVariables(object):
|
|
def get_substitution_variables(self):
|
|
return {'bar': 'Foobar', 'foo': '1 < 3', 'email': 'sub@localhost'}
|