From 4e4e3cb6c25f17847c3b83cfc7e19db38949031f 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 11:17:06 +0200
Subject: [PATCH 2/2] toulouse-axel: fix annual possible days & reference year
 (#44725)

---
 passerelle/contrib/toulouse_axel/models.py | 10 ++--
 tests/test_toulouse_axel.py                | 60 ++++++++++++++++++----
 2 files changed, 58 insertions(+), 12 deletions(-)

diff --git a/passerelle/contrib/toulouse_axel/models.py b/passerelle/contrib/toulouse_axel/models.py
index 63599869..a4b8dcf0 100644
--- a/passerelle/contrib/toulouse_axel/models.py
+++ b/passerelle/contrib/toulouse_axel/models.py
@@ -1204,14 +1204,18 @@ class ToulouseAxel(BaseResource):
             'NameID': {'description': _('Publik ID')},
             'idpersonne': {'description': _('Child ID')},
             'activity_type': {'description': _('Activity type (MAT, MIDI, SOIR, GARD)')},
+            'booking_date': {'description': _('Booking date (to get reference year)')},
         })
-    def clae_booking_activity_annual_possible_days(self, request, NameID, idpersonne, activity_type):
+    def clae_booking_activity_annual_possible_days(self, request, NameID, idpersonne, activity_type, booking_date):
         link = self.get_link(NameID)
+        try:
+            booking_date = datetime.datetime.strptime(booking_date, utils.json_date_format).date()
+        except ValueError:
+            raise APIError('bad date format, should be YYYY-MM-DD', err_code='bad-request', http_status=400)
         if activity_type not in ['MAT', 'MIDI', 'SOIR', 'GARD']:
             raise APIError('bad activity_type, should be MAT, MIDI, SOIR or GARD', err_code='bad-request', http_status=400)
 
-        today = datetime.date.today()
-        reference_year = utils.get_reference_year_from_date(today)
+        reference_year = utils.get_reference_year_from_date(booking_date)
         activities = self.get_child_activities(
             dui=link.dui,
             reference_year=reference_year,
diff --git a/tests/test_toulouse_axel.py b/tests/test_toulouse_axel.py
index 4e32543d..febb6593 100644
--- a/tests/test_toulouse_axel.py
+++ b/tests/test_toulouse_axel.py
@@ -3031,22 +3031,39 @@ def test_clae_booking_activity_annual_possible_days_endpoint_axel_error(app, res
     Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
     with mock.patch('passerelle.contrib.toulouse_axel.schemas.enfants_activites') as operation:
         operation.side_effect = AxelError('FooBar')
-        resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=MAT')
+        resp = app.get(
+            '/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535'
+            '&activity_type=MAT&booking_date=2019-09-01')
     assert resp.json['err_desc'] == "Axel error: FooBar"
     assert resp.json['err'] == 'error'
 
 
+@freezegun.freeze_time('2019-09-01')
+@pytest.mark.parametrize('value', ['foo', '20/01/2020', '2020'])
+def test_clae_booking_activity_annual_possible_days_endpoint_bad_date_format(app, resource, value):
+    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
+    resp = app.get(
+        '/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535'
+        '&activity_type=MAT&booking_date=%s' % value, status=400)
+    assert resp.json['err_desc'] == "bad date format, should be YYYY-MM-DD"
+    assert resp.json['err'] == 'bad-request'
+
+
 @freezegun.freeze_time('2019-09-01')
 def test_clae_booking_activity_annual_possible_days_endpoint_activity_type(app, resource):
     Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
-    resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=FOO', status=400)
+    resp = app.get(
+        '/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535'
+        '&activity_type=FOO&booking_date=2019-09-01', status=400)
     assert resp.json['err_desc'] == "bad activity_type, should be MAT, MIDI, SOIR or GARD"
     assert resp.json['err'] == 'bad-request'
 
 
 @freezegun.freeze_time('2019-09-01')
 def test_clae_booking_activity_annual_possible_days_endpoint_no_result(app, resource):
-    resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=MAT')
+    resp = app.get(
+        '/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535'
+        '&activity_type=MAT&booking_date=2019-09-01')
     assert resp.json['err_desc'] == "Person not found"
     assert resp.json['err'] == 'not-found'
 
@@ -3055,12 +3072,16 @@ def test_clae_booking_activity_annual_possible_days_endpoint_no_result(app, reso
     with open(filepath) as xml:
         content = xml.read()
     with mock_getdata(content, 'EnfantsActivites'):
-        resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=4242&activity_type=MAT')
+        resp = app.get(
+            '/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=4242'
+            '&activity_type=MAT&booking_date=2019-09-01')
     assert resp.json['err_desc'] == "Child not found"
     assert resp.json['err'] == 'not-found'
 
     with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value={'ACTIVITE': []}):
-        resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=MAT')
+        resp = app.get(
+            '/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535'
+            '&activity_type=MAT&booking_date=2019-09-01')
     assert resp.json['err'] == 0
     assert resp.json['data'] == []
 
@@ -3070,7 +3091,9 @@ def test_clae_booking_activity_annual_possible_days_endpoint(app, resource, chil
     Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
     activities = child_activities_data['ENFANT'][0]
     with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
-        resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=MAT')
+        resp = app.get(
+            '/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535'
+            '&activity_type=MAT&booking_date=2019-09-01')
     assert resp.json['err'] == 0
     assert resp.json['data'] == [
         {
@@ -3100,7 +3123,9 @@ def test_clae_booking_activity_annual_possible_days_endpoint(app, resource, chil
         },
     ]
     with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
-        resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=MIDI')
+        resp = app.get(
+            '/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535'
+            '&activity_type=MIDI&booking_date=2019-09-01')
     assert resp.json['err'] == 0
     assert resp.json['data'] == [
         {
@@ -3130,7 +3155,9 @@ def test_clae_booking_activity_annual_possible_days_endpoint(app, resource, chil
         },
     ]
     with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
-        resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=SOIR')
+        resp = app.get(
+            '/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535'
+            '&activity_type=SOIR&booking_date=2019-09-01')
     assert resp.json['err'] == 0
     assert resp.json['data'] == [
         {
@@ -3160,7 +3187,9 @@ def test_clae_booking_activity_annual_possible_days_endpoint(app, resource, chil
         },
     ]
     with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
-        resp = app.get('/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535&activity_type=GARD')
+        resp = app.get(
+            '/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535'
+            '&activity_type=GARD&booking_date=2019-09-01')
     assert resp.json['err'] == 0
     assert resp.json['data'] == [
         {
@@ -3191,6 +3220,19 @@ def test_clae_booking_activity_annual_possible_days_endpoint(app, resource, chil
     ]
 
 
+def test_clae_booking_activity_annual_possible_days_next_year(app, resource):
+    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
+    with freezegun.freeze_time('2020-07-01'):
+        with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities') as mock_activities:
+            mock_activities.return_value = {}
+            app.get(
+                '/toulouse-axel/test/clae_booking_activity_annual_possible_days?NameID=yyy&idpersonne=3535'
+                '&activity_type=GARD&booking_date=2020-09-01')
+    assert mock_activities.call_args_list == [
+        mock.call(child_id='3535', dui='XXX', reference_year=2020),
+    ]
+
+
 @freezegun.freeze_time('2019-09-01')
 def test_clae_booking_activity_prefill_endpoint_axel_error(app, resource):
     Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
-- 
2.20.1

