Projet

Général

Profil

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

Nicolas Roche, 06 mai 2019 14:09

Télécharger (12,1 ko)

Voir les différences:

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

 tests/test_matomo_utils.py | 159 ++++++++++---------------------------
 1 file changed, 44 insertions(+), 115 deletions(-)
tests/test_matomo_utils.py
211 211
        assert matomo.token_auth == '1234'
212 212

  
213 213
    with override_settings(MATOMO_SERVER={}):
214
        try:
214
        with pytest.raises(MatomoException) as exc:
215 215
            matomo = MatomoWS()
216
        except MatomoError as exc:
217
            assert str(exc) == "no settings for matomo: 'URL'"
218
        else:
219
            assert False
216
        assert "no settings for matomo: 'URL'" in str(exc)
220 217

  
221 218
def test_parse_response():
222 219
    """parser used by all matomo webservice calls"""
......
230 227

  
231 228
        # error (not XML format)
232 229
        content = """this is not XML"""
233
        try:
230
        with pytest.raises(MatomoException) as exc:
234 231
            tree = matomo.parse_response(content)
235
        except MatomoException as exc:
236
            assert str(exc).find("XMLSyntaxError: Start tag expected") != -1
237
        else:
238
            assert False
232
            assert "XMLSyntaxError: Start tag expected" in str(exc)
239 233

  
240 234
def test_parse_error_message():
241 235
    """error handler used by all matomo webservice calls"""
......
255 249
</result>
256 250
"""
257 251
        tree = matomo.parse_response(content)
258
        try:
252
        with pytest.raises(MatomoError) as exc:
259 253
            matomo.raise_on_error(tree)
260
        except MatomoError as exc:
261
            assert str(exc) == 'here is the error message'
262
        else:
263
            assert False
254
        assert 'here is the error message' in str(exc)
264 255

  
265 256
        # error (unexpected format)
266 257
        content = """<?xml version="1.0" encoding="utf-8" ?>
......
269 260
</result>
270 261
"""
271 262
        tree = matomo.parse_response(content)
272
        try:
263
        with pytest.raises(MatomoException) as exc:
273 264
            matomo.raise_on_error(tree)
274
        except MatomoException as exc:
275
            assert str(exc) == 'internal error'
276
        else:
277
            assert False
265
        assert 'internal error' in str(exc)
278 266

  
279 267
@mock.patch('requests.post')
280 268
def test_assert_success(mocked_post):
......
290 278
        # error (KO instead of ok)
291 279
        tree = matomo.parse_response(MATOMO_BAD_RESPONSE_1)
292 280
        matomo.raise_on_error(tree)
293
        try:
281
        with pytest.raises(MatomoException) as exc:
294 282
            matomo.assert_success(tree, 'me')
295
        except MatomoException as exc:
296
            assert str(exc).find('me fails') != -1
297
        else:
298
            assert False
283
        assert 'me fails' in str(exc)
299 284

  
300 285
        # error (no message attribute)
301 286
        tree = matomo.parse_response(MATOMO_BAD_RESPONSE_2)
302 287
        matomo.raise_on_error(tree)
303
        try:
288
        with pytest.raises(MatomoException) as exc:
304 289
            matomo.assert_success(tree, 'me')
305
        except MatomoException as exc:
306
            assert str(exc).find('me fails') != -1
307
        else:
308
            assert False
290
        assert 'me fails' in str(exc)
309 291

  
310 292
@mock.patch('requests.post')
311 293
def test_get_site_from_site_url(mocked_post):
......
321 303
        # no such url
322 304
        content = GET_NO_SITE_FROM_URL
323 305
        mocked_post.return_value.content = content
324
        try:
306
        with pytest.raises(MatomoError) as exc:
325 307
            matomo.get_site_id_from_site_url('combo.dev.publik.love')
326
        except MatomoError as exc:
327
            assert str(exc).find('url not found') != -1
328
        else:
329
            assert False
308
        assert 'url not found' in str(exc)
330 309

  
331 310
        # error on empty id
332 311
        content = GET_SITE_BAD_QUERY
333 312
        mocked_post.return_value.content = content
334
        try:
313
        with pytest.raises(MatomoError) as exc:
335 314
            matomo.get_site_id_from_site_url('combo.dev.publik.love')
336
        except MatomoError as exc:
337
            assert str(exc) == "Please specify a value for 'url'."
338
        else:
339
            assert False
315
        assert "Please specify a value for 'url'." in str(exc)
340 316

  
341 317
        # bad response (error on success response)
342 318
        content = GET_SITE_BAD_RESPONSE
343 319
        mocked_post.return_value.content = content
344
        try:
320
        with pytest.raises(MatomoException) as exc:
345 321
            matomo.get_site_id_from_site_url('combo.dev.publik.love')
346
        except MatomoException as exc:
347
            assert str(exc) == 'get_site_id_from_site_url fails'
348
        else:
349
            assert False
322
        assert 'get_site_id_from_site_url fails' in str(exc)
350 323

  
351 324
@mock.patch('requests.post')
352 325
def test_add_site(mocked_post):
......
365 338
        # error
366 339
        content = ADD_SITE_ERROR
367 340
        mocked_post.return_value.content = content
368
        try:
341
        with pytest.raises(MatomoError) as exc:
369 342
            site_id = matomo.add_site("hobo.dev.publik.love", urls)
370
        except MatomoError as exc:
371
            assert str(exc) == "Please specify a value for 'siteName'."
372
        else:
373
            assert False
343
        assert "Please specify a value for 'siteName'." in str(exc)
374 344

  
375 345
        # strange message
376 346
        content = ADD_SITE_BAD_RESPONSE
377 347
        mocked_post.return_value.content = content
378
        try:
348
        with pytest.raises(MatomoException) as exc:
379 349
            site_id = matomo.add_site("hobo.dev.publik.love", urls)
380
        except MatomoException as exc:
381
            assert str(exc) == 'add_site fails'
382
        else:
383
            assert False
350
        assert 'add_site fails' in str(exc)
384 351

  
385 352
@mock.patch('requests.post')
386 353
def test_add_user(mocked_post):
......
397 364
        # error (user already here)
398 365
        content = USER_ALREADY_THERE
399 366
        mocked_post.return_value.content = content
400
        try:
367
        with pytest.raises(MatomoError) as exc:
401 368
            matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
402
        except MatomoError as exc:
403
            assert str(exc).find("Username 'hobo.dev.publik.love' already") != -1
404
        else:
405
            assert False
369
        assert "Username 'hobo.dev.publik.love' already" in str(exc)
406 370

  
407 371
        # error (mail already registered)
408 372
        content = MAIL_ALREADY_THERE
409 373
        mocked_post.return_value.content = content
410
        try:
374
        with pytest.raises(MatomoError) as exc:
411 375
            matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
412
        except MatomoError as exc:
413
            assert str(exc).find("email 'hobo.dev.publik.love@testor.org'") != -1
414
        else:
415
            assert False
376
        assert "email 'hobo.dev.publik.love@testor.org'" in str(exc)
416 377

  
417 378
        # error (bad credentials)
418 379
        content = BAD_CREDENTIAL
419 380
        mocked_post.return_value.content = content
420
        try:
381
        with pytest.raises(MatomoError) as exc:
421 382
            matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
422
        except MatomoError as exc:
423
            assert str(exc).find("You can\'t access this resource") != -1
424
        else:
425
            assert False
383
        assert "You can\'t access this resource" in str(exc)
426 384

  
427 385
        # bad success message (wrong attribute value)
428 386
        content = MATOMO_BAD_RESPONSE_1
429 387
        mocked_post.return_value.content = content
430
        try:
388
        with pytest.raises(MatomoException) as exc:
431 389
            matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
432
        except MatomoException as exc:
433
            assert str(exc) == 'add_user fails'
434
        else:
435
            assert False
390
        assert 'add_user fails' in str(exc)
436 391

  
437 392
       # bad success message (no message attribute)
438 393
        content = MATOMO_BAD_RESPONSE_2
439 394
        mocked_post.return_value.content = content
440
        try:
395
        with pytest.raises(MatomoException) as exc:
441 396
            matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
442
        except MatomoException as exc:
443
            assert str(exc) == 'add_user fails'
444
        else:
445
            assert False
397
        assert 'add_user fails' in str(exc)
446 398

  
447 399
@mock.patch('requests.post')
448 400
def test_del_user(mocked_post):
......
459 411
        # error (unknown user)
460 412
        content = DEL_UNKNOWN_USER
461 413
        mocked_post.return_value.content = content
462
        try:
414
        with pytest.raises(MatomoError) as exc:
463 415
            matomo.del_user('hobo.dev.publik.love')
464
        except MatomoError as exc:
465
            assert str(exc).find("User 'hobo.dev.publik.love' doesn't exist.") != -1
466
        else:
467
            assert False
416
        assert "User 'hobo.dev.publik.love' doesn't exist." in str(exc)
468 417

  
469 418
@mock.patch('requests.post')
470 419
def test_get_javascript_tag(mocked_post):
......
481 430
        # error (bad credentials)
482 431
        content = BAD_CREDENTIAL
483 432
        mocked_post.return_value.content = content
484
        try:
433
        with pytest.raises(MatomoError) as exc:
485 434
            javascript_tag = matomo.get_javascript_tag('42')
486
        except MatomoError as exc:
487
            assert str(exc).find("You can't access this resource ") != -1
488
        else:
489
            assert False
435
        assert "You can't access this resource " in str(exc)
490 436

  
491 437
       # bad response (no result tag)
492 438
        content = JAVASCRIPT_TAG_BAD_RESPONSE
493 439
        mocked_post.return_value.content = content
494
        try:
440
        with pytest.raises(MatomoException) as exc:
495 441
            javascript_tag = matomo.get_javascript_tag('42')
496
        except MatomoException as exc:
497
            assert str(exc) == 'get_javascript_tag fails'
498
        else:
499
            assert False
442
        assert 'get_javascript_tag fails' in str(exc)
500 443

  
501 444
@mock.patch('requests.post')
502 445
def test_upgrade_site(mocked_post):
......
521 464
        # error while adding new site
522 465
        contents = [GET_NO_SITE_FROM_URL, MATOMO_ERROR]
523 466
        mocked_post.side_effect = requests_post_mocked_replies(contents)
524
        try:
467
        with pytest.raises(MatomoException):
525 468
            upgrade_site(matomo, "hobo.dev.publik.love", urls)
526
        except MatomoException as exc:
527
            assert True
528
        else:
529
            assert False
530 469

  
531 470
        # error while looking for site already there
532 471
        contents = [MATOMO_ERROR]
533 472
        mocked_post.side_effect = requests_post_mocked_replies(contents)
534
        try:
473
        with pytest.raises(MatomoException) as exc:
535 474
            upgrade_site(matomo, "hobo.dev.publik.love", urls)
536
        except MatomoException as exc:
537
            assert str(exc) == 'here is the error message'
538
        else:
539
            assert False
475
        assert 'here is the error message' in str(exc)
540 476

  
541 477
@mock.patch('requests.post')
542 478
def test_upgrade_user(mocked_post):
......
565 501
        # error (add user fails)
566 502
        contents = [MATOMO_SUCCESS, MATOMO_ERROR]
567 503
        mocked_post.side_effect = requests_post_mocked_replies(contents)
568
        try:
504
        with pytest.raises(MatomoError) as exc:
569 505
            upgrade_user(matomo, 'hobo.dev.publik.love', '42')
570
        except MatomoError:
571
            assert True
572
        else:
573
            assert False
574 506

  
575 507
def test_compute_cnil_acknowledgment_level():
576 508
    """function use to inspect javascript content"""
......
675 607
    Fargo.objects.create(base_url='https://fargo.dev.publik.love')
676 608

  
677 609
    with override_settings(MATOMO_SERVER=CONFIG):
678
        try:
610
        with pytest.raises(MatomoException) as exc:
679 611
            auto_configure_matomo()
680
        except MatomoException as exc:
681
            assert str(exc) == "no portal-user's url available"
682
        else:
683
            assert False
612
    assert "no portal-user's url available" in str(exc)
684 613

  
685 614
@mock.patch('requests.post')
686 615
def test_auto_configure_matomo_error(mocked_post):
687
-