|
1 |
# -*- coding: utf-8 -*-
|
|
2 |
|
|
3 |
import mock
|
|
4 |
import pytest
|
|
5 |
from lxml import etree
|
|
6 |
from requests import Response
|
|
7 |
|
|
8 |
from django.test import override_settings
|
|
9 |
|
|
10 |
from hobo.environment.models import Variable, Wcs, Combo, Fargo
|
|
11 |
from hobo.matomo.utils import MatomoError, MatomoException, MatomoWS, \
|
|
12 |
get_variable, get_matomo_settings, get_tenant_name_and_public_urls, \
|
|
13 |
is_site_already_added, add_user_if_not_already_there, \
|
|
14 |
get_and_normalise_javascript_tag, compute_cnil_acknowledgment_level, \
|
|
15 |
auto_configure_matomo
|
|
16 |
|
|
17 |
pytestmark = pytest.mark.django_db
|
|
18 |
|
|
19 |
CONFIG = {'URL': 'https://matomo.test', 'TOKEN_AUTH': '1234'}
|
|
20 |
|
|
21 |
GET_SITE_42_ALREADY_THERE = """<?xml version="1.0" encoding="utf-8" ?>
|
|
22 |
<result>
|
|
23 |
<row>
|
|
24 |
<idsite>42</idsite>
|
|
25 |
<moretags>...</moretags>
|
|
26 |
</row>
|
|
27 |
</result>
|
|
28 |
"""
|
|
29 |
|
|
30 |
GET_SITE_34_ALREADY_THERE = """<?xml version="1.0" encoding="utf-8" ?>
|
|
31 |
<result>
|
|
32 |
<row>
|
|
33 |
<idsite>34</idsite>
|
|
34 |
<moretags>...</moretags>
|
|
35 |
</row>
|
|
36 |
</result>
|
|
37 |
"""
|
|
38 |
|
|
39 |
GET_SITE_NO_ID_PROVIDED = """<?xml version="1.0" encoding="utf-8" ?>
|
|
40 |
<result>
|
|
41 |
<error message="Please specify a value for 'idSite'." />
|
|
42 |
</result>
|
|
43 |
"""
|
|
44 |
|
|
45 |
GET_SITE_BAD_RESPONSE = """<?xml version="1.0" encoding="utf-8" ?>
|
|
46 |
<result>
|
|
47 |
<row>
|
|
48 |
<not_idsite>there is no idsite tag</not_idsite>
|
|
49 |
<moretags>...</moretags>
|
|
50 |
</row>
|
|
51 |
</result>
|
|
52 |
"""
|
|
53 |
|
|
54 |
GET_SITE_NOT_ALREADY_THERE = """<?xml version="1.0" encoding="utf-8" ?>
|
|
55 |
<result>
|
|
56 |
<error message="An unexpected website was found in the request: website id was set to '42' ." />
|
|
57 |
</result>
|
|
58 |
"""
|
|
59 |
|
|
60 |
ADD_SITE_SUCCESS = """<?xml version="1.0" encoding="utf-8" ?>
|
|
61 |
<result>42</result>
|
|
62 |
"""
|
|
63 |
|
|
64 |
ADD_SITE_ERROR = """<?xml version="1.0" encoding="utf-8" ?>
|
|
65 |
<result>
|
|
66 |
<error message="Please specify a value for 'siteName'." />
|
|
67 |
</result>
|
|
68 |
"""
|
|
69 |
|
|
70 |
ADD_SITE_BAD_RESPONSE = """<?xml version="1.0" encoding="utf-8" ?>
|
|
71 |
<not_result>no result tag</not_result>
|
|
72 |
"""
|
|
73 |
|
|
74 |
ADD_USER_SUCCESS = """<?xml version="1.0" encoding="utf-8" ?>
|
|
75 |
<result>
|
|
76 |
<success message="ok" />
|
|
77 |
</result>
|
|
78 |
"""
|
|
79 |
|
|
80 |
USER_ALREADY_THERE = """<?xml version="1.0" encoding="utf-8" ?>
|
|
81 |
<result>
|
|
82 |
<error message="Username 'hobo_dev_publik_love' already exists." />
|
|
83 |
</result>"""
|
|
84 |
|
|
85 |
MAIL_ALREADY_THERE = """<?xml version="1.0" encoding="utf-8" ?>
|
|
86 |
<result>
|
|
87 |
<error message="User with email 'nestor@testor.org' already exists." />
|
|
88 |
</result>"""
|
|
89 |
|
|
90 |
BAD_CREDENTIAL = """<?xml version="1.0" encoding="utf-8" ?>
|
|
91 |
<result>
|
|
92 |
<error message="You can\'t access this resource as it requires a \'superuser\' access." />
|
|
93 |
</result>"""
|
|
94 |
|
|
95 |
ADD_USER_BAD_RESPONSE_1 = """<?xml version="1.0" encoding="utf-8" ?>
|
|
96 |
<result>
|
|
97 |
<success message="KO" />
|
|
98 |
</result>
|
|
99 |
"""
|
|
100 |
|
|
101 |
ADD_USER_BAD_RESPONSE_2 = """<?xml version="1.0" encoding="utf-8" ?>
|
|
102 |
<result>
|
|
103 |
<success>no message attribute</success>
|
|
104 |
<not_success>no success tag</not_success>
|
|
105 |
</result>
|
|
106 |
"""
|
|
107 |
|
|
108 |
JAVASCRIPT_TAG = """<?xml version="1.0" encoding="utf-8" ?>
|
|
109 |
<result><!-- Matomo -->
|
|
110 |
<script type="text/javascript">
|
|
111 |
var _paq = window._paq || [];
|
|
112 |
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
|
|
113 |
_paq.push(['trackPageView']);
|
|
114 |
_paq.push(['enableLinkTracking']);
|
|
115 |
(function() {
|
|
116 |
var u="//matomo-test.entrouvert.org/";
|
|
117 |
_paq.push(['setTrackerUrl', u+'piwik.php']);
|
|
118 |
_paq.push(['setSiteId', '7']);
|
|
119 |
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
|
|
120 |
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
|
|
121 |
})();
|
|
122 |
</script>
|
|
123 |
<!-- End Matomo Code -->
|
|
124 |
</result>
|
|
125 |
"""
|
|
126 |
|
|
127 |
BAS_CREDENTIALS = """<?xml version="1.0" encoding="utf-8" ?>
|
|
128 |
<result>
|
|
129 |
<error message="You can't access this resource as it requires 'view' access for the website id = 42." />
|
|
130 |
</result>
|
|
131 |
"""
|
|
132 |
|
|
133 |
JAVASCRIPT_TAG_BAD_RESPONSE = """<?xml version="1.0" encoding="utf-8" ?>
|
|
134 |
<no_result_tag/>
|
|
135 |
"""
|
|
136 |
|
|
137 |
def test_get_variable():
|
|
138 |
"""hobo variables from DB"""
|
|
139 |
Variable.objects.create(name='matomo_id_site', value='42')
|
|
140 |
|
|
141 |
id_site_var = get_variable('matomo_id_site')
|
|
142 |
assert id_site_var.value == '42'
|
|
143 |
|
|
144 |
def test_matomo_settings():
|
|
145 |
"""hobo configuration from settings"""
|
|
146 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
147 |
url, token = get_matomo_settings()
|
|
148 |
assert url == 'https://matomo.test'
|
|
149 |
assert token == '1234'
|
|
150 |
|
|
151 |
with override_settings(MATOMO_FEED={}):
|
|
152 |
try:
|
|
153 |
url, token = get_matomo_settings()
|
|
154 |
except MatomoError as exc:
|
|
155 |
assert str(exc) == "no settings for matomo: 'URL'"
|
|
156 |
else:
|
|
157 |
assert False
|
|
158 |
|
|
159 |
def test_get_tenant_name_and_public_urls():
|
|
160 |
Combo.objects.create(base_url='https://combo.dev.publik.love',
|
|
161 |
template_name='portal-user')
|
|
162 |
Wcs.objects.create(base_url='https://wcs.dev.publik.love')
|
|
163 |
Fargo.objects.create(base_url='https://fargo.dev.publik.love')
|
|
164 |
tenant_name, site_urls = get_tenant_name_and_public_urls()
|
|
165 |
assert tenant_name == 'combo.dev.publik.love'
|
|
166 |
assert site_urls[2] == 'https://fargo.dev.publik.love/'
|
|
167 |
|
|
168 |
def test_matomo_constructor():
|
|
169 |
"""build the matomo webservice object"""
|
|
170 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
171 |
matomo = MatomoWS(*get_matomo_settings())
|
|
172 |
assert matomo.url_ws_base == 'https://matomo.test'
|
|
173 |
assert matomo.token_auth == '1234'
|
|
174 |
|
|
175 |
with override_settings(MATOMO_FEED={}):
|
|
176 |
try:
|
|
177 |
matomo = MatomoWS(*get_matomo_settings())
|
|
178 |
except MatomoError as exc:
|
|
179 |
assert str(exc) == "no settings for matomo: 'URL'"
|
|
180 |
else:
|
|
181 |
assert False
|
|
182 |
|
|
183 |
def test_parse_response():
|
|
184 |
"""parser used by all matomo webservice calls"""
|
|
185 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
186 |
matomo = MatomoWS(*get_matomo_settings())
|
|
187 |
|
|
188 |
# no error (expected format)
|
|
189 |
content = """<?xml version="1.0" encoding="utf-8" ?><ok/>"""
|
|
190 |
tree = matomo._parse_response(content)
|
|
191 |
assert tree.tag == 'ok'
|
|
192 |
|
|
193 |
# error (not XML format)
|
|
194 |
content = """this is not XML"""
|
|
195 |
try:
|
|
196 |
tree = matomo._parse_response(content)
|
|
197 |
except MatomoException as exc:
|
|
198 |
assert str(exc).find("XMLSyntaxError: Start tag expected") != -1
|
|
199 |
else:
|
|
200 |
assert False
|
|
201 |
|
|
202 |
def test_get_error_message():
|
|
203 |
"""error handler used by all matomo webservice calls"""
|
|
204 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
205 |
matomo = MatomoWS(*get_matomo_settings())
|
|
206 |
|
|
207 |
# no error (expected format)
|
|
208 |
content = """<?xml version="1.0" encoding="utf-8" ?><ok/>"""
|
|
209 |
tree = matomo._parse_response(content)
|
|
210 |
matomo._raise_on_error(tree)
|
|
211 |
assert tree.tag == 'ok'
|
|
212 |
|
|
213 |
# error (expected format)
|
|
214 |
content = """<?xml version="1.0" encoding="utf-8" ?>
|
|
215 |
<result>
|
|
216 |
<error message="here is the error message" />
|
|
217 |
</result>
|
|
218 |
"""
|
|
219 |
tree = matomo._parse_response(content)
|
|
220 |
try:
|
|
221 |
matomo._raise_on_error(tree)
|
|
222 |
except MatomoError as exc:
|
|
223 |
assert str(exc) == 'here is the error message'
|
|
224 |
else:
|
|
225 |
assert False
|
|
226 |
|
|
227 |
# error (unexpected format)
|
|
228 |
content = """<?xml version="1.0" encoding="utf-8" ?>
|
|
229 |
<result>
|
|
230 |
<error>no 'message' attribute here</error>
|
|
231 |
</result>
|
|
232 |
"""
|
|
233 |
tree = matomo._parse_response(content)
|
|
234 |
try:
|
|
235 |
matomo._raise_on_error(tree)
|
|
236 |
except MatomoException as exc:
|
|
237 |
assert str(exc) == 'internal error'
|
|
238 |
else:
|
|
239 |
assert False
|
|
240 |
|
|
241 |
@mock.patch('requests.post')
|
|
242 |
def test_get_site_from_id(mocked_post):
|
|
243 |
"""webservice to test if the site is already registered"""
|
|
244 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
245 |
matomo = MatomoWS(*get_matomo_settings())
|
|
246 |
|
|
247 |
# site already here
|
|
248 |
content = GET_SITE_42_ALREADY_THERE
|
|
249 |
mocked_post.return_value.content = content
|
|
250 |
assert matomo.get_site_from_id('42') == '42'
|
|
251 |
|
|
252 |
# site not already here
|
|
253 |
content = GET_SITE_NOT_ALREADY_THERE
|
|
254 |
mocked_post.return_value.content = content
|
|
255 |
try:
|
|
256 |
matomo.get_site_from_id(42)
|
|
257 |
except MatomoError as exc:
|
|
258 |
assert str(exc).find('An unexpected website was found in ') != -1
|
|
259 |
else:
|
|
260 |
assert False
|
|
261 |
|
|
262 |
# error on empty id
|
|
263 |
content = GET_SITE_NO_ID_PROVIDED
|
|
264 |
mocked_post.return_value.content = content
|
|
265 |
try:
|
|
266 |
matomo.get_site_from_id(42)
|
|
267 |
except MatomoError as exc:
|
|
268 |
assert str(exc) == "Please specify a value for 'idSite'."
|
|
269 |
else:
|
|
270 |
assert False
|
|
271 |
|
|
272 |
# bad response (error on success response)
|
|
273 |
content = GET_SITE_BAD_RESPONSE
|
|
274 |
mocked_post.return_value.content = content
|
|
275 |
try:
|
|
276 |
matomo.get_site_from_id(42)
|
|
277 |
except MatomoException as exc:
|
|
278 |
assert str(exc) == 'get_site_from_id fails'
|
|
279 |
else:
|
|
280 |
assert False
|
|
281 |
|
|
282 |
@mock.patch('requests.post')
|
|
283 |
def test_is_site_already_added(mocked_post):
|
|
284 |
"""function to test if the site is already regisered"""
|
|
285 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
286 |
matomo = MatomoWS(*get_matomo_settings())
|
|
287 |
|
|
288 |
# site already here
|
|
289 |
content = GET_SITE_42_ALREADY_THERE
|
|
290 |
mocked_post.return_value.content = content
|
|
291 |
assert is_site_already_added(matomo, '42') is True
|
|
292 |
|
|
293 |
# site not already here
|
|
294 |
content = GET_SITE_NOT_ALREADY_THERE
|
|
295 |
mocked_post.return_value.content = content
|
|
296 |
assert is_site_already_added(matomo, '42') is False
|
|
297 |
|
|
298 |
# empty id provided
|
|
299 |
content = GET_SITE_NO_ID_PROVIDED
|
|
300 |
mocked_post.return_value.content = content
|
|
301 |
assert is_site_already_added(matomo, '42') is False
|
|
302 |
|
|
303 |
# strange case
|
|
304 |
content = GET_SITE_34_ALREADY_THERE
|
|
305 |
mocked_post.return_value.content = content
|
|
306 |
assert is_site_already_added(matomo, '42') is False
|
|
307 |
|
|
308 |
# error
|
|
309 |
content = GET_SITE_BAD_RESPONSE
|
|
310 |
mocked_post.return_value.content = content
|
|
311 |
try:
|
|
312 |
is_site_already_added(matomo, '42')
|
|
313 |
except MatomoException as exc:
|
|
314 |
assert str(exc) == 'get_site_from_id fails'
|
|
315 |
else:
|
|
316 |
assert False
|
|
317 |
|
|
318 |
@mock.patch('requests.post')
|
|
319 |
def test_add_site(mocked_post):
|
|
320 |
"""webservice to add a new site"""
|
|
321 |
urls = ['https://combo.dev.publik.love',
|
|
322 |
'https://wcs.dev.publik.love']
|
|
323 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
324 |
matomo = MatomoWS(*get_matomo_settings())
|
|
325 |
|
|
326 |
# success
|
|
327 |
content = ADD_SITE_SUCCESS
|
|
328 |
mocked_post.return_value.content = content
|
|
329 |
site_id = matomo.add_site("hobo_dev_publik_love", urls)
|
|
330 |
assert site_id == '42'
|
|
331 |
|
|
332 |
# error
|
|
333 |
content = ADD_SITE_ERROR
|
|
334 |
mocked_post.return_value.content = content
|
|
335 |
try:
|
|
336 |
site_id = matomo.add_site("hobo_dev_publik_love", urls)
|
|
337 |
except MatomoError as exc:
|
|
338 |
assert str(exc) == "Please specify a value for 'siteName'."
|
|
339 |
else:
|
|
340 |
assert False
|
|
341 |
|
|
342 |
# strange message
|
|
343 |
content = ADD_SITE_BAD_RESPONSE
|
|
344 |
mocked_post.return_value.content = content
|
|
345 |
try:
|
|
346 |
site_id = matomo.add_site("hobo_dev_publik_love", urls)
|
|
347 |
except MatomoException as exc:
|
|
348 |
assert str(exc) == 'add_site fails'
|
|
349 |
else:
|
|
350 |
assert False
|
|
351 |
|
|
352 |
@mock.patch('requests.post')
|
|
353 |
def test_add_user(mocked_post):
|
|
354 |
"""webservice to add new user"""
|
|
355 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
356 |
matomo = MatomoWS(*get_matomo_settings())
|
|
357 |
|
|
358 |
# success
|
|
359 |
content = ADD_USER_SUCCESS
|
|
360 |
mocked_post.return_value.content = content
|
|
361 |
matomo.add_user('nestor', 'xxx', 'nestor@testor.org')
|
|
362 |
assert True
|
|
363 |
|
|
364 |
# error (user already here)
|
|
365 |
content = USER_ALREADY_THERE
|
|
366 |
mocked_post.return_value.content = content
|
|
367 |
try:
|
|
368 |
matomo.add_user('hobo_dev_publik_love', 'xxx', 'nestor@testor.org')
|
|
369 |
except MatomoError as exc:
|
|
370 |
assert str(exc).find("Username 'hobo_dev_publik_love' already") != -1
|
|
371 |
else:
|
|
372 |
assert False
|
|
373 |
|
|
374 |
# error (mail already registered)
|
|
375 |
content = MAIL_ALREADY_THERE
|
|
376 |
mocked_post.return_value.content = content
|
|
377 |
try:
|
|
378 |
matomo.add_user('nestor', 'xxx', 'nestor@testor.org')
|
|
379 |
except MatomoError as exc:
|
|
380 |
assert str(exc).find("User with email 'nestor@testor.org'") != -1
|
|
381 |
else:
|
|
382 |
assert False
|
|
383 |
|
|
384 |
# error (bad credentials)
|
|
385 |
content = BAD_CREDENTIAL
|
|
386 |
mocked_post.return_value.content = content
|
|
387 |
try:
|
|
388 |
matomo.add_user('nestor', 'xxx', 'nestor@testor.org')
|
|
389 |
except MatomoError as exc:
|
|
390 |
assert str(exc).find("You can\'t access this resource") != -1
|
|
391 |
else:
|
|
392 |
assert False
|
|
393 |
|
|
394 |
# bad success message (wrong attribute value)
|
|
395 |
content = ADD_USER_BAD_RESPONSE_1
|
|
396 |
mocked_post.return_value.content = content
|
|
397 |
try:
|
|
398 |
matomo.add_user('nestor', 'xxx', 'nestor@testor.org')
|
|
399 |
except MatomoException as exc:
|
|
400 |
assert str(exc) == 'add_user fails'
|
|
401 |
else:
|
|
402 |
assert False
|
|
403 |
|
|
404 |
# bad success message (no message attribute)
|
|
405 |
content = ADD_USER_BAD_RESPONSE_2
|
|
406 |
mocked_post.return_value.content = content
|
|
407 |
try:
|
|
408 |
matomo.add_user('nestor', 'xxx', 'nestor@testor.org')
|
|
409 |
except MatomoException as exc:
|
|
410 |
assert str(exc) == 'add_user fails'
|
|
411 |
else:
|
|
412 |
assert False
|
|
413 |
|
|
414 |
@mock.patch('requests.post')
|
|
415 |
def test_add_user_if_not_already_there(mocked_post):
|
|
416 |
"""function to assert we have a user"""
|
|
417 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
418 |
matomo = MatomoWS(*get_matomo_settings())
|
|
419 |
|
|
420 |
# success (add a new user)
|
|
421 |
content = ADD_USER_SUCCESS
|
|
422 |
mocked_post.return_value.content = content
|
|
423 |
add_user_if_not_already_there(matomo, 'hobo_dev_publik_love', 'xxx', '42')
|
|
424 |
assert True
|
|
425 |
|
|
426 |
# success (user already here)
|
|
427 |
content = USER_ALREADY_THERE
|
|
428 |
mocked_post.return_value.content = content
|
|
429 |
add_user_if_not_already_there(matomo, 'hobo_dev_publik_love', 'xxx', '42')
|
|
430 |
assert True
|
|
431 |
|
|
432 |
# error (bad credentials)
|
|
433 |
content = BAD_CREDENTIAL
|
|
434 |
mocked_post.return_value.content = content
|
|
435 |
try:
|
|
436 |
add_user_if_not_already_there(matomo, 'tenant_name', 'xxx', '42')
|
|
437 |
except MatomoError:
|
|
438 |
assert True
|
|
439 |
else:
|
|
440 |
assert False
|
|
441 |
|
|
442 |
@mock.patch('requests.post')
|
|
443 |
def test_get_javascript_tag(mocked_post):
|
|
444 |
"""webservice to get matomo JS tag"""
|
|
445 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
446 |
matomo = MatomoWS(*get_matomo_settings())
|
|
447 |
|
|
448 |
# success
|
|
449 |
content = JAVASCRIPT_TAG
|
|
450 |
mocked_post.return_value.content = content
|
|
451 |
javascript_tag = matomo.get_javascript_tag('42')
|
|
452 |
assert javascript_tag.find('(function() {') != -1
|
|
453 |
|
|
454 |
# error (bad credentials)
|
|
455 |
content = BAD_CREDENTIAL
|
|
456 |
mocked_post.return_value.content = content
|
|
457 |
try:
|
|
458 |
javascript_tag = matomo.get_javascript_tag('42')
|
|
459 |
except MatomoError as exc:
|
|
460 |
assert str(exc).find("You can't access this resource ") != -1
|
|
461 |
else:
|
|
462 |
assert False
|
|
463 |
|
|
464 |
# bad response (no result tag)
|
|
465 |
content = JAVASCRIPT_TAG_BAD_RESPONSE
|
|
466 |
mocked_post.return_value.content = content
|
|
467 |
try:
|
|
468 |
javascript_tag = matomo.get_javascript_tag('42')
|
|
469 |
except MatomoException as exc:
|
|
470 |
assert str(exc) == 'get_javascript_tag fails'
|
|
471 |
else:
|
|
472 |
assert False
|
|
473 |
|
|
474 |
def test_compute_cnil_acknowledgment_level():
|
|
475 |
"""function use to inspect javascript content"""
|
|
476 |
warning_content = JAVASCRIPT_TAG
|
|
477 |
|
|
478 |
# can't find cookie's life time extension prevention
|
|
479 |
assert compute_cnil_acknowledgment_level(warning_content) == 'warning'
|
|
480 |
|
|
481 |
# ok
|
|
482 |
success_content = warning_content + '\n...getOriginalVisitorCookieTimeout...'
|
|
483 |
assert compute_cnil_acknowledgment_level(success_content) == 'success'
|
|
484 |
|
|
485 |
# google reference found into javascript
|
|
486 |
error_content = success_content + '\n...google...'
|
|
487 |
assert compute_cnil_acknowledgment_level(error_content) == 'error'
|
|
488 |
|
|
489 |
@mock.patch('requests.post')
|
|
490 |
def test_get_and_normalise_javascript_tag(mocked_post):
|
|
491 |
"""function to get matomo JS tag"""
|
|
492 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
493 |
matomo = MatomoWS(*get_matomo_settings())
|
|
494 |
|
|
495 |
# success
|
|
496 |
content = JAVASCRIPT_TAG
|
|
497 |
mocked_post.return_value.content = content
|
|
498 |
javascript_tag = get_and_normalise_javascript_tag(matomo, '42')
|
|
499 |
print javascript_tag
|
|
500 |
assert javascript_tag.find('(function() {') != -1
|
|
501 |
assert javascript_tag.find('<script') == -1
|
|
502 |
assert javascript_tag.find('script>') == -1
|
|
503 |
assert compute_cnil_acknowledgment_level(javascript_tag) == 'success'
|
|
504 |
|
|
505 |
def auto_conf_mocked_post(url, **kwargs):
|
|
506 |
contents = [GET_SITE_NO_ID_PROVIDED,
|
|
507 |
ADD_SITE_SUCCESS,
|
|
508 |
ADD_USER_SUCCESS,
|
|
509 |
JAVASCRIPT_TAG]
|
|
510 |
response = Response()
|
|
511 |
response._content = contents[auto_conf_mocked_post.cpt]
|
|
512 |
response.status_code = 200
|
|
513 |
|
|
514 |
auto_conf_mocked_post.cpt += 1
|
|
515 |
return response
|
|
516 |
|
|
517 |
@mock.patch('requests.post', side_effect=auto_conf_mocked_post)
|
|
518 |
def test_auto_configure_matomo(mocked_post):
|
|
519 |
Variable.objects.create(name='matomo_id_site', value='42')
|
|
520 |
Variable.objects.create(name='matomo_password', value='567')
|
|
521 |
Variable.objects.create(name='tracking_js_var', value='js_code')
|
|
522 |
Variable.objects.create(name='matomo_error', value='')
|
|
523 |
|
|
524 |
Combo.objects.create(base_url='https://combo.dev.publik.love',
|
|
525 |
template_name='portal-user')
|
|
526 |
Wcs.objects.create(base_url='https://wcs.dev.publik.love')
|
|
527 |
Fargo.objects.create(base_url='https://fargo.dev.publik.love')
|
|
528 |
|
|
529 |
auto_conf_mocked_post.cpt = 0
|
|
530 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
531 |
assert auto_configure_matomo() is True
|
|
532 |
|
|
533 |
@mock.patch('requests.post', side_effect=auto_conf_mocked_post)
|
|
534 |
def test_auto_configure_matomo_no_url(mocked_post):
|
|
535 |
Variable.objects.create(name='matomo_id_site', value='42')
|
|
536 |
Variable.objects.create(name='matomo_password', value='567')
|
|
537 |
Variable.objects.create(name='tracking_js_var', value='js_code')
|
|
538 |
Variable.objects.create(name='matomo_error', value='')
|
|
539 |
|
|
540 |
# no Wc url so as to raise
|
|
541 |
Wcs.objects.create(base_url='https://wcs.dev.publik.love')
|
|
542 |
Fargo.objects.create(base_url='https://fargo.dev.publik.love')
|
|
543 |
|
|
544 |
auto_conf_mocked_post.cpt = 0
|
|
545 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
546 |
assert auto_configure_matomo() is False
|
|
547 |
message = Variable.objects.get(name='matomo_error').value
|
|
548 |
assert message == "no portal-user url available for a matomo's id"
|
|
549 |
|
|
550 |
@mock.patch('requests.post', side_effect=auto_conf_mocked_post)
|
|
551 |
def test_auto_configure_matomo_no_password(mocked_post):
|
|
552 |
Variable.objects.create(name='matomo_id_site', value='42')
|
|
553 |
|
|
554 |
# force to generate new password
|
|
555 |
Variable.objects.create(name='matomo_password', value='')
|
|
556 |
Variable.objects.create(name='tracking_js_var', value='js_code')
|
|
557 |
Variable.objects.create(name='matomo_error', value='')
|
|
558 |
|
|
559 |
Combo.objects.create(base_url='https://combo.dev.publik.love',
|
|
560 |
template_name='portal-user')
|
|
561 |
Wcs.objects.create(base_url='https://wcs.dev.publik.love')
|
|
562 |
Fargo.objects.create(base_url='https://fargo.dev.publik.love')
|
|
563 |
|
|
564 |
auto_conf_mocked_post.cpt = 0
|
|
565 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
566 |
assert auto_configure_matomo() is True
|
|
567 |
|
|
568 |
def auto_conf_mocked_post_error(url, **kwargs):
|
|
569 |
contents = [GET_SITE_NO_ID_PROVIDED,
|
|
570 |
ADD_SITE_SUCCESS,
|
|
571 |
ADD_USER_SUCCESS,
|
|
572 |
JAVASCRIPT_TAG_BAD_RESPONSE]
|
|
573 |
response = Response()
|
|
574 |
response._content = contents[auto_conf_mocked_post.cpt]
|
|
575 |
response.status_code = 200
|
|
576 |
|
|
577 |
auto_conf_mocked_post.cpt += 1
|
|
578 |
return response
|
|
579 |
|
|
580 |
@mock.patch('requests.post', side_effect=auto_conf_mocked_post_error)
|
|
581 |
def test_auto_configure_matomo_error(mocked_post):
|
|
582 |
Variable.objects.create(name='matomo_id_site', value='42')
|
|
583 |
Variable.objects.create(name='matomo_password', value='567')
|
|
584 |
Variable.objects.create(name='tracking_js_var', value='js_code')
|
|
585 |
Variable.objects.create(name='matomo_error', value='')
|
|
586 |
|
|
587 |
Combo.objects.create(base_url='https://combo.dev.publik.love',
|
|
588 |
template_name='portal-user')
|
|
589 |
Wcs.objects.create(base_url='https://wcs.dev.publik.love')
|
|
590 |
Fargo.objects.create(base_url='https://fargo.dev.publik.love')
|
|
591 |
|
|
592 |
auto_conf_mocked_post.cpt = 0
|
|
593 |
with override_settings(MATOMO_FEED=CONFIG):
|
|
594 |
assert auto_configure_matomo() is False
|
|
595 |
message = Variable.objects.get(name='matomo_error').value
|
|
596 |
assert message == 'get_javascript_tag fails'
|