matomo: rewrite managing exceptions into tests (#32796)

This commit is contained in:
Nicolas Roche 2019-05-06 13:44:14 +02:00
parent 3dcdf9552a
commit a218eef30f
1 changed files with 32 additions and 118 deletions

View File

@ -223,12 +223,8 @@ def test_matomo_constructor():
assert matomo.token_auth == '1234'
with override_settings(MATOMO_SERVER={}):
try:
with pytest.raises(MatomoException, match="no settings for matomo: 'URL'"):
matomo = MatomoWS()
except MatomoError as exc:
assert str(exc) == "no settings for matomo: 'URL'"
else:
assert False
def test_parse_response():
"""parser used by all matomo webservice calls"""
@ -242,12 +238,9 @@ def test_parse_response():
# error (not XML format)
content = """this is not XML"""
try:
with pytest.raises(MatomoException,
match="XMLSyntaxError: Start tag expected"):
tree = matomo.parse_response(content)
except MatomoException as exc:
assert str(exc).find("XMLSyntaxError: Start tag expected") != -1
else:
assert False
def test_parse_error_message():
"""error handler used by all matomo webservice calls"""
@ -267,12 +260,8 @@ def test_parse_error_message():
</result>
"""
tree = matomo.parse_response(content)
try:
with pytest.raises(MatomoError, match='here is the error message'):
matomo.raise_on_error(tree)
except MatomoError as exc:
assert str(exc) == 'here is the error message'
else:
assert False
# error (unexpected format)
content = """<?xml version="1.0" encoding="utf-8" ?>
@ -281,12 +270,8 @@ def test_parse_error_message():
</result>
"""
tree = matomo.parse_response(content)
try:
with pytest.raises(MatomoException, match='internal error'):
matomo.raise_on_error(tree)
except MatomoException as exc:
assert str(exc) == 'internal error'
else:
assert False
@mock.patch('requests.post')
def test_assert_success(mocked_post):
@ -302,22 +287,14 @@ 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, match='me fails'):
matomo.assert_success(tree, 'me')
except MatomoException as exc:
assert str(exc).find('me fails') != -1
else:
assert False
# error (no message attribute)
tree = matomo.parse_response(MATOMO_BAD_RESPONSE_2)
matomo.raise_on_error(tree)
try:
with pytest.raises(MatomoException, match='me fails'):
matomo.assert_success(tree, 'me')
except MatomoException as exc:
assert str(exc).find('me fails') != -1
else:
assert False
@mock.patch('requests.post')
def test_get_site_from_site_url(mocked_post):
@ -333,32 +310,21 @@ 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, match='url not found'):
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
# error on empty id
content = GET_SITE_BAD_QUERY
mocked_post.return_value.content = content
try:
with pytest.raises(MatomoError, match="Please specify a value for 'url'."):
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
# bad response (error on success response)
content = GET_SITE_BAD_RESPONSE
mocked_post.return_value.content = content
try:
with pytest.raises(MatomoException,
match='get_site_id_from_site_url fails'):
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
@mock.patch('requests.post')
def test_add_site(mocked_post):
@ -377,22 +343,15 @@ def test_add_site(mocked_post):
# error
content = ADD_SITE_ERROR
mocked_post.return_value.content = content
try:
with pytest.raises(MatomoError,
match="Please specify a value for 'siteName'."):
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
# strange message
content = ADD_SITE_BAD_RESPONSE
mocked_post.return_value.content = content
try:
with pytest.raises(MatomoException, match='add_site fails'):
site_id = matomo.add_site("hobo.dev.publik.love", urls)
except MatomoException as exc:
assert str(exc) == 'add_site fails'
else:
assert False
@mock.patch('requests.post')
def test_add_user(mocked_post):
@ -409,56 +368,39 @@ 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,
match="Username 'hobo.dev.publik.love' already"):
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
# error (mail already registered)
content = MAIL_ALREADY_THERE
mocked_post.return_value.content = content
try:
with pytest.raises(MatomoError,
match="email 'hobo.dev.publik.love@testor.org'"):
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
# error (bad credentials)
content = BAD_CREDENTIAL
mocked_post.return_value.content = content
try:
with pytest.raises(MatomoError, match="You can\'t access this resource"):
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
# bad success message (wrong attribute value)
content = MATOMO_BAD_RESPONSE_1
mocked_post.return_value.content = content
try:
with pytest.raises(MatomoException, match='add_user fails'):
matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
except MatomoException as exc:
assert str(exc) == 'add_user fails'
else:
assert False
# bad success message (no message attribute)
content = MATOMO_BAD_RESPONSE_2
mocked_post.return_value.content = content
try:
with pytest.raises(MatomoException, match='add_user fails'):
matomo.add_user('hobo.dev.publik.love', 'xxx', '42')
except MatomoException as exc:
assert str(exc) == 'add_user fails'
else:
assert False
@mock.patch('requests.post')
def test_del_user(mocked_post):
"""webservice to add new user"""
"""webservice to del an existing user"""
mocked_post.return_value.status_code = 200
with override_settings(MATOMO_SERVER=CONFIG):
matomo = MatomoWS()
@ -471,12 +413,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,
match="User 'hobo.dev.publik.love' doesn't exist."):
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
@mock.patch('requests.post')
def test_get_javascript_tag(mocked_post):
@ -493,22 +432,14 @@ def test_get_javascript_tag(mocked_post):
# error (bad credentials)
content = BAD_CREDENTIAL
mocked_post.return_value.content = content
try:
with pytest.raises(MatomoError, match="You can't access this resource"):
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
# bad response (no result tag)
content = JAVASCRIPT_TAG_BAD_RESPONSE
mocked_post.return_value.content = content
try:
with pytest.raises(MatomoException, match='get_javascript_tag fails'):
javascript_tag = matomo.get_javascript_tag('42')
except MatomoException as exc:
assert str(exc) == 'get_javascript_tag fails'
else:
assert False
@mock.patch('requests.post')
def test_ping(mocked_post):
@ -577,22 +508,14 @@ 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, match='here is the error message'):
upgrade_site(matomo, "hobo.dev.publik.love", urls)
except MatomoException as exc:
assert str(exc) == 'here is the error message'
else:
assert False
@mock.patch('requests.post')
def test_upgrade_user(mocked_post):
@ -621,12 +544,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):
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"""
@ -731,12 +650,8 @@ 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, match="no portal-user's url available"):
auto_configure_matomo()
except MatomoException as exc:
assert str(exc) == "no portal-user's url available"
else:
assert False
@mock.patch('requests.post')
def test_auto_configure_matomo_error(mocked_post):
@ -752,9 +667,8 @@ def test_auto_configure_matomo_error(mocked_post):
DEL_UNKNOWN_USER, MATOMO_SUCCESS,
JAVASCRIPT_TAG_BAD_RESPONSE]
mocked_post.side_effect = requests_post_mocked_replies(contents)
with pytest.raises(MatomoException) as exc:
with pytest.raises(MatomoException, match="get_javascript_tag fails"):
auto_configure_matomo()
assert "get_javascript_tag fails" in str(exc)
tracking_js_var = get_variable('visits_tracking_js')
assert tracking_js_var.value == 'js_code'