2013-12-03 16:51:21 +01:00
|
|
|
import pytest
|
2022-07-11 09:34:05 +02:00
|
|
|
from quixote import get_request
|
2021-03-19 14:41:24 +01:00
|
|
|
|
2015-01-19 16:11:20 +01:00
|
|
|
import wcs.forms.root
|
2013-12-03 16:51:21 +01:00
|
|
|
from wcs.categories import Category
|
|
|
|
from wcs.formdef import FormDef
|
2016-06-14 21:52:47 +02:00
|
|
|
from wcs.qommon import sessions
|
|
|
|
from wcs.qommon.http_request import HTTPRequest
|
2014-12-24 10:23:38 +01:00
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
from .utilities import clean_temporary_pub, create_temporary_pub, get_app
|
2013-12-03 16:51:21 +01:00
|
|
|
|
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def pub():
|
2014-12-24 10:23:38 +01:00
|
|
|
pub = create_temporary_pub()
|
2013-12-03 16:51:21 +01:00
|
|
|
|
|
|
|
req = HTTPRequest(None, {'SCRIPT_NAME': '/'})
|
|
|
|
req._user = None
|
|
|
|
pub._set_request(req)
|
|
|
|
req.session = sessions.Session(id=1)
|
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
FormDef.wipe()
|
|
|
|
Category.wipe()
|
|
|
|
pub.user_class.wipe()
|
|
|
|
|
|
|
|
yield pub
|
|
|
|
clean_temporary_pub()
|
|
|
|
|
|
|
|
|
2022-07-11 09:44:42 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def pickle_pub():
|
|
|
|
yield create_temporary_pub(sql_mode=False)
|
|
|
|
clean_temporary_pub()
|
|
|
|
|
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def user1(pub):
|
2013-12-03 16:51:21 +01:00
|
|
|
user1 = pub.user_class(name='user-one-role')
|
|
|
|
user1.id = 'user-one-role'
|
|
|
|
user1.roles = ['role-1']
|
2022-07-11 09:34:05 +02:00
|
|
|
return user1
|
|
|
|
|
2013-12-03 16:51:21 +01:00
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def user2(pub):
|
2013-12-03 16:51:21 +01:00
|
|
|
user2 = pub.user_class(name='user-other-role')
|
|
|
|
user2.id = 'user-other-role'
|
|
|
|
user2.roles = ['role-2']
|
2022-07-11 09:34:05 +02:00
|
|
|
return user2
|
|
|
|
|
2013-12-03 16:51:21 +01:00
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def category(pub):
|
2013-12-03 16:51:21 +01:00
|
|
|
category = Category()
|
|
|
|
category.name = 'category1'
|
|
|
|
category.store()
|
2022-07-11 09:34:05 +02:00
|
|
|
return category
|
2013-12-03 16:51:21 +01:00
|
|
|
|
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def formdef1(pub, category):
|
2013-12-03 16:51:21 +01:00
|
|
|
formdef1 = FormDef()
|
|
|
|
formdef1.category_id = category.id
|
|
|
|
formdef1.name = formdef1.url_name = 'test-formdef-1'
|
|
|
|
formdef1.store()
|
2022-07-11 09:34:05 +02:00
|
|
|
return formdef1
|
|
|
|
|
2013-12-03 16:51:21 +01:00
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def formdef2(pub, category):
|
2013-12-03 16:51:21 +01:00
|
|
|
formdef2 = FormDef()
|
|
|
|
formdef2.category_id = category.id
|
|
|
|
formdef2.name = formdef2.url_name = 'test-formdef-2'
|
|
|
|
formdef2.store()
|
2022-07-11 09:34:05 +02:00
|
|
|
return formdef2
|
2013-12-03 16:51:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def indexhtml(user=None):
|
2022-07-11 09:34:05 +02:00
|
|
|
req = get_request()
|
2022-07-10 11:38:24 +02:00
|
|
|
req._user = user
|
|
|
|
req.session.user = user.id if user else None
|
2013-12-03 16:51:21 +01:00
|
|
|
return str(wcs.forms.root.RootDirectory()._q_index())
|
|
|
|
|
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
def test_empty_site(pub):
|
2013-12-03 16:51:21 +01:00
|
|
|
assert indexhtml() == ''
|
|
|
|
|
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
def test_public_site_anonymous_access(pub, formdef1, formdef2):
|
2013-12-03 16:51:21 +01:00
|
|
|
output = indexhtml()
|
|
|
|
assert 'href="category1/test-formdef-1/"' in output
|
|
|
|
assert 'href="category1/test-formdef-2/"' in output
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
def test_private_site_anonymous_access(pub, formdef1, formdef2):
|
2013-12-03 16:51:21 +01:00
|
|
|
formdef1.roles = formdef2.roles = ['role-1']
|
|
|
|
formdef1.store()
|
|
|
|
formdef2.store()
|
2015-01-19 16:11:20 +01:00
|
|
|
with pytest.raises(wcs.forms.root.errors.AccessUnauthorizedError):
|
2021-04-02 11:15:34 +02:00
|
|
|
indexhtml()
|
2013-12-03 16:51:21 +01:00
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
def test_semi_private_site_anonymous_access(pub, formdef1, formdef2):
|
2013-12-03 16:51:21 +01:00
|
|
|
formdef1.roles = ['role-1']
|
|
|
|
formdef1.store()
|
|
|
|
output = indexhtml()
|
|
|
|
assert 'href="category1/test-formdef-1/"' not in output
|
|
|
|
assert 'href="category1/test-formdef-2/"' in output
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
def test_private_site_authorized_access(pub, formdef1, formdef2, user1):
|
2013-12-03 16:51:21 +01:00
|
|
|
formdef1.roles = formdef2.roles = ['role-1']
|
|
|
|
formdef1.store()
|
|
|
|
formdef2.store()
|
|
|
|
output = indexhtml(user1)
|
|
|
|
assert 'href="category1/test-formdef-1/"' in output
|
|
|
|
assert 'href="category1/test-formdef-2/"' in output
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
def test_private_site_unauthorized_access(pub, formdef1, formdef2, user2):
|
2013-12-03 16:51:21 +01:00
|
|
|
formdef1.roles = formdef2.roles = ['role-1']
|
|
|
|
formdef1.store()
|
|
|
|
formdef2.store()
|
2015-01-19 16:11:20 +01:00
|
|
|
with pytest.raises(wcs.forms.root.errors.AccessUnauthorizedError):
|
2021-04-02 11:15:34 +02:00
|
|
|
indexhtml(user2)
|
2013-12-03 16:51:21 +01:00
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
def test_private_site_semi_authorized_access(pub, formdef1, formdef2, user1):
|
2013-12-03 16:51:21 +01:00
|
|
|
formdef1.roles = ['role-1']
|
|
|
|
formdef2.roles = ['role-2']
|
|
|
|
formdef1.store()
|
|
|
|
formdef2.store()
|
|
|
|
output = indexhtml(user1)
|
|
|
|
assert 'href="category1/test-formdef-1/"' in output
|
|
|
|
assert 'href="category1/test-formdef-2/"' not in output
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
def test_advertized_site_anonymous_access(pub, formdef1, formdef2):
|
2013-12-03 16:51:21 +01:00
|
|
|
formdef1.roles = formdef2.roles = ['role-1']
|
|
|
|
formdef1.always_advertise = True
|
|
|
|
formdef1.store()
|
|
|
|
formdef2.store()
|
|
|
|
output = indexhtml()
|
|
|
|
assert 'href="category1/test-formdef-1/"' in output
|
|
|
|
assert 'href="category1/test-formdef-2/"' not in output
|
|
|
|
assert 'authentication required' in output # locales ?
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
def test_advertized_site_user_access(pub, formdef1, formdef2, user1):
|
2013-12-03 16:51:21 +01:00
|
|
|
formdef1.roles = formdef2.roles = ['role-2']
|
|
|
|
formdef1.always_advertise = True
|
|
|
|
formdef1.store()
|
|
|
|
formdef2.store()
|
|
|
|
output = indexhtml(user1)
|
|
|
|
assert 'href="category1/test-formdef-1/"' in output
|
|
|
|
assert 'href="category1/test-formdef-2/"' not in output
|
|
|
|
assert 'authentication required' in output # locales ?
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
def test_categories_page(pub, category, formdef1):
|
2020-11-15 15:39:01 +01:00
|
|
|
resp = get_app(pub).get('/categories')
|
|
|
|
assert 'href="category1/"' in resp
|
|
|
|
FormDef.wipe()
|
|
|
|
resp = get_app(pub).get('/categories')
|
|
|
|
assert 'href="category1/"' not in resp
|
|
|
|
|
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
def test_static_directories(pub):
|
2016-06-29 08:48:01 +02:00
|
|
|
assert get_app(pub).get('/static/images/feed-icon-10x10.png')
|
2016-06-14 21:52:47 +02:00
|
|
|
assert get_app(pub).get('/static/css/gadjo.css')
|
|
|
|
assert get_app(pub).get('/static/xstatic/jquery.js')
|
|
|
|
assert get_app(pub).get('/static/xstatic/jquery-ui.js')
|
|
|
|
|
2019-11-12 14:16:54 +01:00
|
|
|
assert 'Directory listing denied' in get_app(pub).get('/static/css/').text
|
2016-06-14 21:52:47 +02:00
|
|
|
assert get_app(pub).get('/static/xxx', status=404)
|
2016-07-06 13:54:16 +02:00
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
def test_jquery_debug_mode(pub, formdef1):
|
2016-07-06 13:54:16 +02:00
|
|
|
resp = get_app(pub).get('/category1/test-formdef-1/')
|
2019-11-12 14:16:54 +01:00
|
|
|
assert 'jquery.min.js' in resp.text
|
2016-07-06 13:54:16 +02:00
|
|
|
pub.cfg['debug'] = {'debug_mode': True}
|
|
|
|
pub.write_cfg()
|
|
|
|
resp = get_app(pub).get('/category1/test-formdef-1/')
|
2019-11-12 14:16:54 +01:00
|
|
|
assert 'jquery.js' in resp.text
|
2021-05-15 15:34:16 +02:00
|
|
|
|
|
|
|
|
2022-07-11 09:34:05 +02:00
|
|
|
def test_i18n_js(pub):
|
2021-05-15 15:34:16 +02:00
|
|
|
get_app(pub).get('/i18n.js')
|
2022-07-11 09:44:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_pickle_site(pickle_pub):
|
|
|
|
resp = get_app(pickle_pub).get('/', status=503)
|
|
|
|
assert resp.text == 'Missing database configuration'
|