From a65fb2fd4f8b5c5211fbaa8e4351b9abf592529c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 9 Apr 2020 11:48:39 +0200
Subject: [PATCH] toulouse_axel: possible days and prefill for a period
 (#41453)

---
 passerelle/contrib/toulouse_axel/models.py |  99 ++++++++---
 tests/test_toulouse_axel.py                | 190 +++++++++++++++++----
 2 files changed, 227 insertions(+), 62 deletions(-)

diff --git a/passerelle/contrib/toulouse_axel/models.py b/passerelle/contrib/toulouse_axel/models.py
index 6b5b23e6..e41ca984 100644
--- a/passerelle/contrib/toulouse_axel/models.py
+++ b/passerelle/contrib/toulouse_axel/models.py
@@ -26,7 +26,8 @@ from collections import namedtuple
 from django.core.cache import cache
 from django.db import models
 from django.http import HttpResponse
-from django.utils.dates import WEEKDAYS
+from django.utils import dateformat
+from django.utils.dates import WEEKDAYS_REV
 from django.utils.encoding import force_text
 from django.utils.translation import ugettext_lazy as _
 
@@ -55,6 +56,8 @@ PAYMENT_SCHEMA = {
     'required': ['transaction_date', 'transaction_id']
 }
 
+WEEKDAYS = {v: k for k, v in WEEKDAYS_REV.items()}
+
 
 class AxelSchema(JSONSchemaFromXMLSchema):
     type_map = {
@@ -1064,6 +1067,12 @@ class ToulouseAxel(BaseResource):
         return {child_id: bool(child.get('ACTIVITE', [])) for child_id, child in children_activities.items()}
 
     def get_booking_data(self, dui, child_id, booking_date, activity_types=None):
+        cache_key = 'toulouse-axel-%s-possible-days-%s-%s-%s-%s' % (
+            self.pk, dui, child_id, booking_date.isoformat(), '-'.join(activity_types or []))
+        result = cache.get(cache_key)
+        if result is not None:
+            return result
+
         reference_year = utils.get_reference_year_from_date(booking_date)
 
         # first get activities information for the child
@@ -1137,6 +1146,7 @@ class ToulouseAxel(BaseResource):
             activity['annee_reference_label'] = '{}/{}'.format(reference_year, reference_year + 1)
             activity['booking'] = booking_days.get(activity['IDACTIVITE'], {})
 
+        cache.set(cache_key, child_activities, 3600)  # 1 hour
         return child_activities
 
     @endpoint(
@@ -1168,14 +1178,14 @@ class ToulouseAxel(BaseResource):
             'idpersonne': {'description': _('Child ID')},
             'booking_date': {'description': _('Booking date')},
         })
-    def clae_booking_info(self, request, NameID, idpersonne, booking_date, activity_types=None):
+    def clae_booking_info(self, request, NameID, idpersonne, booking_date):
         link = self.get_link(NameID)
         try:
             booking_date = datetime.datetime.strptime(booking_date, utils.json_date_format)
         except ValueError:
             raise APIError('bad date format, should be YYYY-MM-DD', err_code='bad-request', http_status=400)
 
-        booking_data = self.get_booking_data(dui=link.dui, child_id=idpersonne, booking_date=booking_date, activity_types=activity_types)
+        booking_data = self.get_booking_data(dui=link.dui, child_id=idpersonne, booking_date=booking_date)
 
         return {'data': booking_data}
 
@@ -1187,48 +1197,87 @@ class ToulouseAxel(BaseResource):
             'idpersonne': {'description': _('Child ID')},
             'booking_date': {'description': _('Booking date')},
         })
-    def clae_booking_activities_info(self, request, NameID, idpersonne, booking_date, activity_types=None):
-        return {'data': self.clae_booking_info(
-            request, NameID, idpersonne, booking_date, activity_types=activity_types)['data'].get('ACTIVITE', [])}
+    def clae_booking_activities_info(self, request, NameID, idpersonne, booking_date):
+        return {'data': self.clae_booking_info(request, NameID, idpersonne, booking_date)['data'].get('ACTIVITE', [])}
 
     @endpoint(
-        description=_("Get list of possible days for an activity"),
+        description=_("Get possible days to book an activity of a child, for a period"),
         perm='can_access',
         parameters={
             'NameID': {'description': _('Publik ID')},
             'idpersonne': {'description': _('Child ID')},
             'activity_type': {'description': _('Activity type (MAT, MIDI, SOIR, GARD)')},
-            'booking_date': {'description': _('Booking date')},
+            'start_date': {'description': _('Start date of the period')},
+            'end_date': {'description': _('End date of the period')},
         })
-    def clae_booking_activity_possible_days(self, request, NameID, idpersonne, activity_type, booking_date):
+    def clae_booking_activity_possible_days(self, request, NameID, idpersonne, activity_type, start_date, end_date):
+        link = self.get_link(NameID)
+        try:
+            start_date = datetime.datetime.strptime(start_date, utils.json_date_format).date()
+            end_date = datetime.datetime.strptime(end_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)
-        activities = self.clae_booking_activities_info(request, NameID, idpersonne, booking_date, activity_types=[activity_type])['data']
-        if not activities:
-            return {'data': []}
-        activity = activities[0]
+
+        today = datetime.date.today()
+        # be sure that start_date is after today + 8 days
+        start_date = max(start_date, today + datetime.timedelta(days=8))
+        # but if start_date is a saturday or a sunday, jump to the next monday
+        if start_date.weekday() > 4:
+            start_date = start_date + datetime.timedelta(days=7 - start_date.weekday())
+
+        # if end_date is not a friday: jump to next or previous friday
+        if end_date.weekday() < 4:
+            # next friday
+            end_date = end_date + datetime.timedelta(4 - end_date.weekday())
+        elif end_date.weekday() > 4:
+            # previous friday
+            end_date = end_date - datetime.timedelta(end_date.weekday() - 4)
+
+        def get_activity_days_for_week(week_start_date, week_end_date):
+            # ask Axel for the booking of a week (starts may be a monday, ends a friday)
+            activities = self.get_booking_data(
+                dui=link.dui, child_id=idpersonne, booking_date=week_start_date, activity_types=[activity_type]).get('ACTIVITE', [])
+            if not activities:
+                return
+            activity = activities[0]
+            day_date = week_start_date
+            while day_date <= week_end_date:
+                day = WEEKDAYS[day_date.weekday()]
+                activity_day = {
+                    'id': '{}-{}-{}-{}'.format(idpersonne, activity_type, activity['id'], day_date.strftime(utils.json_date_format)),
+                    'text': dateformat.format(day_date, 'l j F Y'),
+                    'disabled': activity['booking']['days'][day] is None,
+                    'prefill': activity['booking']['days'][day],
+                    'details': activity,
+                }
+                day_date = day_date + datetime.timedelta(days=1)
+                yield activity_day
+
+        # find the first week from start_date
+        week_start_date, week_end_date = utils.get_week_dates_from_date(start_date)
         activity_days = []
-        for i, day in enumerate(['monday', 'tuesday', 'wednesday', 'thursday', 'friday']):
-            activity_days.append({
-                'id': '{}-{}-{}-{}'.format(idpersonne, activity_type, activity['id'], day),
-                'text': WEEKDAYS[i],
-                'disabled': activity['booking']['days'][day] is None,
-                'prefill': activity['booking']['days'][day],
-                'details': activity,
-            })
+        # cross all weeks until end date
+        while week_end_date <= end_date:
+            activity_days += [d for d in get_activity_days_for_week(max(start_date, week_start_date), week_end_date)]
+            week_start_date = week_start_date + datetime.timedelta(days=7)
+            week_end_date = week_end_date + datetime.timedelta(days=7)
+
         return {'data': activity_days}
 
     @endpoint(
-        description=_("Get list booked days for an activity"),
+        description=_("Get booked days for an activity of a child, for a period"),
         perm='can_access',
         parameters={
             'NameID': {'description': _('Publik ID')},
             'idpersonne': {'description': _('Child ID')},
             'activity_type': {'description': _('Activity type (MAT, MIDI, SOIR, GARD)')},
-            'booking_date': {'description': _('Booking date')},
+            'start_date': {'description': _('Start date of the period')},
+            'end_date': {'description': _('End date of the period')},
         })
-    def clae_booking_activity_prefill(self, request, NameID, idpersonne, activity_type, booking_date):
-        possible_days = self.clae_booking_activity_possible_days(request, NameID, idpersonne, activity_type, booking_date)
+    def clae_booking_activity_prefill(self, request, NameID, idpersonne, activity_type, start_date, end_date):
+        possible_days = self.clae_booking_activity_possible_days(request, NameID, idpersonne, activity_type, start_date, end_date)
         return {'data': [d['id'] for d in possible_days['data'] if d['prefill'] is True]}
 
     @endpoint(
diff --git a/tests/test_toulouse_axel.py b/tests/test_toulouse_axel.py
index fd421243..6577ffe4 100644
--- a/tests/test_toulouse_axel.py
+++ b/tests/test_toulouse_axel.py
@@ -26,6 +26,8 @@ import mock
 import os
 import xml.etree.ElementTree as ET
 
+from django.core.cache import cache
+
 import freezegun
 import pytest
 import xmlschema
@@ -2326,6 +2328,7 @@ def test_clae_booking_info_endpoint(app, resource, child_activities_data):
     }
 
     # test wrong and missing IDACTIVITE
+    cache.clear()
     content = """<PORTAIL>
   <DUI>
     <IDDUI>XXX</IDDUI>
@@ -2540,11 +2543,14 @@ def test_clae_booking_activities_info_endpoint(app, resource, child_activities_d
     ]
 
 
+@freezegun.freeze_time('2019-09-01')
 def test_clae_booking_activity_possible_days_endpoint_axel_error(app, resource):
     Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
     with mock.patch('passerelle.contrib.toulouse_axel.models.enfants_activites') as operation:
         operation.side_effect = AxelError('FooBar')
-        resp = app.get('/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=MAT')
+        resp = app.get(
+            '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535'
+            '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
     assert resp.json['err_desc'] == "Axel error: FooBar"
     assert resp.json['err'] == 'error'
 
@@ -2554,28 +2560,44 @@ def test_clae_booking_activity_possible_days_endpoint_axel_error(app, resource):
     with mock_getdata(content, 'EnfantsActivites'):
         with mock.patch('passerelle.contrib.toulouse_axel.models.reservation_periode') as operation:
             operation.side_effect = AxelError('FooBar')
-            resp = app.get('/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=MAT')
+            resp = app.get(
+                '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535'
+                '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
     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_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_possible_days?NameID=yyy&idpersonne=3535&booking_date=%s&activity_type=MAT' % value, status=400)
+    resp = app.get(
+        '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535'
+        '&start_date=%s&end_date=2020-01-24&activity_type=MAT' % value, status=400)
+    assert resp.json['err_desc'] == "bad date format, should be YYYY-MM-DD"
+    assert resp.json['err'] == 'bad-request'
+    resp = app.get(
+        '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535'
+        '&start_date=2020-02-20&end_date=%s&activity_type=MAT' % 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_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_possible_days?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=FOO', status=400)
+    resp = app.get(
+        '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535'
+        '&start_date=2020-01-20&end_date=2020-01-24&activity_type=FOO', 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_possible_days_endpoint_no_result(app, resource, child_activities_data):
-    resp = app.get('/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=MAT')
+    resp = app.get(
+        '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535'
+        '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
     assert resp.json['err_desc'] == "Person not found"
     assert resp.json['err'] == 'not-found'
 
@@ -2586,7 +2608,9 @@ def test_clae_booking_activity_possible_days_endpoint_no_result(app, resource, c
     with mock_getdata(content, 'EnfantsActivites'):
         with mock.patch('passerelle.contrib.toulouse_axel.models.reservation_periode') as operation:
             operation.side_effect = AxelError('FooBar')
-            resp = app.get('/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=4242&booking_date=2020-01-20&activity_type=MAT')
+            resp = app.get(
+                '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=4242'
+                '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
     assert resp.json['err_desc'] == "Child not found"
     assert resp.json['err'] == 'not-found'
 
@@ -2606,17 +2630,22 @@ def test_clae_booking_activity_possible_days_endpoint_no_result(app, resource, c
     activities = child_activities_data['ENFANT'][0]
     with mock_getdata(content, 'ReservationPeriode'):
         with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
-            resp = app.get('/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=MAT')
+            resp = app.get(
+                '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535'
+                '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
     assert resp.json['err_desc'] == "Child not found"
     assert resp.json['err'] == 'not-found'
 
-    with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.clae_booking_activities_info') as activities_info:
-        activities_info.return_value = {'data': []}
-        resp = app.get('/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=MAT')
+    with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_booking_data') as activities_info:
+        activities_info.return_value = {'ACTIVITE': []}
+        resp = app.get(
+            '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535'
+            '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
     assert resp.json['err'] == 0
     assert resp.json['data'] == []
 
 
+@freezegun.freeze_time('2019-09-01')
 def test_clae_booking_activity_possible_days_endpoint(app, resource, child_activities_data):
     Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
     content = """<PORTAIL>
@@ -2635,20 +2664,22 @@ def test_clae_booking_activity_possible_days_endpoint(app, resource, child_activ
     activities = child_activities_data['ENFANT'][0]
     with mock_getdata(content, 'ReservationPeriode'):
         with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
-            resp = app.get('/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=MAT')
+            resp = app.get(
+                '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535'
+                '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
     assert resp.json['err'] == 0
     assert len(resp.json['data']) == 5
     assert set(resp.json['data'][0].keys()) == set(['id', 'text', 'disabled', 'prefill', 'details'])
-    assert resp.json['data'][0]['id'] == '3535-MAT-A19P1M1-monday'
-    assert resp.json['data'][1]['id'] == '3535-MAT-A19P1M1-tuesday'
-    assert resp.json['data'][2]['id'] == '3535-MAT-A19P1M1-wednesday'
-    assert resp.json['data'][3]['id'] == '3535-MAT-A19P1M1-thursday'
-    assert resp.json['data'][4]['id'] == '3535-MAT-A19P1M1-friday'
-    assert resp.json['data'][0]['text'] == 'Monday'
-    assert resp.json['data'][1]['text'] == 'Tuesday'
-    assert resp.json['data'][2]['text'] == 'Wednesday'
-    assert resp.json['data'][3]['text'] == 'Thursday'
-    assert resp.json['data'][4]['text'] == 'Friday'
+    assert resp.json['data'][0]['id'] == '3535-MAT-A19P1M1-2020-01-20'
+    assert resp.json['data'][1]['id'] == '3535-MAT-A19P1M1-2020-01-21'
+    assert resp.json['data'][2]['id'] == '3535-MAT-A19P1M1-2020-01-22'
+    assert resp.json['data'][3]['id'] == '3535-MAT-A19P1M1-2020-01-23'
+    assert resp.json['data'][4]['id'] == '3535-MAT-A19P1M1-2020-01-24'
+    assert resp.json['data'][0]['text'] == 'Monday 20 January 2020'
+    assert resp.json['data'][1]['text'] == 'Tuesday 21 January 2020'
+    assert resp.json['data'][2]['text'] == 'Wednesday 22 January 2020'
+    assert resp.json['data'][3]['text'] == 'Thursday 23 January 2020'
+    assert resp.json['data'][4]['text'] == 'Friday 24 January 2020'
     assert resp.json['data'][0]['disabled'] is False
     assert resp.json['data'][1]['disabled'] is False
     assert resp.json['data'][2]['disabled'] is True
@@ -2660,12 +2691,73 @@ def test_clae_booking_activity_possible_days_endpoint(app, resource, child_activ
     assert resp.json['data'][3]['prefill'] is False
     assert resp.json['data'][4]['prefill'] is True
 
+    with mock_getdata(content, 'ReservationPeriode'):
+        with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
+            resp = app.get(
+                '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535'
+                '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
 
+    # again - data are in cache
+    resp = app.get(
+        '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535'
+        '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
+    assert resp.json['err'] == 0
+    assert len(resp.json['data']) == 5
+
+
+@pytest.mark.parametrize('today, start_date, first_date, end_date, last_date', [
+    # today is start date -> + 8 days
+    ('2020-03-26', '2020-03-26', '2020-04-03', '2020-04-17', '2020-04-17'),
+    # start date is a friday => starts friday
+    ('2020-03-26', '2020-04-03', '2020-04-03', '2020-04-17', '2020-04-17'),
+    # start date is a saturday => starts on next monday
+    ('2020-03-26', '2020-04-04', '2020-04-06', '2020-04-17', '2020-04-17'),
+    # start date is a sunday => starts on next monday
+    ('2020-03-26', '2020-04-05', '2020-04-06', '2020-04-17', '2020-04-17'),
+    # start date is a monday => starts on monday
+    ('2020-03-26', '2020-04-06', '2020-04-06', '2020-04-17', '2020-04-17'),
+    # start date is a tuesday => starts on tuesday
+    ('2020-03-26', '2020-04-07', '2020-04-07', '2020-04-17', '2020-04-17'),
+    # end date is a not a friday => ends on friday
+    ('2020-03-26', '2020-04-07', '2020-04-07', '2020-04-13', '2020-04-17'),
+    ('2020-03-26', '2020-04-07', '2020-04-07', '2020-04-14', '2020-04-17'),
+    ('2020-03-26', '2020-04-07', '2020-04-07', '2020-04-15', '2020-04-17'),
+    ('2020-03-26', '2020-04-07', '2020-04-07', '2020-04-16', '2020-04-17'),
+    # except if end date is saturday or sunday => ends on previous friday
+    ('2020-03-26', '2020-04-07', '2020-04-07', '2020-04-18', '2020-04-17'),
+    ('2020-03-26', '2020-04-07', '2020-04-07', '2020-04-19', '2020-04-17'),
+    # start date is after end date, same week
+    ('2020-03-26', '2020-04-16', '2020-04-16', '2020-04-15', '2020-04-17'),
+    # start date is after end date, not the same week => result is empty
+    ('2020-03-26', '2020-04-16', None, '2020-04-12', None),
+])
+def test_clae_booking_activity_possible_days_period(app, resource, child_activities_data, today, start_date, first_date, end_date, last_date):
+    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
+
+    with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_booking_data') as activities_info:
+        activities_info.return_value = {'ACTIVITE': [{
+            'id': 'TOTO',
+            'booking': {'days': {'monday': True, 'tuesday': True, 'wednesday': True, 'thursday': True, 'friday': True}}
+        }]}
+        with freezegun.freeze_time(today):
+            resp = app.get(
+                '/toulouse-axel/test/clae_booking_activity_possible_days?NameID=yyy&idpersonne=3535'
+                '&start_date=%s&end_date=%s&activity_type=MAT' % (start_date, end_date))
+    if first_date is None:
+        assert resp.json['data'] == []
+    else:
+        assert resp.json['data'][0]['id'] == '3535-MAT-TOTO-%s' % first_date
+        assert resp.json['data'][-1]['id'] == '3535-MAT-TOTO-%s' % last_date
+
+
+@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')
     with mock.patch('passerelle.contrib.toulouse_axel.models.enfants_activites') as operation:
         operation.side_effect = AxelError('FooBar')
-        resp = app.get('/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=MAT')
+        resp = app.get(
+            '/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535'
+            '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
     assert resp.json['err_desc'] == "Axel error: FooBar"
     assert resp.json['err'] == 'error'
 
@@ -2675,28 +2767,44 @@ def test_clae_booking_activity_prefill_endpoint_axel_error(app, resource):
     with mock_getdata(content, 'EnfantsActivites'):
         with mock.patch('passerelle.contrib.toulouse_axel.models.reservation_periode') as operation:
             operation.side_effect = AxelError('FooBar')
-            resp = app.get('/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=MAT')
+            resp = app.get(
+                '/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535'
+                '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
     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_prefill_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_prefill?NameID=yyy&idpersonne=3535&booking_date=%s&activity_type=MAT' % value, status=400)
+    resp = app.get(
+        '/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535'
+        '&start_date=%s&end_date=2020-01-24&activity_type=MAT' % value, status=400)
+    assert resp.json['err_desc'] == "bad date format, should be YYYY-MM-DD"
+    assert resp.json['err'] == 'bad-request'
+    resp = app.get(
+        '/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535'
+        '&start_date=2020-01-20&end_date=%s&activity_type=MAT' % 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_prefill_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_prefill?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=FOO', status=400)
+    resp = app.get(
+        '/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535'
+        '&start_date=2020-01-20&end_date=2020-01-24&activity_type=FOO', 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_prefill_endpoint_no_result(app, resource, child_activities_data):
-    resp = app.get('/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=MAT')
+    resp = app.get(
+        '/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535'
+        '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
     assert resp.json['err_desc'] == "Person not found"
     assert resp.json['err'] == 'not-found'
 
@@ -2707,7 +2815,9 @@ def test_clae_booking_activity_prefill_endpoint_no_result(app, resource, child_a
     with mock_getdata(content, 'EnfantsActivites'):
         with mock.patch('passerelle.contrib.toulouse_axel.models.reservation_periode') as operation:
             operation.side_effect = AxelError('FooBar')
-            resp = app.get('/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=4242&booking_date=2020-01-20&activity_type=MAT')
+            resp = app.get(
+                '/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=4242'
+                '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
     assert resp.json['err_desc'] == "Child not found"
     assert resp.json['err'] == 'not-found'
 
@@ -2727,47 +2837,53 @@ def test_clae_booking_activity_prefill_endpoint_no_result(app, resource, child_a
     activities = child_activities_data['ENFANT'][0]
     with mock_getdata(content, 'ReservationPeriode'):
         with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.get_child_activities', return_value=activities):
-            resp = app.get('/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=MAT')
+            resp = app.get(
+                '/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535'
+                '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
     assert resp.json['err_desc'] == "Child not found"
     assert resp.json['err'] == 'not-found'
 
     with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.clae_booking_activity_possible_days') as possible_days:
         possible_days.return_value = {'data': []}
-        resp = app.get('/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=MAT')
+        resp = app.get(
+            '/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535'
+            '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
     assert resp.json['err'] == 0
     assert resp.json['data'] == []
 
 
-def test_clae_booking_activity_prefill_endpoint(app, resource, child_activities_data):
+def test_clae_booking_activity_prefill_endpoint(app, resource):
     Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
     with mock.patch('passerelle.contrib.toulouse_axel.models.ToulouseAxel.clae_booking_activity_possible_days') as possible_days:
         possible_days.return_value = {
             'data': [
                 {
-                    'id': '3535-MAT-A19P1M1-monday',
+                    'id': '3535-MAT-A19P1M1-2020-01-20',
                     'prefill': True,
                 },
                 {
-                    'id': '3535-MAT-A19P1M1-tuesday',
+                    'id': '3535-MAT-A19P1M1-2020-01-21',
                     'prefill': False,
                 },
                 {
-                    'id': '3535-MAT-A19P1M1-wednesday',
+                    'id': '3535-MAT-A19P1M1-2020-01-22',
                     'prefill': None,
                 },
                 {
-                    'id': '3535-MAT-A19P1M1-thursday',
+                    'id': '3535-MAT-A19P1M1-2020-01-23',
                     'prefill': False,
                 },
                 {
-                    'id': '3535-MAT-A19P1M1-friday',
+                    'id': '3535-MAT-A19P1M1-2020-01-24',
                     'prefill': True,
                 },
             ]
         }
-        resp = app.get('/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=MAT')
+        resp = app.get(
+            '/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535'
+            '&start_date=2020-01-20&end_date=2020-01-24&activity_type=MAT')
     assert resp.json['err'] == 0
-    assert resp.json['data'] == ['3535-MAT-A19P1M1-monday', '3535-MAT-A19P1M1-friday']
+    assert resp.json['data'] == ['3535-MAT-A19P1M1-2020-01-20', '3535-MAT-A19P1M1-2020-01-24']
 
 
 def test_are_children_registered_axel_error(resource):
-- 
2.20.1

