From acdbbca59c9ea26e6da7aff67d311bc5fb13539c Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Mon, 6 May 2019 13:44:14 +0200
Subject: [PATCH 1/2] matomo: rewrite holding exceptions into tests

---
 tests/test_matomo_utils.py | 159 ++++++++++---------------------------
 1 file changed, 44 insertions(+), 115 deletions(-)

diff --git a/tests/test_matomo_utils.py b/tests/test_matomo_utils.py
index 9a29c67..0145a08 100644
--- a/tests/test_matomo_utils.py
+++ b/tests/test_matomo_utils.py
@@ -211,12 +211,9 @@ def test_matomo_constructor():
         assert matomo.token_auth == '1234'
 
     with override_settings(MATOMO_SERVER={}):
-        try:
+        with pytest.raises(MatomoException) as exc:
             matomo = MatomoWS()
-        except MatomoError as exc:
-            assert str(exc) == "no settings for matomo: 'URL'"
-        else:
-            assert False
+        assert "no settings for matomo: 'URL'" in str(exc)
 
 def test_parse_response():
     """parser used by all matomo webservice calls"""
@@ -230,12 +227,9 @@ def test_parse_response():
 
         # error (not XML format)
         content = """this is not XML"""
-        try:
+        with pytest.raises(MatomoException) as exc:
             tree = matomo.parse_response(content)
-        except MatomoException as exc:
-            assert str(exc).find("XMLSyntaxError: Start tag expected") != -1
-        else:
-            assert False
+            assert "XMLSyntaxError: Start tag expected" in str(exc)
 
 def test_parse_error_message():
     """error handler used by all matomo webservice calls"""
@@ -255,12 +249,9 @@ def test_parse_error_message():
 </result>
 """
         tree = matomo.parse_response(content)
-        try:
+        with pytest.raises(MatomoError) as exc:
             matomo.raise_on_error(tree)
-        except MatomoError as exc:
-            assert str(exc) == 'here is the error message'
-        else:
-            assert False
+        assert 'here is the error message' in str(exc)
 
         # error (unexpected format)
         content = """<?xml version="1.0" encoding="utf-8" ?>
@@ -269,12 +260,9 @@ def test_parse_error_message():
 </result>
 """
         tree = matomo.parse_response(content)
-        try:
+        with pytest.raises(MatomoException) as exc:
             matomo.raise_on_error(tree)
-        except MatomoException as exc:
-            assert str(exc) == 'internal error'
-        else:
-            assert False
+        assert 'internal error' in str(exc)
 
 @mock.patch('requests.post')
 def test_assert_success(mocked_post):
@@ -290,22 +278,16 @@ def test_assert_success(mocked_post):
         # error (KO instead of ok)
         tree = matomo.parse_response(MATOMO_BAD_RESPONSE_1)
         matomo.raise_on_error(tree)
-        try:
+        with pytest.raises(MatomoException) as exc:
             matomo.assert_success(tree, 'me')
-        except MatomoException as exc:
-            assert str(exc).find('me fails') != -1
-        else:
-            assert False
+        assert 'me fails' in str(exc)
 
         # error (no message attribute)
         tree = matomo.parse_response(MATOMO_BAD_RESPONSE_2)
         matomo.raise_on_error(tree)
-        try:
+        with pytest.raises(MatomoException) as exc:
             matomo.assert_success(tree, 'me')
-        except MatomoException as exc:
-            assert str(exc).find('me fails') != -1
-        else:
-            assert False
+        assert 'me fails' in str(exc)
 
 @mock.patch('requests.post')
 def test_get_site_from_site_url(mocked_post):
@@ -321,32 +303,23 @@ def test_get_site_from_site_url(mocked_post):
         # no such url
         content = GET_NO_SITE_FROM_URL
         mocked_post.return_value.content = content
-        try:
+        with pytest.raises(MatomoError) as exc:
             matomo.get_site_id_from_site_url('combo.dev.publik.love')
-        except MatomoError as exc:
-            assert str(exc).find('url not found') != -1
-        else:
-            assert False
+        assert 'url not found' in str(exc)
 
         # error on empty id
         content = GET_SITE_BAD_QUERY
         mocked_post.return_value.content = content
-        try:
+        with pytest.raises(MatomoError) as exc:
             matomo.get_site_id_from_site_url('combo.dev.publik.love')
-        except MatomoError as exc:
-            assert str(exc) == "Please specify a value for 'url'."
-        else:
-            assert False
+        assert "Please specify a value for 'url'." in str(exc)
 
         # bad response (error on success response)
         content = GET_SITE_BAD_RESPONSE
         mocked_post.return_value.content = content
-        try:
+        with pytest.raises(MatomoException) as exc:
             matomo.get_site_id_from_site_url('combo.dev.publik.love')
-        except MatomoException as exc:
-            assert str(exc) == 'get_site_id_from_site_url fails'
-        else:
-            assert False
+        assert 'get_site_id_from_site_url fails' in str(exc)
 
 @mock.patch('requests.post')
 def test_add_site(mocked_post):
@@ -365,22 +338,16 @@ def test_add_site(mocked_post):
         # error
         content = ADD_SITE_ERROR
         mocked_post.return_value.content = content
-        try:
+        with pytest.raises(MatomoError) as exc:
             site_id = matomo.add_site("hobo.dev.publik.love", urls)
-        except MatomoError as exc:
-            assert str(exc) == "Please specify a value for 'siteName'."
-        else:
-            assert False
+        assert "Please specify a value for 'siteName'." in str(exc)
 
         # strange message
         content = ADD_SITE_BAD_RESPONSE
         mocked_post.return_value.content = content
-        try:
+        with pytest.raises(MatomoException) as exc:
             site_id = matomo.add_site("hobo.dev.publik.love", urls)
-        except MatomoException as exc:
-            assert str(exc) == 'add_site fails'
-        else:
-            assert False
+        assert 'add_site fails' in str(exc)
 
 @mock.patch('requests.post')
 def test_add_user(mocked_post):
@@ -397,52 +364,37 @@ def test_add_user(mocked_post):
         # error (user already here)
         content = USER_ALREADY_THERE
         mocked_post.return_value.content = content
-        try:
+        with pytest.raises(MatomoError) as exc:
             matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
-        except MatomoError as exc:
-            assert str(exc).find("Username 'hobo.dev.publik.love' already") != -1
-        else:
-            assert False
+        assert "Username 'hobo.dev.publik.love' already" in str(exc)
 
         # error (mail already registered)
         content = MAIL_ALREADY_THERE
         mocked_post.return_value.content = content
-        try:
+        with pytest.raises(MatomoError) as exc:
             matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
-        except MatomoError as exc:
-            assert str(exc).find("email 'hobo.dev.publik.love@testor.org'") != -1
-        else:
-            assert False
+        assert "email 'hobo.dev.publik.love@testor.org'" in str(exc)
 
         # error (bad credentials)
         content = BAD_CREDENTIAL
         mocked_post.return_value.content = content
-        try:
+        with pytest.raises(MatomoError) as exc:
             matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
-        except MatomoError as exc:
-            assert str(exc).find("You can\'t access this resource") != -1
-        else:
-            assert False
+        assert "You can\'t access this resource" in str(exc)
 
         # bad success message (wrong attribute value)
         content = MATOMO_BAD_RESPONSE_1
         mocked_post.return_value.content = content
-        try:
+        with pytest.raises(MatomoException) as exc:
             matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
-        except MatomoException as exc:
-            assert str(exc) == 'add_user fails'
-        else:
-            assert False
+        assert 'add_user fails' in str(exc)
 
        # bad success message (no message attribute)
         content = MATOMO_BAD_RESPONSE_2
         mocked_post.return_value.content = content
-        try:
+        with pytest.raises(MatomoException) as exc:
             matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
-        except MatomoException as exc:
-            assert str(exc) == 'add_user fails'
-        else:
-            assert False
+        assert 'add_user fails' in str(exc)
 
 @mock.patch('requests.post')
 def test_del_user(mocked_post):
@@ -459,12 +411,9 @@ def test_del_user(mocked_post):
         # error (unknown user)
         content = DEL_UNKNOWN_USER
         mocked_post.return_value.content = content
-        try:
+        with pytest.raises(MatomoError) as exc:
             matomo.del_user('hobo.dev.publik.love')
-        except MatomoError as exc:
-            assert str(exc).find("User 'hobo.dev.publik.love' doesn't exist.") != -1
-        else:
-            assert False
+        assert "User 'hobo.dev.publik.love' doesn't exist." in str(exc)
 
 @mock.patch('requests.post')
 def test_get_javascript_tag(mocked_post):
@@ -481,22 +430,16 @@ def test_get_javascript_tag(mocked_post):
         # error (bad credentials)
         content = BAD_CREDENTIAL
         mocked_post.return_value.content = content
-        try:
+        with pytest.raises(MatomoError) as exc:
             javascript_tag = matomo.get_javascript_tag('42')
-        except MatomoError as exc:
-            assert str(exc).find("You can't access this resource ") != -1
-        else:
-            assert False
+        assert "You can't access this resource " in str(exc)
 
        # bad response (no result tag)
         content = JAVASCRIPT_TAG_BAD_RESPONSE
         mocked_post.return_value.content = content
-        try:
+        with pytest.raises(MatomoException) as exc:
             javascript_tag = matomo.get_javascript_tag('42')
-        except MatomoException as exc:
-            assert str(exc) == 'get_javascript_tag fails'
-        else:
-            assert False
+        assert 'get_javascript_tag fails' in str(exc)
 
 @mock.patch('requests.post')
 def test_upgrade_site(mocked_post):
@@ -521,22 +464,15 @@ def test_upgrade_site(mocked_post):
         # error while adding new site
         contents = [GET_NO_SITE_FROM_URL, MATOMO_ERROR]
         mocked_post.side_effect = requests_post_mocked_replies(contents)
-        try:
+        with pytest.raises(MatomoException):
             upgrade_site(matomo, "hobo.dev.publik.love", urls)
-        except MatomoException as exc:
-            assert True
-        else:
-            assert False
 
         # error while looking for site already there
         contents = [MATOMO_ERROR]
         mocked_post.side_effect = requests_post_mocked_replies(contents)
-        try:
+        with pytest.raises(MatomoException) as exc:
             upgrade_site(matomo, "hobo.dev.publik.love", urls)
-        except MatomoException as exc:
-            assert str(exc) == 'here is the error message'
-        else:
-            assert False
+        assert 'here is the error message' in str(exc)
 
 @mock.patch('requests.post')
 def test_upgrade_user(mocked_post):
@@ -565,12 +501,8 @@ def test_upgrade_user(mocked_post):
         # error (add user fails)
         contents = [MATOMO_SUCCESS, MATOMO_ERROR]
         mocked_post.side_effect = requests_post_mocked_replies(contents)
-        try:
+        with pytest.raises(MatomoError) as exc:
             upgrade_user(matomo, 'hobo.dev.publik.love', '42')
-        except MatomoError:
-            assert True
-        else:
-            assert False
 
 def test_compute_cnil_acknowledgment_level():
     """function use to inspect javascript content"""
@@ -675,12 +607,9 @@ def test_auto_configure_matomo_no_url(mocked_post):
     Fargo.objects.create(base_url='https://fargo.dev.publik.love')
 
     with override_settings(MATOMO_SERVER=CONFIG):
-        try:
+        with pytest.raises(MatomoException) as exc:
             auto_configure_matomo()
-        except MatomoException as exc:
-            assert str(exc) == "no portal-user's url available"
-        else:
-            assert False
+    assert "no portal-user's url available" in str(exc)
 
 @mock.patch('requests.post')
 def test_auto_configure_matomo_error(mocked_post):
-- 
2.20.1

