From 52f32334a7ff05353586852ded338c935072ff74 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Tue, 22 Nov 2022 06:50:57 +0100
Subject: [PATCH 2/4] toulouse-maelis: allow to send an empty dietcode (#71643)

---
 passerelle/contrib/toulouse_maelis/models.py |  4 ++--
 tests/test_toulouse_maelis.py                | 16 +---------------
 2 files changed, 3 insertions(+), 17 deletions(-)

diff --git a/passerelle/contrib/toulouse_maelis/models.py b/passerelle/contrib/toulouse_maelis/models.py
index 23ba3ac2..b7df38d3 100644
--- a/passerelle/contrib/toulouse_maelis/models.py
+++ b/passerelle/contrib/toulouse_maelis/models.py
@@ -284,17 +284,17 @@ class ToulouseMaelis(BaseResource, HTTPResource):
         keys = parent_keys or []
         self.assert_post_data_in_referential('Sex', post_data, keys + ['sexe'])
 
         data = post_data
         for key in keys:
             data = data[key]
 
         if 'dietcode' in data:
-            self.assert_post_data_in_referential('DietCode', post_data, keys + ['dietcode'])
+            self.assert_post_data_in_referential('DietCode', post_data, keys + ['dietcode'], required=False)
         if 'paiInfoBean' in data:
             self.assert_child_pai_payoad_in_referential(post_data, keys + ['paiInfoBean'])
         if 'medicalRecord' in data:
             # dead code as updateFamily seems not to modify medicalRecord
             self.assert_child_medical_record_payload_in_referential(post_data, keys + ['medicalRecord'])
 
     def assert_person_payload_in_referential(self, post_data, parent_keys=None):
         keys = parent_keys or []
@@ -1048,17 +1048,17 @@ class ToulouseMaelis(BaseResource, HTTPResource):
             'NameID': {'description': 'Publik NameID'},
             'child_id': {'description': "Numéro de l'enfant"},
             'dietcode': {'description': 'code du régime alimentaire'},
         },
         methods=['post'],
     )
     def update_child_dietcode(self, request, NameID, child_id, dietcode):
         self.get_link(NameID)
-        self.assert_key_in_referential('DietCode', dietcode, 'dietcode parameter')
+        self.assert_key_in_referential('DietCode', dietcode, 'dietcode parameter', required=False)
 
         self.call('Family', 'createOrUpdateChildDiet', personNumber=child_id, code=dietcode)
         return {'data': 'ok'}
 
     @endpoint(
         display_category='Famille',
         description="Créer ou mettre à jour les informations relatives au PAI d'un enfant",
         name='update-child-pai',
diff --git a/tests/test_toulouse_maelis.py b/tests/test_toulouse_maelis.py
index dd0c57df..6ce00aaf 100644
--- a/tests/test_toulouse_maelis.py
+++ b/tests/test_toulouse_maelis.py
@@ -2611,42 +2611,28 @@ def test_update_child_dietcode(mocked_post, mocked_get, con, app):
 def test_update_child_dietcode_not_linked_error(con, app):
     url = get_endpoint('update-child-dietcode')
 
     resp = app.post_json(url + '?NameID=local&child_id=613878&dietcode=RVS')
     assert resp.json['err'] == 'not-linked'
     assert resp.json['err_desc'] == 'User not linked to family'
 
 
-@mock.patch('passerelle.utils.Request.get')
-@mock.patch('passerelle.utils.Request.post')
-def test_update_child_dietcode_empty_referential_key_error(mocked_post, mocked_get, con, app):
-    mocked_get.return_value = FAMILY_SERVICE_WSDL
-    mocked_post.side_effect = [READ_DIETCODE, UPDATE_DIETCODE]
-    url = get_endpoint('update-child-dietcode')
-
-    Link.objects.create(resource=con, family_id='1312', name_id='local')
-    resp = app.post_json(url + '?NameID=local&child_id=613878&dietcode=')
-    assert resp.json['err'] == 'field-required'
-    assert resp.json['err_desc'] == "dietcode parameter is required and could not be None"
-
-
 @mock.patch('passerelle.utils.Request.get')
 @mock.patch('passerelle.utils.Request.post')
 def test_update_child_dietcode_wrong_referential_key_error(mocked_post, mocked_get, con, app):
     mocked_get.return_value = FAMILY_SERVICE_WSDL
     mocked_post.side_effect = [READ_DIETCODE, UPDATE_DIETCODE]
     url = get_endpoint('update-child-dietcode')
 
     Link.objects.create(resource=con, family_id='1312', name_id='local')
     resp = app.post_json(url + '?NameID=local&child_id=613878&dietcode=plop')
     assert resp.json['err'] == 'wrong-key'
     assert (
-        resp.json['err_desc']
-        == "dietcode parameter key value 'plop' do not belong to 'DietCode' required referential"
+        resp.json['err_desc'] == "dietcode parameter key value 'plop' do not belong to 'DietCode' referential"
     )
 
 
 @mock.patch('passerelle.utils.Request.get')
 @mock.patch('passerelle.utils.Request.post')
 def test_update_child_pai(mocked_post, mocked_get, con, app):
     mocked_get.return_value = FAMILY_SERVICE_WSDL
     mocked_post.side_effect = [READ_PAI, UPDATE_PAI]
-- 
2.30.2

