Projet

Général

Profil

0003-caluire-axel-add-invoice-endpoint-53884.patch

Nicolas Roche, 12 mai 2021 12:15

Télécharger (8,62 ko)

Voir les différences:

Subject: [PATCH 3/4] caluire-axel: add invoice endpoint (#53884)

 passerelle/contrib/caluire_axel/models.py |  39 +++++++
 tests/test_caluire_axel.py                | 131 ++++++++++++++++++++++
 2 files changed, 170 insertions(+)
passerelle/contrib/caluire_axel/models.py
355 355
            )
356 356

  
357 357
        data = result.json_response['DATA']['PORTAIL']['GETLISTFACTURES']
358 358
        result = []
359 359
        for facture in data.get('FACTURE', []):
360 360
            result.append(utils.normalize_invoice(facture, link.family_id, historical=True))
361 361
        return result
362 362

  
363
    def get_invoice(self, regie_id, invoice_id, name_id, historical=None, nb_mounts_limit=None):
364
        if historical:
365
            invoices_data = self.get_historical_invoices(regie_id, name_id, nb_mounts_limit)
366
        else:
367
            invoices_data = self.get_invoices(regie_id, name_id)
368
        for invoice in invoices_data:
369
            if invoice['display_id'] == invoice_id:
370
                return invoice
371

  
363 372
    @endpoint(
364 373
        display_category=_('Invoices'),
365 374
        display_order=1,
366 375
        name='regie',
367 376
        perm='can_access',
368 377
        pattern=r'^(?P<regie_id>[\w-]+)/invoices/?$',
369 378
        example_pattern='{regie_id}/invoices',
370 379
        description=_("Get invoices to pay"),
......
392 401
        },
393 402
    )
394 403
    def invoices_history(self, request, regie_id, NameID, nb_mounts_limit='3'):
395 404
        invoices_data = self.get_historical_invoices(
396 405
            regie_id, name_id=NameID, nb_mounts_limit=nb_mounts_limit
397 406
        )
398 407
        return {'data': invoices_data}
399 408

  
409
    @endpoint(
410
        display_category=_('Invoices'),
411
        display_order=3,
412
        name='regie',
413
        perm='can_access',
414
        pattern=r'^(?P<regie_id>[\w-]+)/invoice/(?P<invoice_id>(historical-)?\w+-\d+)/?$',
415
        example_pattern='{regie_id}/invoice/{invoice_id}',
416
        description=_('Get invoice details'),
417
        parameters={
418
            'NameID': {'description': _('Publik ID')},
419
            'regie_id': {'description': _('Regie identifier'), 'example_value': '42-PERISCOL'},
420
            'invoice_id': {'description': _('Invoice identifier'), 'example_value': 'DUI-42'},
421
            'nb_mounts_limit': {'description': _('Number of months of history'), 'example_value': '12'},
422
        },
423
    )
424
    def invoice(self, request, regie_id, invoice_id, NameID, nb_mounts_limit='12'):
425
        real_invoice_id = invoice_id.split('-')[-1]
426
        historical = invoice_id.startswith('historical-')
427
        invoice = self.get_invoice(
428
            regie_id=regie_id,
429
            invoice_id=real_invoice_id,
430
            name_id=NameID,
431
            historical=historical,
432
            nb_mounts_limit=nb_mounts_limit,
433
        )
434
        if invoice is None:
435
            raise APIError('Invoice not found', err_code='not-found')
436

  
437
        return {'data': invoice}
438

  
400 439

  
401 440
class Link(models.Model):
402 441
    resource = models.ForeignKey(CaluireAxel, on_delete=models.CASCADE)
403 442
    name_id = models.CharField(blank=False, max_length=256)
404 443
    family_id = models.CharField(blank=False, max_length=128)
405 444
    person_id = models.CharField(blank=False, max_length=128)
406 445

  
407 446
    class Meta:
tests/test_caluire_axel.py
889 889
                    'ENCAISSE': '0.00',
890 890
                    'DATEECHEANCE': '2020-01-04',
891 891
                    'DATEEMISSION': '2019-12-12',
892 892
                    'EXISTEPDF': False,
893 893
                }
894 894
            },
895 895
        },
896 896
    ]
897

  
898

  
899
def test_invoice_endpoint_axel_error(app, resource):
900
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
901
    with mock.patch('passerelle.contrib.caluire_axel.schemas.get_factures_a_payer') as operation:
902
        operation.side_effect = AxelError('FooBar')
903
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42?NameID=yyy')
904
    assert resp.json['err_desc'] == "Axel error: FooBar"
905
    assert resp.json['err'] == 'error'
906

  
907

  
908
def test_invoice_endpoint_bad_request(app, resource):
909
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
910
    with mock_getdata(None, 'GetListFactures'):
911
        resp = app.get(
912
            '/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-42?NameID=yyy&nb_mounts_limit=not_a_number',
913
            status=400,
914
        )
915
    assert resp.json['err_desc'] == "nb_mounts_limit must be an integer"
916
    assert resp.json['err'] == 'bad-request'
917

  
918

  
919
def test_invoice_endpoint_no_result(app, resource):
920
    resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42?NameID=yyy')
921
    assert resp.json['err_desc'] == "Person not found"
922
    assert resp.json['err'] == 'not-found'
923

  
924
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
925
    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
926
    with open(filepath) as xml:
927
        invoices = xml.read()
928
    content = (
929
        '''<PORTAIL>
930
  <GETFACTURESAPAYER>
931
    %s
932
  </GETFACTURESAPAYER>
933
</PORTAIL>'''
934
        % invoices
935
    )
936
    with mock_getdata(content, 'GetFacturesaPayer'):
937
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-35?NameID=yyy')
938
    assert resp.json['err_desc'] == "Invoice not found"
939
    assert resp.json['err'] == 'not-found'
940
    content = (
941
        '''<PORTAIL>
942
    <GETLISTFACTURES>
943
        %s
944
    </GETLISTFACTURES>
945
</PORTAIL>'''
946
        % invoices
947
    )
948
    with mock_getdata(content, 'GetListFactures'):
949
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-44?NameID=yyy')
950
    assert resp.json['err_desc'] == "Invoice not found"
951
    assert resp.json['err'] == 'not-found'
952

  
953

  
954
def test_invoice_endpoint(app, resource):
955
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
956
    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
957
    with open(filepath) as xml:
958
        invoices = xml.read()
959
    content = (
960
        '''<PORTAIL>
961
  <GETFACTURESAPAYER>
962
    %s
963
  </GETFACTURESAPAYER>
964
</PORTAIL>'''
965
        % invoices
966
    )
967
    with mock_getdata(content, 'GetFacturesaPayer'):
968
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42?NameID=yyy')
969
    assert resp.json['err'] == 0
970
    assert resp.json['data'] == {
971
        'id': 'XXX-42',
972
        'display_id': '42',
973
        'label': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
974
        'amount': '4.94',
975
        'total_amount': '44.94',
976
        'amount_paid': '40.00',
977
        'online_payment': True,
978
        'created': '2019-11-12',
979
        'pay_limit_date': '2019-12-04',
980
        'has_pdf': True,
981
        'paid': False,
982
        'vendor': {
983
            'caluire-axel': {
984
                'IDFACTURE': 42,
985
                'MONTANT': '44.94',
986
                'ENCAISSE': '40.00',
987
                'FACTURATION': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
988
                'DATEECHEANCE': '2019-12-04',
989
                'DATEEMISSION': '2019-11-12',
990
                'EXISTEPDF': True,
991
            }
992
        },
993
    }
994

  
995
    content = (
996
        '''<PORTAIL>
997
    <GETLISTFACTURES>
998
        %s
999
    </GETLISTFACTURES>
1000
</PORTAIL>'''
1001
        % invoices
1002
    )
1003
    with mock_getdata(content, 'GetListFactures'):
1004
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-42?NameID=yyy')
1005
    assert resp.json['err'] == 0
1006
    assert resp.json['data'] == {
1007
        'id': 'historical-XXX-42',
1008
        'display_id': '42',
1009
        'label': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
1010
        'amount': 0,
1011
        'total_amount': '44.94',
1012
        'online_payment': False,
1013
        'pay_limit_date': '',
1014
        'has_pdf': True,
1015
        'paid': False,
1016
        'vendor': {
1017
            'caluire-axel': {
1018
                'IDFACTURE': 42,
1019
                'MONTANT': '44.94',
1020
                'ENCAISSE': '40.00',
1021
                'FACTURATION': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
1022
                'DATEECHEANCE': '2019-12-04',
1023
                'DATEEMISSION': '2019-11-12',
1024
                'EXISTEPDF': True,
1025
            },
1026
        },
1027
    }
897
-