From f08cb624f222fe89d7ba2d3a23759302e0bb92b0 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 9 Dec 2022 17:52:30 +0100 Subject: [PATCH 2/3] toulouse_maelis: add tool to check SOAP requests (#72205) --- tests/test_toulouse_maelis.py | 7 ++++++- tests/utils.py | 22 ++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/tests/test_toulouse_maelis.py b/tests/test_toulouse_maelis.py index a0b18358..7137e375 100644 --- a/tests/test_toulouse_maelis.py +++ b/tests/test_toulouse_maelis.py @@ -291,7 +291,12 @@ def family_service(): def test_link(family_service, con, app): - family_service.add_soap_response('readFamily', get_xml_file('R_read_family.xml')) + def request_check(request): + assert request.dossierNumber == 1312 + + family_service.add_soap_response( + 'readFamily', get_xml_file('R_read_family.xml'), request_check=request_check + ) url = get_endpoint('link') assert Link.objects.count() == 0 diff --git a/tests/utils.py b/tests/utils.py index 26d7dc49..605b167b 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -90,19 +90,24 @@ class ResponsesSoap: self.binding = self.port.binding self.address = self.port.binding_options['address'] - def soap_matcher(self, operation_name): + def soap_matcher(self, operation_name, request_check=None): operation = self.binding.get(operation_name) input_element_qname = operation.input.body.qname def matcher(prepared_request): doc = ET.parse(io.BytesIO(prepared_request.body)) if doc.find(f'.//{str(input_element_qname)}') is not None: - return True, f'Element "{str(input_element_qname)}" found' + try: + return True, f'Element "{str(input_element_qname)}" found' + finally: + if request_check: + request = operation.input.deserialize(doc.getroot()) + request_check(request) return False, None return matcher - def add_soap_response(self, mock, operation_name, response_content): + def add_soap_response(self, mock, operation_name, response_content, request_check=None): operation = self.binding.get(operation_name) doc = ET.parse(io.BytesIO(response_content)) try: @@ -114,14 +119,19 @@ class ResponsesSoap: self.address, body=response_content, status=200, - match=(self.soap_matcher(operation_name),), + match=(self.soap_matcher(operation_name, request_check),), ) @contextlib.contextmanager def __call__(self): with responses.RequestsMock() as mock: mock.add(responses.GET, self.wsdl_url, body=self.wsdl_content, status=200) - mock.add_soap_response = lambda operation, response_content: self.add_soap_response( - mock, operation, response_content + mock.add_soap_response = ( + lambda operation, response_content, request_check=None: self.add_soap_response( + mock, + operation, + response_content, + request_check=request_check, + ) ) yield mock -- 2.37.2