From f55580cf226a5624bb2d0f5f2e55186b8136e952 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:40:21 +0200
Subject: [PATCH 1/2] toulouse-axel: booking for next year (#44727)

---
 passerelle/contrib/toulouse_axel/models.py |  6 +--
 tests/test_toulouse_axel.py                | 44 ++++++++++++++++++++++
 2 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/passerelle/contrib/toulouse_axel/models.py b/passerelle/contrib/toulouse_axel/models.py
index 69f5f5ca..6cdff956 100644
--- a/passerelle/contrib/toulouse_axel/models.py
+++ b/passerelle/contrib/toulouse_axel/models.py
@@ -1275,10 +1275,10 @@ class ToulouseAxel(BaseResource):
 
         # check dates
         today = datetime.date.today()
-        reference_year = utils.get_reference_year_from_date(today)
         start_date_min = today + datetime.timedelta(days=8)
-        end_date_max = datetime.date(reference_year+1, 7, 31)
         start_date = datetime.datetime.strptime(post_data['booking_start_date'], utils.json_date_format).date()
+        reference_year = utils.get_reference_year_from_date(start_date)
+        end_date_max = datetime.date(reference_year + 1, 7, 31)
         end_date = datetime.datetime.strptime(post_data['booking_end_date'], utils.json_date_format).date()
         if start_date > end_date:
             raise APIError('booking_start_date should be before booking_end_date', err_code='bad-request', http_status=400)
@@ -1288,7 +1288,7 @@ class ToulouseAxel(BaseResource):
             raise APIError('booking_end_date max value: %s' % end_date_max, err_code='bad-request', http_status=400)
 
         # get known activities for this child, to have the ids
-        child_activities_info = self.get_child_activities(link.dui, reference_year, post_data['child_id'])
+        child_activities_info = self.get_child_activities(dui=link.dui, reference_year=reference_year, child_id=post_data['child_id'])
         child_known_activities_by_type = {a['TYPEACTIVITE']: a for a in child_activities_info.get('ACTIVITE', [])}
 
         # build activity list to post
diff --git a/tests/test_toulouse_axel.py b/tests/test_toulouse_axel.py
index 4167398d..ccaf2616 100644
--- a/tests/test_toulouse_axel.py
+++ b/tests/test_toulouse_axel.py
@@ -3398,6 +3398,13 @@ def test_clae_booking_endpoint_date_error(app, resource, booking_params):
     assert resp.json['err_desc'] == "booking_end_date max value: 2020-07-31"
     assert resp.json['err'] == 'bad-request'
 
+    booking_params['booking_start_date'] = '2021-01-01'
+    booking_params['booking_end_date'] = '2021-12-31'
+    with freezegun.freeze_time('2019-09-01'):
+        resp = app.post_json('/toulouse-axel/test/clae_booking?NameID=yyy', params=booking_params, status=400)
+    assert resp.json['err_desc'] == "booking_end_date max value: 2021-07-31"
+    assert resp.json['err'] == 'bad-request'
+
 
 def test_clae_booking_endpoint_no_result(app, resource, booking_params):
     resp = app.post_json('/toulouse-axel/test/clae_booking?NameID=yyy', params=booking_params)
@@ -3602,6 +3609,43 @@ def test_clae_booking_regime(app, resource, booking_params, child_activities_dat
         assert payload['ENFANT'][0]['REGIME'] == 'SV'
 
 
+@freezegun.freeze_time('2019-09-01')
+def test_clae_booking_endpoint_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]
+    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'
+    booking_params = {
+        'booking_start_date': '2020-09-01',
+        'booking_end_date': '2020-09-04',
+        'booking_list_MAT': [],
+        'booking_list_MIDI': None,
+        'booking_list_SOIR': None,
+        'booking_list_GARD': None,
+        'child_id': '3535',
+        'regime': 'AV',
+    }
+    with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities') as mock_activities:
+        mock_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?NameID=yyy', params=booking_params)
+    assert mock_activities.call_args_list == [
+        mock.call(child_id='3535', dui='XXX', reference_year=2020),
+    ]
+    payload = operation.call_args_list[0][0][1]['PORTAIL']['DUI']
+    assert len(payload['ENFANT']) == 1
+    assert payload['ENFANT'][0]['IDPERSONNE'] == '3535'
+    assert len(payload['ENFANT'][0]['ACTIVITE']) == 1
+    activity = payload['ENFANT'][0]['ACTIVITE'][0]
+    assert len(activity['PERIODE']) == 1
+    assert activity['PERIODE'][0]['DATEDEBUT'] == '2020-09-01'
+    assert activity['PERIODE'][0]['DATEDFIN'] == '2020-09-04'
+
+
 @freezegun.freeze_time('2019-09-01')
 def test_clae_booking_annual_endpoint_axel_error(app, resource, annual_booking_params):
     Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
-- 
2.20.1

