From f05c71c83843f744f1812a31202c74675cd54e0e Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Mon, 21 Nov 2022 18:09:01 +0100
Subject: [PATCH 1/4] toulouse-maelis: correct child payload check (#71643)

---
 passerelle/contrib/toulouse_maelis/models.py |  2 +-
 tests/test_toulouse_maelis.py                | 40 ++++++++++++++++++++
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/passerelle/contrib/toulouse_maelis/models.py b/passerelle/contrib/toulouse_maelis/models.py
index 9ce62f14..23ba3ac2 100644
--- a/passerelle/contrib/toulouse_maelis/models.py
+++ b/passerelle/contrib/toulouse_maelis/models.py
@@ -760,17 +760,17 @@ class ToulouseMaelis(BaseResource, HTTPResource):
                 'type': 'bool',
                 'example_value': 'false',
             },
         },
         post={'request_body': {'schema': {'application/json': schemas.CREATE_CHILD_SCHEMA}}},
     )
     def create_child(self, request, NameID, post_data, force=False):
         family_id = self.get_link(NameID).family_id
-        self.assert_update_coordinate_payload_in_referential(post_data)
+        self.assert_child_payload_in_referential(post_data)
 
         payload = {
             'numDossier': family_id,
             'isForceCreateChild': force,
             'child': post_data,
         }
         response = self.call('Family', 'createChild', **payload)
         data = serialize_object(response)
diff --git a/tests/test_toulouse_maelis.py b/tests/test_toulouse_maelis.py
index 54dd13bc..dd0c57df 100644
--- a/tests/test_toulouse_maelis.py
+++ b/tests/test_toulouse_maelis.py
@@ -1886,16 +1886,36 @@ def test_create_child_not_linked_error(con, app):
         'birth/place': 'Port Arthur',
     }
 
     resp = app.post_json(url + '?NameID=local', params=params)
     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_create_child_wrong_referential_key_error(mocked_post, mocked_get, con, app):
+    mocked_get.return_value = FAMILY_SERVICE_WSDL
+    mocked_post.side_effect = []
+    url = get_endpoint('create-child')
+    params = {
+        'lastname': 'DOE',
+        'firstname': 'JANNIS',
+        'sexe': 'plop',
+        'birth/dateBirth': '1943-01-19',
+        'birth/place': 'Port Arthur',
+    }
+
+    Link.objects.create(resource=con, family_id='1312', name_id='local')
+    resp = app.post_json(url + '?NameID=local', params=params)
+    assert resp.json['err'] == 'wrong-key'
+    assert resp.json['err_desc'] == "sexe key value 'plop' do not belong to 'Sex' required referential"
+
+
 @mock.patch('passerelle.utils.Request.get')
 @mock.patch('passerelle.utils.Request.post')
 def test_create_child_connection_error(mocked_post, mocked_get, con, app):
     mocked_get.return_value = FAMILY_SERVICE_WSDL
     mocked_post.side_effect = CONNECTION_ERROR
     url = get_endpoint('create-child')
     params = {
         'lastname': 'DOE',
@@ -1963,16 +1983,36 @@ def test_update_child_not_linked_error(con, app):
         'birth/place': 'Port Arthur',
     }
 
     resp = app.post_json(url + '?NameID=local&child_id=613880', params=params)
     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_wrong_referential_key_error(mocked_post, mocked_get, con, app):
+    mocked_get.return_value = FAMILY_SERVICE_WSDL
+    mocked_post.side_effect = []
+    url = get_endpoint('create-child')
+    params = {
+        'lastname': 'DOE',
+        'firstname': 'JANNIS',
+        'sexe': 'plop',
+        'birth/dateBirth': '1943-01-19',
+        'birth/place': 'Port Arthur',
+    }
+
+    Link.objects.create(resource=con, family_id='1312', name_id='local')
+    resp = app.post_json(url + '?NameID=local', params=params)
+    assert resp.json['err'] == 'wrong-key'
+    assert resp.json['err_desc'] == "sexe key value 'plop' do not belong to 'Sex' required referential"
+
+
 @mock.patch('passerelle.utils.Request.get')
 @mock.patch('passerelle.utils.Request.post')
 def test_update_child_connection_error(mocked_post, mocked_get, con, app):
     mocked_get.return_value = FAMILY_SERVICE_WSDL
     mocked_post.side_effect = CONNECTION_ERROR
     url = get_endpoint('update-child')
     params = {
         'lastname': 'DOE',
-- 
2.30.2

