1
|
import os
|
2
|
import shutil
|
3
|
import time
|
4
|
|
5
|
import pytest
|
6
|
|
7
|
from quixote import cleanup, get_publisher
|
8
|
from wcs.qommon import errors, sessions
|
9
|
from wcs.qommon.ident.password_accounts import PasswordAccount
|
10
|
from wcs.qommon.http_request import HTTPRequest
|
11
|
from wcs.qommon.template import get_current_theme
|
12
|
from wcs.categories import Category
|
13
|
from wcs.workflows import Workflow
|
14
|
from wcs.formdef import FormDef
|
15
|
from wcs import fields
|
16
|
|
17
|
from utilities import get_app, login, create_temporary_pub, clean_temporary_pub
|
18
|
|
19
|
|
20
|
def setup_module(module):
|
21
|
cleanup()
|
22
|
|
23
|
global pub
|
24
|
|
25
|
pub = create_temporary_pub()
|
26
|
|
27
|
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
|
28
|
pub.set_app_dir(req)
|
29
|
pub.cfg['identification'] = {'methods': ['password']}
|
30
|
pub.write_cfg()
|
31
|
|
32
|
|
33
|
def teardown_module(module):
|
34
|
clean_temporary_pub()
|
35
|
|
36
|
|
37
|
def create_user():
|
38
|
if pub.user_class.get_users_with_name_identifier('user'):
|
39
|
return pub.user_class.get_users_with_name_identifier('user')[0]
|
40
|
user1 = pub.user_class(name='user')
|
41
|
user1.name_identifiers = ['user']
|
42
|
user1.is_admin = False
|
43
|
user1.store()
|
44
|
|
45
|
account1 = PasswordAccount(id='user')
|
46
|
account1.set_password('user')
|
47
|
account1.user_id = user1.id
|
48
|
account1.store()
|
49
|
|
50
|
pub.cfg['identification'] = {'methods': ['password']}
|
51
|
pub.write_cfg()
|
52
|
|
53
|
return user1
|
54
|
|
55
|
|
56
|
def create_formdef():
|
57
|
FormDef.wipe()
|
58
|
formdef = FormDef()
|
59
|
formdef.name = 'test'
|
60
|
formdef.fields = []
|
61
|
formdef.store()
|
62
|
return formdef
|
63
|
|
64
|
|
65
|
def teardown_module(module):
|
66
|
shutil.rmtree(pub.APP_DIR)
|
67
|
|
68
|
|
69
|
def test_with_user():
|
70
|
user = create_user()
|
71
|
app = login(get_app(pub), username='user', password='user')
|
72
|
resp = app.get('/', status=200)
|
73
|
resp = app.get('/myspace/', status=200)
|
74
|
|
75
|
|
76
|
def test_myspace_with_user_forms():
|
77
|
user = create_user()
|
78
|
formdef = create_formdef()
|
79
|
|
80
|
cat = Category(name='cat')
|
81
|
cat.store()
|
82
|
formdef.category_id = cat.id
|
83
|
formdef.enable_tracking_codes = True
|
84
|
|
85
|
wf = Workflow(name='status')
|
86
|
st1 = wf.add_status('Status1', 'st1')
|
87
|
wf.store()
|
88
|
formdef.workflow_id = wf.id
|
89
|
formdef.store()
|
90
|
|
91
|
formdef.data_class().wipe()
|
92
|
formdata = formdef.data_class()()
|
93
|
formdata.user_id = user.id
|
94
|
formdata.status = 'wf-st1'
|
95
|
formdata.receipt_time = time.localtime()
|
96
|
formdata.data = {}
|
97
|
formdata.store()
|
98
|
draft = formdef.data_class()()
|
99
|
draft.user_id = user.id
|
100
|
draft.status = 'draft'
|
101
|
draft.receipt_time = time.localtime()
|
102
|
draft.data = {}
|
103
|
draft.store()
|
104
|
|
105
|
app = login(get_app(pub), username='user', password='user')
|
106
|
resp = app.get('/myspace/')
|
107
|
assert formdata.id != draft.id
|
108
|
assert '<a href="/test/%s/"' % formdata.id in resp
|
109
|
assert '<a href="/test/%s"' % draft.id in resp
|
110
|
resp = app.get('/test/%s/' % formdata.id, status=302)
|
111
|
assert resp.location == 'http://example.net/cat/test/%s/' % formdata.id
|
112
|
resp = app.get('/test/%s/' % draft.id, status=302)
|
113
|
resp = resp.follow(status=302)
|
114
|
assert resp.location.startswith('http://example.net/cat/test/?mt=')
|
115
|
resp = resp.follow(status=200)
|
116
|
|
117
|
# disable formdef: formdatas are still visible and accessible, drafts are not
|
118
|
formdef.disabled = True
|
119
|
formdef.store()
|
120
|
resp = app.get('/myspace/')
|
121
|
assert formdata.id != draft.id
|
122
|
assert '<a href="/test/%s/"' % formdata.id in resp
|
123
|
assert not '<a href="/test/%s"' % draft.id in resp
|
124
|
resp = app.get('/cat/test/%s' % formdata.id)
|
125
|
resp.status_int = 200
|
126
|
resp = app.get('/cat/test/%s/' % draft.id, status=302)
|
127
|
resp = resp.follow(status=403)
|
128
|
|
129
|
|
130
|
def test_form_category_redirection():
|
131
|
Category.wipe()
|
132
|
cat = Category(name='baz')
|
133
|
cat.store()
|
134
|
|
135
|
FormDef.wipe()
|
136
|
formdef = FormDef()
|
137
|
formdef.name = 'foobar'
|
138
|
formdef.category_id = cat.id
|
139
|
formdef.fields = []
|
140
|
formdef.store()
|
141
|
|
142
|
# check we get a redirection to /category/formdef/
|
143
|
resp = get_app(pub).get('/foobar/')
|
144
|
assert resp.location.endswith('/baz/foobar/')
|
145
|
|
146
|
# check missing trailing slashs are ok
|
147
|
resp = get_app(pub).get('/foobar')
|
148
|
assert resp.location.endswith('/baz/foobar')
|
149
|
resp = resp.follow()
|
150
|
assert resp.location.endswith('/baz/foobar/')
|
151
|
|
152
|
# even if there's a query string
|
153
|
resp = get_app(pub).get('/foobar?test=1')
|
154
|
assert resp.location.endswith('/baz/foobar?test=1')
|
155
|
resp = resp.follow()
|
156
|
assert resp.location.endswith('/baz/foobar/')
|
157
|
|
158
|
# check direct category access without trailing slash
|
159
|
resp = get_app(pub).get('/baz')
|
160
|
assert resp.location.endswith('/baz/')
|
161
|
resp = get_app(pub).get('/baz?test=1')
|
162
|
assert resp.location.endswith('/baz/')
|
163
|
|
164
|
|
165
|
def test_form_and_category_same_slug():
|
166
|
Category.wipe()
|
167
|
cat = Category(name='foobar')
|
168
|
cat.store()
|
169
|
|
170
|
FormDef.wipe()
|
171
|
formdef = FormDef()
|
172
|
formdef.name = 'foobar'
|
173
|
formdef.category_id = cat.id
|
174
|
formdef.fields = []
|
175
|
formdef.store()
|
176
|
|
177
|
# check we get to the form, not the category
|
178
|
resp = get_app(pub).get('/foobar/')
|
179
|
assert resp.form
|
180
|
|
181
|
|
182
|
def test_form_and_category_same_slug2():
|
183
|
Category.wipe()
|
184
|
cat = Category(name='test')
|
185
|
cat.store()
|
186
|
|
187
|
FormDef.wipe()
|
188
|
formdef = FormDef()
|
189
|
formdef.name = 'foobar'
|
190
|
formdef.category_id = cat.id
|
191
|
formdef.fields = []
|
192
|
formdef.store()
|
193
|
|
194
|
formdef = FormDef()
|
195
|
formdef.name = 'test'
|
196
|
formdef.fields = []
|
197
|
formdef.store()
|
198
|
|
199
|
# check we get to the foobar form
|
200
|
resp = get_app(pub).get('/foobar/')
|
201
|
assert resp.location == 'http://example.net/test/foobar/'
|
202
|
resp = resp.follow()
|
203
|
assert resp.form
|
204
|
assert '<h3>foobar</h3>' in resp
|
205
|
|
206
|
# check we get to the test form
|
207
|
resp = get_app(pub).get('/test/')
|
208
|
assert resp.form
|
209
|
assert '<h3>test</h3>' in resp
|