Projet

Général

Profil

0002-matomo-rewrite-holding-exceptions-into-tests.patch

Nicolas Roche, 06 mai 2019 17:19

Télécharger (12,3 ko)

Voir les différences:

Subject: [PATCH 2/2] matomo: rewrite holding exceptions into tests

 tests/test_matomo_utils.py | 147 ++++++++-----------------------------
 1 file changed, 30 insertions(+), 117 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):
......
471 412
        # error (unknown user)
472 413
        content = DEL_UNKNOWN_USER
473 414
        mocked_post.return_value.content = content
474
        try:
415
        with pytest.raises(MatomoError) as exc:
475 416
            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
417
        assert "User 'hobo.dev.publik.love' doesn't exist." in str(exc)
480 418

  
481 419
@mock.patch('requests.post')
482 420
def test_get_javascript_tag(mocked_post):
......
493 431
        # error (bad credentials)
494 432
        content = BAD_CREDENTIAL
495 433
        mocked_post.return_value.content = content
496
        try:
434
        with pytest.raises(MatomoError, match="You can't access this resource"):
497 435
            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 436

  
503 437
       # bad response (no result tag)
504 438
        content = JAVASCRIPT_TAG_BAD_RESPONSE
505 439
        mocked_post.return_value.content = content
506
        try:
440
        with pytest.raises(MatomoException, match='get_javascript_tag fails'):
507 441
            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 442

  
513 443
@mock.patch('requests.post')
514 444
def test_ping(mocked_post):
......
570 500
        # error while adding new site
571 501
        contents = [GET_NO_SITE_FROM_URL, MATOMO_ERROR]
572 502
        mocked_post.side_effect = requests_post_mocked_replies(contents)
573
        try:
503
        with pytest.raises(MatomoException):
574 504
            upgrade_site(matomo, "hobo.dev.publik.love", urls)
575
        except MatomoException as exc:
576
            assert True
577
        else:
578
            assert False
579 505

  
580 506
        # error while looking for site already there
581 507
        contents = [MATOMO_ERROR]
582 508
        mocked_post.side_effect = requests_post_mocked_replies(contents)
583
        try:
509
        with pytest.raises(MatomoException, match='here is the error message'):
584 510
            upgrade_site(matomo, "hobo.dev.publik.love", urls)
585
        except MatomoException as exc:
586
            assert str(exc) == 'here is the error message'
587
        else:
588
            assert False
589 511

  
590 512
@mock.patch('requests.post')
591 513
def test_upgrade_user(mocked_post):
......
614 536
        # error (add user fails)
615 537
        contents = [MATOMO_SUCCESS, MATOMO_ERROR]
616 538
        mocked_post.side_effect = requests_post_mocked_replies(contents)
617
        try:
539
        with pytest.raises(MatomoError):
618 540
            upgrade_user(matomo, 'hobo.dev.publik.love', '42')
619
        except MatomoError:
620
            assert True
621
        else:
622
            assert False
623 541

  
624 542
def test_compute_cnil_acknowledgment_level():
625 543
    """function use to inspect javascript content"""
......
724 642
    Fargo.objects.create(base_url='https://fargo.dev.publik.love')
725 643

  
726 644
    with override_settings(MATOMO_SERVER=CONFIG):
727
        try:
645
        with pytest.raises(MatomoException, match="no portal-user's url available"):
728 646
            auto_configure_matomo()
729
        except MatomoException as exc:
730
            assert str(exc) == "no portal-user's url available"
731
        else:
732
            assert False
733 647

  
734 648
@mock.patch('requests.post')
735 649
def test_auto_configure_matomo_error(mocked_post):
......
745 659
                    DEL_UNKNOWN_USER, MATOMO_SUCCESS,
746 660
                    JAVASCRIPT_TAG_BAD_RESPONSE]
747 661
        mocked_post.side_effect = requests_post_mocked_replies(contents)
748
        with pytest.raises(MatomoException) as exc:
662
        with pytest.raises(MatomoException, match="get_javascript_tag fails"):
749 663
            auto_configure_matomo()
750
        assert "get_javascript_tag fails" in str(exc)
751 664
        tracking_js_var = get_variable('visits_tracking_js')
752 665
        assert tracking_js_var.value == 'js_code'
753 666

  
754
-