Projet

Général

Profil

0001-matomo-simulate-first-tracking-visit-32796.patch

Nicolas Roche, 06 mai 2019 00:03

Télécharger (6,01 ko)

Voir les différences:

Subject: [PATCH] matomo: simulate first tracking visit (#32796)

 hobo/matomo/utils.py       |  9 +++++++++
 tests/test_matomo_utils.py | 46 ++++++++++++++++++++++++++++++++++++++--------
 tests/test_matomo_views.py |  5 ++++-
 3 files changed, 51 insertions(+), 9 deletions(-)
hobo/matomo/utils.py
215 215
            raise MatomoException('get_javascript_tag fails')
216 216
        return tag.text
217 217

  
218
    def ping(self, id_site):
219
        url = "%s/matomo.php" % self.url_ws_base
220
        data = '{"requests":["?idsite=%s&action_name=ping&rec=1"]}' % id_site
221
        resp = requests.post(url, data=data)
222
        if resp.content != '{"status":"success","tracked":1,"invalid":0}':
223
            raise MatomoException('ping fails: %s' % resp.content)
224

  
225

  
218 226
def upgrade_site(matomo, tenant_name, site_urls):
219 227
    try:
220 228
        # tenant name match because it is the basename of one of registered urls
......
293 301
    id_site = upgrade_site(matomo, tenant_name, site_urls)
294 302
    logme_url = upgrade_user(matomo, tenant_name, id_site)
295 303
    tracking_js = upgrade_javascript_tag(matomo, id_site)
304
    matomo.ping(id_site)
296 305

  
297 306
    # save matomo's variables
298 307
    logme_url_var = get_variable('matomo_logme_url')
tests/test_matomo_utils.py
161 161
<no_result_tag/>
162 162
"""
163 163

  
164
PING_SUCCESS = '{"status":"success","tracked":1,"invalid":0}'
165
PING_ERROR = 'somethings else'
166

  
164 167
def requests_post_mocked_replies(contents):
165 168
    """buid an iterator for mock's side_effect parameter"""
166 169
    responses = []
......
499 502
            assert False
500 503

  
501 504
@mock.patch('requests.post')
505
def test_ping(mocked_post):
506
    """webservice to simulate a first tracking call"""
507
    with override_settings(MATOMO_SERVER=CONFIG):
508
        matomo = MatomoWS()
509

  
510
        # success
511
        content = PING_SUCCESS
512
        mocked_post.return_value.content = content
513
        matomo.ping('42')
514
        assert True
515

  
516
        # error
517
        content = PING_ERROR
518
        mocked_post.return_value.content = content
519
        with pytest.raises(MatomoException) as exc:
520
            matomo.ping('42')
521
        assert 'ping fails' in str(exc)
522

  
523
@mock.patch('requests.post')
502 524
def test_upgrade_site(mocked_post):
503 525
    """function to test if the site is already regisered"""
504 526
    urls = ['https://combo.dev.publik.love',
......
627 649
    value1 = get_variable_value('cnil_compliant_visits_tracking_js', 'undefined')
628 650
    value2 = get_variable_value('visits_tracking_js', 'undefined')
629 651
    assert value1 == 'undefined'
630
    assert value2 == 'undefined' 
652
    assert value2 == 'undefined'
631 653

  
632 654
@mock.patch('requests.post')
633 655
def test_upgrade_javascript_tag(mocked_post):
......
658 680
    with override_settings(MATOMO_SERVER=CONFIG):
659 681
        contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS,
660 682
                    DEL_UNKNOWN_USER, MATOMO_SUCCESS,
661
                    JAVASCRIPT_TAG]
683
                    JAVASCRIPT_TAG, PING_SUCCESS]
662 684
        mocked_post.side_effect = requests_post_mocked_replies(contents)
663 685
        assert auto_configure_matomo() is True
664 686
        logme_url_var = get_variable('matomo_logme_url')
......
670 692

  
671 693
@mock.patch('requests.post')
672 694
def test_auto_configure_matomo_no_url(mocked_post):
673
    # no Wc url so as to raise
695
    # no Combo url so as to raise
674 696
    Wcs.objects.create(base_url='https://wcs.dev.publik.love')
675 697
    Fargo.objects.create(base_url='https://fargo.dev.publik.love')
676 698

  
......
696 718
                    DEL_UNKNOWN_USER, MATOMO_SUCCESS,
697 719
                    JAVASCRIPT_TAG_BAD_RESPONSE]
698 720
        mocked_post.side_effect = requests_post_mocked_replies(contents)
699
        try:
721
        with pytest.raises(MatomoException) as exc:
700 722
            auto_configure_matomo()
701
        except MatomoException as exc:
702
            assert str(exc) == "get_javascript_tag fails"
703
        else:
704
            assert False
723
        assert "get_javascript_tag fails" in str(exc)
724
        tracking_js_var = get_variable('visits_tracking_js')
725
        assert tracking_js_var.value == 'js_code'
726

  
727
    with override_settings(MATOMO_SERVER=CONFIG):
728
        contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS,
729
                    DEL_UNKNOWN_USER, MATOMO_SUCCESS,
730
                    JAVASCRIPT_TAG, PING_ERROR]
731
        mocked_post.side_effect = requests_post_mocked_replies(contents)
732
        with pytest.raises(MatomoException) as exc:
733
            auto_configure_matomo()
734
        assert 'ping fails' in str(exc)
705 735
        tracking_js_var = get_variable('visits_tracking_js')
706 736
        assert tracking_js_var.value == 'js_code'
tests/test_matomo_views.py
62 62
</result>
63 63
"""
64 64

  
65
PING_SUCCESS = '{"status":"success","tracked":1,"invalid":0}'
66
PING_ERROR = 'somethings else'
67

  
65 68
@pytest.fixture
66 69
def admin_user():
67 70
    try:
......
123 126
def auto_conf_mocked_post(url, **kwargs):
124 127
    contents = [GET_NO_SITE_FROM_URL, ADD_SITE_SUCCESS,
125 128
                DEL_UNKNOWN_USER, MATOMO_SUCCESS,
126
                JAVASCRIPT_TAG]
129
                JAVASCRIPT_TAG, PING_SUCCESS]
127 130
    response = Response()
128 131
    response._content = contents[auto_conf_mocked_post.cpt]
129 132
    response.status_code = 200
130
-