Projet

Général

Profil

0005-wip-on-soaperror-set-availability-of-connector-to-do.patch

Benjamin Dauvergne, 23 mars 2022 11:14

Télécharger (4,83 ko)

Voir les différences:

Subject: [PATCH 5/7] wip: on soaperror set availability of connector to down
 when retrieving the WSDL schema

 passerelle/apps/soap/models.py | 21 +++++++++++++++++++--
 passerelle/base/models.py      |  4 ++--
 tests/test_soap.py             | 10 ++++++++++
 3 files changed, 31 insertions(+), 4 deletions(-)
passerelle/apps/soap/models.py
21 21
import zeep.helpers
22 22
import zeep.xsd
23 23
from django.db import models
24
from django.forms import ValidationError
24 25
from django.utils.functional import cached_property
25 26
from django.utils.translation import ugettext_lazy as _
26 27

  
27 28
from passerelle.base.models import BaseResource, HTTPResource
28 29
from passerelle.utils.api import endpoint
30
from passerelle.utils.conversion import exception_to_text
29 31
from passerelle.utils.json import unflatten
30 32

  
31 33

  
......
42 44
    class Meta:
43 45
        verbose_name = _('SOAP connector')
44 46

  
47
    def clean(self):
48
        try:
49
            self.operations_and_schemas
50
        except Exception as e:
51
            raise ValidationError(e)
52

  
45 53
    @classmethod
46 54
    def get_manager_form_class(cls, **kwargs):
47 55
        form_class = super().get_manager_form_class(**kwargs)
......
107 115

  
108 116
    def get_endpoints_infos(self):
109 117
        endpoints = super().get_endpoints_infos()
110
        for name, input_schema, output_schema in self.operations_and_schemas:
118

  
119
        try:
120
            operations_and_schemas = self.operations_and_schemas
121
        except Exception as e:
122
            self.set_availability_status('down', message=exception_to_text(e)[:500])
123
            return endpoints
124

  
125
        for name, input_schema, output_schema in operations_and_schemas:
111 126
            kwargs = dict(
112 127
                name='method',
113 128
                pattern=f'{name}/',
......
137 152
    @property
138 153
    def operations_and_schemas(self):
139 154
        operations = self.client.service._binding._operations
155
        operations_and_schemas = []
140 156
        for name in operations:
141 157
            operation = operations[name]
142 158
            input_type = operation.input.body.type
143 159
            output_type = operation.output.body.type
144 160
            input_schema = self.type2schema(input_type, keep_root=True)
145 161
            output_schema = self.type2schema(output_type, compress=True)
146
            yield name, input_schema, output_schema
162
            operations_and_schemas.append((name, input_schema, output_schema))
163
        return operations_and_schemas
147 164

  
148 165
    def type2schema(self, xsd_type, keep_root=False, compress=False):
149 166
        # simplify schema: when a type contains a unique element, it will try
passerelle/base/models.py
280 280

  
281 281
    def get_endpoints_infos(self):
282 282
        endpoints = []
283
        for name, method in inspect.getmembers(self, predicate=inspect.ismethod):
283
        for name, method in inspect.getmembers(type(self), predicate=inspect.isfunction):
284 284
            if hasattr(method, 'endpoint_info'):
285 285
                method.endpoint_info.object = self
286 286
                endpoint_name = method.endpoint_info.name
......
499 499
        except Exception as e:
500 500
            from passerelle.utils.conversion import exception_to_text
501 501

  
502
            self.set_availability('down', message=exception_to_text(e)[:500])
502
            self.set_availability_status('down', message=exception_to_text(e)[:500])
503 503
        else:
504 504
            self.set_availability_status('up')
505 505

  
tests/test_soap.py
251 251
    }
252 252

  
253 253

  
254
class BrokenSOAP12(SOAP12):
255
    WSDL_CONTENT = SOAP12.WSDL_CONTENT[-100:]  # truncate the WSDL to break it
256

  
257

  
254 258
@pytest.fixture(params=[SOAP11, SOAP12])
255 259
def soap(request):
256 260
    p = request.param()
......
272 276
        response = app.get(f'/soap/{connector.slug}/')
273 277
        assert 'Method sayHello' in response
274 278

  
279
    @pytest.mark.parametrize('soap', [BrokenSOAP12], indirect=True)
280
    def test_homepage_broken_wsdl(self, app, connector, soap):
281
        response = app.get(f'/soap/{connector.slug}/')
282
        response = app.get(f'/soap/{connector.slug}/')
283
        assert response.pyquery('.down')
284

  
275 285

  
276 286
@pytest.fixture
277 287
def connector(db, soap):
278
-