Project

General

Profile

0001-cartads_cs-add-endpoint-to-get-list-of-additionnal-p.patch

Frédéric Péters, 11 Sep 2019 11:12 AM

Download (4.99 KB)

View differences:

Subject: [PATCH] cartads_cs: add endpoint to get list of additionnal pieces
 (#36024)

 passerelle/apps/cartads_cs/models.py | 34 ++++++++++++++++++++
 tests/test_cartads_cs.py             | 47 ++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
passerelle/apps/cartads_cs/models.py
447 447
                break
448 448
        return {'result': result}
449 449

  
450
    @endpoint(perm='can_access',
451
              description=_('Get list of additional file items'),
452
              parameters={
453
                  'tracking_code': TRACKING_CODE_PARAM,
454
              })
455
    def additional_pieces(self, request, tracking_code):
456
        client = self.soap_client(wsdl_url=self.get_wsdl_url('ServicePiece'))
457
        dossier = CartaDSDossier.objects.get(tracking_code=tracking_code)
458
        resp = client.service.GetPiecesDossierACompleter(self.get_token(), dossier.cartads_id_dossier)
459

  
460
        signer = Signer(salt='cart@ds_cs')
461
        upload_token = signer.sign(tracking_code)
462

  
463
        pieces = [
464
            {'id': 'comp-%s-%s' % (x['IdDosPiece'], x['IdPiece']),
465
             'text': x['LibellePiece'],
466
             'description': x['Descriptif'],
467
             'codePiece': x['CodePiece'],
468
             'files': [],
469
             'max_files': 6,
470
             } for x in resp]
471

  
472
        for piece in pieces:
473
            if request:
474
                upload_url = request.build_absolute_uri('%supload/%s/%s/' % (
475
                    self.get_absolute_url(),
476
                    piece['id'],
477
                    upload_token))
478
            else:
479
                upload_url = None
480
            piece['files'] = []
481
            piece['files'].append({'url': upload_url})
482
        return {'data': pieces}
483

  
450 484
    @endpoint(methods=['post'],
451 485
              pattern='^(?P<id_piece>[\w-]+)/(?P<token>[\w:_-]+)/$',
452 486
              description=_('Upload a single document file'),
tests/test_cartads_cs.py
1 1
# -*- coding: utf-8 -*-
2 2

  
3
from collections import OrderedDict
3 4
import datetime
4 5
import os
5 6

  
......
83 84
            'LibelleEtape': 'En cours de saisie'
84 85
            }]
85 86

  
87
    def GetPiecesDossierACompleter(self, token, dossier_id):
88
        return [
89
            OrderedDict([
90
                (u'CodePiece', 'PC07'),
91
                (u'DateDemande', datetime.datetime(2019, 4, 15, 0, 0)),
92
                (u'DatePresentation', None),
93
                (u'DateReception', None),
94
                (u'Descriptif', u"Un document graphique..."),
95
                (u'IdDosPiece', 133837L),
96
                (u'IdPiece', 58L),
97
                (u'LibellePiece', u"Document graphique permettant..."),
98
                (u'NbDocuments', 0L)]),
99
            OrderedDict([
100
                (u'CodePiece', 'PC16-1'),
101
                (u'DateDemande', datetime.datetime(2019, 4, 15, 0, 0)),
102
                (u'DatePresentation', None),
103
                (u'DateReception', None),
104
                (u'Descriptif', u'Formulaire attestant...'),
105
                (u'IdDosPiece', 133840L),
106
                (u'IdPiece', 99L),
107
                (u'LibellePiece', u'Formulaire attestant...'),
108
                (u'NbDocuments', 0L)]),
109
        ]
110

  
86 111

  
87 112
def pdf_mock(url, request):
88 113
    return {'content': '%PDF...', 'status_code': 200}
......
315 340

  
316 341
    resp = app.get('/cartads-cs/test/status?dossier_id=%s' % dossier.id)
317 342
    assert resp.json['status_label'] == u"File refused (Le modèle sélectionné ne correspond à aucun Cerfa géré par l'application.)"
343

  
344

  
345
def test_additional_pieces_management(connector, app, cached_data):
346
    CartaDSDossier.objects.all().delete()
347
    test_send(connector, app, cached_data)
348
    dossier = CartaDSDossier.objects.all()[0]
349
    with mock.patch('passerelle.apps.cartads_cs.models.CartaDSCS.soap_client') as client:
350
        client.return_value = mock.Mock(service=FakeService())
351
        resp = app.get('/cartads-cs/test/additional_pieces?tracking_code=%s' % dossier.tracking_code)
352
    data = resp.json['data']
353
    assert len(data) == 2
354
    assert data[0]['text'] == 'Document graphique permettant...'
355
    assert data[0]['max_files'] == 6
356
    assert data[1]['text'] == u'Formulaire attestant...'
357
    assert data[1]['max_files'] == 6
358
    for piece in data:
359
        assert len(piece['files']) == 1
360
        assert piece['files'][0].keys() == ['url']
361

  
362
    resp = app.post(data[0]['files'][0]['url'],
363
                    upload_files=[('files[]', 'test.pdf', '%PDF...')])
364
    assert resp.json[0]['token']
318
-