tests: run formdata and register tests with both pickle and sql
This commit is contained in:
parent
08709d91dc
commit
a81be6e3b5
|
@ -1,3 +1,4 @@
|
|||
import pytest
|
||||
import sys
|
||||
import shutil
|
||||
import time
|
||||
|
@ -9,33 +10,45 @@ from wcs import fields, formdef
|
|||
from wcs.formdef import FormDef
|
||||
from wcs.formdata import Evolution
|
||||
|
||||
from utilities import create_temporary_pub
|
||||
from utilities import create_temporary_pub, clean_temporary_pub
|
||||
|
||||
def setup_module(module):
|
||||
cleanup()
|
||||
def pytest_generate_tests(metafunc):
|
||||
if 'pub' in metafunc.fixturenames:
|
||||
metafunc.parametrize('pub', ['pickle', 'sql'], indirect=True)
|
||||
|
||||
global pub, formdef
|
||||
@pytest.fixture
|
||||
def pub(request):
|
||||
pub = create_temporary_pub(sql_mode=(request.param == 'sql'))
|
||||
|
||||
pub = create_temporary_pub()
|
||||
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
|
||||
pub.set_app_dir(req)
|
||||
pub.cfg['identification'] = {'methods': ['password']}
|
||||
pub.cfg['language'] = {'language': 'en'}
|
||||
pub.write_cfg()
|
||||
|
||||
FormDef.wipe()
|
||||
|
||||
global formdef
|
||||
formdef = FormDef()
|
||||
formdef.name = 'foobar'
|
||||
formdef.url_name = 'foobar'
|
||||
formdef.fields = []
|
||||
formdef.store()
|
||||
|
||||
return pub
|
||||
|
||||
|
||||
def teardown_module(module):
|
||||
shutil.rmtree(pub.APP_DIR)
|
||||
clean_temporary_pub()
|
||||
|
||||
|
||||
def test_basic():
|
||||
def test_basic(pub):
|
||||
formdata = formdef.data_class()()
|
||||
substvars = formdata.get_substitution_variables()
|
||||
assert substvars.get('form_status') == 'Unknown'
|
||||
assert substvars.get('form_name') == 'foobar'
|
||||
|
||||
def test_saved():
|
||||
def test_saved(pub):
|
||||
formdef.data_class().wipe()
|
||||
formdata = formdef.data_class()()
|
||||
formdata.store()
|
||||
|
@ -46,16 +59,16 @@ def test_saved():
|
|||
assert substvars.get('form_url_backoffice').endswith('/backoffice/foobar/1/')
|
||||
assert substvars.get('form_status_url').endswith('/foobar/1/status')
|
||||
|
||||
def test_display_id():
|
||||
def test_display_id(pub):
|
||||
formdef.data_class().wipe()
|
||||
formdata = formdef.data_class()()
|
||||
formdata.id_display = 'bar'
|
||||
formdata.store()
|
||||
substvars = formdata.get_substitution_variables()
|
||||
assert substvars.get('form_number') == 'bar'
|
||||
assert substvars.get('form_number_raw') == '1'
|
||||
assert substvars.get('form_number_raw') == str(formdata.id)
|
||||
|
||||
def test_just_created():
|
||||
def test_just_created(pub):
|
||||
formdef.data_class().wipe()
|
||||
formdata = formdef.data_class()()
|
||||
formdata.just_created()
|
||||
|
@ -67,20 +80,21 @@ def test_just_created():
|
|||
assert substvars.get('form_receipt_time')
|
||||
assert substvars.get('form_evolution')
|
||||
|
||||
def test_field():
|
||||
def test_field(pub):
|
||||
formdef.data_class().wipe()
|
||||
formdef.fields = [fields.StringField(id='0', label='string')]
|
||||
formdef.store()
|
||||
formdata = formdef.data_class()()
|
||||
substvars = formdata.get_substitution_variables()
|
||||
assert substvars.get('form_f0') == ''
|
||||
assert 'form_f0' in substvars
|
||||
assert not substvars.get('form_f0')
|
||||
|
||||
formdata.data = {'0': 'test'}
|
||||
substvars = formdata.get_substitution_variables()
|
||||
assert substvars.get('form_f0') == 'test'
|
||||
assert substvars.get('form_field_string') == 'test'
|
||||
|
||||
def test_field_varname():
|
||||
def test_field_varname(pub):
|
||||
formdef.data_class().wipe()
|
||||
formdef.fields = [fields.StringField(id='0', label='string', varname='foo')]
|
||||
formdef.store()
|
||||
|
@ -90,7 +104,7 @@ def test_field_varname():
|
|||
assert substvars.get('form_f0') == 'test'
|
||||
assert substvars.get('form_var_foo') == 'test'
|
||||
|
||||
def test_file_field():
|
||||
def test_file_field(pub):
|
||||
formdef.data_class().wipe()
|
||||
formdef.fields = [fields.FileField(id='0', label='file', varname='foo')]
|
||||
formdef.store()
|
||||
|
@ -104,7 +118,7 @@ def test_file_field():
|
|||
assert substvars.get('form_var_foo_url').endswith('/foobar/1/download?f=0')
|
||||
assert isinstance(substvars.get('form_var_foo_raw'), Upload)
|
||||
|
||||
def test_get_submitter():
|
||||
def test_get_submitter(pub):
|
||||
formdef.data_class().wipe()
|
||||
formdef.fields = [fields.StringField(id='0', label='email', varname='foo',
|
||||
prefill={'type': 'user', 'value': 'email'})]
|
||||
|
@ -122,7 +136,7 @@ def test_get_submitter():
|
|||
formdata.user_id = user.id
|
||||
assert formdef.get_submitter_email(formdata) == 'bar@localhost'
|
||||
|
||||
def test_get_last_update_time():
|
||||
def test_get_last_update_time(pub):
|
||||
formdef.data_class().wipe()
|
||||
formdef.store()
|
||||
|
||||
|
@ -145,7 +159,7 @@ def test_get_last_update_time():
|
|||
formdata.evolution = None
|
||||
assert formdata.last_update_time == formdata.receipt_time
|
||||
|
||||
def test_password_field():
|
||||
def test_password_field(pub):
|
||||
formdef.data_class().wipe()
|
||||
formdef.fields = [fields.PasswordField(id='0', label='pwd')]
|
||||
formdef.store()
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import pytest
|
||||
import hashlib
|
||||
import re
|
||||
import shutil
|
||||
|
@ -6,12 +7,15 @@ from quixote import cleanup
|
|||
from wcs.qommon.http_request import HTTPRequest
|
||||
from wcs.qommon.ident.password_accounts import PasswordAccount
|
||||
|
||||
from utilities import get_app, create_temporary_pub, emails
|
||||
from utilities import get_app, create_temporary_pub, clean_temporary_pub, emails
|
||||
|
||||
def setup_module(module):
|
||||
cleanup()
|
||||
global pub
|
||||
pub = create_temporary_pub()
|
||||
def pytest_generate_tests(metafunc):
|
||||
if 'pub' in metafunc.fixturenames:
|
||||
metafunc.parametrize('pub', ['pickle', 'sql'], indirect=True)
|
||||
|
||||
@pytest.fixture
|
||||
def pub(request):
|
||||
pub = create_temporary_pub(sql_mode=(request.param == 'sql'))
|
||||
|
||||
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
|
||||
pub.set_app_dir(req)
|
||||
|
@ -19,11 +23,12 @@ def setup_module(module):
|
|||
pub.cfg['identities'] = {'creation': 'self'}
|
||||
pub.write_cfg()
|
||||
|
||||
def teardown_module(module):
|
||||
global pub
|
||||
shutil.rmtree(pub.APP_DIR)
|
||||
return pub
|
||||
|
||||
def test_no_user_registration():
|
||||
def teardown_module(module):
|
||||
clean_temporary_pub()
|
||||
|
||||
def test_no_user_registration(pub):
|
||||
# makes sure the page is not published unless configured
|
||||
app = get_app(pub)
|
||||
pub.cfg['identification'] = {'methods': ['password']}
|
||||
|
@ -33,19 +38,19 @@ def test_no_user_registration():
|
|||
pub.cfg['identities'] = {'creation': 'self'}
|
||||
pub.write_cfg()
|
||||
|
||||
def test_link_on_login_page():
|
||||
def test_link_on_login_page(pub):
|
||||
app = get_app(pub)
|
||||
page = app.get('/login/')
|
||||
assert '/register/' in page.body
|
||||
|
||||
def test_no_password():
|
||||
def test_no_password(pub):
|
||||
app = get_app(pub)
|
||||
page = app.get('/register/')
|
||||
register_form = page.forms[0]
|
||||
assert 'username' in register_form.fields
|
||||
assert 'password' not in register_form.fields
|
||||
|
||||
def test_user_registration_mismatch():
|
||||
def test_user_registration_mismatch(pub):
|
||||
pub.cfg['passwords'] = {'generate': False}
|
||||
pub.write_cfg()
|
||||
app = get_app(pub)
|
||||
|
@ -57,7 +62,7 @@ def test_user_registration_mismatch():
|
|||
resp = register_form.submit()
|
||||
assert 'Passwords do not match' in resp.body
|
||||
|
||||
def do_user_registration(username='foo', password='bar'):
|
||||
def do_user_registration(pub, username='foo', password='bar'):
|
||||
initial_user_count = pub.user_class.count()
|
||||
initial_account_count = PasswordAccount.count()
|
||||
app = get_app(pub)
|
||||
|
@ -80,27 +85,27 @@ def do_user_registration(username='foo', password='bar'):
|
|||
user2 = PasswordAccount.get_with_credentials(username, password)
|
||||
assert user.id == user2.id
|
||||
|
||||
def test_user_registration():
|
||||
def test_user_registration(pub):
|
||||
pub.user_class.wipe()
|
||||
PasswordAccount.wipe()
|
||||
pub.cfg['passwords'] = {'generate': False}
|
||||
pub.write_cfg()
|
||||
do_user_registration()
|
||||
do_user_registration(pub)
|
||||
|
||||
account = PasswordAccount.get('foo')
|
||||
assert account.password == 'bar' # check it's in clear text
|
||||
|
||||
def test_user_password_hashing():
|
||||
def test_user_password_hashing(pub):
|
||||
pub.user_class.wipe()
|
||||
PasswordAccount.wipe()
|
||||
pub.cfg['passwords'] = {'generate': False, 'hashing_algo': 'sha256'}
|
||||
pub.write_cfg()
|
||||
do_user_registration()
|
||||
do_user_registration(pub)
|
||||
|
||||
account = PasswordAccount.get('foo')
|
||||
assert account.password == hashlib.sha256('bar').hexdigest()
|
||||
|
||||
def test_admin_notification():
|
||||
def test_admin_notification(pub):
|
||||
pub.cfg['identities'] = {'creation': 'self', 'notify-on-register': True}
|
||||
pub.write_cfg()
|
||||
pub.user_class.wipe()
|
||||
|
@ -112,12 +117,12 @@ def test_admin_notification():
|
|||
|
||||
pub.cfg['passwords'] = {'generate': False}
|
||||
pub.write_cfg()
|
||||
do_user_registration()
|
||||
do_user_registration(pub)
|
||||
|
||||
assert emails.get('New Registration')
|
||||
assert emails.get('New Registration').get('email_rcpt') == ['admin@localhost']
|
||||
|
||||
def test_user_notification():
|
||||
def test_user_notification(pub):
|
||||
pub.cfg['identities'] = {'creation': 'self', 'notify-on-register': False,
|
||||
'email-as-username': True}
|
||||
pub.write_cfg()
|
||||
|
@ -130,7 +135,7 @@ def test_user_notification():
|
|||
|
||||
pub.cfg['passwords'] = {'generate': True}
|
||||
pub.write_cfg()
|
||||
do_user_registration(username='foo@localhost', password=None)
|
||||
do_user_registration(pub, username='foo@localhost', password=None)
|
||||
|
||||
account = PasswordAccount.get('foo@localhost')
|
||||
|
||||
|
@ -138,13 +143,13 @@ def test_user_notification():
|
|||
assert emails.get('Welcome to example.net').get('to') == 'foo@localhost'
|
||||
assert account.password in emails.get('Welcome to example.net').get('payload')
|
||||
|
||||
def test_user_login():
|
||||
def test_user_login(pub):
|
||||
pub.cfg['identities'] = {'creation': 'self', 'notify-on-register': False}
|
||||
pub.user_class.wipe()
|
||||
PasswordAccount.wipe()
|
||||
pub.cfg['passwords'] = {'generate': False, 'hashing_algo': 'sha256'}
|
||||
pub.write_cfg()
|
||||
do_user_registration()
|
||||
do_user_registration(pub)
|
||||
|
||||
# wrong password
|
||||
app = get_app(pub)
|
||||
|
@ -162,13 +167,14 @@ def test_user_login():
|
|||
resp = resp.forms[0].submit()
|
||||
assert resp.location == 'http://example.net/'
|
||||
|
||||
def test_forgotten():
|
||||
def test_forgotten(pub):
|
||||
pub.cfg['identities'] = {'creation': 'self', 'notify-on-register': False}
|
||||
pub.user_class.wipe()
|
||||
PasswordAccount.wipe()
|
||||
pub.cfg['passwords'] = {'generate': False, 'hashing_algo': 'sha256'}
|
||||
pub.write_cfg()
|
||||
do_user_registration()
|
||||
do_user_registration(pub)
|
||||
user_id = pub.user_class.select()[0].id
|
||||
|
||||
app = get_app(pub)
|
||||
resp = app.get('/login/')
|
||||
|
@ -184,7 +190,7 @@ def test_forgotten():
|
|||
resp = resp.forms[0].submit()
|
||||
assert 'There is no user with that name or it has no email contact.'
|
||||
|
||||
user = pub.user_class.get(1)
|
||||
user = pub.user_class.get(user_id)
|
||||
user.email = 'foo@localhost'
|
||||
user.store()
|
||||
|
||||
|
|
Loading…
Reference in New Issue