Projet

Général

Profil

0001-toulouse-axel-allow-to-call-invoice-endpoint-anonymo.patch

Nicolas Roche, 26 août 2021 18:20

Télécharger (9 ko)

Voir les différences:

Subject: [PATCH] toulouse-axel: allow to call invoice endpoint anonymously
 (#56412)

 passerelle/contrib/toulouse_axel/models.py | 24 ++++---
 tests/test_toulouse_axel.py                | 76 ++++++++++++++++++++--
 2 files changed, 86 insertions(+), 14 deletions(-)
passerelle/contrib/toulouse_axel/models.py
690 690
        data = result.json_response['DATA']['PORTAIL']['DUI']
691 691
        result = []
692 692
        for facture in data.get('FACTURES', []):
693 693
            if facture['IDREGIE'] != regie_id:
694 694
                continue
695 695
            result.append(utils.normalize_invoice(facture, dui))
696 696
        return result
697 697

  
698
    def get_historical_invoices(self, name_id):
699
        link = self.get_link(name_id)
698
    def get_historical_invoices(self, dui=None, name_id=None):
699
        assert name_id or dui
700
        if name_id:
701
            dui = self.get_link(name_id).dui
702

  
700 703
        try:
701
            result = schemas.list_dui_factures(
702
                self, {'LISTFACTURE': {'NUMDUI': link.dui, 'DEBUT': '1970-01-01'}}
703
            )
704
            result = schemas.list_dui_factures(self, {'LISTFACTURE': {'NUMDUI': dui, 'DEBUT': '1970-01-01'}})
704 705
        except axel.AxelError as e:
705 706
            raise APIError(
706 707
                'Axel error: %s' % e,
707 708
                err_code='error',
708 709
                data={'xml_request': e.xml_request, 'xml_response': e.xml_response},
709 710
            )
710 711

  
711 712
        data = result.json_response['DATA']['PORTAIL']['LISTFACTURE']
712 713
        result = []
713 714
        for direction in data.get('DIRECTION', []):
714 715
            for facture in direction.get('FACTURE', []):
715 716
                result.append(
716 717
                    utils.normalize_invoice(
717 718
                        facture,
718
                        link.dui,
719
                        dui,
719 720
                        historical=True,
720 721
                        vendor_base={
721 722
                            'NUMDIRECTION': direction['NUMDIRECTION'],
722 723
                            'IDDIRECTION': direction['IDDIRECTION'],
723 724
                            'LIBDIRECTION': direction['LIBDIRECTION'],
724 725
                        },
725 726
                    )
726 727
                )
727 728
        return result
728 729

  
729 730
    def get_invoice(self, regie_id, invoice_id, dui=None, name_id=None, historical=None):
730 731
        if historical:
731
            invoices_data = self.get_historical_invoices(name_id=name_id)
732
            invoices_data = self.get_historical_invoices(dui=dui, name_id=name_id)
732 733
        else:
733 734
            invoices_data = self.get_invoices(regie_id=regie_id, dui=dui, name_id=name_id)
734 735
        for invoice in invoices_data:
735 736
            if invoice['display_id'] == invoice_id:
736 737
                return invoice
737 738

  
738 739
    @endpoint(
739 740
        display_category=_('Invoices'),
......
778 779
        example_pattern='{regie_id}/invoice/{invoice_id}',
779 780
        description=_('Get invoice details'),
780 781
        parameters={
781 782
            'NameID': {'description': _('Publik ID')},
782 783
            'regie_id': {'description': _('Regie identifier'), 'example_value': '42-PERISCOL'},
783 784
            'invoice_id': {'description': _('Invoice identifier'), 'example_value': 'DUI-42'},
784 785
        },
785 786
    )
786
    def invoice(self, request, regie_id, invoice_id, NameID):
787
        real_invoice_id = invoice_id.split('-')[-1]
787
    def invoice(self, request, regie_id, invoice_id, NameID=None):
788 788
        historical = invoice_id.startswith('historical-')
789
        if historical:
790
            invoice_id = invoice_id[len('historical-') :]
791
        real_invoice_id = invoice_id.split('-')[-1]
792
        dui = invoice_id[: -(len(real_invoice_id) + 1)]
789 793
        invoice = self.get_invoice(
790
            regie_id=regie_id, name_id=NameID, invoice_id=real_invoice_id, historical=historical
794
            regie_id=regie_id, dui=dui, invoice_id=real_invoice_id, historical=historical
791 795
        )
792 796
        if invoice is None:
793 797
            raise APIError('Invoice not found', err_code='not-found')
794 798

  
795 799
        return {'data': invoice}
796 800

  
797 801
    @endpoint(
798 802
        display_category=_('Invoices'),
tests/test_toulouse_axel.py
2273 2273
    with mock.patch('passerelle.contrib.toulouse_axel.schemas.ref_facture_a_payer') as operation:
2274 2274
        operation.side_effect = AxelError('FooBar')
2275 2275
        resp = app.get('/toulouse-axel/test/regie/MAREGIE/invoice/XXX-42?NameID=yyy')
2276 2276
    assert resp.json['err_desc'] == "Axel error: FooBar"
2277 2277
    assert resp.json['err'] == 'error'
2278 2278

  
2279 2279

  
2280 2280
def test_invoice_endpoint_no_result(app, resource):
2281
    resp = app.get('/toulouse-axel/test/regie/MAREGIE/invoice/XXX-42?NameID=yyy')
2282
    assert resp.json['err_desc'] == "Person not found"
2283
    assert resp.json['err'] == 'not-found'
2284

  
2285 2281
    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
2286 2282
    filepath = os.path.join(os.path.dirname(__file__), 'data/toulouse_axel/invoices.xml')
2287 2283
    with open(filepath) as xml:
2288 2284
        content = xml.read()
2289 2285
    with mock_getdata(content, 'RefFactureAPayer'):
2290 2286
        resp = app.get('/toulouse-axel/test/regie/MAREGIE/invoice/XXX-35?NameID=yyy')
2291 2287
    assert resp.json['err_desc'] == "Invoice not found"
2292 2288
    assert resp.json['err'] == 'not-found'
......
2363 2359
                'MONTANT': '28.98',
2364 2360
                'NOFACTURE': 42,
2365 2361
                'NUMDIRECTION': 10,
2366 2362
            }
2367 2363
        },
2368 2364
    }
2369 2365

  
2370 2366

  
2367
@pytest.mark.parametrize('dui', ['XXX', 'S-XXX'])
2368
def test_invoice_endpoint_anonymous(app, resource, dui):
2369
    Link.objects.create(resource=resource, name_id='yyy', dui=dui, person_id='42')
2370
    filepath = os.path.join(os.path.dirname(__file__), 'data/toulouse_axel/invoices.xml')
2371
    with open(filepath) as xml:
2372
        content = xml.read().replace('XXX', dui)
2373
    with mock_getdata(content, 'RefFactureAPayer'):
2374
        resp = app.get('/toulouse-axel/test/regie/MAREGIE/invoice/%s-42' % dui)
2375
    assert resp.json['err'] == 0
2376
    assert resp.json['data'] == {
2377
        'id': '%s-42' % dui,
2378
        'display_id': '42',
2379
        'label': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
2380
        'amount': '4.94',
2381
        'total_amount': '44.94',
2382
        'amount_paid': '40.00',
2383
        'online_payment': True,
2384
        'created': '2019-11-12',
2385
        'pay_limit_date': '2019-12-04',
2386
        'has_pdf': True,
2387
        'paid': False,
2388
        'vendor': {
2389
            'toulouse-axel': {
2390
                'IDFACTURATION': '4242-35AA',
2391
                'IDFACTURE': 42,
2392
                'IDREGIE': 'MAREGIE',
2393
                'DATEECHEANCE': '2019-12-04',
2394
                'DATEEMISSION': '2019-11-12',
2395
                'EXISTEPDF': '1',
2396
                'LIBELLE': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
2397
                'MONTANTTOTAL': '44.94',
2398
                'NUMFACTURE': 42,
2399
                'RESTEAPAYER': '4.94',
2400
            }
2401
        },
2402
    }
2403

  
2404
    filepath = os.path.join(os.path.dirname(__file__), 'data/toulouse_axel/invoices_history.xml')
2405
    with open(filepath) as xml:
2406
        content = xml.read().replace('XXX', dui)
2407
    with mock_getdata(content, 'ListeDuiFacturesPayeesRecettees'):
2408
        resp = app.get('/toulouse-axel/test/regie/MAREGIE/invoice/historical-%s-42' % dui)
2409
    assert resp.json['err'] == 0
2410
    assert resp.json['data'] == {
2411
        'amount': 0,
2412
        'created': '2017-03-23',
2413
        'display_id': '42',
2414
        'has_pdf': True,
2415
        'id': 'historical-%s-42' % dui,
2416
        'label': 'PRESTATIONS SEPTEMBRE 2015',
2417
        'online_payment': False,
2418
        'paid': False,
2419
        'pay_limit_date': '',
2420
        'total_amount': '28.98',
2421
        'vendor': {
2422
            'toulouse-axel': {
2423
                'EMISSION': '2017-03-23',
2424
                'IDAXEL': 'AXEL',
2425
                'IDDIRECTION': 'DIR-A',
2426
                'IDFACTURE': 42,
2427
                'IDFAMILLE': dui,
2428
                'IPDF': 'O',
2429
                'LIBDIRECTION': 'DIRECTION A',
2430
                'LIBELLE': 'PRESTATIONS SEPTEMBRE 2015',
2431
                'MONTANT': '28.98',
2432
                'NOFACTURE': 42,
2433
                'NUMDIRECTION': 10,
2434
            }
2435
        },
2436
    }
2437

  
2438

  
2371 2439
def test_invoice_pdf_endpoint_axel_error(app, resource):
2372 2440
    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
2373 2441
    with mock.patch('passerelle.contrib.toulouse_axel.schemas.ref_facture_a_payer') as operation:
2374 2442
        operation.side_effect = AxelError('FooBar')
2375 2443
        resp = app.get('/toulouse-axel/test/regie/MAREGIE/invoice/XXX-42/pdf?NameID=yyy', status=404)
2376 2444
    assert resp.json['err_desc'] == "Axel error: FooBar"
2377 2445
    assert resp.json['err'] == 'error'
2378 2446

  
2379
-