From a7d39ac007ecd240b3ac2593726fe5bba5c58b38 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 11 May 2020 09:21:48 +0200
Subject: [PATCH] api_particulier: fix connection error (#42659)

---
 passerelle/apps/api_particulier/models.py |  4 +-
 tests/test_api_particulier.py             | 61 ++++++++++++-----------
 2 files changed, 33 insertions(+), 32 deletions(-)

diff --git a/passerelle/apps/api_particulier/models.py b/passerelle/apps/api_particulier/models.py
index 302a8cdf..8b3ae45d 100644
--- a/passerelle/apps/api_particulier/models.py
+++ b/passerelle/apps/api_particulier/models.py
@@ -34,6 +34,7 @@ from django.utils.translation import ugettext_lazy as _
 
 from passerelle.base.models import BaseResource
 from passerelle.utils.api import endpoint
+from passerelle.utils.conversion import exception_to_text
 from passerelle.utils.jsonresponse import APIError
 
 
@@ -75,7 +76,6 @@ class APIParticulier(BaseResource):
         headers = {'X-API-KEY': self.api_key}
         if user:
             headers['X-User'] = user
-        response = None
         try:
             response = self.requests.get(
                 url,
@@ -84,7 +84,7 @@ class APIParticulier(BaseResource):
         except requests.RequestException as e:
             raise APIError(
                 u'API-particulier platform "%s" connection error: %s' %
-                (self.platform, response.status_code),
+                (self.platform, exception_to_text(e)),
                 log_error=True,
                 data={
                     'code': 'connection-error',
diff --git a/tests/test_api_particulier.py b/tests/test_api_particulier.py
index 9d26f7ff..4197dbc2 100644
--- a/tests/test_api_particulier.py
+++ b/tests/test_api_particulier.py
@@ -22,6 +22,8 @@ from httmock import urlmatch, HTTMock, response
 
 from django.core.urlresolvers import reverse
 
+import requests
+
 from passerelle.apps.api_particulier.models import APIParticulier
 from passerelle.base.models import ResourceLog
 
@@ -107,6 +109,11 @@ def api_particulier_error_500(url, request):
     return response(500, {'error': 500}, request=request)
 
 
+@urlmatch(netloc=r'^particulier.*\.api\.gouv\.fr$')
+def api_particulier_connection_error(url, request):
+    raise requests.RequestException('connection timed-out')
+
+
 @urlmatch(netloc=r'^particulier.*\.api\.gouv\.fr$')
 def api_particulier_error_not_json(url, request):
     return response(200, 'something bad happened', request=request)
@@ -138,6 +145,16 @@ def resource(db):
 
 
 def test_error(app, resource, mock_api_particulier):
+    vector = [
+        (['impots_svair', 'avis-imposition'], {
+            'numero_fiscal': 12,
+            'reference_avis': 15,
+        }),
+        (['caf_famille', 'situation-familiale'], {
+            'code_postal': 12,
+            'numero_allocataire': 15
+        }),
+    ]
     with HTTMock(api_particulier_error_500):
         def do(endpoint, params):
             resp = endpoint_get(
@@ -150,16 +167,6 @@ def test_error(app, resource, mock_api_particulier):
             assert resp.json['err'] == 1
             assert resp.json['data']['status_code'] == 500
             assert resp.json['data']['code'] == 'non-200'
-        vector = [
-            (['impots_svair', 'avis-imposition'], {
-                'numero_fiscal': 12,
-                'reference_avis': 15,
-            }),
-            (['caf_famille', 'situation-familiale'], {
-                'code_postal': 12,
-                'numero_allocataire': 15
-            }),
-        ]
         for endpoints, params in vector:
             for endpoint in endpoints:
                 do(endpoint, params)
@@ -175,16 +182,6 @@ def test_error(app, resource, mock_api_particulier):
             assert resp.json['err'] == 1
             assert 'returned non-JSON content' in resp.json['err_desc']
             assert resp.json['data']['code'] == 'non-json'
-        vector = [
-            (['impots_svair', 'avis-imposition'], {
-                'numero_fiscal': 12,
-                'reference_avis': 15,
-            }),
-            (['caf_famille', 'situation-familiale'], {
-                'code_postal': 12,
-                'numero_allocataire': 15
-            }),
-        ]
         for endpoints, params in vector:
             for endpoint in endpoints:
                 do(endpoint, params)
@@ -200,16 +197,20 @@ def test_error(app, resource, mock_api_particulier):
             assert resp.json['err'] == 1
             assert resp.json['err_desc'] == 'No matching tax notice was found.'
             assert resp.json['data']['code'] == 'not-found'
-        vector = [
-            (['impots_svair', 'avis-imposition'], {
-                'numero_fiscal': 12,
-                'reference_avis': 15,
-            }),
-            (['caf_famille', 'situation-familiale'], {
-                'code_postal': 12,
-                'numero_allocataire': 15
-            }),
-        ]
+        for endpoints, params in vector:
+            for endpoint in endpoints:
+                do(endpoint, params)
+    with HTTMock(api_particulier_connection_error):
+        def do(endpoint, params):
+            resp = endpoint_get(
+                '/api-particulier/test/%s' % endpoint,
+                app,
+                resource,
+                endpoint,
+                params=params)
+            assert resp.status_code == 200
+            assert resp.json['err'] == 1
+            assert resp.json['err_desc'] == 'API-particulier platform "test" connection error: connection timed-out'
         for endpoints, params in vector:
             for endpoint in endpoints:
                 do(endpoint, params)
-- 
2.20.1

