From 9378a1d32d67fcc3702b91202ead63ffcf1dad2d Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Wed, 4 Aug 2021 12:28:25 +0200
Subject: [PATCH] caluire-axel: hide invoices created in the future (#55803)

---
 passerelle/contrib/caluire_axel/models.py | 10 ++++++++--
 tests/data/caluire_axel/invoices.xml      |  9 +++++++++
 tests/test_caluire_axel.py                | 22 +++++++++++++++++++++-
 3 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/passerelle/contrib/caluire_axel/models.py b/passerelle/contrib/caluire_axel/models.py
index 568faad4..ce934fd5 100644
--- a/passerelle/contrib/caluire_axel/models.py
+++ b/passerelle/contrib/caluire_axel/models.py
@@ -990,18 +990,21 @@ class CaluireAxel(BaseResource):
 
         code = result.json_response['DATA']['PORTAIL']['GETFACTURESAPAYER']['CODE']
         if code < 0:
             raise APIError('Wrong get-invoices status', err_code='get-invoices-code-error-%s' % code)
 
         data = result.json_response['DATA']['PORTAIL']['GETFACTURESAPAYER']
         result = []
 
+        date_now = localtime().date()
         for facture in data.get('FACTURE', []):
-            result.append(utils.normalize_invoice(facture, family_id))
+            date_creation = datetime.datetime.strptime(facture['EMISSION'], axel.json_date_format).date()
+            if date_creation <= date_now:
+                result.append(utils.normalize_invoice(facture, family_id))
         return result
 
     def get_historical_invoices(self, regie_id, family_id, nb_mounts_limit):
         try:
             nb_mois = int(nb_mounts_limit)
         except ValueError:
             raise APIError('nb_mounts_limit must be an integer', err_code='bad-request', http_status=400)
         try:
@@ -1027,19 +1030,22 @@ class CaluireAxel(BaseResource):
         code = result.json_response['DATA']['PORTAIL']['GETLISTFACTURES']['CODE']
         if code < 0:
             raise APIError(
                 'Wrong get-historical-invoices status',
                 err_code='get-historical-invoices-code-error-%s' % code,
             )
 
         data = result.json_response['DATA']['PORTAIL']['GETLISTFACTURES']
+        date_now = localtime().date()
         result = []
         for facture in data.get('FACTURE', []):
-            result.append(utils.normalize_invoice(facture, family_id, historical=True))
+            date_creation = datetime.datetime.strptime(facture['EMISSION'], axel.json_date_format).date()
+            if date_creation <= date_now:
+                result.append(utils.normalize_invoice(facture, family_id, historical=True))
         return result
 
     def get_invoice(self, regie_id, invoice_id, family_id, historical=None, nb_mounts_limit=None):
         if historical:
             invoices_data = self.get_historical_invoices(
                 regie_id=regie_id, family_id=family_id, nb_mounts_limit=nb_mounts_limit
             )
         else:
diff --git a/tests/data/caluire_axel/invoices.xml b/tests/data/caluire_axel/invoices.xml
index a6582904..65968962 100644
--- a/tests/data/caluire_axel/invoices.xml
+++ b/tests/data/caluire_axel/invoices.xml
@@ -12,8 +12,17 @@
   <IDFACTURE>43</IDFACTURE>
   <MONTANT>44.94</MONTANT>
   <ENCAISSE>0.00</ENCAISSE>
   <FACTURATION>PRESTATIONS PERISCOLAIRES NOVEMBRE 2019</FACTURATION>
   <EMISSION>12/12/2019</EMISSION>
   <ECHEANCE>04/01/2020</ECHEANCE>
   <EXISTEPDF>n</EXISTEPDF>
 </FACTURE>
+<FACTURE>
+  <IDFACTURE>44</IDFACTURE>
+  <MONTANT>44.44</MONTANT>
+  <ENCAISSE>0.00</ENCAISSE>
+  <FACTURATION>PRESTATIONS PERISCOLAIRES DECEMBRE 2019</FACTURATION>
+  <EMISSION>12/01/2020</EMISSION>
+  <ECHEANCE>04/02/2020</ECHEANCE>
+  <EXISTEPDF>n</EXISTEPDF>
+</FACTURE>
diff --git a/tests/test_caluire_axel.py b/tests/test_caluire_axel.py
index f95c823f..d62f6c1d 100644
--- a/tests/test_caluire_axel.py
+++ b/tests/test_caluire_axel.py
@@ -2815,17 +2815,17 @@ def test_invoices_endpoint_no_invoice(app, resource):
     </GETFACTURESAPAYER>
 </PORTAIL>'''
     with mock_data(content, 'GetFacturesaPayer'):
         resp = app.get('/caluire-axel/test/regie/MAREGIE/invoices?NameID=yyy')
     assert resp.json['err'] == 0
     assert resp.json['data'] == []
 
 
-@freezegun.freeze_time('2019-12-05')
+@freezegun.freeze_time('2019-12-12')
 def test_invoices_endpoint(app, resource):
     Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
     filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
     with open(filepath) as xml:
         content = (
             '''
 <PORTAIL>
   <GETFACTURESAPAYER>
@@ -2933,16 +2933,17 @@ def test_invoices_history_endpoint_no_invoice(app, resource):
     </GETLISTFACTURES>
 </PORTAIL>'''
     with mock_data(content, 'GetListFactures'):
         resp = app.get('/caluire-axel/test/regie/MAREGIE/invoices/history?NameID=yyy')
     assert resp.json['err'] == 0
     assert resp.json['data'] == []
 
 
+@freezegun.freeze_time('2019-12-12')
 def test_invoices_history_endpoint(app, resource):
     Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
     filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
     with open(filepath) as xml:
         content = (
             '''<PORTAIL>
     <GETLISTFACTURES>
         %s
@@ -3038,16 +3039,17 @@ def test_invoice_endpoint_bad_request(app, resource):
     </GETLISTFACTURES>
 </PORTAIL>'''
     with mock_data(content, 'GetListFactures'):
         resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-42?NameID=yyy')
     assert resp.json['err_desc'] == "Wrong get-historical-invoices status"
     assert resp.json['err'] == 'get-historical-invoices-code-error--3'
 
 
+@freezegun.freeze_time('2019-12-12')
 def test_invoice_endpoint_no_result(app, resource):
     resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42?NameID=yyy')
     assert resp.json['err_desc'] == "Person not found"
     assert resp.json['err'] == 'not-found'
 
     Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
     filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
     with open(filepath) as xml:
@@ -3059,24 +3061,35 @@ def test_invoice_endpoint_no_result(app, resource):
   </GETFACTURESAPAYER>
 </PORTAIL>'''
         % invoices
     )
     with mock_data(content, 'GetFacturesaPayer'):
         resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-35?NameID=yyy')
     assert resp.json['err_desc'] == "Invoice not found"
     assert resp.json['err'] == 'not-found'
+
+    with mock_data(content, 'GetFacturesaPayer'):
+        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-44?NameID=yyy')
+    assert resp.json['err_desc'] == "Invoice not found"
+    assert resp.json['err'] == 'not-found'
+
     content = (
         '''<PORTAIL>
     <GETLISTFACTURES>
         %s
     </GETLISTFACTURES>
 </PORTAIL>'''
         % invoices
     )
+    with mock_data(content, 'GetListFactures'):
+        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-35?NameID=yyy')
+    assert resp.json['err_desc'] == "Invoice not found"
+    assert resp.json['err'] == 'not-found'
+
     with mock_data(content, 'GetListFactures'):
         resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-44?NameID=yyy')
     assert resp.json['err_desc'] == "Invoice not found"
     assert resp.json['err'] == 'not-found'
 
 
 @freezegun.freeze_time('2019-12-04')
 def test_invoice_endpoint(app, resource):
@@ -3188,16 +3201,17 @@ def test_invoice_pdf_endpoint_bad_request(app, resource):
         resp = app.get(
             '/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-42/pdf?NameID=yyy&nb_mounts_limit=not_a_number',
             status=404,
         )
     assert resp.json['err_desc'] == "nb_mounts_limit must be an integer"
     assert resp.json['err'] == 'bad-request'
 
 
+@freezegun.freeze_time('2019-12-13')
 def test_invoice_pdf_endpoint_no_result(app, resource):
     resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pdf?NameID=yyy', status=404)
     assert resp.json['err_desc'] == "Person not found"
     assert resp.json['err'] == 'not-found'
 
     Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
     filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
     with open(filepath) as xml:
@@ -3311,16 +3325,17 @@ def test_pay_invoice_endpoint_axel_error(app, resource):
 
 def test_pay_invoice_endpoint_bad_request(app, resource):
     resp = app.post_json(
         '/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pay?NameID=yyy', params={}, status=400
     )
     assert resp.json['err_desc'] == "'transaction_date' is a required property"
 
 
+@freezegun.freeze_time('2019-12-04')
 def test_pay_invoice_endpoint_no_result(app, resource):
     payload = {
         'transaction_date': '2021-06-15T12:00:00',
         'transaction_id': 'foo',
     }
     filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
     with open(filepath) as xml:
         content = (
@@ -3331,16 +3346,21 @@ def test_pay_invoice_endpoint_no_result(app, resource):
 </PORTAIL>'''
             % xml.read()
         )
     with mock_data(content, 'GetFacturesaPayer'):
         resp = app.post_json('/caluire-axel/test/regie/MAREGIE/invoice/XXX-35/pay?NameID=yyy', params=payload)
     assert resp.json['err_desc'] == "Invoice not found"
     assert resp.json['err'] == 'not-found'
 
+    with mock_data(content, 'GetFacturesaPayer'):
+        resp = app.post_json('/caluire-axel/test/regie/MAREGIE/invoice/XXX-44/pay?NameID=yyy', params=payload)
+    assert resp.json['err_desc'] == "Invoice not found"
+    assert resp.json['err'] == 'not-found'
+
 
 def test_pay_invoice_endpoint_wrong_response(app, resource):
     payload = {
         'transaction_date': '2021-06-15T12:00:00',
         'transaction_id': 'foo',
     }
     with mock.patch(
         'passerelle.contrib.caluire_axel.models.CaluireAxel.get_invoice',
-- 
2.30.2

