From d7e8054a3ae99648218c3e19c447ef187de3d661 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 14 Jan 2020 11:38:16 +0100
Subject: [PATCH 6/6] toulouse_axel: zeep upgrade for encoding in logs (#38543)

cf https://github.com/mvantellingen/python-zeep/commit/7277be32d5921b0b5f1c250fe607d74aaee2125f
Here toulouse axel sends headers like:
<?xml version="1.0" encoding="ISO-8859-1"?>
---
 passerelle/contrib/iparapheur/models.py    |  4 +++-
 passerelle/contrib/toulouse_axel/models.py |  2 +-
 setup.py                                   |  2 +-
 tests/test_iparapheur.py                   | 12 +++++++-----
 4 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/passerelle/contrib/iparapheur/models.py b/passerelle/contrib/iparapheur/models.py
index 40f607b4..7e7eaed3 100644
--- a/passerelle/contrib/iparapheur/models.py
+++ b/passerelle/contrib/iparapheur/models.py
@@ -22,6 +22,7 @@ from django.utils.text import slugify
 from django.utils.translation import ugettext_lazy as _
 from django.http import HttpResponse, Http404
 
+from zeep import Settings
 from zeep.exceptions import Fault as WebFault, TransportError, XMLSyntaxError
 
 from passerelle.base.models import BaseResource, HTTPResource
@@ -99,7 +100,8 @@ class IParapheur(BaseResource, HTTPResource):
 
     def get_client(self, strict_mode=True):
         try:
-            soap_client = self.soap_client(strict=strict_mode)
+            settings = Settings(strict=strict_mode)
+            soap_client = self.soap_client(settings=settings)
 
             # overrides the service port address URL defined in the WSDL.
             if self.wsdl_endpoint_location:
diff --git a/passerelle/contrib/toulouse_axel/models.py b/passerelle/contrib/toulouse_axel/models.py
index e7dec568..a3c002fb 100644
--- a/passerelle/contrib/toulouse_axel/models.py
+++ b/passerelle/contrib/toulouse_axel/models.py
@@ -217,7 +217,7 @@ class Operation(object):
             force_text(serialized_request),
             '')  # FIXME: What is the user parameter for ?
 
-        xml_result = ET.fromstring(result)
+        xml_result = ET.fromstring(result.encode('utf-8'))
         indent(xml_result)
         pretty_result = ET.tostring(xml_result)
         if xml_result.find('RESULTAT/STATUS').text != 'OK':
diff --git a/setup.py b/setup.py
index 1820b6d2..2cf0f9a9 100755
--- a/setup.py
+++ b/setup.py
@@ -104,7 +104,7 @@ setup(name='passerelle',
             'python-dateutil',
             'Pillow',
             'jsonschema < 3.1',
-            'zeep < 3.0',
+            'zeep >= 3.0.2',
             'pycrypto',
             'unidecode',
             'paramiko',
diff --git a/tests/test_iparapheur.py b/tests/test_iparapheur.py
index eefa61bd..d7afb90a 100644
--- a/tests/test_iparapheur.py
+++ b/tests/test_iparapheur.py
@@ -187,7 +187,8 @@ def test_create_file(mocked_post, mocked_get, app, conn):
     assert (BASE_URL,) == mocked_post.call_args[0]
     assert resp.json['err'] == 1
     assert 'zeep.exceptions.TransportError' in resp.json['err_class']
-    assert 'Server returned HTTP status 200 (<nada>)' in resp.json['err_desc']
+    assert 'Server returned response (200) with invalid XML' in resp.json['err_desc']
+    assert 'Premature end of data in tag nada' in resp.json['err_desc']
 
     # Unknown value for "visibility"
     err_data = data.copy()
@@ -211,7 +212,8 @@ def test_create_file(mocked_post, mocked_get, app, conn):
     assert (BASE_URL,) == mocked_post.call_args[0]
     assert resp.json['err'] == 1
     assert 'zeep.exceptions.TransportError' in resp.json['err_class']
-    assert 'Server returned HTTP status 200 (<nada>)' in resp.json['err_desc']
+    assert 'Server returned response (200) with invalid XML' in resp.json['err_desc']
+    assert 'Premature end of data in tag nada' in resp.json['err_desc']
 
 @mock.patch('passerelle.utils.Request.get', side_effect=iph_mocked_get)
 @mock.patch('passerelle.utils.Request.post')
@@ -295,12 +297,12 @@ def test_get_file_status(mocked_post, mocked_get, app, conn):
     soap_response = """<nada>"""
     response._content = soap_response
     mocked_post.return_value = response
-    #resp = app.get(url, status=500)
     resp = app.get(url)
     assert resp.json['err'] == 1
-    #assert 'zeep.exceptions.TransportError' in resp.json['err_class']
     assert 'passerelle.utils.jsonresponse.APIError' in resp.json['err_class']
-    assert 'Server returned HTTP status 200 (<nada>)' in resp.json['err_desc']
+    assert 'Transport Error: Server returned response (200) with invalid XML' in resp.json['err_desc']
+    assert 'Premature end of data in tag nada' in resp.json['err_desc']
+
 
 @mock.patch('passerelle.utils.Request.get', side_effect=iph_mocked_get)
 @mock.patch('passerelle.utils.Request.post')
-- 
2.20.1

