From f2c44d68556bcd112a6fa55402c3aa1798a59d84 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 3 Jun 2020 10:59:13 +0200 Subject: [PATCH 2/2] api_particulier: do not log 404 as errors (#38721) --- passerelle/apps/api_particulier/models.py | 10 +++++---- tests/test_api_particulier.py | 27 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/passerelle/apps/api_particulier/models.py b/passerelle/apps/api_particulier/models.py index a3744f2e..91dceae8 100644 --- a/passerelle/apps/api_particulier/models.py +++ b/passerelle/apps/api_particulier/models.py @@ -63,6 +63,8 @@ class APIParticulier(BaseResource): blank=True, verbose_name=_('API key')) + log_requests_errors = False + @property def url(self): return self.PLATFORMS[self.platform]['url'] @@ -83,6 +85,7 @@ class APIParticulier(BaseResource): raise APIError( u'API-particulier platform "%s" connection error: %s' % (self.platform, response.status_code), + log_error=True, data={ 'platform': self.platform, 'error': six.text_type(e), @@ -94,6 +97,7 @@ class APIParticulier(BaseResource): raise APIError( u'API-particulier platform "%s" returned non-JSON content with status %s: %s' % (self.platform, response.status_code, content), + log_error=True, data={ 'status_code': response.status_code, 'exception': six.text_type(e), @@ -102,13 +106,11 @@ class APIParticulier(BaseResource): }) if response.status_code != 200: if data.get('error') == 'not_found': - return { - 'err': 1, - 'err_desc': data.get('message', 'not-found'), - } + raise APIError(data.get('message', 'not-found')) raise APIError( u'API-particulier platform "%s" returned a non 200 status %s: %s' % (self.platform, response.status_code, data), + log_error=True, data={ 'status_code': response.status_code, 'platform': self.platform, diff --git a/tests/test_api_particulier.py b/tests/test_api_particulier.py index 26f91f39..34fcdc62 100644 --- a/tests/test_api_particulier.py +++ b/tests/test_api_particulier.py @@ -16,12 +16,14 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import logging import pytest from httmock import urlmatch, HTTMock, response from django.core.urlresolvers import reverse from passerelle.apps.api_particulier.models import APIParticulier +from passerelle.base.models import ResourceLog from utils import make_resource, endpoint_get @@ -246,3 +248,28 @@ def test_detail_page(app, resource): assert 'API Particulier Prod' in response.text assert 'family allowance' in response.text assert 'fiscal information' in response.text + + +@pytest.mark.parametrize( + 'mock,should_log', [ + (api_particulier_error_not_found, False), (api_particulier_error_500, True), + (api_particulier_error_not_json, True) + ] +) +def test_api_particulier_dont_log_not_found(app, resource, mock, should_log): + with HTTMock(mock): + resp = endpoint_get( + '/api-particulier/test/avis-imposition', + app, + resource, + 'avis-imposition', + params={ + 'numero_fiscal': 12, + 'reference_avis': 15, + }) + logs = ResourceLog.objects.all() + assert logs.count() == 3 + if should_log: + assert logs.filter(levelno=logging.ERROR).count() == 1 + else: + assert not logs.filter(levelno=logging.ERROR).exists() -- 2.20.1