From 5903c6d2d8c40070606542e1617159e2bb52845f Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Mon, 6 May 2019 18:47:54 +0200
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(-)

diff --git a/hobo/matomo/utils.py b/hobo/matomo/utils.py
index f2fbd5d..45e981a 100644
--- a/hobo/matomo/utils.py
+++ b/hobo/matomo/utils.py
@@ -155,6 +155,8 @@ class MatomoWS(object):
         data['module'] = 'API'
         data['token_auth'] = self.token_auth
         resp = requests.post(self.url_ws_base, data=data)
+        if resp.status_code != 200:
+            raise MatomoException('unexpected status code: %s' % resp.status_code)
         tree = self.parse_response(resp.content)
         self.raise_on_error(tree)
         return tree
@@ -219,6 +221,8 @@ class MatomoWS(object):
         url = "%s/matomo.php" % self.url_ws_base
         data = '{"requests":["?idsite=%s&action_name=ping&rec=1"]}' % id_site
         resp = requests.post(url, json=data)
+        if resp.status_code != 200:
+            raise MatomoException('unexpected status code: %s' % resp.status_code)
         try:
             tree = resp.json()
         except ValueError:
diff --git a/tests/test_matomo_utils.py b/tests/test_matomo_utils.py
index 21c769c..6976feb 100644
--- a/tests/test_matomo_utils.py
+++ b/tests/test_matomo_utils.py
@@ -273,8 +273,7 @@ def test_parse_error_message():
         with pytest.raises(MatomoException, match='internal error'):
             matomo.raise_on_error(tree)
 
-@mock.patch('requests.post')
-def test_assert_success(mocked_post):
+def test_assert_success():
     """webservice to add new user"""
     with override_settings(MATOMO_SERVER=CONFIG):
         matomo = MatomoWS()
@@ -296,9 +295,27 @@ def test_assert_success(mocked_post):
         with pytest.raises(MatomoException, match='me fails'):
             matomo.assert_success(tree, 'me')
 
+@mock.patch('requests.post')
+def test_call(mocked_post):
+    """test status_code"""
+    with override_settings(MATOMO_SERVER=CONFIG):
+        matomo = MatomoWS()
+
+        # success (status 200)
+        content = MATOMO_SUCCESS
+        mocked_post.return_value.status_code = 200
+        mocked_post.return_value.content = content
+        matomo.call({})
+
+        # failure (status 500)
+        mocked_post.return_value.status_code = 500
+        with pytest.raises(MatomoException, match='unexpected status code: 500'):
+            matomo.call({})
+
 @mock.patch('requests.post')
 def test_get_site_from_site_url(mocked_post):
     """webservice to test if the site is already registered"""
+    mocked_post.return_value.status_code = 200
     with override_settings(MATOMO_SERVER=CONFIG):
         matomo = MatomoWS()
 
@@ -329,6 +346,7 @@ def test_get_site_from_site_url(mocked_post):
 @mock.patch('requests.post')
 def test_add_site(mocked_post):
     """webservice to add a new site"""
+    mocked_post.return_value.status_code = 200
     urls = ['https://combo.dev.publik.love',
             'https://wcs.dev.publik.love']
     with override_settings(MATOMO_SERVER=CONFIG):
@@ -356,6 +374,7 @@ def test_add_site(mocked_post):
 @mock.patch('requests.post')
 def test_add_user(mocked_post):
     """webservice to add new user"""
+    mocked_post.return_value.status_code = 200
     with override_settings(MATOMO_SERVER=CONFIG):
         matomo = MatomoWS()
 
@@ -420,6 +439,7 @@ def test_del_user(mocked_post):
 @mock.patch('requests.post')
 def test_get_javascript_tag(mocked_post):
     """webservice to get matomo JS tag"""
+    mocked_post.return_value.status_code = 200
     with override_settings(MATOMO_SERVER=CONFIG):
         matomo = MatomoWS()
 
@@ -447,6 +467,7 @@ def test_ping(mocked_post):
     with override_settings(MATOMO_SERVER=CONFIG):
         matomo = MatomoWS()
         response = Response()
+        response.status_code = 200
 
         # success
         content = PING_SUCCESS
@@ -485,6 +506,11 @@ def test_ping(mocked_post):
                            match='internal error on ping \(JSON expected\)'):
             matomo.ping('42')
 
+        # failure (status 500)
+        mocked_post.return_value.status_code = 500
+        with pytest.raises(MatomoException, match='unexpected status code: 500'):
+            matomo.ping('42')
+
 @mock.patch('requests.post')
 def test_upgrade_site(mocked_post):
     """function to test if the site is already regisered"""
@@ -607,6 +633,7 @@ def test_put_tracking_js():
 @mock.patch('requests.post')
 def test_upgrade_javascript_tag(mocked_post):
     """function to get matomo JS tag"""
+    mocked_post.return_value.status_code = 200
     with override_settings(MATOMO_SERVER=CONFIG):
         matomo = MatomoWS()
 
@@ -643,8 +670,7 @@ def test_auto_configure_matomo(mocked_post):
         tracking_js2_var = get_variable('cnil_compliant_visits_tracking_js')
         assert tracking_js2_var.value != ''
 
-@mock.patch('requests.post')
-def test_auto_configure_matomo_no_url(mocked_post):
+def test_auto_configure_matomo_no_url():
     # no Combo url so as to raise
     Wcs.objects.create(base_url='https://wcs.dev.publik.love')
     Fargo.objects.create(base_url='https://fargo.dev.publik.love')
-- 
2.20.1

