Projet

Général

Profil

0002-api_particulier-do-not-log-404-as-errors-38721.patch

Valentin Deniaud, 08 juin 2020 16:24

Télécharger (4,17 ko)

Voir les différences:

Subject: [PATCH 2/3] api_particulier: do not log 404 as errors (#38721)

 passerelle/apps/api_particulier/models.py | 13 ++++++-----
 tests/test_api_particulier.py             | 27 +++++++++++++++++++++++
 2 files changed, 35 insertions(+), 5 deletions(-)
passerelle/apps/api_particulier/models.py
63 63
        blank=True,
64 64
        verbose_name=_('API key'))
65 65

  
66
    log_requests_errors = False
67

  
66 68
    @property
67 69
    def url(self):
68 70
        return self.PLATFORMS[self.platform]['url']
......
83 85
            raise APIError(
84 86
                u'API-particulier platform "%s" connection error: %s' %
85 87
                (self.platform, response.status_code),
88
                log_error=True,
86 89
                data={
87 90
                    'platform': self.platform,
88 91
                    'error': six.text_type(e),
......
94 97
            raise APIError(
95 98
                u'API-particulier platform "%s" returned non-JSON content with status %s: %s' %
96 99
                (self.platform, response.status_code, content),
100
                log_error=True,
97 101
                data={
98 102
                    'status_code': response.status_code,
99 103
                    'exception': six.text_type(e),
......
101 105
                    'content': content,
102 106
                })
103 107
        if response.status_code != 200:
104
            if data.get('error') == 'not_found':
105
                return {
106
                    'err': 1,
107
                    'err_desc': data.get('message', 'not-found'),
108
                }
108
            # avoid logging 404 errors indicating no matching data was found
109
            if data.get('error') == 'not_found' and 'incorrects ou ne correspondent' in data['message']:
110
                raise APIError(data.get('message', 'not-found'))
109 111
            raise APIError(
110 112
                u'API-particulier platform "%s" returned a non 200 status %s: %s' %
111 113
                (self.platform, response.status_code, data),
114
                log_error=True,
112 115
                data={
113 116
                    'status_code': response.status_code,
114 117
                    'platform': self.platform,
tests/test_api_particulier.py
16 16
# You should have received a copy of the GNU Affero General Public License
17 17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 18

  
19
import logging
19 20
import pytest
20 21
from httmock import urlmatch, HTTMock, response
21 22

  
22 23
from django.core.urlresolvers import reverse
23 24

  
24 25
from passerelle.apps.api_particulier.models import APIParticulier
26
from passerelle.base.models import ResourceLog
25 27

  
26 28
from utils import make_resource, endpoint_get
27 29

  
......
246 248
    assert 'API Particulier Prod' in response.text
247 249
    assert 'family allowance' in response.text
248 250
    assert 'fiscal information' in response.text
251

  
252

  
253
@pytest.mark.parametrize(
254
    'mock,should_log', [
255
        (api_particulier_error_not_found, False), (api_particulier_error_500, True),
256
        (api_particulier_error_not_json, True)
257
    ]
258
)
259
def test_api_particulier_dont_log_not_found(app, resource, mock, should_log):
260
    with HTTMock(mock):
261
        resp = endpoint_get(
262
            '/api-particulier/test/avis-imposition',
263
            app,
264
            resource,
265
            'avis-imposition',
266
            params={
267
                'numero_fiscal': 12,
268
                'reference_avis': 15,
269
            })
270
    logs = ResourceLog.objects.all()
271
    assert logs.count() == 3
272
    if should_log:
273
        assert logs.filter(levelno=logging.ERROR).count() == 1
274
    else:
275
        assert not logs.filter(levelno=logging.ERROR).exists()
249
-