From b6cc56abe05ba9fdbf6707d0971f7b8f52b7d845 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 1 Jul 2020 11:24:37 +0200 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(-) diff --git a/passerelle/apps/api_particulier/models.py b/passerelle/apps/api_particulier/models.py index 70de9b13..49f01e64 100644 --- a/passerelle/apps/api_particulier/models.py +++ b/passerelle/apps/api_particulier/models.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + # passerelle - uniform access to multiple data sources and services # Copyright (C) 2017 Entr'ouvert # @@ -17,7 +19,6 @@ '''Gateway to API-Particulier web-service from SGMAP: https://particulier.api.gouv.fr/ ''' - from collections import OrderedDict import requests @@ -107,17 +108,27 @@ class APIParticulier(BaseResource): 'content': content, }) if response.status_code != 200: - # avoid logging 404 errors indicating no matching data was found - if data.get('error') == 'not_found' and \ - ('incorrects ou ne correspondent' in data['message'] or - 'Dossier allocataire inexistant' in data['message']): - raise APIError( - 'No matching tax notice was found.', - data={ - 'code': 'not-found', - 'platform': self.platform, - 'content': data, - }) + # avoid logging 404 errors about non-transport failure + if data.get('error') == 'not_found': + message = data['message'] + if 'incorrects ou ne correspondent' in message or \ + 'Dossier allocataire inexistant' in message: + raise APIError( + 'No matching record was found.', + data={ + 'code': 'not-found', + 'platform': self.platform, + 'content': data, + }) + elif u'Dossier radié' in message: + raise APIError( + 'Deregistrated record, no document could be edited.', + data={ + 'code': 'not-found', + 'platform': self.platform, + 'content': data, + }) + raise APIError( u'API-particulier platform "%s" returned a non 200 status %s: %s' % (self.platform, response.status_code, data), diff --git a/tests/test_api_particulier.py b/tests/test_api_particulier.py index c043f4dc..79e34b7b 100644 --- a/tests/test_api_particulier.py +++ b/tests/test_api_particulier.py @@ -135,6 +135,14 @@ def api_particulier_error_not_found_caf(url, request): }, request=request) +@urlmatch(netloc=r'^particulier.*\.api\.gouv\.fr$') +def api_particulier_error_not_found_deregistrated(url, request): + return response(404, { + 'error': 'not_found', + 'message': 'Dossier radié. Le document ne peut être édité.' + }, request=request) + + @pytest.yield_fixture def mock_api_particulier(): with HTTMock(api_particulier_v2_avis_imposition, api_particulier_v2_situation_familiale): @@ -203,7 +211,7 @@ def test_error(app, resource, mock_api_particulier): params=params) assert resp.status_code == 200 assert resp.json['err'] == 1 - assert resp.json['err_desc'] == 'No matching tax notice was found.' + assert resp.json['err_desc'] == 'No matching record was found.' assert resp.json['data']['code'] == 'not-found' for endpoints, params in vector: for endpoint in endpoints: @@ -265,7 +273,8 @@ def test_detail_page(app, resource): @pytest.mark.parametrize( 'mock,should_log', [ (api_particulier_error_not_found, False), (api_particulier_error_500, True), - (api_particulier_error_not_json, True), (api_particulier_error_not_found_caf, False) + (api_particulier_error_not_json, True), (api_particulier_error_not_found_caf, False), + (api_particulier_error_not_found_deregistrated, False) ] ) def test_api_particulier_dont_log_not_found(app, resource, mock, should_log): -- 2.20.1