Projet

Général

Profil

0003-matomo-manage-http-status-codes-32796.patch

Nicolas Roche, 06 mai 2019 21:00

Télécharger (5,23 ko)

Voir les différences:

Subject: [PATCH 3/4] matomo: manage http status codes (#32796)

 hobo/matomo/utils.py       |  4 ++++
 tests/test_matomo_utils.py | 34 ++++++++++++++++++++++++++++++----
 2 files changed, 34 insertions(+), 4 deletions(-)
hobo/matomo/utils.py
155 155
        data['module'] = 'API'
156 156
        data['token_auth'] = self.token_auth
157 157
        resp = requests.post(self.url_ws_base, data=data)
158
        if resp.status_code != 200:
159
            raise MatomoException('unexpected status code: %s' % resp.status_code)
158 160
        tree = self.parse_response(resp.content)
159 161
        self.raise_on_error(tree)
160 162
        return tree
......
219 221
        url = "%s/matomo.php" % self.url_ws_base
220 222
        data = '{"requests":["?idsite=%s&action_name=ping&rec=1"]}' % id_site
221 223
        resp = requests.post(url, json=data)
224
        if resp.status_code != 200:
225
            raise MatomoException('unexpected status code: %s' % resp.status_code)
222 226
        try:
223 227
            tree = resp.json()
224 228
        except ValueError:
tests/test_matomo_utils.py
273 273
        with pytest.raises(MatomoException, match='internal error'):
274 274
            matomo.raise_on_error(tree)
275 275

  
276
@mock.patch('requests.post')
277
def test_assert_success(mocked_post):
276
def test_assert_success():
278 277
    """webservice to add new user"""
279 278
    with override_settings(MATOMO_SERVER=CONFIG):
280 279
        matomo = MatomoWS()
......
296 295
        with pytest.raises(MatomoException, match='me fails'):
297 296
            matomo.assert_success(tree, 'me')
298 297

  
298
@mock.patch('requests.post')
299
def test_call(mocked_post):
300
    """test status_code"""
301
    with override_settings(MATOMO_SERVER=CONFIG):
302
        matomo = MatomoWS()
303

  
304
        # success (status 200)
305
        content = MATOMO_SUCCESS
306
        mocked_post.return_value.status_code = 200
307
        mocked_post.return_value.content = content
308
        matomo.call({})
309

  
310
        # failure (status 500)
311
        mocked_post.return_value.status_code = 500
312
        with pytest.raises(MatomoException, match='unexpected status code: 500'):
313
            matomo.call({})
314

  
299 315
@mock.patch('requests.post')
300 316
def test_get_site_from_site_url(mocked_post):
301 317
    """webservice to test if the site is already registered"""
318
    mocked_post.return_value.status_code = 200
302 319
    with override_settings(MATOMO_SERVER=CONFIG):
303 320
        matomo = MatomoWS()
304 321

  
......
329 346
@mock.patch('requests.post')
330 347
def test_add_site(mocked_post):
331 348
    """webservice to add a new site"""
349
    mocked_post.return_value.status_code = 200
332 350
    urls = ['https://combo.dev.publik.love',
333 351
            'https://wcs.dev.publik.love']
334 352
    with override_settings(MATOMO_SERVER=CONFIG):
......
356 374
@mock.patch('requests.post')
357 375
def test_add_user(mocked_post):
358 376
    """webservice to add new user"""
377
    mocked_post.return_value.status_code = 200
359 378
    with override_settings(MATOMO_SERVER=CONFIG):
360 379
        matomo = MatomoWS()
361 380

  
......
420 439
@mock.patch('requests.post')
421 440
def test_get_javascript_tag(mocked_post):
422 441
    """webservice to get matomo JS tag"""
442
    mocked_post.return_value.status_code = 200
423 443
    with override_settings(MATOMO_SERVER=CONFIG):
424 444
        matomo = MatomoWS()
425 445

  
......
447 467
    with override_settings(MATOMO_SERVER=CONFIG):
448 468
        matomo = MatomoWS()
449 469
        response = Response()
470
        response.status_code = 200
450 471

  
451 472
        # success
452 473
        content = PING_SUCCESS
......
485 506
                           match='internal error on ping \(JSON expected\)'):
486 507
            matomo.ping('42')
487 508

  
509
        # failure (status 500)
510
        mocked_post.return_value.status_code = 500
511
        with pytest.raises(MatomoException, match='unexpected status code: 500'):
512
            matomo.ping('42')
513

  
488 514
@mock.patch('requests.post')
489 515
def test_upgrade_site(mocked_post):
490 516
    """function to test if the site is already regisered"""
......
607 633
@mock.patch('requests.post')
608 634
def test_upgrade_javascript_tag(mocked_post):
609 635
    """function to get matomo JS tag"""
636
    mocked_post.return_value.status_code = 200
610 637
    with override_settings(MATOMO_SERVER=CONFIG):
611 638
        matomo = MatomoWS()
612 639

  
......
643 670
        tracking_js2_var = get_variable('cnil_compliant_visits_tracking_js')
644 671
        assert tracking_js2_var.value != ''
645 672

  
646
@mock.patch('requests.post')
647
def test_auto_configure_matomo_no_url(mocked_post):
673
def test_auto_configure_matomo_no_url():
648 674
    # no Combo url so as to raise
649 675
    Wcs.objects.create(base_url='https://wcs.dev.publik.love')
650 676
    Fargo.objects.create(base_url='https://fargo.dev.publik.love')
651
-