161 |
161 |
<no_result_tag/>
|
162 |
162 |
"""
|
163 |
163 |
|
|
164 |
PING_SUCCESS = '{"status":"success","tracked":1,"invalid":0}'
|
|
165 |
PING_ERROR = '{"status":"not success"}'
|
|
166 |
PING_NOSTATUS_ERROR = '{}'
|
|
167 |
PING_NOJSON_ERROR = 'not json'
|
|
168 |
|
164 |
169 |
def requests_post_mocked_replies(contents):
|
165 |
170 |
"""buid an iterator for mock's side_effect parameter"""
|
166 |
171 |
responses = []
|
... | ... | |
441 |
446 |
javascript_tag = matomo.get_javascript_tag('42')
|
442 |
447 |
assert 'get_javascript_tag fails' in str(exc)
|
443 |
448 |
|
|
449 |
@mock.patch('requests.post')
|
|
450 |
def test_ping(mocked_post):
|
|
451 |
"""webservice to simulate a first tracking call"""
|
|
452 |
with override_settings(MATOMO_SERVER=CONFIG):
|
|
453 |
matomo = MatomoWS()
|
|
454 |
|
|
455 |
# success
|
|
456 |
content = PING_SUCCESS
|
|
457 |
mocked_post.return_value.content = content
|
|
458 |
matomo.ping('42')
|
|
459 |
assert True
|
|
460 |
|
|
461 |
# error
|
|
462 |
content = PING_ERROR
|
|
463 |
mocked_post.return_value.content = content
|
|
464 |
with pytest.raises(MatomoException) as exc:
|
|
465 |
matomo.ping('42')
|
|
466 |
assert 'ping fails' in str(exc)
|
|
467 |
|
|
468 |
# failure (no status)
|
|
469 |
content = PING_NOSTATUS_ERROR
|
|
470 |
mocked_post.return_value.content = content
|
|
471 |
with pytest.raises(MatomoException) as exc:
|
|
472 |
matomo.ping('42')
|
|
473 |
assert 'internal error on ping (status expected)' in str(exc)
|
|
474 |
|
|
475 |
# failure (no JSON)
|
|
476 |
content = PING_NOJSON_ERROR
|
|
477 |
mocked_post.return_value.content = content
|
|
478 |
with pytest.raises(MatomoException) as exc:
|
|
479 |
matomo.ping('42')
|
|
480 |
assert 'internal error on ping (JSON expected)' in str(exc)
|
|
481 |
|
444 |
482 |
@mock.patch('requests.post')
|
445 |
483 |
def test_upgrade_site(mocked_post):
|
446 |
484 |
"""function to test if the site is already regisered"""
|
... | ... | |
559 |
597 |
value1 = get_variable_value('cnil_compliant_visits_tracking_js', 'undefined')
|
560 |
598 |
value2 = get_variable_value('visits_tracking_js', 'undefined')
|
561 |
599 |
assert value1 == 'undefined'
|
562 |
|
assert value2 == 'undefined'
|
|
600 |
assert value2 == 'undefined'
|
563 |
601 |
|
564 |
602 |
@mock.patch('requests.post')
|
565 |
603 |
def test_upgrade_javascript_tag(mocked_post):
|
... | ... | |
590 |
628 |
with override_settings(MATOMO_SERVER=CONFIG):
|
591 |
629 |
contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS,
|
592 |
630 |
DEL_UNKNOWN_USER, MATOMO_SUCCESS,
|
593 |
|
JAVASCRIPT_TAG]
|
|
631 |
JAVASCRIPT_TAG, PING_SUCCESS]
|
594 |
632 |
mocked_post.side_effect = requests_post_mocked_replies(contents)
|
595 |
633 |
assert auto_configure_matomo() is True
|
596 |
634 |
logme_url_var = get_variable('matomo_logme_url')
|
... | ... | |
602 |
640 |
|
603 |
641 |
@mock.patch('requests.post')
|
604 |
642 |
def test_auto_configure_matomo_no_url(mocked_post):
|
605 |
|
# no Wc url so as to raise
|
|
643 |
# no Combo url so as to raise
|
606 |
644 |
Wcs.objects.create(base_url='https://wcs.dev.publik.love')
|
607 |
645 |
Fargo.objects.create(base_url='https://fargo.dev.publik.love')
|
608 |
646 |
|
... | ... | |
625 |
663 |
DEL_UNKNOWN_USER, MATOMO_SUCCESS,
|
626 |
664 |
JAVASCRIPT_TAG_BAD_RESPONSE]
|
627 |
665 |
mocked_post.side_effect = requests_post_mocked_replies(contents)
|
628 |
|
try:
|
|
666 |
with pytest.raises(MatomoException) as exc:
|
|
667 |
auto_configure_matomo()
|
|
668 |
assert "get_javascript_tag fails" in str(exc)
|
|
669 |
tracking_js_var = get_variable('visits_tracking_js')
|
|
670 |
assert tracking_js_var.value == 'js_code'
|
|
671 |
|
|
672 |
with override_settings(MATOMO_SERVER=CONFIG):
|
|
673 |
contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS,
|
|
674 |
DEL_UNKNOWN_USER, MATOMO_SUCCESS,
|
|
675 |
JAVASCRIPT_TAG, PING_ERROR]
|
|
676 |
mocked_post.side_effect = requests_post_mocked_replies(contents)
|
|
677 |
with pytest.raises(MatomoException) as exc:
|
629 |
678 |
auto_configure_matomo()
|
630 |
|
except MatomoException as exc:
|
631 |
|
assert str(exc) == "get_javascript_tag fails"
|
632 |
|
else:
|
633 |
|
assert False
|
|
679 |
assert 'ping fails' in str(exc)
|
634 |
680 |
tracking_js_var = get_variable('visits_tracking_js')
|
635 |
681 |
assert tracking_js_var.value == 'js_code'
|