Projet

Général

Profil

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

Nicolas Roche, 02 juin 2021 17:31

Télécharger (9,52 ko)

Voir les différences:

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

 passerelle/contrib/caluire_axel/models.py |  42 ++++++
 tests/test_caluire_axel.py                | 151 ++++++++++++++++++++++
 2 files changed, 193 insertions(+)
passerelle/contrib/caluire_axel/models.py
601 601
            )
602 602

  
603 603
        data = result.json_response['DATA']['PORTAIL']['GETLISTFACTURES']
604 604
        result = []
605 605
        for facture in data.get('FACTURE', []):
606 606
            result.append(utils.normalize_invoice(facture, family_id, historical=True))
607 607
        return result
608 608

  
609
    def get_invoice(self, regie_id, invoice_id, family_id, historical=None, nb_mounts_limit=None):
610
        if historical:
611
            invoices_data = self.get_historical_invoices(
612
                regie_id=regie_id, family_id=family_id, nb_mounts_limit=nb_mounts_limit
613
            )
614
        else:
615
            invoices_data = self.get_invoices(regie_id=regie_id, family_id=family_id)
616
        for invoice in invoices_data:
617
            if invoice['display_id'] == invoice_id:
618
                return invoice
619

  
609 620
    @endpoint(
610 621
        display_category=_('Invoices'),
611 622
        display_order=1,
612 623
        name='regie',
613 624
        perm='can_access',
614 625
        pattern=r'^(?P<regie_id>[\w-]+)/invoices/?$',
615 626
        example_pattern='{regie_id}/invoices',
616 627
        description=_("Get invoices to pay"),
......
640 651
    )
641 652
    def invoices_history(self, request, regie_id, NameID, nb_mounts_limit='3'):
642 653
        link = self.get_link(NameID)
643 654
        invoices_data = self.get_historical_invoices(
644 655
            regie_id, family_id=link.family_id, nb_mounts_limit=nb_mounts_limit
645 656
        )
646 657
        return {'data': invoices_data}
647 658

  
659
    @endpoint(
660
        display_category=_('Invoices'),
661
        display_order=3,
662
        name='regie',
663
        perm='can_access',
664
        pattern=r'^(?P<regie_id>[\w-]+)/invoice/(?P<invoice_id>(historical-)?\w+-\d+)/?$',
665
        example_pattern='{regie_id}/invoice/{invoice_id}',
666
        description=_('Get invoice details'),
667
        parameters={
668
            'NameID': {'description': _('Publik ID')},
669
            'regie_id': {'description': _('Regie identifier'), 'example_value': 'ENF'},
670
            'invoice_id': {'description': _('Invoice identifier'), 'example_value': 'IDFAM-42'},
671
            'nb_mounts_limit': {'description': _('Number of months of history'), 'example_value': '12'},
672
        },
673
    )
674
    def invoice(self, request, regie_id, invoice_id, NameID, nb_mounts_limit='12'):
675
        link = self.get_link(NameID)
676
        real_invoice_id = invoice_id.split('-')[-1]
677
        historical = invoice_id.startswith('historical-')
678
        invoice = self.get_invoice(
679
            regie_id=regie_id,
680
            invoice_id=real_invoice_id,
681
            family_id=link.family_id,
682
            historical=historical,
683
            nb_mounts_limit=nb_mounts_limit,
684
        )
685
        if invoice is None:
686
            raise APIError('Invoice not found', err_code='not-found')
687

  
688
        return {'data': invoice}
689

  
648 690

  
649 691
class Link(models.Model):
650 692
    resource = models.ForeignKey(CaluireAxel, on_delete=models.CASCADE)
651 693
    name_id = models.CharField(blank=False, max_length=256)
652 694
    family_id = models.CharField(blank=False, max_length=128)
653 695
    person_id = models.CharField(blank=False, max_length=128)
654 696

  
655 697
    class Meta:
tests/test_caluire_axel.py
1382 1382
                    'ENCAISSE': '0.00',
1383 1383
                    'ECHEANCE': '2020-01-04',
1384 1384
                    'EMISSION': '2019-12-12',
1385 1385
                    'EXISTEPDF': False,
1386 1386
                }
1387 1387
            },
1388 1388
        },
1389 1389
    ]
1390

  
1391

  
1392
def test_invoice_endpoint_axel_error(app, resource):
1393
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
1394
    with mock.patch('passerelle.contrib.caluire_axel.schemas.get_factures_a_payer') as operation:
1395
        operation.side_effect = AxelError('FooBar')
1396
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42?NameID=yyy')
1397
    assert resp.json['err_desc'] == "Axel error: FooBar"
1398
    assert resp.json['err'] == 'error'
1399

  
1400

  
1401
def test_invoice_endpoint_bad_request(app, resource):
1402
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
1403
    with mock_data(None, 'GetListFactures'):
1404
        resp = app.get(
1405
            '/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-42?NameID=yyy&nb_mounts_limit=not_a_number',
1406
            status=400,
1407
        )
1408
    assert resp.json['err_desc'] == "nb_mounts_limit must be an integer"
1409
    assert resp.json['err'] == 'bad-request'
1410

  
1411
    content = '''<PORTAIL>
1412
    <GETFACTURESAPAYER>
1413
        <CODE>-3</CODE>
1414
    </GETFACTURESAPAYER>
1415
</PORTAIL>'''
1416
    with mock_data(content, 'GetFacturesaPayer'):
1417
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42?NameID=yyy')
1418
    assert resp.json['err_desc'] == "Wrong get-invoices status"
1419
    assert resp.json['err'] == 'get-invoices-code-error--3'
1420

  
1421
    content = '''<PORTAIL>
1422
    <GETLISTFACTURES>
1423
        <CODE>-3</CODE>
1424
    </GETLISTFACTURES>
1425
</PORTAIL>'''
1426
    with mock_data(content, 'GetListFactures'):
1427
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-42?NameID=yyy')
1428
    assert resp.json['err_desc'] == "Wrong get-historical-invoices status"
1429
    assert resp.json['err'] == 'get-historical-invoices-code-error--3'
1430

  
1431

  
1432
def test_invoice_endpoint_no_result(app, resource):
1433
    resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42?NameID=yyy')
1434
    assert resp.json['err_desc'] == "Person not found"
1435
    assert resp.json['err'] == 'not-found'
1436

  
1437
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
1438
    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
1439
    with open(filepath) as xml:
1440
        invoices = xml.read()
1441
    content = (
1442
        '''<PORTAIL>
1443
  <GETFACTURESAPAYER>
1444
    %s
1445
  </GETFACTURESAPAYER>
1446
</PORTAIL>'''
1447
        % invoices
1448
    )
1449
    with mock_data(content, 'GetFacturesaPayer'):
1450
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-35?NameID=yyy')
1451
    assert resp.json['err_desc'] == "Invoice not found"
1452
    assert resp.json['err'] == 'not-found'
1453
    content = (
1454
        '''<PORTAIL>
1455
    <GETLISTFACTURES>
1456
        %s
1457
    </GETLISTFACTURES>
1458
</PORTAIL>'''
1459
        % invoices
1460
    )
1461
    with mock_data(content, 'GetListFactures'):
1462
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-44?NameID=yyy')
1463
    assert resp.json['err_desc'] == "Invoice not found"
1464
    assert resp.json['err'] == 'not-found'
1465

  
1466

  
1467
def test_invoice_endpoint(app, resource):
1468
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
1469
    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
1470
    with open(filepath) as xml:
1471
        invoices = xml.read()
1472
    content = (
1473
        '''<PORTAIL>
1474
  <GETFACTURESAPAYER>
1475
    %s
1476
  </GETFACTURESAPAYER>
1477
</PORTAIL>'''
1478
        % invoices
1479
    )
1480
    with mock_data(content, 'GetFacturesaPayer'):
1481
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42?NameID=yyy')
1482
    assert resp.json['err'] == 0
1483
    assert resp.json['data'] == {
1484
        'id': 'XXX-42',
1485
        'display_id': '42',
1486
        'label': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
1487
        'amount': '4.94',
1488
        'total_amount': '44.94',
1489
        'amount_paid': '40.00',
1490
        'online_payment': True,
1491
        'created': '2019-11-12',
1492
        'pay_limit_date': '2019-12-04',
1493
        'has_pdf': True,
1494
        'paid': False,
1495
        'vendor': {
1496
            'caluire-axel': {
1497
                'IDFACTURE': 42,
1498
                'MONTANT': '44.94',
1499
                'ENCAISSE': '40.00',
1500
                'FACTURATION': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
1501
                'ECHEANCE': '2019-12-04',
1502
                'EMISSION': '2019-11-12',
1503
                'EXISTEPDF': True,
1504
            }
1505
        },
1506
    }
1507

  
1508
    content = (
1509
        '''<PORTAIL>
1510
    <GETLISTFACTURES>
1511
        %s
1512
    </GETLISTFACTURES>
1513
</PORTAIL>'''
1514
        % invoices
1515
    )
1516
    with mock_data(content, 'GetListFactures'):
1517
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-42?NameID=yyy')
1518
    assert resp.json['err'] == 0
1519
    assert resp.json['data'] == {
1520
        'id': 'historical-XXX-42',
1521
        'display_id': '42',
1522
        'label': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
1523
        'amount': 0,
1524
        'total_amount': '44.94',
1525
        'online_payment': False,
1526
        'pay_limit_date': '',
1527
        'has_pdf': True,
1528
        'paid': False,
1529
        'vendor': {
1530
            'caluire-axel': {
1531
                'IDFACTURE': 42,
1532
                'MONTANT': '44.94',
1533
                'ENCAISSE': '40.00',
1534
                'FACTURATION': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
1535
                'ECHEANCE': '2019-12-04',
1536
                'EMISSION': '2019-11-12',
1537
                'EXISTEPDF': True,
1538
            },
1539
        },
1540
    }
1390
-