matomo: manage http status codes (#32796)

This commit is contained in:
Nicolas Roche 2019-05-06 18:47:54 +02:00
parent a218eef30f
commit 8118976981
2 changed files with 34 additions and 4 deletions

View File

@ -156,6 +156,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
@ -220,6 +222,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:

View File

@ -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')