From 79ddaddd6f6bfb5210368e3368a57ca15757d3ea 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 | 86 +++- 3 files changed, 677 insertions(+), 18 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..71e211e 100644 --- a/tests/test_dpark.py +++ b/tests/test_dpark.py @@ -1,7 +1,11 @@ # -*- coding: utf-8 -*- import base64 import mock +import os import pytest +import xml.etree.ElementTree as ET + +import httmock from zeep.exceptions import TransportError, Fault as WebFault @@ -11,12 +15,36 @@ from passerelle.contrib.dpark.models import DPark, Pairing from passerelle.utils.jsonresponse import APIError +SLUG = 'test' +WSDL_URL = 'https://example.net/dpark?wsdl' +OPERATIONAL_URL = 'https://example.net/dpark' + +with open(os.path.join(os.path.dirname(__file__), 'data/dpark.awws.wsdl')) as f: + WSDL_CONTENT = f.read() + + @pytest.fixture def dpark(db): - return make_resource(DPark, **{ - 'wsdl_url': 'http://example.net/whatever?wsdl', 'slug': 'test', - 'operation_url': 'http://example.net/whatever' + resource = make_resource(DPark, **{ + 'slug': SLUG, + 'wsdl_url': WSDL_URL, + 'operation_url': OPERATIONAL_URL, }) + resource.mock_requests = [] + resource.mock_responses = [] + + @httmock.urlmatch(scheme='https', netloc='example.net', path='/dpark', query='wsdl', method='GET') + def wsdl(url, request): + return WSDL_CONTENT + + @httmock.urlmatch(scheme='https', netloc='example.net', path='/dpark', method='POST') + def request(url, request): + idx = len(resource.mock_requests) + resource.mock_requests.append(request) + return resource.mock_responses[idx] + + with httmock.HTTMock(wsdl, request): + yield resource class ReplyDataClass(dict): @@ -487,7 +515,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 +523,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', @@ -519,15 +547,39 @@ def test_send_files(dpark, app, settings): # add custom document type settings.DPARK_DOCUMENT_CODES = {'toto': '73'} - with mock.patch('passerelle.contrib.dpark.models.DPark.call') as soap_call: - soap_call.return_value = ReplyDataClass(**{'CodeRetour': '01', 'MessageRetour': None}) - resp = app.post_json(url, params=params) - assert resp.json['data'] is True - assert soap_call.call_args[0][1] == 'E-8-N5UTAK6P' - assert soap_call.call_args[0][2] == '61718' - assert soap_call.call_args[0][3] == 4 - 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][1]['NomFichier'] == 'cartegrise2.pdf' - assert soap_call.call_args[0][4][1]['TypeDocument'] == '6' + response = """ + + + + + + 01 + + + + +""" + + dpark.mock_responses.append(response) + resp = app.post_json(url, params=params) + assert resp.json['err'] == 0 + assert resp.json['data'] is True + assert len(dpark.mock_requests) == 1 + root = ET.fromstring(dpark.mock_requests[0].body) + pj_node = root.find('{http://schemas.xmlsoap.org/soap/envelope/}Body/{urn:Webservice_Residants}PLS_ENVOIPJ') + assert pj_node.find('NumeroTeledossier').text == 'E-8-N5UTAK6P' + assert pj_node.find('NumeroDossier').text == '61718' + assert pj_node.find('NbFichier').text == '4' + assert len(pj_node.findall('Bloc_Fichiers')) == 4 + assert pj_node.findall('Bloc_Fichiers')[0].find('TypeDocument').text == '6' + assert pj_node.findall('Bloc_Fichiers')[0].find('NomFichier').text == 'cartegrise.pdf' + assert pj_node.findall('Bloc_Fichiers')[0].find('Fichier').text == base64.b64encode('carte grise 1') + assert pj_node.findall('Bloc_Fichiers')[1].find('TypeDocument').text == '6' + assert pj_node.findall('Bloc_Fichiers')[1].find('NomFichier').text == 'cartegrise2.pdf' + assert pj_node.findall('Bloc_Fichiers')[1].find('Fichier').text == base64.b64encode('carte grise 2') + assert pj_node.findall('Bloc_Fichiers')[2].find('TypeDocument').text == '2' + assert pj_node.findall('Bloc_Fichiers')[2].find('NomFichier').text == 'address_proof.pdf' + assert pj_node.findall('Bloc_Fichiers')[2].find('Fichier').text == base64.b64encode('this is my proof of address') + assert pj_node.findall('Bloc_Fichiers')[3].find('TypeDocument').text == '73' + assert pj_node.findall('Bloc_Fichiers')[3].find('NomFichier').text == 'cartegrisetoto.pdf' + assert pj_node.findall('Bloc_Fichiers')[3].find('Fichier').text == base64.b64encode('whatever') -- 2.18.0