Projet

Général

Profil

0001-api_entreprise-handle-properly-request-errors-33375.patch

Serghei Mihai, 23 mai 2019 15:54

Télécharger (2,5 ko)

Voir les différences:

Subject: [PATCH] api_entreprise: handle properly request errors (#33375)

 passerelle/apps/api_entreprise/models.py |  3 +--
 tests/test_api_entreprise.py             | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)
passerelle/apps/api_entreprise/models.py
88 88
            response = self.requests.get(url, data=params)
89 89
        except requests.RequestException as e:
90 90
            raise APIError(u'API-entreprise connection error: %s' %
91
                           response.status_code,
92
                           data={'error': exception_to_text(e)})
91
                           exception_to_text(e), data=[])
93 92
        try:
94 93
            data = response.json()
95 94
        except ValueError as e:
tests/test_api_entreprise.py
18 18

  
19 19
import pytest
20 20
import mock
21
import requests
21 22

  
22 23
from httmock import urlmatch, HTTMock, response
23 24

  
......
258 259
def api_entreprise_error_500(url, request):
259 260
    return response(500, 'bad error happened', request=request)
260 261

  
262
@urlmatch(netloc='^entreprise.api.gouv.fr$')
263
def api_entreprise_connection_error(url, request):
264
    raise requests.RequestException('connection timed-out')
261 265

  
262 266
@urlmatch(netloc='^entreprise.api.gouv.fr$')
263 267
def api_entreprise_error_not_json(url, request):
......
435 439
        assert response.status_code == 200
436 440
        assert response.json['err'] == 1
437 441
        assert response.json['err_desc'] == 'Page not found'
442

  
443

  
444
def test_connection_error(app, resource, mock_api_entreprise):
445
    with HTTMock(api_entreprise_connection_error):
446
        response = app.get('/api-entreprise/test/entreprises/443170139/',
447
                           params=REQUEST_PARAMS)
448
        assert response.status_code == 200
449
        assert response.json['err'] == 1
450
        assert response.json['err_desc'] == 'API-entreprise connection error: connection timed-out'
451
        assert response.json['data'] == []
438
-