Projet

Général

Profil

0001-api_particulier-dont-log-404-for-deregistrated-recor.patch

Valentin Deniaud, 01 juillet 2020 11:56

Télécharger (4,52 ko)

Voir les différences:

Subject: [PATCH] api_particulier: dont log 404 for deregistrated record
 (#44318)

 passerelle/apps/api_particulier/models.py | 35 +++++++++++++++--------
 tests/test_api_particulier.py             | 13 +++++++--
 2 files changed, 34 insertions(+), 14 deletions(-)
passerelle/apps/api_particulier/models.py
1
# -*- coding: utf-8 -*-
2

  
1 3
# passerelle - uniform access to multiple data sources and services
2 4
# Copyright (C) 2017  Entr'ouvert
3 5
#
......
17 19
'''Gateway to API-Particulier web-service from SGMAP:
18 20
   https://particulier.api.gouv.fr/
19 21
'''
20

  
21 22
from collections import OrderedDict
22 23
import requests
23 24

  
......
107 108
                    'content': content,
108 109
                })
109 110
        if response.status_code != 200:
110
            # avoid logging 404 errors indicating no matching data was found
111
            if data.get('error') == 'not_found' and \
112
                    ('incorrects ou ne correspondent' in data['message'] or
113
                     'Dossier allocataire inexistant' in data['message']):
114
                raise APIError(
115
                    'No matching tax notice was found.',
116
                    data={
117
                        'code': 'not-found',
118
                        'platform': self.platform,
119
                        'content': data,
120
                    })
111
            # avoid logging 404 errors about non-transport failure
112
            if data.get('error') == 'not_found':
113
                message = data['message']
114
                if 'incorrects ou ne correspondent' in message or \
115
                        'Dossier allocataire inexistant' in message:
116
                    raise APIError(
117
                        'No matching record was found.',
118
                        data={
119
                            'code': 'not-found',
120
                            'platform': self.platform,
121
                            'content': data,
122
                        })
123
                elif u'Dossier radié' in message:
124
                    raise APIError(
125
                        'Deregistrated record, no document could be edited.',
126
                        data={
127
                            'code': 'not-found',
128
                            'platform': self.platform,
129
                            'content': data,
130
                        })
131

  
121 132
            raise APIError(
122 133
                u'API-particulier platform "%s" returned a non 200 status %s: %s' %
123 134
                (self.platform, response.status_code, data),
tests/test_api_particulier.py
135 135
    }, request=request)
136 136

  
137 137

  
138
@urlmatch(netloc=r'^particulier.*\.api\.gouv\.fr$')
139
def api_particulier_error_not_found_deregistrated(url, request):
140
    return response(404, {
141
        'error': 'not_found',
142
        'message': 'Dossier radié. Le document ne peut être édité.'
143
    }, request=request)
144

  
145

  
138 146
@pytest.yield_fixture
139 147
def mock_api_particulier():
140 148
    with HTTMock(api_particulier_v2_avis_imposition, api_particulier_v2_situation_familiale):
......
203 211
                params=params)
204 212
            assert resp.status_code == 200
205 213
            assert resp.json['err'] == 1
206
            assert resp.json['err_desc'] == 'No matching tax notice was found.'
214
            assert resp.json['err_desc'] == 'No matching record was found.'
207 215
            assert resp.json['data']['code'] == 'not-found'
208 216
        for endpoints, params in vector:
209 217
            for endpoint in endpoints:
......
265 273
@pytest.mark.parametrize(
266 274
    'mock,should_log', [
267 275
        (api_particulier_error_not_found, False), (api_particulier_error_500, True),
268
        (api_particulier_error_not_json, True), (api_particulier_error_not_found_caf, False)
276
        (api_particulier_error_not_json, True), (api_particulier_error_not_found_caf, False),
277
        (api_particulier_error_not_found_deregistrated, False)
269 278
    ]
270 279
)
271 280
def test_api_particulier_dont_log_not_found(app, resource, mock, should_log):
272
-