dpark: convert soap errors to APIError (#76903)
gitea/passerelle/pipeline/head This commit looks good Details

This commit is contained in:
Benjamin Dauvergne 2023-04-23 17:40:16 +02:00
parent fc9444cd98
commit a20835e118
3 changed files with 27 additions and 30 deletions

View File

@ -192,7 +192,7 @@ def get_address_params(data):
def get_service(instance): def get_service(instance):
client = instance.soap_client() client = instance.soap_client(api_error=True)
service_name = list(client.wsdl.services)[0] service_name = list(client.wsdl.services)[0]
port_name = list(client.wsdl.services[service_name].ports)[0] port_name = list(client.wsdl.services[service_name].ports)[0]
binding_name = str(client.wsdl.services[service_name].ports[port_name].binding.name) binding_name = str(client.wsdl.services[service_name].ports[port_name].binding.name)

View File

@ -108,9 +108,14 @@ class SOAPClient(Client):
) )
super().__init__(wsdl_url, transport=transport, **kwargs) super().__init__(wsdl_url, transport=transport, **kwargs)
@property def bind(self, *args, **kwargs):
def service(self): service = super().bind(*args, **kwargs)
service = super().service if self.api_error:
service.__class__ = ServiceProxyWrapper
return service
def create_service(self, *args, **kwargs):
service = super().create_service(*args, **kwargs)
if self.api_error: if self.api_error:
service.__class__ = ServiceProxyWrapper service.__class__ = ServiceProxyWrapper
return service return service

View File

@ -1,15 +1,15 @@
import base64 import base64
import os import os
import xml.etree.ElementTree as ET
from unittest import mock from unittest import mock
import pytest import pytest
import requests
import responses
from django.utils.encoding import force_str from django.utils.encoding import force_str
from passerelle.contrib.dpark.models import DPark, Pairing from passerelle.contrib.dpark.models import DPark, Pairing
from passerelle.utils.conversion import to_pdf from passerelle.utils.conversion import to_pdf
from passerelle.utils.jsonresponse import APIError from passerelle.utils.jsonresponse import APIError
from passerelle.utils.soap import SOAPError
from tests.utils import ResponsesSoap, make_resource from tests.utils import ResponsesSoap, make_resource
SLUG = 'test' SLUG = 'test'
@ -23,27 +23,6 @@ with open(os.path.join(os.path.dirname(__file__), 'data/small.jpg'), 'rb') as f:
JPEG_CONTENT = f.read() JPEG_CONTENT = f.read()
def make_response(name, items):
base = '''<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>'''
root = ET.fromstring(base)
body = root.find('{http://schemas.xmlsoap.org/soap/envelope/}Body')
response_tag_name = '{urn:Webservice_Residants}PLS_%sResponse' % name
result_tag_name = 'PLS_%sResult' % name
response = ET.SubElement(body, response_tag_name)
result = ET.SubElement(response, result_tag_name)
for key, value in items:
elt = ET.SubElement(result, key)
if value:
elt.text = value
return ET.tostring(root)
@pytest.fixture @pytest.fixture
def dpark(db): def dpark(db):
resource = make_resource( resource = make_resource(
@ -94,10 +73,23 @@ def get_service():
yield get_service yield get_service
def test_call_service_error(dpark, app, get_service): @responses.activate
get_service.return_value = MockedService(error_class=SOAPError('boom!')) def test_call_service_error(dpark, app):
responses.add(responses.GET, WSDL_URL, body=WSDL_CONTENT.encode())
responses.add(responses.POST, SERVICE_URL, body=requests.ConnectionError())
resp = app.get('/dpark/test/ping/') resp = app.get('/dpark/test/ping/')
assert 'boom!' in resp.json['err_desc'] assert 'is unreachable' in resp.json['err_desc']
soap_fault = '''<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Unmarshalling Error: Not a number: abc </faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>'''
responses.add(responses.POST, SERVICE_URL, body=soap_fault.encode())
resp = app.get('/dpark/test/ping/')
assert 'Unmarshalling' in resp.json['err_desc']
def test_ping(dpark, app, get_service): def test_ping(dpark, app, get_service):