From 1f53559635ea4686e26e0fe2750bdebe6ee6d18e Mon Sep 17 00:00:00 2001 From: Josue Kouka Date: Fri, 14 Sep 2018 12:04:31 +0200 Subject: [PATCH] dpark: pass file's raw content to zeep (#26417) --- passerelle/contrib/dpark/models.py | 5 +- tests/data/dpark.awws.wsdl | 604 +++++++++++++++++++++++++++++ tests/test_dpark.py | 42 +- 3 files changed, 647 insertions(+), 4 deletions(-) create mode 100644 tests/data/dpark.awws.wsdl diff --git a/passerelle/contrib/dpark/models.py b/passerelle/contrib/dpark/models.py index 909627e..fa8c3d3 100644 --- a/passerelle/contrib/dpark/models.py +++ b/passerelle/contrib/dpark/models.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import base64 import json from django.conf import settings @@ -399,8 +400,10 @@ class DPark(BaseResource): errors.extend(value_errors) continue + # Use raw content as zeep encode the content in base64 + file_raw_content = base64.b64decode(value['content']) attached_files.append({ - 'TypeDocument': doc_id, 'NomFichier': value['filename'], 'Fichier': value['content']}) + 'TypeDocument': doc_id, 'NomFichier': value['filename'], 'Fichier': file_raw_content}) # deduce the number of files if errors: raise APIError(errors) diff --git a/tests/data/dpark.awws.wsdl b/tests/data/dpark.awws.wsdl new file mode 100644 index 0000000..f998337 --- /dev/null +++ b/tests/data/dpark.awws.wsdl @@ -0,0 +1,604 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + R&eacute;sum&eacute; : &lt;indiquez ici ce que fait la proc&eacute;dure&gt;<br /> Syntaxe :<br />[ &lt;R&eacute;sultat&gt; = ] FPS_Rech_Immat (&lt;Immatriculation&gt; est cha&#238;ne, &lt;Date&gt; est cha&#238;ne)<br /><br /> Param&egrave;tres :<br /> Immatriculation (cha&#238;ne ANSI) : &lt;indiquez ici le r&#244;le de Immatriculation&gt;<br /> Date (cha&#238;ne ANSI) : &lt;indiquez ici le r&#244;le de Date&gt;<br /> Valeur de retour :<br /> Retour_FPS : // Aucune<br /><br /> Exemple :<br /> Indiquez ici un exemple d'utilisation.<br /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/test_dpark.py b/tests/test_dpark.py index dcb7aac..c51e10e 100644 --- a/tests/test_dpark.py +++ b/tests/test_dpark.py @@ -1,11 +1,13 @@ # -*- coding: utf-8 -*- import base64 +from lxml import objectify as xobject import mock +import os import pytest from zeep.exceptions import TransportError, Fault as WebFault -from utils import make_resource +from utils import make_resource, FakedResponse from passerelle.contrib.dpark.models import DPark, Pairing from passerelle.utils.jsonresponse import APIError @@ -487,7 +489,7 @@ def test_send_files(dpark, app, settings): 'content': base64.b64encode('this is my proof of address')} params['cartegrise,1'] = { 'filename': 'cartegrise.pdf', 'content_type': 'application/pdf', - 'content': base64.b64encode('whatever') + 'content': base64.b64encode('carte grise 1') } params['toto,6'] = { 'filename': 'cartegrisetoto.pdf', 'content_type': 'application/pdf', @@ -495,7 +497,7 @@ def test_send_files(dpark, app, settings): } params['cartegrise,6'] = { 'filename': 'cartegrise2.pdf', 'content_type': 'application/pdf', - 'content': base64.b64encode('whatever') + 'content': base64.b64encode('carte grise 2') } params['taxe_habitat'] = { 'filename': 'cartegrise2.pdf', 'content_type': 'application/pdf', @@ -529,5 +531,39 @@ def test_send_files(dpark, app, settings): assert len(soap_call.call_args[0][4]) == 4 assert soap_call.call_args[0][4][0]['NomFichier'] == 'cartegrise.pdf' assert soap_call.call_args[0][4][0]['TypeDocument'] == '6' + assert soap_call.call_args[0][4][0]['Fichier'] == 'carte grise 1' assert soap_call.call_args[0][4][1]['NomFichier'] == 'cartegrise2.pdf' assert soap_call.call_args[0][4][1]['TypeDocument'] == '6' + assert soap_call.call_args[0][4][1]['Fichier'] == 'carte grise 2' + + wsldfile = os.path.realpath('tests/data/dpark.awws.wsdl') + + def fake_request(method, url, **kwargs): + if method == 'GET': + content = open(wsldfile).read() + else: + content = """ + + + + + + + 01 + + + + + + """ + return FakedResponse(content=content, status=200) + + with mock.patch('passerelle.utils.Request.request') as request: + request.side_effect = fake_request + resp = app.post_json(url, params=params) + xmsg = request.call_args[1]['data'] + xmsg = xobject.fromstring(xmsg) + doc = xmsg.Body.getchildren()[0].getchildren()[3].Fichier.text + assert base64.b64decode(doc) == 'carte grise 1' + doc = xmsg.Body.getchildren()[0].getchildren()[4].Fichier.text + assert base64.b64decode(doc) == 'carte grise 2' -- 2.18.0