Projet

Général

Profil

0002-matomo-rewrite-managing-exceptions-into-tests-32796.patch

Nicolas Roche, 06 mai 2019 21:00

Télécharger (12,5 ko)

Voir les différences:

Subject: [PATCH 2/4] matomo: rewrite managing exceptions into tests (#32796)

 tests/test_matomo_utils.py | 150 ++++++++-----------------------------
 1 file changed, 32 insertions(+), 118 deletions(-)
tests/test_matomo_utils.py
223 223
        assert matomo.token_auth == '1234'
224 224

  
225 225
    with override_settings(MATOMO_SERVER={}):
226
        try:
226
        with pytest.raises(MatomoException, match="no settings for matomo: 'URL'"):
227 227
            matomo = MatomoWS()
228
        except MatomoError as exc:
229
            assert str(exc) == "no settings for matomo: 'URL'"
230
        else:
231
            assert False
232 228

  
233 229
def test_parse_response():
234 230
    """parser used by all matomo webservice calls"""
......
242 238

  
243 239
        # error (not XML format)
244 240
        content = """this is not XML"""
245
        try:
241
        with pytest.raises(MatomoException,
242
                           match="XMLSyntaxError: Start tag expected"):
246 243
            tree = matomo.parse_response(content)
247
        except MatomoException as exc:
248
            assert str(exc).find("XMLSyntaxError: Start tag expected") != -1
249
        else:
250
            assert False
251 244

  
252 245
def test_parse_error_message():
253 246
    """error handler used by all matomo webservice calls"""
......
267 260
</result>
268 261
"""
269 262
        tree = matomo.parse_response(content)
270
        try:
263
        with pytest.raises(MatomoError, match='here is the error message'):
271 264
            matomo.raise_on_error(tree)
272
        except MatomoError as exc:
273
            assert str(exc) == 'here is the error message'
274
        else:
275
            assert False
276 265

  
277 266
        # error (unexpected format)
278 267
        content = """<?xml version="1.0" encoding="utf-8" ?>
......
281 270
</result>
282 271
"""
283 272
        tree = matomo.parse_response(content)
284
        try:
273
        with pytest.raises(MatomoException, match='internal error'):
285 274
            matomo.raise_on_error(tree)
286
        except MatomoException as exc:
287
            assert str(exc) == 'internal error'
288
        else:
289
            assert False
290 275

  
291 276
@mock.patch('requests.post')
292 277
def test_assert_success(mocked_post):
......
302 287
        # error (KO instead of ok)
303 288
        tree = matomo.parse_response(MATOMO_BAD_RESPONSE_1)
304 289
        matomo.raise_on_error(tree)
305
        try:
290
        with pytest.raises(MatomoException, match='me fails'):
306 291
            matomo.assert_success(tree, 'me')
307
        except MatomoException as exc:
308
            assert str(exc).find('me fails') != -1
309
        else:
310
            assert False
311 292

  
312 293
        # error (no message attribute)
313 294
        tree = matomo.parse_response(MATOMO_BAD_RESPONSE_2)
314 295
        matomo.raise_on_error(tree)
315
        try:
296
        with pytest.raises(MatomoException, match='me fails'):
316 297
            matomo.assert_success(tree, 'me')
317
        except MatomoException as exc:
318
            assert str(exc).find('me fails') != -1
319
        else:
320
            assert False
321 298

  
322 299
@mock.patch('requests.post')
323 300
def test_get_site_from_site_url(mocked_post):
......
333 310
        # no such url
334 311
        content = GET_NO_SITE_FROM_URL
335 312
        mocked_post.return_value.content = content
336
        try:
313
        with pytest.raises(MatomoError, match='url not found'):
337 314
            matomo.get_site_id_from_site_url('combo.dev.publik.love')
338
        except MatomoError as exc:
339
            assert str(exc).find('url not found') != -1
340
        else:
341
            assert False
342 315

  
343 316
        # error on empty id
344 317
        content = GET_SITE_BAD_QUERY
345 318
        mocked_post.return_value.content = content
346
        try:
319
        with pytest.raises(MatomoError, match="Please specify a value for 'url'."):
347 320
            matomo.get_site_id_from_site_url('combo.dev.publik.love')
348
        except MatomoError as exc:
349
            assert str(exc) == "Please specify a value for 'url'."
350
        else:
351
            assert False
352 321

  
353 322
        # bad response (error on success response)
354 323
        content = GET_SITE_BAD_RESPONSE
355 324
        mocked_post.return_value.content = content
356
        try:
325
        with pytest.raises(MatomoException,
326
                           match='get_site_id_from_site_url fails'):
357 327
            matomo.get_site_id_from_site_url('combo.dev.publik.love')
358
        except MatomoException as exc:
359
            assert str(exc) == 'get_site_id_from_site_url fails'
360
        else:
361
            assert False
362 328

  
363 329
@mock.patch('requests.post')
364 330
def test_add_site(mocked_post):
......
377 343
        # error
378 344
        content = ADD_SITE_ERROR
379 345
        mocked_post.return_value.content = content
380
        try:
346
        with pytest.raises(MatomoError,
347
                           match="Please specify a value for 'siteName'."):
381 348
            site_id = matomo.add_site("hobo.dev.publik.love", urls)
382
        except MatomoError as exc:
383
            assert str(exc) == "Please specify a value for 'siteName'."
384
        else:
385
            assert False
386 349

  
387 350
        # strange message
388 351
        content = ADD_SITE_BAD_RESPONSE
389 352
        mocked_post.return_value.content = content
390
        try:
353
        with pytest.raises(MatomoException, match='add_site fails'):
391 354
            site_id = matomo.add_site("hobo.dev.publik.love", urls)
392
        except MatomoException as exc:
393
            assert str(exc) == 'add_site fails'
394
        else:
395
            assert False
396 355

  
397 356
@mock.patch('requests.post')
398 357
def test_add_user(mocked_post):
......
409 368
        # error (user already here)
410 369
        content = USER_ALREADY_THERE
411 370
        mocked_post.return_value.content = content
412
        try:
371
        with pytest.raises(MatomoError,
372
                           match="Username 'hobo.dev.publik.love' already"):
413 373
            matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
414
        except MatomoError as exc:
415
            assert str(exc).find("Username 'hobo.dev.publik.love' already") != -1
416
        else:
417
            assert False
418 374

  
419 375
        # error (mail already registered)
420 376
        content = MAIL_ALREADY_THERE
421 377
        mocked_post.return_value.content = content
422
        try:
378
        with pytest.raises(MatomoError,
379
                           match="email 'hobo.dev.publik.love@testor.org'"):
423 380
            matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
424
        except MatomoError as exc:
425
            assert str(exc).find("email 'hobo.dev.publik.love@testor.org'") != -1
426
        else:
427
            assert False
428 381

  
429 382
        # error (bad credentials)
430 383
        content = BAD_CREDENTIAL
431 384
        mocked_post.return_value.content = content
432
        try:
385
        with pytest.raises(MatomoError, match="You can\'t access this resource"):
433 386
            matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
434
        except MatomoError as exc:
435
            assert str(exc).find("You can\'t access this resource") != -1
436
        else:
437
            assert False
438 387

  
439 388
        # bad success message (wrong attribute value)
440 389
        content = MATOMO_BAD_RESPONSE_1
441 390
        mocked_post.return_value.content = content
442
        try:
391
        with pytest.raises(MatomoException, match='add_user fails'):
443 392
            matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
444
        except MatomoException as exc:
445
            assert str(exc) == 'add_user fails'
446
        else:
447
            assert False
448 393

  
449 394
       # bad success message (no message attribute)
450 395
        content = MATOMO_BAD_RESPONSE_2
451 396
        mocked_post.return_value.content = content
452
        try:
397
        with pytest.raises(MatomoException, match='add_user fails'):
453 398
            matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
454
        except MatomoException as exc:
455
            assert str(exc) == 'add_user fails'
456
        else:
457
            assert False
458 399

  
459 400
@mock.patch('requests.post')
460 401
def test_del_user(mocked_post):
461
    """webservice to add new user"""
402
    """webservice to del an existing user"""
403
    mocked_post.return_value.status_code = 200
462 404
    with override_settings(MATOMO_SERVER=CONFIG):
463 405
        matomo = MatomoWS()
464 406

  
......
471 413
        # error (unknown user)
472 414
        content = DEL_UNKNOWN_USER
473 415
        mocked_post.return_value.content = content
474
        try:
416
        with pytest.raises(MatomoError,
417
                           match="User 'hobo.dev.publik.love' doesn't exist."):
475 418
            matomo.del_user('hobo.dev.publik.love')
476
        except MatomoError as exc:
477
            assert str(exc).find("User 'hobo.dev.publik.love' doesn't exist.") != -1
478
        else:
479
            assert False
480 419

  
481 420
@mock.patch('requests.post')
482 421
def test_get_javascript_tag(mocked_post):
......
493 432
        # error (bad credentials)
494 433
        content = BAD_CREDENTIAL
495 434
        mocked_post.return_value.content = content
496
        try:
435
        with pytest.raises(MatomoError, match="You can't access this resource"):
497 436
            javascript_tag = matomo.get_javascript_tag('42')
498
        except MatomoError as exc:
499
            assert str(exc).find("You can't access this resource ") != -1
500
        else:
501
            assert False
502 437

  
503 438
       # bad response (no result tag)
504 439
        content = JAVASCRIPT_TAG_BAD_RESPONSE
505 440
        mocked_post.return_value.content = content
506
        try:
441
        with pytest.raises(MatomoException, match='get_javascript_tag fails'):
507 442
            javascript_tag = matomo.get_javascript_tag('42')
508
        except MatomoException as exc:
509
            assert str(exc) == 'get_javascript_tag fails'
510
        else:
511
            assert False
512 443

  
513 444
@mock.patch('requests.post')
514 445
def test_ping(mocked_post):
......
577 508
        # error while adding new site
578 509
        contents = [GET_NO_SITE_FROM_URL, MATOMO_ERROR]
579 510
        mocked_post.side_effect = requests_post_mocked_replies(contents)
580
        try:
511
        with pytest.raises(MatomoException):
581 512
            upgrade_site(matomo, "hobo.dev.publik.love", urls)
582
        except MatomoException as exc:
583
            assert True
584
        else:
585
            assert False
586 513

  
587 514
        # error while looking for site already there
588 515
        contents = [MATOMO_ERROR]
589 516
        mocked_post.side_effect = requests_post_mocked_replies(contents)
590
        try:
517
        with pytest.raises(MatomoException, match='here is the error message'):
591 518
            upgrade_site(matomo, "hobo.dev.publik.love", urls)
592
        except MatomoException as exc:
593
            assert str(exc) == 'here is the error message'
594
        else:
595
            assert False
596 519

  
597 520
@mock.patch('requests.post')
598 521
def test_upgrade_user(mocked_post):
......
621 544
        # error (add user fails)
622 545
        contents = [MATOMO_SUCCESS, MATOMO_ERROR]
623 546
        mocked_post.side_effect = requests_post_mocked_replies(contents)
624
        try:
547
        with pytest.raises(MatomoError):
625 548
            upgrade_user(matomo, 'hobo.dev.publik.love', '42')
626
        except MatomoError:
627
            assert True
628
        else:
629
            assert False
630 549

  
631 550
def test_compute_cnil_acknowledgment_level():
632 551
    """function use to inspect javascript content"""
......
731 650
    Fargo.objects.create(base_url='https://fargo.dev.publik.love')
732 651

  
733 652
    with override_settings(MATOMO_SERVER=CONFIG):
734
        try:
653
        with pytest.raises(MatomoException, match="no portal-user's url available"):
735 654
            auto_configure_matomo()
736
        except MatomoException as exc:
737
            assert str(exc) == "no portal-user's url available"
738
        else:
739
            assert False
740 655

  
741 656
@mock.patch('requests.post')
742 657
def test_auto_configure_matomo_error(mocked_post):
......
752 667
                    DEL_UNKNOWN_USER, MATOMO_SUCCESS,
753 668
                    JAVASCRIPT_TAG_BAD_RESPONSE]
754 669
        mocked_post.side_effect = requests_post_mocked_replies(contents)
755
        with pytest.raises(MatomoException) as exc:
670
        with pytest.raises(MatomoException, match="get_javascript_tag fails"):
756 671
            auto_configure_matomo()
757
        assert "get_javascript_tag fails" in str(exc)
758 672
        tracking_js_var = get_variable('visits_tracking_js')
759 673
        assert tracking_js_var.value == 'js_code'
760 674

  
761
-