From 6a1d112fce0de582a85e5c5819279604efe62318 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Wed, 21 Jul 2021 17:29:28 +0200
Subject: [PATCH] caluire-axel: hide closed activities on get_agenda (#55743)

---
 passerelle/contrib/caluire_axel/models.py |  2 +
 tests/test_caluire_axel.py                | 71 +++++++++++++++++++++++
 2 files changed, 73 insertions(+)

diff --git a/passerelle/contrib/caluire_axel/models.py b/passerelle/contrib/caluire_axel/models.py
index 97ef331f..8dd8191b 100644
--- a/passerelle/contrib/caluire_axel/models.py
+++ b/passerelle/contrib/caluire_axel/models.py
@@ -612,16 +612,18 @@ class CaluireAxel(BaseResource):
             elif pivot_date.weekday() == 2:
                 # wednesday, jump to next thursday
                 pivot_date += datetime.timedelta(days=1)
 
         bookings = []
         activity_type = self.get_activity_type(activity_id)
         days = result.json_response['DATA']['PORTAIL']['GETAGENDA'].get('JOUR', [])
         for day in days:
+            if day['FERME']:
+                continue
             day_date = datetime.datetime.strptime(day['JOURDATE'], axel.json_date_format).date()
             if day_date.weekday() == 2 and ignore_wednesday:
                 continue
             if day_date.weekday() >= 5 and ignore_weekend:
                 continue
             booking = {
                 'id': '%s:%s:%s' % (child_id, activity_id, day['JOURDATE']),
                 'text': dateformat.format(day_date, 'l j F Y'),
diff --git a/tests/test_caluire_axel.py b/tests/test_caluire_axel.py
index c524d08a..5d371479 100644
--- a/tests/test_caluire_axel.py
+++ b/tests/test_caluire_axel.py
@@ -1563,16 +1563,87 @@ def test_get_agenda_periscolaire_endpoint(app, resource, family_data, activities
     assert resp.json['data'][10]['id'] == '50632:ETUDES:2020-10-29'
     assert resp.json['data'][11]['id'] == '50632:GARDERIES:2020-10-29'
     assert resp.json['data'][12]['id'] == '50632:ACCMAT:2020-10-30'
     assert resp.json['data'][13]['id'] == '50632:ECOLELEM:2020-10-30'
     assert resp.json['data'][14]['id'] == '50632:ETUDES:2020-10-30'
     assert resp.json['data'][15]['id'] == '50632:GARDERIES:2020-10-30'
 
 
+def test_get_agenda_periscolaire_endpoint_closed_activity(app, resource, family_data, activities_full):
+    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
+    content = '''<PORTAIL>
+    <GETAGENDA>
+        <CODE>7</CODE>
+        <JOUR>
+          <JOURDATE>26/10/2020</JOURDATE>
+          <MATIN>.</MATIN>
+          <MIDI></MIDI>
+          <APRESMIDI></APRESMIDI>
+          <FERME>O</FERME>
+        </JOUR>
+        <JOUR>
+          <JOURDATE>27/10/2020</JOURDATE>
+          <MATIN>.</MATIN>
+          <MIDI></MIDI>
+          <APRESMIDI></APRESMIDI>
+          <FERME>O</FERME>
+        </JOUR>
+        <JOUR>
+          <JOURDATE>28/10/2020</JOURDATE>
+          <MATIN>.</MATIN>
+          <MIDI></MIDI>
+          <APRESMIDI></APRESMIDI>
+          <FERME>O</FERME>
+        </JOUR>
+        <JOUR>
+          <JOURDATE>29/10/2020</JOURDATE>
+          <MATIN>.</MATIN>
+          <MIDI></MIDI>
+          <APRESMIDI></APRESMIDI>
+          <FERME>O</FERME>
+        </JOUR>
+        <JOUR>
+          <JOURDATE>30/10/2020</JOURDATE>
+          <MATIN>.</MATIN>
+          <MIDI></MIDI>
+          <APRESMIDI></APRESMIDI>
+          <FERME>O</FERME>
+        </JOUR>
+        <JOUR>
+          <JOURDATE>31/10/2020</JOURDATE>
+          <MATIN>.</MATIN>
+          <MIDI></MIDI>
+          <APRESMIDI></APRESMIDI>
+          <FERME>O</FERME>
+        </JOUR>
+        <JOUR>
+          <JOURDATE>01/11/2020</JOURDATE>
+          <MATIN>.</MATIN>
+          <MIDI></MIDI>
+          <APRESMIDI></APRESMIDI>
+          <FERME>O</FERME>
+        </JOUR>
+    </GETAGENDA>
+</PORTAIL>'''
+    with mock_data(content, 'GetAgenda'):
+        with mock.patch(
+            'passerelle.contrib.caluire_axel.models.CaluireAxel.get_family_data',
+            return_value=family_data,
+        ):
+            with mock.patch(
+                'passerelle.contrib.caluire_axel.models.CaluireAxel.get_child_activities',
+                return_value=activities_full,
+            ):
+                resp = app.get(
+                    '/caluire-axel/test/get_agenda_periscolaire?NameID=yyy&idpersonne=50632&start_date=2020-09-01&end_date=2021-08-31'
+                )
+    assert len(resp.json['data']) == 0
+
+
 @pytest.mark.parametrize('code', [0, -1, -2, -3, -4])
 def test_get_agenda_periscolaire_endpoint_wrong_code(app, resource, family_data, activities, code):
     Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
     content = (
         '''<PORTAIL>
     <GETAGENDA>
         <CODE>%s</CODE>
     </GETAGENDA>
-- 
2.30.2

