From 2aea1a5b9641abdab8b4926f53b87f10c23aaec5 Mon Sep 17 00:00:00 2001 From: Josue Kouka Date: Wed, 7 Sep 2016 10:42:29 +0200 Subject: [PATCH] iparapheur: add handling of invalid response content --- passerelle/contrib/iparapheur/soap.py | 20 ++++++++++++++++++-- tests/test_iparapheur.py | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/passerelle/contrib/iparapheur/soap.py b/passerelle/contrib/iparapheur/soap.py index f2bf80a..27acec5 100644 --- a/passerelle/contrib/iparapheur/soap.py +++ b/passerelle/contrib/iparapheur/soap.py @@ -23,12 +23,20 @@ import StringIO from suds.client import Client from suds.transport.http import HttpAuthenticated -from suds.transport import Reply +from suds.transport import Reply, TransportError from suds.plugin import MessagePlugin, DocumentPlugin from suds.sudsobject import asdict +class CustomTransportError(TransportError): + + def __init__(self, *args, **kwargs): + super(CustomTransportError, self).__init__(*args, **kwargs) + self.http_status = 200 + self.log_error = False + + class Filter(MessagePlugin): def marshalled(self, context): @@ -75,7 +83,15 @@ class Transport(HttpAuthenticated): self.addcredentials(request) resp = self.model.requests.post(request.url, data=request.message, headers=request.headers, **self.get_requests_kwargs()) - return Reply(resp.status_code, resp.headers, resp.content) + + if resp.status_code in (202, 204): + return None + elif not resp.ok(): + raise CustomTransportError( + resp.reason, + resp.status_code, fp=StringIO.StringIO(resp.content)) + else: + return Reply(resp.status_code, resp.headers, resp.content) def get_client(instance): transport = Transport(instance) diff --git a/tests/test_iparapheur.py b/tests/test_iparapheur.py index 134aad6..9aba5cd 100644 --- a/tests/test_iparapheur.py +++ b/tests/test_iparapheur.py @@ -154,3 +154,21 @@ def test_get_file(http_open, mocked_post, mocked_get, setup, xmlmime, wsdl_file) file_sent = os.path.join(os.path.dirname(__file__), 'data/iparapheur_test.pdf') assert resp.headers['Content-Type'] == 'application/pdf' assert hashlib.md5(resp.body[:8192]).hexdigest() == hashlib.md5(file(file_sent).read()[:8192]).hexdigest() + + +@mock.patch('passerelle.utils.LoggedRequest.get') +@mock.patch('passerelle.utils.LoggedRequest.post') +@mock.patch('passerelle.contrib.iparapheur.soap.HttpAuthenticated.open') +def test_invalid_response(http_open, mocked_post, mocked_get, setup, xmlmime, wsdl_file): + app, conn = setup + file_id = str(uuid.uuid4()) + + http_open.return_value = file(xmlmime) + mocked_get.return_value = mock.Mock(content = file(wsdl_file).read(), + status_code=200) + mocked_post.return_value = mock.Mock(status_code=502, content='

Bad Gateway

', reason='Bad Gateway') + url = reverse('generic-endpoint', kwargs={'slug': conn.slug, + 'connector': 'iparapheur', 'endpoint': 'get-file', + 'rest': file_id}) + resp = app.get(url) + import pdb; pdb.set_trace() -- 2.9.3