0002-api_particulier-do-not-log-404-as-errors-38721.patch
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), |
... | ... | |
102 | 106 |
}) |
103 | 107 |
if response.status_code != 200: |
104 | 108 |
if data.get('error') == 'not_found': |
105 |
return { |
|
106 |
'err': 1, |
|
107 |
'err_desc': data.get('message', 'not-found'), |
|
108 |
} |
|
109 |
raise APIError(data.get('message', 'not-found')) |
|
109 | 110 |
raise APIError( |
110 | 111 |
u'API-particulier platform "%s" returned a non 200 status %s: %s' % |
111 | 112 |
(self.platform, response.status_code, data), |
113 |
log_error=True, |
|
112 | 114 |
data={ |
113 | 115 |
'status_code': response.status_code, |
114 | 116 |
'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/impots_adresse', |
|
263 |
app, |
|
264 |
resource, |
|
265 |
'impots_adresse', |
|
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 |
- |