From fa327ef177db7d26ccc2ee449c2f9094884b6d8a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 31 Mar 2020 14:27:20 +0200
Subject: [PATCH] toulouse_axel: prefill endpoint (#41170)

---
 passerelle/contrib/toulouse_axel/models.py |  13 +++
 tests/test_toulouse_axel.py                | 109 +++++++++++++++++++++
 2 files changed, 122 insertions(+)

diff --git a/passerelle/contrib/toulouse_axel/models.py b/passerelle/contrib/toulouse_axel/models.py
index a2f1ef34..7fcfe790 100644
--- a/passerelle/contrib/toulouse_axel/models.py
+++ b/passerelle/contrib/toulouse_axel/models.py
@@ -1215,6 +1215,19 @@ class ToulouseAxel(BaseResource):
             })
         return {'data': activity_days}
 
+    @endpoint(
+        description=_("Get list booked days for an activity"),
+        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')},
+        })
+    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)
+        return {'data': [d['id'] for d in possible_days['data'] if d['prefill'] is True]}
+
     @endpoint(
         description=_("CLAE/Cantine booking"),
         perm='can_access',
diff --git a/tests/test_toulouse_axel.py b/tests/test_toulouse_axel.py
index 7ef8a1ca..472eed3c 100644
--- a/tests/test_toulouse_axel.py
+++ b/tests/test_toulouse_axel.py
@@ -2647,6 +2647,115 @@ def test_clae_booking_activity_possible_days_endpoint(app, resource, child_activ
     assert resp.json['data'][4]['prefill'] is True
 
 
+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')
+    assert resp.json['err_desc'] == "Axel error: FooBar"
+    assert resp.json['err'] == 'error'
+
+    filepath = os.path.join(os.path.dirname(__file__), 'data/toulouse_axel/child_activities.xml')
+    with open(filepath) as xml:
+        content = xml.read()
+    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')
+    assert resp.json['err_desc'] == "Axel error: FooBar"
+    assert resp.json['err'] == 'error'
+
+
+@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)
+    assert resp.json['err_desc'] == "bad date format, should be YYYY-MM-DD"
+    assert resp.json['err'] == 'bad-request'
+
+
+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)
+    assert resp.json['err_desc'] == "bad activity_type, should be MAT, MIDI, SOIR or GARD"
+    assert resp.json['err'] == 'bad-request'
+
+
+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')
+    assert resp.json['err_desc'] == "Person not found"
+    assert resp.json['err'] == 'not-found'
+
+    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
+    filepath = os.path.join(os.path.dirname(__file__), 'data/toulouse_axel/child_activities.xml')
+    with open(filepath) as xml:
+        content = xml.read()
+    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')
+    assert resp.json['err_desc'] == "Child not found"
+    assert resp.json['err'] == 'not-found'
+
+    content = """<PORTAIL>
+  <DUI>
+    <IDDUI>XXX</IDDUI>
+    <ENFANT>
+      <IDPERSONNE>4242</IDPERSONNE>
+      <ACTIVITE>
+        <ANNEEREFERENCE>2019</ANNEEREFERENCE>
+        <IDACTIVITE>A19P1M1</IDACTIVITE>
+        <JOUR>00000</JOUR>
+      </ACTIVITE>
+    </ENFANT>
+  </DUI>
+</PORTAIL>"""
+    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')
+    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')
+    assert resp.json['err'] == 0
+    assert resp.json['data'] == []
+
+
+def test_clae_booking_activity_prefill_endpoint(app, resource, child_activities_data):
+    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',
+                    'prefill': True,
+                },
+                {
+                    'id': '3535-MAT-A19P1M1-tuesday',
+                    'prefill': False,
+                },
+                {
+                    'id': '3535-MAT-A19P1M1-wednesday',
+                    'prefill': None,
+                },
+                {
+                    'id': '3535-MAT-A19P1M1-thursday',
+                    'prefill': False,
+                },
+                {
+                    'id': '3535-MAT-A19P1M1-friday',
+                    'prefill': True,
+                },
+            ]
+        }
+        resp = app.get('/toulouse-axel/test/clae_booking_activity_prefill?NameID=yyy&idpersonne=3535&booking_date=2020-01-20&activity_type=MAT')
+    assert resp.json['err'] == 0
+    assert resp.json['data'] == ['3535-MAT-A19P1M1-monday', '3535-MAT-A19P1M1-friday']
+
+
 def test_are_children_registered_axel_error(resource):
     with mock.patch('passerelle.contrib.toulouse_axel.models.enfants_activites') as operation:
         operation.side_effect = AxelError('FooBar')
-- 
2.20.1

