From 54f95f319278bad23edef3983dac07bab018d3a8 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Wed, 12 Jun 2019 14:20:16 +0200
Subject: [PATCH] iparapheur: accept wcs style parameters (#33869)

---
 passerelle/contrib/iparapheur/models.py |  6 +--
 tests/test_iparapheur.py                | 52 ++++++++++++++++---------
 2 files changed, 37 insertions(+), 21 deletions(-)

diff --git a/passerelle/contrib/iparapheur/models.py b/passerelle/contrib/iparapheur/models.py
index 5c8eefb1..895fbc40 100644
--- a/passerelle/contrib/iparapheur/models.py
+++ b/passerelle/contrib/iparapheur/models.py
@@ -139,9 +139,9 @@ class IParapheur(BaseResource, HTTPResource):
         subtyp = data['subtype']
         email = data.get('email')
         visibility = data['visibility']
-        content = base64.b64decode(data['data'])
-        content_type = data.get('content_type') if data.get('content_type') \
-            else get_magic_mime(content)
+        content = base64.b64decode(data.get('file', {}).get('content') or data['data'])
+        content_type = data.get('file', {}).get('content_type') or data.get('content_type') \
+            or get_magic_mime(content)
 
         soap_client = get_client(self)
         if visibility not in ['PUBLIC', 'SERVICE', 'CONFIDENTIEL']:
diff --git a/tests/test_iparapheur.py b/tests/test_iparapheur.py
index 2ce9df1d..066f5f04 100644
--- a/tests/test_iparapheur.py
+++ b/tests/test_iparapheur.py
@@ -110,24 +110,40 @@ def test_create_file(mocked_post, mocked_get, app, conn):
         title, ext = filename.split('.')
         base64_data = 'VGVzdCBEb2N1bWVudA=='
         data = {'type': typ, 'subtype': subtyp, 'visibility': visibility,
-                'title': title, 'data': base64_data, 'content-type':'application/pdf'}
-        url = reverse('generic-endpoint', kwargs={'connector': 'iparapheur',
-                                'endpoint': 'create-file', 'slug': conn.slug})
-        resp = app.post_json(url, params=data, status=403)
-        url += '?apikey=%s' % API_KEY
-        resp = app.post_json(url, params=data)
-        # check output call args
-        assert (BASE_URL,) == mocked_post.call_args[0]
-        xml = ET.fromstring(mocked_post.call_args[1].get('data'))
-        req = xml.find('soap:Body', SOAP_NAMESPACES).find('ns1:CreerDossierRequest', SOAP_NAMESPACES)
-        assert req.find('ns1:DossierTitre', SOAP_NAMESPACES).text == title
-        assert req.find('ns1:DossierID', SOAP_NAMESPACES).text == title
-        assert req.find('ns1:TypeTechnique', SOAP_NAMESPACES).text == typ
-        assert req.find('ns1:SousType', SOAP_NAMESPACES).text == subtyp
-        assert req.find('ns1:Visibilite', SOAP_NAMESPACES).text == visibility
-        assert req.find('ns1:DocumentPrincipal', SOAP_NAMESPACES).text == base64_data
-        assert resp.json['data']['RecordId'] == file_id
-
+                'title': title}
+
+        for param_style in ('old_style', 'new_style'):
+            real_data = data.copy()
+            if param_style == 'old_style':
+                real_data.update({'data': base64_data, 'content-type':'application/pdf'})
+            else:
+                real_data.update({
+                    'file': {
+                        'content': base64_data, 'content_type':'application/pdf'
+                    }
+                })
+
+            url = reverse('generic-endpoint', kwargs={'connector': 'iparapheur',
+                                    'endpoint': 'create-file', 'slug': conn.slug})
+            resp = app.post_json(url, params=real_data, status=403)
+            url += '?apikey=%s' % API_KEY
+            resp = app.post_json(url, params=real_data)
+            # check output call args
+            assert (BASE_URL,) == mocked_post.call_args[0]
+            xml = ET.fromstring(mocked_post.call_args[1].get('data'))
+            req = xml.find('soap:Body', SOAP_NAMESPACES).find('ns1:CreerDossierRequest', SOAP_NAMESPACES)
+            assert req.find('ns1:DossierTitre', SOAP_NAMESPACES).text == title
+            assert req.find('ns1:DossierID', SOAP_NAMESPACES).text == title
+            assert req.find('ns1:TypeTechnique', SOAP_NAMESPACES).text == typ
+            assert req.find('ns1:SousType', SOAP_NAMESPACES).text == subtyp
+            assert req.find('ns1:Visibilite', SOAP_NAMESPACES).text == visibility
+            assert req.find('ns1:DocumentPrincipal', SOAP_NAMESPACES).text == base64_data
+            assert resp.json['data']['RecordId'] == file_id
+
+    data = {
+        'type': typ, 'subtype': subtyp, 'visibility': visibility,
+        'title': title, 'data': base64_data, 'content-type':'application/pdf'
+    }
     # KO
     soap_response = """<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><CreerDossierResponse xmlns="http://www.adullact.org/spring-ws/iparapheur/1.0" xmlns:xmime="http://www.w3.org/2005/05/xmlmime"><MessageRetour><codeRetour>KO</codeRetour><message>KOmessage</message><severite>INFO</severite></MessageRetour></CreerDossierResponse></S:Body></S:Envelope>"""
     response._content = soap_response
-- 
2.20.1

