Projet

Général

Profil

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

Nicolas Roche, 06 mai 2019 14:09

Télécharger (7,23 ko)

Voir les différences:

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

 hobo/matomo/utils.py       | 20 ++++++++++++
 tests/test_matomo_utils.py | 62 +++++++++++++++++++++++++++++++++-----
 tests/test_matomo_views.py |  5 ++-
 3 files changed, 78 insertions(+), 9 deletions(-)
hobo/matomo/utils.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17
import json
17 18
import hashlib
18 19
import re
19 20
import requests
......
215 216
            raise MatomoException('get_javascript_tag fails')
216 217
        return tag.text
217 218

  
219
    def ping(self, id_site):
220
        """JSON API here"""
221
        url = "%s/matomo.php" % self.url_ws_base
222
        data = '{"requests":["?idsite=%s&action_name=ping&rec=1"]}' % id_site
223
        resp = requests.post(url, data=data)
224
        try:
225
            tree = json.loads(resp.content)
226
        except ValueError:
227
            raise MatomoException(
228
                'internal error on ping (JSON expected): %s' % resp.content)
229
        try:
230
            if tree['status'] != 'success':
231
                raise MatomoException('ping fails: %s' % resp.content)
232
        except KeyError:
233
            raise MatomoException(
234
                'internal error on ping (status expected): %s' % resp.content)
235

  
236

  
218 237
def upgrade_site(matomo, tenant_name, site_urls):
219 238
    try:
220 239
        # tenant name match because it is the basename of one of registered urls
......
293 312
    id_site = upgrade_site(matomo, tenant_name, site_urls)
294 313
    logme_url = upgrade_user(matomo, tenant_name, id_site)
295 314
    tracking_js = upgrade_javascript_tag(matomo, id_site)
315
    matomo.ping(id_site)
296 316

  
297 317
    # save matomo's variables
298 318
    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 = '{"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'
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
-