From 9299fcbdd60e1c6028cd854addc3d2e0edd43410 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 22 Jul 2021 11:33:10 +0200 Subject: [PATCH] toulouse_axel: accept DUI with dashes (#55753) --- passerelle/contrib/toulouse_axel/models.py | 8 ++--- tests/test_toulouse_axel.py | 37 ++++++++++++---------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/passerelle/contrib/toulouse_axel/models.py b/passerelle/contrib/toulouse_axel/models.py index 6f89d863..dd7c3231 100644 --- a/passerelle/contrib/toulouse_axel/models.py +++ b/passerelle/contrib/toulouse_axel/models.py @@ -774,7 +774,7 @@ class ToulouseAxel(BaseResource): display_order=3, name='regie', perm='can_access', - pattern=r'^(?P[\w-]+)/invoice/(?P(historical-)?\w+-\d+)/?$', + pattern=r'^(?P[\w-]+)/invoice/(?P(historical-)?[\w-]+-\d+)/?$', example_pattern='{regie_id}/invoice/{invoice_id}', description=_('Get invoice details'), parameters={ @@ -799,7 +799,7 @@ class ToulouseAxel(BaseResource): display_order=4, name='regie', perm='can_access', - pattern=r'^(?P[\w-]+)/invoice/(?P(historical-)?\w+-\d+)/pdf/?$', + pattern=r'^(?P[\w-]+)/invoice/(?P(historical-)?[\w-]+-\d+)/pdf/?$', example_pattern='{regie_id}/invoice/{invoice_id}/pdf', description=_('Get invoice as a PDF file'), parameters={ @@ -851,7 +851,7 @@ class ToulouseAxel(BaseResource): name='regie', methods=['post'], perm='can_access', - pattern=r'^(?P[\w-]+)/invoice/(?P\w+-\d+)/pay/?$', + pattern=r'^(?P[\w-]+)/invoice/(?P[\w-]+-\d+)/pay/?$', example_pattern='{regie_id}/invoice/{invoice_id}/pay', description=_('Notify an invoice as paid'), parameters={ @@ -868,7 +868,7 @@ class ToulouseAxel(BaseResource): ) def pay_invoice(self, request, regie_id, invoice_id, **kwargs): data = json_loads(request.body) - dui, invoice_id = invoice_id.split('-') + dui, invoice_id = invoice_id.rsplit('-', 1) invoice = self.get_invoice(regie_id=regie_id, dui=dui, invoice_id=invoice_id) if invoice is None: diff --git a/tests/test_toulouse_axel.py b/tests/test_toulouse_axel.py index c0ea55bd..c97accae 100644 --- a/tests/test_toulouse_axel.py +++ b/tests/test_toulouse_axel.py @@ -2296,16 +2296,17 @@ def test_invoice_endpoint_no_result(app, resource): assert resp.json['err'] == 'not-found' -def test_invoice_endpoint(app, resource): - Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42') +@pytest.mark.parametrize('dui', ['XXX', 'S-XXX']) +def test_invoice_endpoint(app, resource, dui): + Link.objects.create(resource=resource, name_id='yyy', dui=dui, person_id='42') filepath = os.path.join(os.path.dirname(__file__), 'data/toulouse_axel/invoices.xml') with open(filepath) as xml: - content = xml.read() + content = xml.read().replace('XXX', dui) with mock_getdata(content, 'RefFactureAPayer'): - resp = app.get('/toulouse-axel/test/regie/MAREGIE/invoice/XXX-42?NameID=yyy') + resp = app.get('/toulouse-axel/test/regie/MAREGIE/invoice/%s-42?NameID=yyy' % dui) assert resp.json['err'] == 0 assert resp.json['data'] == { - 'id': 'XXX-42', + 'id': '%s-42' % dui, 'display_id': '42', 'label': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019', 'amount': '4.94', @@ -2334,16 +2335,16 @@ def test_invoice_endpoint(app, resource): filepath = os.path.join(os.path.dirname(__file__), 'data/toulouse_axel/invoices_history.xml') with open(filepath) as xml: - content = xml.read() + content = xml.read().replace('XXX', dui) with mock_getdata(content, 'ListeDuiFacturesPayeesRecettees'): - resp = app.get('/toulouse-axel/test/regie/MAREGIE/invoice/historical-XXX-42?NameID=yyy') + resp = app.get('/toulouse-axel/test/regie/MAREGIE/invoice/historical-%s-42?NameID=yyy' % dui) assert resp.json['err'] == 0 assert resp.json['data'] == { 'amount': 0, 'created': '2017-03-23', 'display_id': '42', 'has_pdf': True, - 'id': 'historical-XXX-42', + 'id': 'historical-%s-42' % dui, 'label': 'PRESTATIONS SEPTEMBRE 2015', 'online_payment': False, 'paid': False, @@ -2355,7 +2356,7 @@ def test_invoice_endpoint(app, resource): 'IDAXEL': 'AXEL', 'IDDIRECTION': 'DIR-A', 'IDFACTURE': 42, - 'IDFAMILLE': 'XXX', + 'IDFAMILLE': dui, 'IPDF': 'O', 'LIBDIRECTION': 'DIRECTION A', 'LIBELLE': 'PRESTATIONS SEPTEMBRE 2015', @@ -2421,21 +2422,22 @@ def test_invoice_pdf_endpoint_no_result(app, resource): assert resp.json['err'] == 'error' -def test_invoice_pdf_endpoint(app, resource): - Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42') +@pytest.mark.parametrize('dui', ['XXX', 'S-XXX']) +def test_invoice_pdf_endpoint(app, resource, dui): + Link.objects.create(resource=resource, name_id='yyy', dui=dui, person_id='42') pdf_content = ''' ''' with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_invoice') as invoice: invoice.return_value = {'has_pdf': True, 'display_id': '42'} with mock_getdata(pdf_content, 'RefFacturePDF'): - app.get('/toulouse-axel/test/regie/MAREGIE/invoice/XXX-42/pdf?NameID=yyy') + app.get('/toulouse-axel/test/regie/MAREGIE/invoice/%s-42/pdf?NameID=yyy' % dui) assert invoice.call_args_list[0][1]['historical'] is False with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_invoice') as invoice: invoice.return_value = {'has_pdf': True, 'display_id': '42'} with mock_getdata(pdf_content, 'RefFacturePDF'): - app.get('/toulouse-axel/test/regie/MAREGIE/invoice/historical-XXX-42/pdf?NameID=yyy') + app.get('/toulouse-axel/test/regie/MAREGIE/invoice/historical-%s-42/pdf?NameID=yyy' % dui) assert invoice.call_args_list[0][1]['historical'] is True @@ -2488,19 +2490,20 @@ def test_pay_invoice_endpoint_no_result(app, resource): assert resp.json['err'] == 'not-found' -def test_pay_invoice_endpoint(app, resource): +@pytest.mark.parametrize('dui', ['XXX', 'S-XXX']) +def test_pay_invoice_endpoint(app, resource, dui): payload = { 'transaction_date': '2020-01-01T12:00:00', 'transaction_id': 'foo', } - Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42') + Link.objects.create(resource=resource, name_id='yyy', dui=dui, person_id='42') filepath = os.path.join(os.path.dirname(__file__), 'data/toulouse_axel/invoices.xml') with open(filepath) as xml: - content = xml.read() + content = xml.read().replace('XXX', dui) with mock_getdata(content, 'RefFactureAPayer'): with mock.patch('passerelle.contrib.toulouse_axel.schemas.form_paiement_dui') as operation: resp = app.post_json( - '/toulouse-axel/test/regie/MAREGIE/invoice/XXX-42/pay?NameID=yyy', params=payload + '/toulouse-axel/test/regie/MAREGIE/invoice/%s-42/pay?NameID=yyy' % dui, params=payload ) assert resp.json['err'] == 0 assert resp.json['data'] is True -- 2.32.0.rc0