Projet

Général

Profil

0001-iparapheur-add-endpoint-to-display-raw-wsdl-30257.patch

Nicolas Roche, 28 février 2019 14:21

Télécharger (4,91 ko)

Voir les différences:

Subject: [PATCH] iparapheur: add endpoint to display raw wsdl (#30257)

 passerelle/contrib/iparapheur/models.py       | 10 +++++++-
 passerelle/contrib/iparapheur/soap.py         | 10 +++++++-
 .../iparapheur/iparapheur_detail.html         |  4 ++++
 tests/test_iparapheur.py                      | 23 +++++++++++++++++++
 4 files changed, 45 insertions(+), 2 deletions(-)
passerelle/contrib/iparapheur/models.py
31 31
from passerelle.utils.jsonresponse import APIError
32 32

  
33 33
from .soap import get_client as soap_get_client
34
from .soap import get_wsdl_string as soap_get_wsdl_string
34 35

  
35 36

  
36 37
def get_client(model):
......
39 40
    except ConnectionError as e:
40 41
        raise APIError('i-Parapheur error: %s' % e)
41 42

  
42

  
43 43
def format_type(t):
44 44
    return {'id': unicode(t), 'text': unicode(t)}
45 45

  
......
111 111
    def ping(self, request):
112 112
        return {'data': self.call('echo', 'ping')}
113 113

  
114
    @endpoint(perm='can_access')
115
    def wsdl(self, request):
116
        try:
117
            wsdl = soap_get_wsdl_string(self)
118
        except ConnectionError as e:
119
            raise APIError('i-Parapheur WSDL error: %s' % e)
120
        return HttpResponse(wsdl, content_type='text/xml')
121

  
114 122
    @endpoint(perm='can_access')
115 123
    def subtypes(self, request, type=None):
116 124
        if type:
passerelle/contrib/iparapheur/soap.py
22 22

  
23 23
from suds.client import Client
24 24
from suds.transport.http import HttpAuthenticated
25
from suds.transport import Reply, TransportError
25
from suds.transport import Request, Reply, TransportError
26 26
from suds.plugin import MessagePlugin, DocumentPlugin
27 27

  
28 28
from requests.exceptions import RequestException
......
91 91
    transport = Transport(instance)
92 92
    return Client(instance.wsdl_url, transport=transport,
93 93
                  plugins=[Handlewsdl(), Filter()], cache=None)
94

  
95
def get_wsdl_string(instance):
96
    '''download wsdl like suds's DocumentReader does it (but do not parse it)'''
97
    transport = Transport(instance)
98
    fp = transport.open(Request(instance.wsdl_url))
99
    content = fp.read()
100
    fp.close()
101
    return content
passerelle/contrib/iparapheur/templates/iparapheur/iparapheur_detail.html
6 6

  
7 7
{% block endpoints %}
8 8
<ul>
9
  {% url 'generic-endpoint' connector='iparapheur' slug=object.slug endpoint='wsdl' as wsdl_url %}
10
  <li>{% trans 'Download WSDL file:' %}
11
    <a href="{{ wsdl_url }}">{{ site_base_uri }}{{ wsdl_url }}</a>[?debug]
12
  </li>
9 13
  {% url 'generic-endpoint' connector='iparapheur' slug=object.slug endpoint='ping' as ping_url %}
10 14
  <li>{% trans 'Check WS availability:' %}
11 15
    <a href="{{ ping_url }}">{{ site_base_uri }}{{ ping_url }}</a>[?debug]
tests/test_iparapheur.py
422 422
        f.write('some text')
423 423
    with text_path.open() as f:
424 424
        assert 'text/plain' == get_magic_mime(f.read())
425

  
426
@mock.patch('passerelle.utils.Request.get')
427
def test_call_wsdl(mocked_get, app, conn, xmlmime, wsdl_file):
428
    mocked_get.return_value = mock.Mock(content=file(wsdl_file).read(),
429
                                        status_code=200)
430
    url = reverse('generic-endpoint', kwargs={'slug': conn.slug,
431
                  'connector': 'iparapheur', 'endpoint': 'wsdl'})
432
    url += '?apikey=%s' % API_KEY
433
    resp = app.get(url)
434
    assert resp.headers['content-type'] == 'text/xml'
435
    assert resp.content == file(wsdl_file).read()
436

  
437
@mock.patch('passerelle.contrib.iparapheur.models.soap_get_wsdl_string',
438
            side_effect=ConnectionError('mocked error'))
439
def test_call_wsdl_connectionerror(soap_client, app, conn):
440
    url = reverse('generic-endpoint', kwargs={'connector': 'iparapheur',
441
                    'endpoint': 'wsdl', 'slug': conn.slug})
442
    resp = app.get(url, status=403)
443
    url += '?apikey=%s' % API_KEY
444
    resp = app.get(url)
445
    assert resp.json['err'] == 1
446
    assert resp.json['data'] == None
447
    assert 'mocked error' in resp.json['err_desc']
425
-