1 |
|
import shutil
|
2 |
|
|
3 |
1 |
import pytest
|
4 |
|
from quixote import cleanup
|
|
2 |
from quixote import get_request
|
5 |
3 |
|
6 |
4 |
import wcs.forms.root
|
7 |
5 |
from wcs.categories import Category
|
... | ... | |
9 |
7 |
from wcs.qommon import sessions
|
10 |
8 |
from wcs.qommon.http_request import HTTPRequest
|
11 |
9 |
|
12 |
|
from .utilities import create_temporary_pub, get_app
|
13 |
|
|
14 |
|
|
15 |
|
def setup_module(module):
|
16 |
|
cleanup()
|
|
10 |
from .utilities import clean_temporary_pub, create_temporary_pub, get_app
|
17 |
11 |
|
18 |
|
global pub, req
|
19 |
|
global user1, user2
|
20 |
|
global category
|
21 |
12 |
|
|
13 |
@pytest.fixture
|
|
14 |
def pub():
|
22 |
15 |
pub = create_temporary_pub()
|
23 |
16 |
|
24 |
17 |
req = HTTPRequest(None, {'SCRIPT_NAME': '/'})
|
... | ... | |
26 |
19 |
pub._set_request(req)
|
27 |
20 |
req.session = sessions.Session(id=1)
|
28 |
21 |
|
|
22 |
FormDef.wipe()
|
|
23 |
Category.wipe()
|
|
24 |
pub.user_class.wipe()
|
|
25 |
|
|
26 |
yield pub
|
|
27 |
clean_temporary_pub()
|
|
28 |
|
|
29 |
|
|
30 |
@pytest.fixture
|
|
31 |
def user1(pub):
|
29 |
32 |
user1 = pub.user_class(name='user-one-role')
|
30 |
33 |
user1.id = 'user-one-role'
|
31 |
34 |
user1.roles = ['role-1']
|
|
35 |
return user1
|
|
36 |
|
32 |
37 |
|
|
38 |
@pytest.fixture
|
|
39 |
def user2(pub):
|
33 |
40 |
user2 = pub.user_class(name='user-other-role')
|
34 |
41 |
user2.id = 'user-other-role'
|
35 |
42 |
user2.roles = ['role-2']
|
|
43 |
return user2
|
|
44 |
|
36 |
45 |
|
|
46 |
@pytest.fixture
|
|
47 |
def category(pub):
|
37 |
48 |
category = Category()
|
38 |
49 |
category.name = 'category1'
|
39 |
50 |
category.store()
|
|
51 |
return category
|
40 |
52 |
|
41 |
53 |
|
42 |
|
def create_formdef():
|
43 |
|
global formdef1, formdef2
|
44 |
|
|
|
54 |
@pytest.fixture
|
|
55 |
def formdef1(pub, category):
|
45 |
56 |
formdef1 = FormDef()
|
46 |
57 |
formdef1.category_id = category.id
|
47 |
58 |
formdef1.name = formdef1.url_name = 'test-formdef-1'
|
48 |
59 |
formdef1.store()
|
|
60 |
return formdef1
|
|
61 |
|
49 |
62 |
|
|
63 |
@pytest.fixture
|
|
64 |
def formdef2(pub, category):
|
50 |
65 |
formdef2 = FormDef()
|
51 |
66 |
formdef2.category_id = category.id
|
52 |
67 |
formdef2.name = formdef2.url_name = 'test-formdef-2'
|
53 |
68 |
formdef2.store()
|
54 |
|
|
55 |
|
|
56 |
|
def teardown_module(module):
|
57 |
|
shutil.rmtree(pub.APP_DIR)
|
|
69 |
return formdef2
|
58 |
70 |
|
59 |
71 |
|
60 |
72 |
def indexhtml(user=None):
|
|
73 |
req = get_request()
|
61 |
74 |
req._user = user
|
62 |
75 |
req.session.user = user.id if user else None
|
63 |
76 |
return str(wcs.forms.root.RootDirectory()._q_index())
|
64 |
77 |
|
65 |
78 |
|
66 |
|
def test_empty_site():
|
67 |
|
FormDef.wipe()
|
|
79 |
def test_empty_site(pub):
|
68 |
80 |
assert indexhtml() == ''
|
69 |
81 |
|
70 |
82 |
|
71 |
|
def test_public_site_anonymous_access():
|
72 |
|
FormDef.wipe()
|
73 |
|
create_formdef()
|
|
83 |
def test_public_site_anonymous_access(pub, formdef1, formdef2):
|
74 |
84 |
output = indexhtml()
|
75 |
85 |
assert 'href="category1/test-formdef-1/"' in output
|
76 |
86 |
assert 'href="category1/test-formdef-2/"' in output
|
77 |
87 |
|
78 |
88 |
|
79 |
|
def test_private_site_anonymous_access():
|
80 |
|
FormDef.wipe()
|
81 |
|
create_formdef()
|
|
89 |
def test_private_site_anonymous_access(pub, formdef1, formdef2):
|
82 |
90 |
formdef1.roles = formdef2.roles = ['role-1']
|
83 |
91 |
formdef1.store()
|
84 |
92 |
formdef2.store()
|
... | ... | |
86 |
94 |
indexhtml()
|
87 |
95 |
|
88 |
96 |
|
89 |
|
def test_semi_private_site_anonymous_access():
|
90 |
|
FormDef.wipe()
|
91 |
|
create_formdef()
|
|
97 |
def test_semi_private_site_anonymous_access(pub, formdef1, formdef2):
|
92 |
98 |
formdef1.roles = ['role-1']
|
93 |
99 |
formdef1.store()
|
94 |
100 |
output = indexhtml()
|
... | ... | |
96 |
102 |
assert 'href="category1/test-formdef-2/"' in output
|
97 |
103 |
|
98 |
104 |
|
99 |
|
def test_private_site_authorized_access():
|
100 |
|
FormDef.wipe()
|
101 |
|
create_formdef()
|
|
105 |
def test_private_site_authorized_access(pub, formdef1, formdef2, user1):
|
102 |
106 |
formdef1.roles = formdef2.roles = ['role-1']
|
103 |
107 |
formdef1.store()
|
104 |
108 |
formdef2.store()
|
... | ... | |
107 |
111 |
assert 'href="category1/test-formdef-2/"' in output
|
108 |
112 |
|
109 |
113 |
|
110 |
|
def test_private_site_unauthorized_access():
|
111 |
|
FormDef.wipe()
|
112 |
|
create_formdef()
|
|
114 |
def test_private_site_unauthorized_access(pub, formdef1, formdef2, user2):
|
113 |
115 |
formdef1.roles = formdef2.roles = ['role-1']
|
114 |
116 |
formdef1.store()
|
115 |
117 |
formdef2.store()
|
... | ... | |
117 |
119 |
indexhtml(user2)
|
118 |
120 |
|
119 |
121 |
|
120 |
|
def test_private_site_semi_authorized_access():
|
121 |
|
FormDef.wipe()
|
122 |
|
create_formdef()
|
|
122 |
def test_private_site_semi_authorized_access(pub, formdef1, formdef2, user1):
|
123 |
123 |
formdef1.roles = ['role-1']
|
124 |
124 |
formdef2.roles = ['role-2']
|
125 |
125 |
formdef1.store()
|
... | ... | |
129 |
129 |
assert 'href="category1/test-formdef-2/"' not in output
|
130 |
130 |
|
131 |
131 |
|
132 |
|
def test_advertized_site_anonymous_access():
|
133 |
|
FormDef.wipe()
|
134 |
|
create_formdef()
|
|
132 |
def test_advertized_site_anonymous_access(pub, formdef1, formdef2):
|
135 |
133 |
formdef1.roles = formdef2.roles = ['role-1']
|
136 |
134 |
formdef1.always_advertise = True
|
137 |
135 |
formdef1.store()
|
... | ... | |
142 |
140 |
assert 'authentication required' in output # locales ?
|
143 |
141 |
|
144 |
142 |
|
145 |
|
def test_advertized_site_user_access():
|
146 |
|
FormDef.wipe()
|
147 |
|
create_formdef()
|
|
143 |
def test_advertized_site_user_access(pub, formdef1, formdef2, user1):
|
148 |
144 |
formdef1.roles = formdef2.roles = ['role-2']
|
149 |
145 |
formdef1.always_advertise = True
|
150 |
146 |
formdef1.store()
|
... | ... | |
155 |
151 |
assert 'authentication required' in output # locales ?
|
156 |
152 |
|
157 |
153 |
|
158 |
|
def test_categories_page():
|
159 |
|
FormDef.wipe()
|
160 |
|
create_formdef()
|
|
154 |
def test_categories_page(pub, category, formdef1):
|
161 |
155 |
resp = get_app(pub).get('/categories')
|
162 |
156 |
assert 'href="category1/"' in resp
|
163 |
157 |
FormDef.wipe()
|
... | ... | |
165 |
159 |
assert 'href="category1/"' not in resp
|
166 |
160 |
|
167 |
161 |
|
168 |
|
def test_static_directories():
|
|
162 |
def test_static_directories(pub):
|
169 |
163 |
assert get_app(pub).get('/static/images/feed-icon-10x10.png')
|
170 |
164 |
assert get_app(pub).get('/static/css/gadjo.css')
|
171 |
165 |
assert get_app(pub).get('/static/xstatic/jquery.js')
|
... | ... | |
175 |
169 |
assert get_app(pub).get('/static/xxx', status=404)
|
176 |
170 |
|
177 |
171 |
|
178 |
|
def test_jquery_debug_mode():
|
179 |
|
FormDef.wipe()
|
180 |
|
create_formdef()
|
|
172 |
def test_jquery_debug_mode(pub, formdef1):
|
181 |
173 |
resp = get_app(pub).get('/category1/test-formdef-1/')
|
182 |
174 |
assert 'jquery.min.js' in resp.text
|
183 |
175 |
pub.cfg['debug'] = {'debug_mode': True}
|
... | ... | |
186 |
178 |
assert 'jquery.js' in resp.text
|
187 |
179 |
|
188 |
180 |
|
189 |
|
def test_i18n_js():
|
|
181 |
def test_i18n_js(pub):
|
190 |
182 |
get_app(pub).get('/i18n.js')
|
191 |
|
-
|