From c7f66791c6a7991ed9a44fe0074c17355bb228ed Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 2 Jul 2020 14:57:42 +0200
Subject: [PATCH 2/2] toulouse-axel: annual booking for next year (#44727)

---
 passerelle/contrib/toulouse_axel/models.py  |  6 ++--
 passerelle/contrib/toulouse_axel/schemas.py |  5 +--
 tests/test_toulouse_axel.py                 | 37 ++++++++++++---------
 3 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/passerelle/contrib/toulouse_axel/models.py b/passerelle/contrib/toulouse_axel/models.py
index 6cdff956..7cffabca 100644
--- a/passerelle/contrib/toulouse_axel/models.py
+++ b/passerelle/contrib/toulouse_axel/models.py
@@ -1400,8 +1400,10 @@ class ToulouseAxel(BaseResource):
 
         # build dates of the period
         today = datetime.date.today()
-        reference_year = utils.get_reference_year_from_date(today)
-        start_date = today + datetime.timedelta(days=8)
+        start_date_min = today + datetime.timedelta(days=8)
+        start_date = datetime.datetime.strptime(post_data['booking_date'], utils.json_date_format).date()
+        start_date = max(start_date, start_date_min)
+        reference_year = utils.get_reference_year_from_date(start_date)
         end_date = datetime.date(reference_year+1, 7, 31)
 
         # get known activities for this child, to have the ids
diff --git a/passerelle/contrib/toulouse_axel/schemas.py b/passerelle/contrib/toulouse_axel/schemas.py
index ba5d31b7..61253040 100644
--- a/passerelle/contrib/toulouse_axel/schemas.py
+++ b/passerelle/contrib/toulouse_axel/schemas.py
@@ -425,7 +425,8 @@ ANNUAL_BOOKING_SCHEMA = {
                 {'type': 'null'},
                 {'type': 'string', 'enum': ['', 'SV', 'AV']}
             ]
-        }
+        },
+        'booking_date': copy.deepcopy(utils.date_type)
     },
-    'required': ['booking_list_MAT', 'booking_list_MIDI', 'booking_list_SOIR', 'booking_list_GARD', 'child_id']
+    'required': ['booking_list_MAT', 'booking_list_MIDI', 'booking_list_SOIR', 'booking_list_GARD', 'child_id', 'booking_date']
 }
diff --git a/tests/test_toulouse_axel.py b/tests/test_toulouse_axel.py
index ccaf2616..8e8e6480 100644
--- a/tests/test_toulouse_axel.py
+++ b/tests/test_toulouse_axel.py
@@ -153,6 +153,7 @@ def annual_booking_params():
         'booking_list_GARD': ['3535:GARD:A19P1M4:wednesday'],
         'child_id': '3535',
         'regime': 'AV',
+        'booking_date': '2019-08-01',
     }
 
 
@@ -3796,24 +3797,30 @@ def test_clae_booking_annual_endpoint(app, resource, annual_booking_params, chil
         assert 'ACTIVITE' not in payload['ENFANT'][0]
 
 
-def test_clae_booking_annual_reference_year(app, resource, annual_booking_params, child_activities_data):
+@freezegun.freeze_time('2019-09-01')
+def test_clae_booking_annual_next_year(app, resource, child_activities_data):
     Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
     activities = child_activities_data['ENFANT'][0]
-    with freezegun.freeze_time('2020-04-16'):
-        with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
-            with mock.patch('passerelle.contrib.toulouse_axel.schemas.reservation_annuelle') as operation:
-                operation.return_value = schemas.OperationResult(json_response={}, xml_request='', xml_response='')
-                app.post_json('/toulouse-axel/test/clae_booking_annual?NameID=yyy', params=annual_booking_params)
-    payload = operation.call_args_list[0][0][1]['PORTAIL']['DUI']
-    assert payload['ENFANT'][0]['ACTIVITE'][0]['PERIODE'][0]['DATEDEBUT'] == '2020-04-24'
-    assert payload['ENFANT'][0]['ACTIVITE'][0]['PERIODE'][0]['DATEDFIN'] == '2020-07-31'
-    with freezegun.freeze_time('2020-08-01'):
-        with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
-            with mock.patch('passerelle.contrib.toulouse_axel.schemas.reservation_annuelle') as operation:
-                operation.return_value = schemas.OperationResult(json_response={}, xml_request='', xml_response='')
-                app.post_json('/toulouse-axel/test/clae_booking_annual?NameID=yyy', params=annual_booking_params)
+    for activity in activities['ACTIVITE']:
+        activity['DATEDEBUT'] = '2020-08-01'
+        activity['DATEENTREE'] = '2020-08-01'
+        activity['DATEFIN'] = '2021-07-31'
+        activity['DATESORTIE'] = '2021-07-31'
+    annual_booking_params = {
+        'booking_list_MAT': [],
+        'booking_list_MIDI': None,
+        'booking_list_SOIR': None,
+        'booking_list_GARD': None,
+        'child_id': '3535',
+        'regime': 'AV',
+        'booking_date': '2020-08-01',
+    }
+    with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
+        with mock.patch('passerelle.contrib.toulouse_axel.schemas.reservation_annuelle') as operation:
+            operation.return_value = schemas.OperationResult(json_response={}, xml_request='', xml_response='')
+            app.post_json('/toulouse-axel/test/clae_booking_annual?NameID=yyy', params=annual_booking_params)
     payload = operation.call_args_list[0][0][1]['PORTAIL']['DUI']
-    assert payload['ENFANT'][0]['ACTIVITE'][0]['PERIODE'][0]['DATEDEBUT'] == '2020-08-09'
+    assert payload['ENFANT'][0]['ACTIVITE'][0]['PERIODE'][0]['DATEDEBUT'] == '2020-08-01'
     assert payload['ENFANT'][0]['ACTIVITE'][0]['PERIODE'][0]['DATEDFIN'] == '2021-07-31'
 
 
-- 
2.20.1

