2 |
2 |
|
3 |
3 |
import pytest
|
4 |
4 |
|
|
5 |
import copy
|
5 |
6 |
import json
|
6 |
7 |
import re
|
7 |
|
import requests
|
8 |
|
import subprocess
|
9 |
|
import shutil
|
10 |
|
import sys
|
11 |
|
import tempfile
|
12 |
|
import time
|
13 |
|
import os
|
14 |
8 |
|
15 |
9 |
from django.apps import apps
|
16 |
10 |
from django.conf import settings
|
17 |
11 |
from django.core.cache import cache
|
18 |
12 |
from django.urls import reverse
|
19 |
13 |
from django.db import connection
|
20 |
|
from django.test import override_settings
|
21 |
14 |
from django.test.client import RequestFactory
|
22 |
15 |
from django.test.utils import CaptureQueriesContext
|
23 |
16 |
from django.utils.six.moves.urllib import parse as urlparse
|
... | ... | |
27 |
20 |
from combo.data.library import get_cell_classes
|
28 |
21 |
from combo.data.models import CellBase, LinkListCell, Page, ValidityInfo
|
29 |
22 |
from combo.apps.search.engines import engines
|
30 |
|
from combo.apps.wcs.models import (WcsFormCell, WcsCurrentFormsCell,
|
31 |
|
WcsFormsOfCategoryCell, WcsCurrentDraftsCell, WcsCategoryCell,
|
32 |
|
TrackingCodeInputCell, BackofficeSubmissionCell, WcsCareFormsCell)
|
|
23 |
from combo.apps.wcs.models import (
|
|
24 |
WcsFormCell, WcsCurrentFormsCell,
|
|
25 |
WcsFormsOfCategoryCell, WcsCurrentDraftsCell, WcsCategoryCell,
|
|
26 |
TrackingCodeInputCell, BackofficeSubmissionCell, WcsCareFormsCell)
|
33 |
27 |
|
34 |
|
from combo.apps.search.models import SearchCell, IndexedCell
|
|
28 |
from combo.apps.search.models import SearchCell
|
35 |
29 |
from combo.apps.search.utils import index_site, search_site
|
36 |
30 |
from django.contrib.auth.models import AnonymousUser
|
37 |
|
from django.test.client import RequestFactory
|
38 |
31 |
|
39 |
32 |
from combo.utils import NothingInCacheException
|
40 |
33 |
|
... | ... | |
42 |
35 |
|
43 |
36 |
pytestmark = pytest.mark.django_db
|
44 |
37 |
|
45 |
|
wcs_present = pytest.mark.skipif('WCS_MANAGE' not in os.environ or not os.path.exists(os.environ['WCS_MANAGE']),
|
46 |
|
reason='WCS_MANAGE not defined in environment')
|
47 |
|
|
48 |
|
WCS_MANAGE = os.environ.get('WCS_MANAGE')
|
49 |
|
|
50 |
|
WCS_SCRIPTS = {
|
51 |
|
'setup-auth': """
|
52 |
|
from quixote import get_publisher
|
53 |
|
|
54 |
|
get_publisher().cfg['identification'] = {'methods': ['password']}
|
55 |
|
get_publisher().cfg['debug'] = {'display_exceptions': 'text'}
|
56 |
|
get_publisher().write_cfg()
|
57 |
|
""",
|
58 |
|
'create-user': """
|
59 |
|
from quixote import get_publisher
|
60 |
|
from qommon.ident.password_accounts import PasswordAccount
|
61 |
|
|
62 |
|
user = get_publisher().user_class()
|
63 |
|
user.name = 'foo bar'
|
64 |
|
user.email = 'foo@example.net'
|
65 |
|
user.store()
|
66 |
|
account = PasswordAccount(id='user')
|
67 |
|
account.set_password('user')
|
68 |
|
account.user_id = user.id
|
69 |
|
account.store()
|
70 |
|
""",
|
71 |
|
'create-data': """
|
72 |
|
import datetime
|
73 |
|
from quixote import get_publisher
|
74 |
|
|
75 |
|
from wcs.categories import Category
|
76 |
|
from wcs.formdef import FormDef
|
77 |
|
from wcs.roles import Role
|
78 |
|
from wcs import fields
|
79 |
|
|
80 |
|
cats = []
|
81 |
|
for i in range(1, 10):
|
82 |
|
cat = Category()
|
83 |
|
cat.name = 'Test %d' % i
|
84 |
|
cat.description = 'Hello world'
|
85 |
|
cat.store()
|
86 |
|
cats.append(cat)
|
87 |
|
|
88 |
|
formdef = FormDef()
|
89 |
|
formdef.name = 'form title'
|
90 |
|
formdef.category_id = cat.id
|
91 |
|
formdef.keywords = 'foo, bar'
|
92 |
|
formdef.fields = [
|
93 |
|
fields.StringField(id='1', label='1st field', type='string'),
|
94 |
|
fields.ItemField(id='2', label='2nd field', type='item',
|
95 |
|
items=['foo', 'bar', 'baz']),
|
|
38 |
|
|
39 |
WCS_FORMDEFS_DATA = [
|
|
40 |
{'slug': 'form-title', 'title': 'form title', 'url': '/form-title/', 'keywords': ['foo', 'bar']},
|
|
41 |
{'slug': 'a-second-form-title', 'title': 'a second form title', 'url': '/a-second-form-title/'},
|
|
42 |
{'slug': 'a-private-form', 'title': 'a private form', 'url': '/a-private-form/'},
|
|
43 |
{'slug': 'third-form-title', 'title': 'third form title', 'url': '/third-form-title/'},
|
|
44 |
]
|
|
45 |
|
|
46 |
WCS_CATEGORIES_DATA = [
|
|
47 |
{'slug': 'test-%s' % i, 'title': 'Test %s' % i, 'url': '/test-%s/' % i}
|
|
48 |
for i in [3, 9]
|
|
49 |
]
|
|
50 |
|
|
51 |
WCS_CATEGORIES_FORMDEFS_DATA = [
|
|
52 |
{'slug': 'form-title', 'title': 'form title', 'url': '/form-title/', 'keywords': ['foo', 'bar'], 'count': 42},
|
|
53 |
{'slug': 'a-second-form-title', 'title': 'a second form title', 'url': '/a-second-form-title/', 'count': 35},
|
|
54 |
]
|
|
55 |
|
|
56 |
WCS_USER_FORMS_DATA = [
|
|
57 |
{'name': 'name', 'title': 'Title', 'url': '/form/1/', 'form_receipt_datetime': '2015-01-01T00:00:00', 'readable': True,
|
|
58 |
'category_slug': 'test-9'},
|
|
59 |
{'name': 'name', 'url': '/form/2/', 'form_receipt_datetime': '2015-01-01T00:00:00', 'readable': True},
|
|
60 |
{'name': 'name', 'title': 'Title', 'url': '/form/3/', 'form_receipt_datetime': '2015-01-01T00:00:00'},
|
|
61 |
{'name': 'name', 'title': 'Title', 'url': '/form/4/', 'form_receipt_datetime': '2015-01-01T00:00:00', 'readable': True,
|
|
62 |
'form_status_is_endpoint': True, 'category_slug': 'test-9'},
|
|
63 |
]
|
|
64 |
|
|
65 |
WCS_FORMS_DATA = [
|
|
66 |
{
|
|
67 |
'form_receipt_datetime': '2019-10-17T16:46:03',
|
|
68 |
'form_url_backoffice': '/backoffice/management/foobar/1/',
|
|
69 |
},
|
|
70 |
{
|
|
71 |
'form_receipt_datetime': '2019-10-17T16:46:04',
|
|
72 |
'form_url_backoffice': '/backoffice/management/foobar/2/',
|
|
73 |
},
|
|
74 |
]
|
|
75 |
|
|
76 |
WCS_USER_DRAFTS_DATA = [
|
|
77 |
{'name': 'name', 'title': 'Title', 'url': '/form/1/', 'form_receipt_datetime': '2015-01-01T00:00:00', 'category_slug': 'test-9'},
|
|
78 |
{'name': 'name', 'url': '/form/2/', 'form_receipt_datetime': '2015-01-01T00:00:00'},
|
|
79 |
{'name': 'name', 'title': 'Title', 'url': '/form/3/', 'form_receipt_datetime': '2015-01-01T00:00:00'},
|
|
80 |
{'name': 'name', 'title': 'Title', 'url': '/form/4/', 'form_receipt_datetime': '2015-01-01T00:00:00',
|
|
81 |
'form_status_is_endpoint': True, 'category_slug': 'test-9'},
|
96 |
82 |
]
|
97 |
|
formdef.store()
|
98 |
|
|
99 |
|
user = get_publisher().user_class.select()[0]
|
100 |
|
|
101 |
|
for i in range(50):
|
102 |
|
formdata = formdef.data_class()()
|
103 |
|
formdata.just_created()
|
104 |
|
formdata.receipt_time = datetime.datetime(2015, 1, 1).timetuple()
|
105 |
|
formdata.data = {'1': 'FOO BAR %d' % i}
|
106 |
|
if i%4 == 0:
|
107 |
|
formdata.data['2'] = 'foo'
|
108 |
|
formdata.data['2_display'] = 'foo'
|
109 |
|
elif i%4 == 1:
|
110 |
|
formdata.data['2'] = 'bar'
|
111 |
|
formdata.data['2_display'] = 'bar'
|
112 |
|
else:
|
113 |
|
formdata.data['2'] = 'baz'
|
114 |
|
formdata.data['2_display'] = 'baz'
|
115 |
|
if i%3 == 0:
|
116 |
|
formdata.jump_status('new')
|
117 |
|
else:
|
118 |
|
formdata.jump_status('finished')
|
119 |
|
if i%7 == 0:
|
120 |
|
formdata.user_id = user.id
|
121 |
|
formdata.store()
|
122 |
|
|
123 |
|
# another formdef in same category
|
124 |
|
formdef = FormDef()
|
125 |
|
formdef.name = 'a second form title'
|
126 |
|
formdef.category_id = cat.id
|
127 |
|
formdef.fields = []
|
128 |
|
formdef.store()
|
129 |
|
|
130 |
|
# a formdef in another category
|
131 |
|
formdef = FormDef()
|
132 |
|
formdef.name = 'third form title'
|
133 |
|
formdef.category_id = cats[2].id
|
134 |
|
formdef.fields = []
|
135 |
|
formdef.enable_tracking_codes = True
|
136 |
|
formdef.store()
|
137 |
|
|
138 |
|
# a draft of that formdef
|
139 |
|
formdata = formdef.data_class()()
|
140 |
|
formdata.data = {}
|
141 |
|
formdata.page_no = 1
|
142 |
|
formdata.status = 'draft'
|
143 |
|
formdata.receipt_time = datetime.datetime(2015, 1, 1).timetuple()
|
144 |
|
formdata.user_id = user.id
|
145 |
|
formdata.store()
|
146 |
|
|
147 |
|
if '127.0.0.2' in get_publisher().get_frontoffice_url():
|
148 |
|
# create a tracking code on second website only
|
149 |
|
code = get_publisher().tracking_code_class()
|
150 |
|
code.id = 'CNPHNTFB'
|
151 |
|
code.formdata = formdata
|
152 |
|
|
153 |
|
# a private formdef
|
154 |
|
role = Role(name='Blah')
|
155 |
|
role.store()
|
156 |
|
|
157 |
|
formdef = FormDef()
|
158 |
|
formdef.name = 'a private form'
|
159 |
|
formdef.category_id = cats[2].id
|
160 |
|
formdef.roles = [role.id]
|
161 |
|
formdef.backoffice_submission_roles = [role.id]
|
162 |
|
formdef.fields = []
|
163 |
|
formdef.store()
|
164 |
|
|
165 |
|
user2 = get_publisher().user_class() # agent
|
166 |
|
user2.name = 'foo2 bar2'
|
167 |
|
user2.email = 'foo2@example.net'
|
168 |
|
user2.roles = [role.id]
|
169 |
|
user2.store()
|
170 |
|
""",
|
171 |
|
|
172 |
|
}
|
173 |
|
|
174 |
|
WCS_DIR = tempfile.mkdtemp()
|
175 |
|
WCS_PID = None
|
176 |
83 |
|
177 |
84 |
|
178 |
85 |
class MockUser(object):
|
... | ... | |
191 |
98 |
return 'xyz'
|
192 |
99 |
|
193 |
100 |
|
194 |
|
def run_wcs_script(script, hostname):
|
195 |
|
script_path = os.path.join(WCS_DIR, script + '.py')
|
196 |
|
fd = open(script_path, 'w')
|
197 |
|
fd.write(WCS_SCRIPTS[script])
|
198 |
|
fd.close()
|
199 |
|
subprocess.check_call([sys.executable, WCS_MANAGE, 'runscript', '--app-dir', WCS_DIR,
|
200 |
|
'--vhost', hostname, script_path],
|
201 |
|
env={'DJANGO_SETTINGS_MODULE': 'wcs.settings'})
|
202 |
|
|
203 |
|
|
204 |
|
def setup_module(module):
|
205 |
|
global WCS_PID
|
206 |
|
|
207 |
|
if not WCS_MANAGE:
|
208 |
|
return
|
209 |
|
|
210 |
|
for hostname in ['127.0.0.1', '127.0.0.2']:
|
211 |
|
os.mkdir(os.path.join(WCS_DIR, hostname))
|
212 |
|
|
213 |
|
run_wcs_script('setup-auth', hostname)
|
214 |
|
run_wcs_script('create-user', hostname)
|
215 |
|
run_wcs_script('create-data', hostname)
|
216 |
|
|
217 |
|
fd = open(os.path.join(WCS_DIR, hostname, 'site-options.cfg'), 'w')
|
218 |
|
fd.write('''[api-secrets]
|
219 |
|
combo = combo
|
220 |
|
''')
|
221 |
|
fd.close()
|
|
101 |
class MockedRequestResponse(mock.Mock):
|
|
102 |
status_code = 200
|
222 |
103 |
|
223 |
|
with open(os.path.join(WCS_DIR, 'wcs.cfg'), 'w') as fd:
|
224 |
|
fd.write('''[main]
|
225 |
|
app_dir = %s\n''' % WCS_DIR)
|
|
104 |
def json(self):
|
|
105 |
return json.loads(self.content)
|
226 |
106 |
|
227 |
|
with open(os.path.join(WCS_DIR, 'local_settings.py'), 'w') as fd:
|
228 |
|
fd.write('''
|
229 |
|
WCS_LEGACY_CONFIG_FILE = '%s/wcs.cfg'
|
230 |
|
THEMES_DIRECTORY = '/'
|
231 |
|
ALLOWED_HOSTS = ['127.0.0.1', '127.0.0.2']
|
232 |
|
''' % WCS_DIR)
|
233 |
107 |
|
234 |
|
WCS_PID = os.fork()
|
235 |
|
if not WCS_PID:
|
236 |
|
os.chdir(os.path.dirname(WCS_MANAGE))
|
237 |
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'wcs.settings'
|
238 |
|
os.environ['WCS_SETTINGS_FILE'] = os.path.join(WCS_DIR, 'local_settings.py')
|
239 |
|
os.execvp('python', [sys.executable, 'manage.py', 'runserver', '--noreload', '0.0.0.0:8999'])
|
240 |
|
sys.exit(0)
|
|
108 |
def get_data_from_url(url):
|
|
109 |
if '/api/formdefs/' in url:
|
|
110 |
return WCS_FORMDEFS_DATA
|
|
111 |
if '/api/categories/' in url:
|
|
112 |
if '/formdefs/' in url:
|
|
113 |
return WCS_CATEGORIES_FORMDEFS_DATA
|
|
114 |
return WCS_CATEGORIES_DATA
|
|
115 |
if '/api/user/forms' in url:
|
|
116 |
return WCS_USER_FORMS_DATA
|
|
117 |
if '/api/forms' in url:
|
|
118 |
return WCS_FORMS_DATA
|
|
119 |
if '/api/user/drafts' in url:
|
|
120 |
return WCS_USER_DRAFTS_DATA
|
|
121 |
return []
|
241 |
122 |
|
242 |
|
time.sleep(5)
|
243 |
123 |
|
244 |
|
def teardown_module(module):
|
245 |
|
if WCS_PID:
|
246 |
|
os.kill(WCS_PID, 9)
|
247 |
|
shutil.rmtree(WCS_DIR)
|
|
124 |
def mocked_requests_send(request, **kwargs):
|
|
125 |
request_url = urlparse.urlparse(request.url)
|
|
126 |
data = copy.deepcopy(get_data_from_url(request_url.path))
|
|
127 |
for elem in data:
|
|
128 |
for key in ['url', 'form_url_backoffice']:
|
|
129 |
if key not in elem:
|
|
130 |
continue
|
|
131 |
elem_url = elem[key]
|
|
132 |
elem[key] = '{scheme}://{netloc}{url}'.format(scheme=request_url.scheme, netloc=request_url.netloc, url=elem_url)
|
248 |
133 |
|
249 |
|
|
250 |
|
def check_wcs_open(url):
|
251 |
|
session = requests.Session()
|
252 |
|
scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
|
253 |
|
path = '/login/'
|
254 |
|
login_url = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
|
255 |
|
resp = session.post(login_url, data={'username': 'user', 'password': 'user'})
|
256 |
|
resp = session.get(url)
|
257 |
|
assert resp.status_code == 200
|
|
134 |
return MockedRequestResponse(content=json.dumps({'data': data}))
|
258 |
135 |
|
259 |
136 |
|
260 |
137 |
@pytest.fixture
|
... | ... | |
264 |
141 |
ctx['request'].session = {}
|
265 |
142 |
return ctx
|
266 |
143 |
|
267 |
|
@wcs_present
|
268 |
|
def test_form_cell_setup():
|
|
144 |
|
|
145 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
146 |
def test_form_cell_setup(mock_send):
|
269 |
147 |
cell = WcsFormCell()
|
270 |
148 |
form_class = cell.get_default_form_class()
|
271 |
149 |
form = form_class()
|
... | ... | |
280 |
158 |
(u'other:third-form-title', u'test2 : third form title')]
|
281 |
159 |
|
282 |
160 |
|
283 |
|
@wcs_present
|
284 |
|
def test_form_cell_save_cache():
|
|
161 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
162 |
def test_form_cell_save_cache(mock_send):
|
285 |
163 |
page = Page(title='xxx', slug='test_form_cell_save_cache', template_name='standard')
|
286 |
164 |
page.save()
|
287 |
165 |
cell = WcsFormCell(page=page, placeholder='content', order=0)
|
... | ... | |
291 |
169 |
assert cell.cached_title == 'form title'
|
292 |
170 |
assert cell.get_additional_label() == 'form title'
|
293 |
171 |
# make sure cached attributes are removed from serialized pages
|
294 |
|
assert not 'cached_' in json.dumps(page.get_serialized_page())
|
|
172 |
assert 'cached_' not in json.dumps(page.get_serialized_page())
|
295 |
173 |
|
296 |
174 |
# check content provided to search engine
|
297 |
175 |
assert cell.render_for_search() == ''
|
... | ... | |
309 |
187 |
assert WcsFormCell.objects.get(id=cell.id).cached_title == 'form title'
|
310 |
188 |
|
311 |
189 |
|
312 |
|
@wcs_present
|
313 |
190 |
def test_form_cell_validity():
|
314 |
191 |
page = Page.objects.create(title='xxx', slug='test_form_cell_save_cache', template_name='standard')
|
315 |
192 |
cell = WcsFormCell.objects.create(page=page, placeholder='content', order=0)
|
... | ... | |
335 |
212 |
assert validity_info.invalid_since is not None
|
336 |
213 |
|
337 |
214 |
|
338 |
|
@wcs_present
|
339 |
215 |
def test_form_cell_load():
|
340 |
216 |
page = Page(title='xxx', slug='test_form_cell_save_cache', template_name='standard')
|
341 |
217 |
page.save()
|
... | ... | |
353 |
229 |
assert cell.cached_title == 'form title'
|
354 |
230 |
|
355 |
231 |
|
356 |
|
@wcs_present
|
357 |
|
def test_category_cell_save_cache():
|
|
232 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
233 |
def test_category_cell_save_cache(mock_send):
|
358 |
234 |
page = Page(title='xxx', slug='test_category_cell_save_cache', template_name='standard')
|
359 |
235 |
page.save()
|
360 |
236 |
cell = WcsCategoryCell(page=page, placeholder='content', order=0)
|
... | ... | |
365 |
241 |
assert cell.get_additional_label() == 'Test 3'
|
366 |
242 |
|
367 |
243 |
|
368 |
|
@wcs_present
|
369 |
244 |
def test_category_cell_validity():
|
370 |
245 |
page = Page.objects.create(title='xxx', slug='test_category_cell_save_cache', template_name='standard')
|
371 |
246 |
cell = WcsCategoryCell.objects.create(page=page, placeholder='content', order=0)
|
... | ... | |
391 |
266 |
assert validity_info.invalid_since is not None
|
392 |
267 |
|
393 |
268 |
|
394 |
|
@wcs_present
|
395 |
|
def test_form_cell_render():
|
|
269 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
270 |
def test_form_cell_render(mock_send):
|
396 |
271 |
page = Page(title='xxx', slug='test_form_cell_render', template_name='standard')
|
397 |
272 |
page.save()
|
398 |
273 |
cell = WcsFormCell(page=page, placeholder='content', order=0)
|
... | ... | |
402 |
277 |
assert 'http://127.0.0.1:8999/form-title/tryauth' in result
|
403 |
278 |
assert 'form title' in result
|
404 |
279 |
|
405 |
|
@wcs_present
|
406 |
|
def test_current_forms_cell_setup():
|
|
280 |
|
|
281 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
282 |
def test_current_forms_cell_setup(mock_send):
|
407 |
283 |
cell = WcsCurrentFormsCell()
|
408 |
284 |
form_class = cell.get_default_form_class()
|
409 |
285 |
form = form_class()
|
... | ... | |
423 |
299 |
cell.done_forms = True
|
424 |
300 |
assert cell.get_additional_label() == 'All Sites - Done Forms'
|
425 |
301 |
|
426 |
|
@wcs_present
|
427 |
|
def test_current_forms_cell_render(context):
|
|
302 |
|
|
303 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
304 |
def test_current_forms_cell_render(mock_send, context):
|
428 |
305 |
page = Page(title='xxx', slug='test_current_forms_cell_render', template_name='standard')
|
429 |
306 |
page.save()
|
430 |
307 |
cell = WcsCurrentFormsCell(page=page, placeholder='content', order=0)
|
... | ... | |
437 |
314 |
with pytest.raises(NothingInCacheException):
|
438 |
315 |
result = cell.render(context)
|
439 |
316 |
|
440 |
|
context['synchronous'] = True # to get fresh content
|
|
317 |
context['synchronous'] = True # to get fresh content
|
441 |
318 |
|
442 |
319 |
# default is to get current forms from all wcs sites
|
443 |
320 |
result = cell.render(context)
|
444 |
|
assert 'http://127.0.0.1:8999/form-title/1/' in result
|
445 |
|
assert 'http://127.0.0.1:8999/form-title/22/' in result
|
446 |
|
assert not 'http://127.0.0.1:8999/form-title/8/' in result
|
447 |
|
assert 'http://127.0.0.2:8999/form-title/1/' in result
|
448 |
|
assert 'http://127.0.0.2:8999/form-title/22/' in result
|
|
321 |
assert 'http://127.0.0.1:8999/form/1/' in result
|
|
322 |
assert 'http://127.0.0.1:8999/form/2/' not in result # no title
|
|
323 |
assert 'http://127.0.0.1:8999/form/3/' not in result # not readable
|
|
324 |
assert 'http://127.0.0.1:8999/form/4/' not in result # done
|
|
325 |
assert 'http://127.0.0.2:8999/form/1/' in result
|
|
326 |
assert 'http://127.0.0.2:8999/form/2/' not in result
|
|
327 |
assert 'http://127.0.0.2:8999/form/3/' not in result
|
|
328 |
assert 'http://127.0.0.2:8999/form/4/' not in result
|
449 |
329 |
|
450 |
330 |
# done forms
|
451 |
331 |
cell.current_forms = False
|
... | ... | |
453 |
333 |
cell.include_drafts = False
|
454 |
334 |
cell.save()
|
455 |
335 |
result = cell.render(context)
|
456 |
|
assert not 'http://127.0.0.1:8999/form-title/1/' in result
|
457 |
|
assert 'http://127.0.0.1:8999/form-title/8/' in result
|
458 |
|
|
459 |
|
data = cell.get_data(context)
|
460 |
|
assert data['default']['data'][0]['site_slug'] == 'default'
|
461 |
|
assert data['other']['data'][0]['site_slug'] == 'other'
|
462 |
|
|
463 |
|
# check flat list
|
464 |
|
extra_context = cell.get_cell_extra_context(context)
|
465 |
|
assert len(extra_context['forms']) == 10
|
466 |
|
assert len([x for x in extra_context['forms'] if x['site_slug'] == 'default']) == 5
|
467 |
|
assert len([x for x in extra_context['forms'] if x['site_slug'] == 'other']) == 5
|
|
336 |
assert 'http://127.0.0.1:8999/form/1/' not in result # wip
|
|
337 |
assert 'http://127.0.0.1:8999/form/4/' in result # done
|
|
338 |
assert 'http://127.0.0.2:8999/form/1/' not in result # wip
|
|
339 |
assert 'http://127.0.0.2:8999/form/4/' in result # done
|
468 |
340 |
|
469 |
341 |
# limit to a category
|
470 |
342 |
cell.categories = {'data': ['default:test-3']}
|
... | ... | |
472 |
344 |
assert len(extra_context['forms']) == 0
|
473 |
345 |
cell.categories = {'data': ['default:test-9']}
|
474 |
346 |
extra_context = cell.get_cell_extra_context(context)
|
475 |
|
assert len(extra_context['forms']) == 5
|
|
347 |
assert len(extra_context['forms']) == 1
|
476 |
348 |
|
477 |
349 |
# check both category limit and all forms
|
478 |
350 |
cell.current_forms = True
|
479 |
351 |
cell.done_forms = True
|
480 |
352 |
extra_context = cell.get_cell_extra_context(context)
|
481 |
|
assert len(extra_context['forms']) == 8
|
|
353 |
assert len(extra_context['forms']) == 2
|
482 |
354 |
|
483 |
355 |
# check both category limit and no forms
|
484 |
356 |
cell.current_forms = False
|
... | ... | |
516 |
388 |
assert 'There are no done forms' in result
|
517 |
389 |
|
518 |
390 |
|
519 |
|
@wcs_present
|
520 |
|
def test_current_forms_cell_validity(context):
|
|
391 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
392 |
def test_current_forms_cell_validity(mock_send, context):
|
521 |
393 |
page = Page.objects.create(title='xxx', slug='test_current_forms_cell_render', template_name='standard')
|
522 |
394 |
cell = WcsCurrentFormsCell.objects.create(page=page, placeholder='content', order=0)
|
523 |
395 |
context['request'].user = MockUser()
|
... | ... | |
533 |
405 |
assert ValidityInfo.objects.exists() is False
|
534 |
406 |
|
535 |
407 |
|
536 |
|
@wcs_present
|
537 |
|
def test_current_forms_cell_check_validity(context):
|
|
408 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
409 |
def test_current_forms_cell_check_validity(mock_send, context):
|
538 |
410 |
page = Page.objects.create(title='xxx', slug='test_current_forms_cell_render', template_name='standard')
|
539 |
411 |
cell = WcsCurrentFormsCell.objects.create(page=page, placeholder='content', order=0)
|
540 |
412 |
|
... | ... | |
564 |
436 |
assert validity_info.invalid_since is not None
|
565 |
437 |
|
566 |
438 |
|
567 |
|
@wcs_present
|
568 |
|
def test_current_forms_cell_render_single_site(context):
|
|
439 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
440 |
def test_current_forms_cell_render_single_site(mock_send, context):
|
569 |
441 |
page = Page(title='xxx', slug='test_current_forms_cell_render', template_name='standard')
|
570 |
442 |
page.save()
|
571 |
443 |
cell = WcsCurrentFormsCell(page=page, placeholder='content', order=0)
|
... | ... | |
579 |
451 |
with pytest.raises(NothingInCacheException):
|
580 |
452 |
result = cell.render(context)
|
581 |
453 |
|
582 |
|
context['synchronous'] = True # to get fresh content
|
|
454 |
context['synchronous'] = True # to get fresh content
|
583 |
455 |
|
584 |
456 |
result = cell.render(context)
|
585 |
|
assert 'http://127.0.0.1:8999/form-title/1/' in result
|
586 |
|
assert 'http://127.0.0.1:8999/form-title/22/' in result
|
587 |
|
assert 'http://127.0.0.2:8999/form-title/1/' not in result
|
588 |
|
assert 'http://127.0.0.2:8999/form-title/22/' not in result
|
|
457 |
assert 'http://127.0.0.1:8999/form/1/' in result
|
|
458 |
assert 'http://127.0.0.2:8999/form/1/' not in result
|
589 |
459 |
|
590 |
|
@wcs_present
|
591 |
|
def test_current_forms_unknown_name_id(caplog, context):
|
|
460 |
|
|
461 |
def test_current_forms_unknown_name_id(context):
|
592 |
462 |
page = Page(title='xxx', slug='test_current_forms_cell_render', template_name='standard')
|
593 |
463 |
page.save()
|
594 |
464 |
cell = WcsCurrentFormsCell(page=page, placeholder='content', order=0)
|
... | ... | |
600 |
470 |
# query should fail as nothing is cached
|
601 |
471 |
cache.clear()
|
602 |
472 |
with pytest.raises(NothingInCacheException):
|
603 |
|
result = cell.render(context)
|
|
473 |
cell.render(context)
|
604 |
474 |
|
605 |
|
context['synchronous'] = True # to get fresh content
|
|
475 |
context['synchronous'] = True # to get fresh content
|
606 |
476 |
|
607 |
|
result = cell.render(context)
|
608 |
|
assert 'http://127.0.0.1:8999/' not in result
|
609 |
|
assert len(caplog.records) == 0
|
|
477 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
478 |
mock_json = mock.Mock(status_code=200)
|
|
479 |
requests_get.return_value = mock_json
|
|
480 |
cell.render(context)
|
|
481 |
assert requests_get.call_args_list[0][0][0] == '/api/users/xyz/forms?limit=100&sort=desc'
|
610 |
482 |
|
611 |
483 |
|
612 |
|
@wcs_present
|
613 |
484 |
def test_care_forms_cell_setup():
|
614 |
485 |
cell = WcsCareFormsCell()
|
615 |
486 |
form_class = cell.get_default_form_class()
|
... | ... | |
621 |
492 |
assert cell.get_additional_label() == 'test'
|
622 |
493 |
|
623 |
494 |
|
624 |
|
@wcs_present
|
625 |
|
def test_care_forms_cell_render(context):
|
|
495 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
496 |
def test_care_forms_cell_render(mock_send, context):
|
626 |
497 |
page = Page(title='xxx', slug='test_care_forms_cell_render', template_name='standard')
|
627 |
498 |
page.save()
|
628 |
499 |
cell = WcsCareFormsCell(page=page, placeholder='content', order=0)
|
... | ... | |
637 |
508 |
|
638 |
509 |
context['synchronous'] = True # to get fresh content
|
639 |
510 |
|
640 |
|
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
641 |
|
response1 = {'err': 0, 'data': [
|
642 |
|
{
|
643 |
|
'form_receipt_datetime': '2019-10-17T16:46:03',
|
644 |
|
'form_url_backoffice': 'http://127.0.0.1:8999/backoffice/management/foobar/1/',
|
645 |
|
},
|
646 |
|
{
|
647 |
|
'form_receipt_datetime': '2019-10-17T16:46:04',
|
648 |
|
'form_url_backoffice': 'http://127.0.0.1:8999/backoffice/management/foobar/2/',
|
649 |
|
},
|
650 |
|
]}
|
651 |
|
response2 = {'err': 0, 'data': [
|
652 |
|
{
|
653 |
|
'form_receipt_datetime': '2019-10-17T16:46:05',
|
654 |
|
'form_url_backoffice': 'http://127.0.0.2:8999/backoffice/management/foobar/42/',
|
655 |
|
},
|
656 |
|
]}
|
657 |
|
mock_json = mock.Mock(status_code=200)
|
658 |
|
mock_json.json.side_effect = [response1, response2]
|
659 |
|
requests_get.return_value = mock_json
|
660 |
|
|
661 |
|
result = cell.render(context)
|
|
511 |
result = cell.render(context)
|
662 |
512 |
|
663 |
513 |
assert 'http://127.0.0.1:8999/backoffice/management/foobar/1' in result
|
664 |
514 |
assert 'http://127.0.0.1:8999/backoffice/management/foobar/2' in result
|
665 |
515 |
assert '"http://127.0.0.1:8999/backoffice/management/"' in result
|
666 |
|
assert 'http://127.0.0.2:8999/backoffice/management/foobar/42' in result
|
|
516 |
assert 'http://127.0.0.2:8999/backoffice/management/foobar/1' in result
|
|
517 |
assert 'http://127.0.0.2:8999/backoffice/management/foobar/2' in result
|
667 |
518 |
assert '"http://127.0.0.2:8999/backoffice/management/"' in result
|
668 |
519 |
|
669 |
520 |
data = cell.get_data(context)
|
... | ... | |
671 |
522 |
assert 'other' in data
|
672 |
523 |
|
673 |
524 |
|
674 |
|
@wcs_present
|
675 |
525 |
def test_care_forms_cell_validity(context):
|
676 |
526 |
page = Page.objects.create(title='xxx', slug='test_care_forms_cell_render', template_name='standard')
|
677 |
527 |
cell = WcsCareFormsCell.objects.create(page=page, placeholder='content', order=0)
|
... | ... | |
698 |
548 |
assert validity_info.invalid_since is not None
|
699 |
549 |
|
700 |
550 |
|
701 |
|
@wcs_present
|
702 |
|
def test_care_forms_cell_render_single_site(context):
|
|
551 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
552 |
def test_care_forms_cell_render_single_site(mock_send, context):
|
703 |
553 |
page = Page(title='xxx', slug='test_care_forms_cell_render', template_name='standard')
|
704 |
554 |
page.save()
|
705 |
555 |
cell = WcsCareFormsCell(page=page, placeholder='content', order=0)
|
... | ... | |
724 |
574 |
assert 'other' not in data
|
725 |
575 |
|
726 |
576 |
|
727 |
|
@wcs_present
|
728 |
|
def test_forms_of_category_cell_setup():
|
|
577 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
578 |
def test_forms_of_category_cell_setup(mock_send):
|
729 |
579 |
cell = WcsFormsOfCategoryCell()
|
730 |
580 |
form_class = cell.get_default_form_class()
|
731 |
581 |
form = form_class()
|
... | ... | |
736 |
586 |
(u'other:test-9', u'test2 : Test 9')]
|
737 |
587 |
|
738 |
588 |
|
739 |
|
@wcs_present
|
740 |
|
def test_forms_of_category_cell_render(context):
|
|
589 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
590 |
def test_forms_of_category_cell_render(mock_send, context):
|
741 |
591 |
page = Page(title='xxx', slug='test_forms_of_category_cell_render', template_name='standard')
|
742 |
592 |
page.save()
|
743 |
593 |
cell = WcsFormsOfCategoryCell(page=page, placeholder='content', order=0)
|
... | ... | |
785 |
635 |
assert len(list(cell.get_external_links_data())) == 2
|
786 |
636 |
|
787 |
637 |
# existing category, but empty
|
788 |
|
cell.category_reference = 'default:test-1'
|
789 |
|
cell.save()
|
790 |
|
result = cell.render(context)
|
791 |
|
assert '<h2>' not in result
|
792 |
|
context['combo_display_even_empty_categories'] = True
|
793 |
|
result = cell.render(context)
|
794 |
|
assert '<h2>' in result
|
795 |
|
context.pop('combo_display_even_empty_categories')
|
|
638 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
639 |
mock_json = mock.Mock(status_code=200)
|
|
640 |
requests_get.return_value = mock_json
|
|
641 |
cell.render(context)
|
|
642 |
result = cell.render(context)
|
|
643 |
assert '<h2>' not in result
|
|
644 |
context['combo_display_even_empty_categories'] = True
|
|
645 |
result = cell.render(context)
|
|
646 |
assert '<h2>' in result
|
|
647 |
context.pop('combo_display_even_empty_categories')
|
796 |
648 |
|
797 |
649 |
|
798 |
|
@wcs_present
|
799 |
650 |
def test_forms_of_category_cell_validity(context):
|
800 |
651 |
page = Page.objects.create(title='xxx', slug='test_forms_of_category_cell_render', template_name='standard')
|
801 |
652 |
cell = WcsFormsOfCategoryCell.objects.create(page=page, placeholder='content', order=0)
|
... | ... | |
836 |
687 |
assert validity_info.invalid_since is not None
|
837 |
688 |
|
838 |
689 |
|
839 |
|
@wcs_present
|
840 |
690 |
def test_forms_of_category_cell_check_validity(context):
|
841 |
691 |
page = Page.objects.create(title='xxx', slug='test_forms_of_category_cell_render', template_name='standard')
|
842 |
692 |
cell = WcsFormsOfCategoryCell.objects.create(page=page, placeholder='content', order=0)
|
... | ... | |
856 |
706 |
assert validity_info.invalid_since is not None
|
857 |
707 |
|
858 |
708 |
|
859 |
|
@wcs_present
|
860 |
709 |
def test_current_drafts_cell_render_unlogged(context):
|
861 |
710 |
page = Page(title='xxx', slug='test_current_drafts_cell_render', template_name='standard')
|
862 |
711 |
page.save()
|
863 |
712 |
cell = WcsCurrentDraftsCell(page=page, placeholder='content', order=0)
|
864 |
713 |
cell.save()
|
865 |
|
context['synchronous'] = True # to get fresh content
|
866 |
|
result = cell.render(context)
|
867 |
|
assert not 'http://127.0.0.1:8999/third-form-title' in result # no form
|
|
714 |
context['synchronous'] = True # to get fresh content
|
|
715 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
716 |
mock_json = mock.Mock(status_code=200)
|
|
717 |
requests_get.return_value = mock_json
|
|
718 |
result = cell.render(context)
|
|
719 |
assert requests_get.call_args_list[0][0][0] == '/api/user/drafts'
|
|
720 |
assert 'http://127.0.0.1:8999/third-form-title' not in result # no form
|
868 |
721 |
|
869 |
722 |
|
870 |
|
@wcs_present
|
871 |
|
def test_current_drafts_cell_render_logged_in(context):
|
|
723 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
724 |
def test_current_drafts_cell_render_logged_in(mock_send, context):
|
872 |
725 |
page = Page(title='xxx', slug='test_current_drafts_cell_render', template_name='standard')
|
873 |
726 |
page.save()
|
874 |
727 |
cell = WcsCurrentDraftsCell(page=page, placeholder='content', order=0)
|
875 |
728 |
cell.save()
|
876 |
|
context['synchronous'] = True # to get fresh content
|
|
729 |
context['synchronous'] = True # to get fresh content
|
877 |
730 |
|
878 |
731 |
context['request'].user = MockUser()
|
879 |
732 |
|
880 |
733 |
# default is to get current forms from all wcs sites
|
881 |
734 |
result = cell.render(context)
|
882 |
|
assert 'http://127.0.0.1:8999/third-form-title/1' in result
|
883 |
|
for url in re.findall(r'href=[\'"]?([^\'" >]+)', result):
|
884 |
|
check_wcs_open(url)
|
|
735 |
assert 'http://127.0.0.1:8999/form/1/' in result
|
|
736 |
assert 'http://127.0.0.1:8999/form/2/' not in result # no title
|
|
737 |
assert 'http://127.0.0.1:8999/form/3/' in result
|
|
738 |
assert 'http://127.0.0.1:8999/form/4/' not in result # done
|
|
739 |
assert 'http://127.0.0.2:8999/form/1/' in result
|
|
740 |
assert 'http://127.0.0.2:8999/form/2/' not in result
|
|
741 |
assert 'http://127.0.0.2:8999/form/3/' in result
|
|
742 |
assert 'http://127.0.0.2:8999/form/4/' not in result
|
885 |
743 |
|
886 |
744 |
# check flat list
|
887 |
745 |
extra_context = cell.get_cell_extra_context(context)
|
888 |
|
assert len(extra_context['drafts']) == 2
|
889 |
|
assert len([x for x in extra_context['drafts'] if x['site_slug'] == 'default']) == 1
|
890 |
|
assert len([x for x in extra_context['drafts'] if x['site_slug'] == 'other']) == 1
|
|
746 |
assert len(extra_context['drafts']) == 8
|
|
747 |
assert len([x for x in extra_context['drafts'] if x['site_slug'] == 'default']) == 4
|
|
748 |
assert len([x for x in extra_context['drafts'] if x['site_slug'] == 'other']) == 4
|
891 |
749 |
|
892 |
750 |
# limit to a category
|
893 |
751 |
cell.categories = {'data': ['default:test-3']}
|
894 |
752 |
extra_context = cell.get_cell_extra_context(context)
|
895 |
|
assert len(extra_context['drafts']) == 1
|
|
753 |
assert len(extra_context['drafts']) == 0
|
896 |
754 |
cell.categories = {'data': ['default:test-9']}
|
897 |
755 |
extra_context = cell.get_cell_extra_context(context)
|
898 |
|
assert len(extra_context['drafts']) == 0
|
|
756 |
assert len(extra_context['drafts']) == 2
|
899 |
757 |
|
900 |
758 |
# check empty message
|
901 |
|
result = cell.render(context)
|
|
759 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
760 |
mock_json = mock.Mock(status_code=200)
|
|
761 |
requests_get.return_value = mock_json
|
|
762 |
result = cell.render(context)
|
902 |
763 |
assert 'There are no current drafts.' in result
|
903 |
764 |
|
|
765 |
context['request'].user = MockUserWithNameId()
|
|
766 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
767 |
mock_json = mock.Mock(status_code=200)
|
|
768 |
requests_get.return_value = mock_json
|
|
769 |
cell.render(context)
|
|
770 |
assert requests_get.call_args_list[0][0][0] == '/api/users/xyz/drafts'
|
904 |
771 |
|
905 |
|
@wcs_present
|
906 |
|
def test_current_drafts_cell_check_validity(context):
|
|
772 |
|
|
773 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
774 |
def test_current_drafts_cell_check_validity(mock_send, context):
|
907 |
775 |
page = Page.objects.create(title='xxx', slug='test_current_forms_cell_render', template_name='standard')
|
908 |
776 |
cell = WcsCurrentDraftsCell.objects.create(page=page, placeholder='content', order=0)
|
909 |
777 |
|
... | ... | |
933 |
801 |
assert validity_info.invalid_since is not None
|
934 |
802 |
|
935 |
803 |
|
936 |
|
@wcs_present
|
937 |
|
def test_manager_forms_of_category_cell(app, admin_user):
|
938 |
|
Page.objects.all().delete()
|
|
804 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
805 |
def test_manager_forms_of_category_cell(mock_send, app, admin_user):
|
939 |
806 |
page = Page(title='One', slug='one', template_name='standard')
|
940 |
807 |
page.save()
|
941 |
808 |
app = login(app)
|
... | ... | |
953 |
820 |
resp = resp.forms[0].submit()
|
954 |
821 |
assert resp.status_int == 302
|
955 |
822 |
|
956 |
|
@wcs_present
|
|
823 |
|
957 |
824 |
def test_manager_current_forms(app, admin_user):
|
958 |
|
Page.objects.all().delete()
|
959 |
825 |
page = Page(title='One', slug='one', template_name='standard')
|
960 |
826 |
page.save()
|
961 |
827 |
app = login(app)
|
... | ... | |
990 |
856 |
settings.KNOWN_SERVICES = temp_settings
|
991 |
857 |
|
992 |
858 |
|
993 |
|
@wcs_present
|
994 |
859 |
def test_tracking_code_cell(app, nocache):
|
995 |
|
Page.objects.all().delete()
|
996 |
860 |
page = Page(title='One', slug='index', template_name='standard')
|
997 |
861 |
page.save()
|
998 |
862 |
cell = TrackingCodeInputCell(page=page, placeholder='content', order=0)
|
... | ... | |
1000 |
864 |
|
1001 |
865 |
resp = app.get('/')
|
1002 |
866 |
resp.form['code'] = 'FOOBAR'
|
1003 |
|
resp = resp.form.submit()
|
|
867 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
868 |
mock_json = mock.Mock(status_code=200)
|
|
869 |
requests_get.return_value = mock_json
|
|
870 |
resp = resp.form.submit()
|
|
871 |
assert len(requests_get.call_args_list) == 2
|
|
872 |
assert requests_get.call_args_list[0][0][0] == '/api/code/FOOBAR'
|
|
873 |
assert requests_get.call_args_list[1][0][0] == '/api/code/FOOBAR'
|
|
874 |
remote_service_urls = [c[1]['remote_service']['url'] for c in requests_get.call_args_list]
|
|
875 |
assert set(remote_service_urls) == set(['http://127.0.0.1:8999/', 'http://127.0.0.2:8999/'])
|
1004 |
876 |
assert resp.status_code == 302
|
1005 |
877 |
resp = resp.follow()
|
1006 |
878 |
assert '<li class="error">The tracking code could not been found.</li>' in resp.text
|
1007 |
879 |
|
1008 |
880 |
resp = app.get('/')
|
1009 |
881 |
resp.form['code'] = 'FOO?BAR?bad<code>'
|
1010 |
|
resp = resp.form.submit()
|
|
882 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
883 |
mock_json = mock.Mock(status_code=200)
|
|
884 |
requests_get.return_value = mock_json
|
|
885 |
resp = resp.form.submit()
|
|
886 |
assert requests_get.call_args_list[0][0][0] == '/api/code/FOO%3FBAR%3FBAD%3CCODE%3E'
|
1011 |
887 |
assert resp.status_code == 302
|
1012 |
888 |
resp = resp.follow()
|
1013 |
889 |
assert '<li class="error">The tracking code could not been found.</li>' in resp.text
|
1014 |
890 |
|
1015 |
891 |
resp = app.get('/')
|
1016 |
892 |
resp.form['code'] = 'CNPHNTFB'
|
1017 |
|
resp = resp.form.submit()
|
|
893 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
894 |
requests_get.return_value = MockedRequestResponse(content=json.dumps({'err': 0, 'load_url': 'http://127.0.0.2:8999/code/CNPHNTFB/load'}))
|
|
895 |
resp = resp.form.submit()
|
1018 |
896 |
assert resp.status_code == 302
|
1019 |
897 |
assert resp.location == 'http://127.0.0.2:8999/code/CNPHNTFB/load'
|
1020 |
898 |
|
1021 |
899 |
# space/case
|
1022 |
900 |
resp = app.get('/')
|
1023 |
901 |
resp.form['code'] = ' cnphntfb'
|
1024 |
|
resp = resp.form.submit()
|
|
902 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
903 |
requests_get.side_effect = [
|
|
904 |
mock.Mock(status_code=200),
|
|
905 |
MockedRequestResponse(content=json.dumps({'err': 0, 'load_url': 'http://127.0.0.2:8999/code/CNPHNTFB/load'}))
|
|
906 |
]
|
|
907 |
resp = resp.form.submit()
|
|
908 |
assert requests_get.call_args_list[0][0][0] == '/api/code/CNPHNTFB'
|
|
909 |
assert requests_get.call_args_list[1][0][0] == '/api/code/CNPHNTFB'
|
1025 |
910 |
assert resp.status_code == 302
|
1026 |
911 |
assert resp.location == 'http://127.0.0.2:8999/code/CNPHNTFB/load'
|
1027 |
912 |
|
... | ... | |
1030 |
915 |
cell.save()
|
1031 |
916 |
resp = app.get('/')
|
1032 |
917 |
resp.form['code'] = 'CNPHNTFB'
|
1033 |
|
resp = resp.form.submit()
|
|
918 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
919 |
mock_json = mock.Mock(status_code=200)
|
|
920 |
requests_get.return_value = mock_json
|
|
921 |
resp = resp.form.submit()
|
|
922 |
assert len(requests_get.call_args_list) == 1
|
|
923 |
assert requests_get.call_args_list[0][1]['remote_service']['url'] == 'http://127.0.0.1:8999/'
|
1034 |
924 |
resp = resp.follow()
|
1035 |
925 |
assert '<li class="error">The tracking code could not been found.</li>' in resp.text
|
1036 |
926 |
|
... | ... | |
1038 |
928 |
resp = app.get('/')
|
1039 |
929 |
resp.form['url'] = 'http://example.org/'
|
1040 |
930 |
resp.form['code'] = 'CNPHNTFB'
|
1041 |
|
resp = resp.form.submit()
|
|
931 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
932 |
mock_json = mock.Mock(status_code=200)
|
|
933 |
requests_get.return_value = mock_json
|
|
934 |
resp = resp.form.submit()
|
1042 |
935 |
assert resp.location == 'http://example.org/?unknown-tracking-code'
|
1043 |
936 |
|
1044 |
937 |
resp = app.get('/')
|
1045 |
938 |
resp.form['url'] = 'http://example.org/?foo=bar'
|
1046 |
939 |
resp.form['code'] = 'CNPHNTFB'
|
1047 |
|
resp = resp.form.submit()
|
|
940 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
941 |
mock_json = mock.Mock(status_code=200)
|
|
942 |
requests_get.return_value = mock_json
|
|
943 |
resp = resp.form.submit()
|
1048 |
944 |
assert resp.location == 'http://example.org/?foo=bar&unknown-tracking-code'
|
1049 |
945 |
|
1050 |
946 |
# redirect to an unknown site
|
... | ... | |
1067 |
963 |
resp = app.post(reverse('wcs-tracking-code'), params={'cell': cell.id}, status=400)
|
1068 |
964 |
|
1069 |
965 |
|
1070 |
|
@wcs_present
|
1071 |
|
def test_cell_assets(settings, app, admin_user):
|
|
966 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
967 |
def test_cell_assets(mock_send, settings, app, admin_user):
|
1072 |
968 |
page = Page.objects.create(title='xxx', slug='test_cell_assets', template_name='standard')
|
1073 |
969 |
cell1 = WcsFormCell.objects.create(page=page, placeholder='content', order=0, formdef_reference=u'default:form-title')
|
1074 |
970 |
|
... | ... | |
1117 |
1013 |
assert u'>Picture — %s (test)<' % cell1.get_label_for_asset() in resp.text
|
1118 |
1014 |
|
1119 |
1015 |
|
1120 |
|
@wcs_present
|
1121 |
|
def test_tracking_code_search(app, nocache):
|
|
1016 |
def test_tracking_code_search(settings, app, nocache):
|
1122 |
1017 |
settings.TEMPLATE_VARS['is_portal_agent'] = True
|
1123 |
|
assert len(app.get('/api/search/tracking-code/').json.get('data')) == 0
|
1124 |
|
assert app.get('/api/search/tracking-code/').json.get('err') == 0
|
1125 |
|
assert len(app.get('/api/search/tracking-code/?q=123').json.get('data')) == 0
|
1126 |
|
assert len(app.get('/api/search/tracking-code/?q=BBCCDFF').json.get('data')) == 0
|
1127 |
|
assert len(app.get('/api/search/tracking-code/?q=BBCCDDFF').json.get('data')) == 0
|
1128 |
|
assert len(app.get('/api/search/tracking-code/?q=CNPHNTFB').json.get('data')) == 1
|
1129 |
|
assert len(app.get('/api/search/tracking-code/?q=BBCCDDFFG').json.get('data')) == 0
|
1130 |
|
assert len(app.get('/api/search/tracking-code/?q= cnphntfb').json.get('data')) == 1
|
1131 |
|
|
1132 |
|
@wcs_present
|
1133 |
|
def test_tracking_code_search_rate_limit(app):
|
|
1018 |
|
|
1019 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
1020 |
result = app.get('/api/search/tracking-code/').json
|
|
1021 |
assert len(result.get('data')) == 0
|
|
1022 |
assert result.get('err') == 0
|
|
1023 |
assert requests_get.called is False # no code
|
|
1024 |
|
|
1025 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
1026 |
assert len(app.get('/api/search/tracking-code/?q=123').json.get('data')) == 0
|
|
1027 |
assert requests_get.called is False # no letters
|
|
1028 |
|
|
1029 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
1030 |
assert len(app.get('/api/search/tracking-code/?q=BBCCDFF').json.get('data')) == 0
|
|
1031 |
assert requests_get.called is False # too short
|
|
1032 |
|
|
1033 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
1034 |
mock_json = mock.Mock(status_code=200)
|
|
1035 |
requests_get.return_value = mock_json
|
|
1036 |
assert len(app.get('/api/search/tracking-code/?q=BBCCDDFF').json.get('data')) == 0
|
|
1037 |
assert len(requests_get.call_args_list) == 2
|
|
1038 |
assert requests_get.call_args_list[0][0][0] == '/api/code/BBCCDDFF'
|
|
1039 |
assert requests_get.call_args_list[1][0][0] == '/api/code/BBCCDDFF'
|
|
1040 |
remote_service_urls = [c[1]['remote_service']['url'] for c in requests_get.call_args_list]
|
|
1041 |
assert set(remote_service_urls) == set(['http://127.0.0.1:8999/', 'http://127.0.0.2:8999/'])
|
|
1042 |
|
|
1043 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
1044 |
requests_get.return_value = MockedRequestResponse(content=json.dumps({'err': 0, 'load_url': 'http://127.0.0.2:8999/code/CNPHNTFB/load'}))
|
|
1045 |
assert len(app.get('/api/search/tracking-code/?q=CNPHNTFB').json.get('data')) == 1
|
|
1046 |
assert requests_get.call_args_list[0][0][0] == '/api/code/CNPHNTFB'
|
|
1047 |
|
|
1048 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
1049 |
assert len(app.get('/api/search/tracking-code/?q=BBCCDDFFG').json.get('data')) == 0
|
|
1050 |
assert requests_get.called is False # too long
|
|
1051 |
|
|
1052 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
1053 |
requests_get.side_effect = [
|
|
1054 |
mock.Mock(status_code=200),
|
|
1055 |
MockedRequestResponse(content=json.dumps({'err': 0, 'load_url': 'http://127.0.0.2:8999/code/CNPHNTFB/load'}))
|
|
1056 |
]
|
|
1057 |
assert len(app.get('/api/search/tracking-code/?q= cnphntfb').json.get('data')) == 1
|
|
1058 |
assert requests_get.call_args_list[0][0][0] == '/api/code/CNPHNTFB'
|
|
1059 |
|
|
1060 |
|
|
1061 |
def test_tracking_code_search_rate_limit(settings, app):
|
1134 |
1062 |
settings.TEMPLATE_VARS['is_portal_agent'] = True
|
1135 |
|
for i in range(3):
|
1136 |
|
assert app.get('/api/search/tracking-code/?q=BBCCDDFF').json.get('err') == 0
|
1137 |
|
for i in range(3): # make sure we hit ratelimit
|
1138 |
|
app.get('/api/search/tracking-code/?q=BBCCDDFF')
|
|
1063 |
settings.WCS_TRACKING_CODE_RATE_LIMIT = '0/s'
|
1139 |
1064 |
assert app.get('/api/search/tracking-code/?q=BBCCDDFF').json.get('err') == 1
|
1140 |
1065 |
|
1141 |
|
Page.objects.all().delete()
|
1142 |
1066 |
page = Page(title='One', slug='index', template_name='standard')
|
1143 |
1067 |
page.save()
|
1144 |
1068 |
cell = TrackingCodeInputCell(page=page, placeholder='content', order=0)
|
1145 |
1069 |
cell.save()
|
1146 |
1070 |
|
1147 |
1071 |
resp = app.get('/')
|
1148 |
|
for i in range(3): # make sure we hit ratelimit
|
1149 |
|
app.get('/api/search/tracking-code/?q=BBCCDDFF')
|
1150 |
1072 |
resp.form['code'] = 'FOOBAR'
|
1151 |
1073 |
resp = resp.form.submit()
|
1152 |
1074 |
assert resp.status_code == 302
|
... | ... | |
1154 |
1076 |
assert '<li class="error">Looking up tracking code is currently rate limited.</li>' in resp.text
|
1155 |
1077 |
|
1156 |
1078 |
resp = app.get('/')
|
1157 |
|
for i in range(3): # make sure we hit ratelimit
|
1158 |
|
app.get('/api/search/tracking-code/?q=BBCCDDFF')
|
1159 |
1079 |
resp.form['code'] = 'FOOBAR'
|
1160 |
1080 |
resp.form['url'] = 'http://example.org/'
|
1161 |
1081 |
resp = resp.form.submit(status=403)
|
1162 |
1082 |
|
1163 |
1083 |
|
1164 |
|
@wcs_present
|
1165 |
1084 |
def test_wcs_search_engines(app):
|
1166 |
1085 |
settings.TEMPLATE_VARS['is_portal_agent'] = True
|
1167 |
1086 |
search_engines = engines.get_engines()
|
... | ... | |
1173 |
1092 |
assert len([x for x in search_engines.keys() if x.startswith('formdata:')]) == 0
|
1174 |
1093 |
|
1175 |
1094 |
|
1176 |
|
@wcs_present
|
1177 |
1095 |
def test_backoffice_submission_cell_render(context):
|
1178 |
1096 |
page = Page(title='xxx', slug='test_backoffice_submission_cell_render', template_name='standard')
|
1179 |
1097 |
page.save()
|
... | ... | |
1183 |
1101 |
|
1184 |
1102 |
context['synchronous'] = True # to get fresh content
|
1185 |
1103 |
|
1186 |
|
result = cell.render(context)
|
1187 |
|
assert '/backoffice/submission/a-private-form/' not in result
|
1188 |
|
assert context['all_formdefs'] == {}
|
1189 |
|
assert 'h2' not in result
|
1190 |
|
|
1191 |
|
context['request'].user = MockUser()
|
1192 |
|
|
1193 |
|
result = cell.render(context)
|
1194 |
|
assert '/backoffice/submission/a-private-form/' not in result
|
|
1104 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
1105 |
mock_json = mock.Mock(status_code=200)
|
|
1106 |
requests_get.return_value = mock_json
|
|
1107 |
result = cell.render(context)
|
|
1108 |
assert requests_get.call_args_list[0][0][0] == '/api/formdefs/?backoffice-submission=on'
|
1195 |
1109 |
assert context['all_formdefs'] == {}
|
1196 |
1110 |
assert 'h2' not in result
|
1197 |
1111 |
|
1198 |
|
class MockUser2(MockUser):
|
1199 |
|
email = 'foo2@example.net'
|
1200 |
|
context['request'].user = MockUser2()
|
1201 |
|
|
1202 |
|
result = cell.render(context)
|
|
1112 |
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
|
1113 |
requests_get.return_value = MockedRequestResponse(content=json.dumps(
|
|
1114 |
{'data': [{'backoffice_submission_url': '/backoffice/submission/a-private-form/', 'title': 'Foo'}]}))
|
|
1115 |
result = cell.render(context)
|
1203 |
1116 |
assert '/backoffice/submission/a-private-form/' in result
|
1204 |
1117 |
assert list(context['all_formdefs'].keys()) == ['default']
|
1205 |
1118 |
assert 'h2' in result
|
1206 |
1119 |
|
1207 |
|
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
1208 |
|
# empty response
|
1209 |
|
mock_json = mock.Mock(status_code=200)
|
1210 |
|
requests_get.return_value = mock_json
|
1211 |
|
result = cell.render(context)
|
1212 |
|
assert '/backoffice/submission/a-private-form/' not in result
|
1213 |
|
|
1214 |
1120 |
|
1215 |
|
@wcs_present
|
1216 |
1121 |
def test_manager_link_list_cell_duplicate():
|
1217 |
1122 |
page = Page.objects.create(title='xxx', slug='new', template_name='standard')
|
1218 |
1123 |
cell = LinkListCell.objects.create(order=0, page=page)
|
... | ... | |
1236 |
1141 |
assert new_item.cached_json == item.cached_json
|
1237 |
1142 |
|
1238 |
1143 |
|
1239 |
|
@wcs_present
|
1240 |
|
def test_manager_add_edit_delete_list_link_item(app, admin_user):
|
1241 |
|
Page.objects.all().delete()
|
|
1144 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
1145 |
def test_manager_add_edit_delete_list_link_item(mock_send, app, admin_user):
|
1242 |
1146 |
page = Page.objects.create(title='One', slug='one', template_name='standard')
|
1243 |
1147 |
cell = LinkListCell.objects.create(order=0, placeholder='content', page=page)
|
1244 |
1148 |
app = login(app)
|
... | ... | |
1273 |
1177 |
assert WcsFormCell.objects.count() == 0
|
1274 |
1178 |
|
1275 |
1179 |
|
1276 |
|
@wcs_present
|
1277 |
1180 |
def test_import_export_pages_with_links():
|
1278 |
1181 |
page = Page(title=u'bar', slug='bar', order=1)
|
1279 |
1182 |
page.save()
|
... | ... | |
1289 |
1192 |
)
|
1290 |
1193 |
|
1291 |
1194 |
site_export = [x.get_serialized_page() for x in Page.objects.all()]
|
1292 |
|
Page.objects.all().delete()
|
1293 |
1195 |
|
1294 |
1196 |
Page.load_serialized_pages(site_export)
|
1295 |
1197 |
|
... | ... | |
1322 |
1224 |
assert 'keyword-bar' in resp
|
1323 |
1225 |
|
1324 |
1226 |
|
1325 |
|
@wcs_present
|
1326 |
1227 |
def test_view_page_with_wcs_cells_num_queries(app, admin_user):
|
1327 |
1228 |
page = Page.objects.create(title=u'bar', slug='index', order=1)
|
1328 |
1229 |
for i in range(0, 15):
|
... | ... | |
1342 |
1243 |
assert len(ctx.captured_queries) == 61
|
1343 |
1244 |
|
1344 |
1245 |
|
1345 |
|
@wcs_present
|
1346 |
1246 |
def test_hourly():
|
1347 |
1247 |
appconfig = apps.get_app_config('wcs')
|
1348 |
1248 |
page = Page.objects.create(title='xxx', slug='test_current_forms_cell_render', template_name='standard')
|
... | ... | |
1358 |
1258 |
assert hasattr(klass, 'check_validity') is False
|
1359 |
1259 |
|
1360 |
1260 |
|
1361 |
|
@wcs_present
|
1362 |
|
def test_search_external_forms_links(context):
|
1363 |
|
|
|
1261 |
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
|
1262 |
def test_search_external_forms_links(mock_send, context):
|
1364 |
1263 |
page = Page(title='xxx', slug='test_forms_of_category_cell_search',
|
1365 |
1264 |
template_name='standard')
|
1366 |
1265 |
page.save()
|