From 95a15bb6dace002747266e76a9d8936e81473b04 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Wed, 19 Jun 2019 18:54:10 +0200
Subject: [PATCH] misc: add parameters rename capabilities (#34178)

---
 passerelle/views.py            |  9 ++++
 tests/test_generic_endpoint.py | 92 ++++++++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+)

diff --git a/passerelle/views.py b/passerelle/views.py
index e47f6410..7c9c736d 100644
--- a/passerelle/views.py
+++ b/passerelle/views.py
@@ -331,6 +331,15 @@ class GenericEndpointView(GenericConnectorMixin, SingleObjectMixin, View):
                     raise APIError(e.message, http_status=400)
                 d['post_data'] = data
 
+                renamed_post_data = {}
+                for p_name, p_value in json_schema.get('properties', {}).items():
+                    p_new_name = p_value.get('rename')
+                    if p_new_name and p_name in data:
+                        renamed_post_data[p_new_name] = data[p_name]
+
+                if renamed_post_data:
+                    d['renamed_post_data'] = renamed_post_data
+
         return d
 
     @csrf_exempt
diff --git a/tests/test_generic_endpoint.py b/tests/test_generic_endpoint.py
index 9e070330..f3645102 100644
--- a/tests/test_generic_endpoint.py
+++ b/tests/test_generic_endpoint.py
@@ -22,6 +22,8 @@ import json
 import random
 import warnings
 
+from django.test import RequestFactory
+import json
 import mock
 import pytest
 
@@ -31,7 +33,9 @@ from passerelle.apps.arcgis.models import ArcGIS
 from passerelle.base.models import ResourceLog, ProxyLogger, BaseResource, HTTPResource
 from passerelle.contrib.mdel.models import MDEL
 from passerelle.contrib.stub_invoices.models import StubInvoicesConnector
+from passerelle.views import GenericEndpointView
 from passerelle.utils.api import endpoint
+from passerelle.utils.jsonresponse import APIError
 
 
 @pytest.fixture
@@ -344,3 +348,91 @@ def test_https_warnings(app, db, monkeypatch, httpbin_secure, relax_openssl):
     with warnings.catch_warnings():
         warnings.simplefilter('error')
         resource.requests.get(httpbin_secure.join('/get/'))
+
+
+class FakeEndpoint(object):
+
+    def __init__(self, endpoint_info):
+        self.endpoint_info = endpoint_info
+
+
+def test_get_params_json_schema(app, db, monkeypatch):
+    schema = {
+        '$schema': 'http://json-schema.org/draft-03/schema#',
+        'type': 'object',
+        'properties': {
+            'contact_nom': {
+                'type': 'string',
+                'required': True
+            },
+            'contact_tel': {
+                'type': 'string'
+            }
+        }
+    }
+    endpoint_info = endpoint(
+        post={
+            'request_body': {
+                'schema': {
+                    'application/json': schema
+                }
+            }
+        })
+    endpoint_obj = FakeEndpoint(endpoint_info)
+    view = GenericEndpointView(endpoint=endpoint_obj)
+
+    data = {
+        'contact_nom': 'john',
+        'contact_tel': '01'
+    }
+    request = RequestFactory().post('/someurl', json.dumps(data), content_type='application/json')
+    params = view.get_params(request)
+    assert params['post_data'] == data
+
+    # Missing required parameter
+    data = {
+        'contact_tel': '01'
+    }
+    request = RequestFactory().post('/someurl', json.dumps(data), content_type='application/json')
+    with pytest.raises(APIError):
+        params = view.get_params(request)
+
+    # Schema with rename parameter
+    schema = {
+        '$schema': 'http://json-schema.org/draft-03/schema#',
+        'type': 'object',
+        'properties': {
+            'contact_nom': {
+                'type': 'string',
+                'required': True,
+                'rename': 'ContactNom'
+            },
+            'contact_tel': {
+                'type': 'string',
+                'rename': 'ContactTel'
+            }
+        }
+    }
+    endpoint_info = endpoint(
+        post={
+            'request_body': {
+                'schema': {
+                    'application/json': schema
+                }
+            }
+        })
+    endpoint_obj = FakeEndpoint(endpoint_info)
+    view = GenericEndpointView(endpoint=endpoint_obj)
+
+    data = {
+        'contact_nom': 'john',
+        'contact_tel': '01'
+    }
+    request = RequestFactory().post('/someurl', json.dumps(data), content_type='application/json')
+    params = view.get_params(request)
+    assert params['post_data'] == data
+
+    assert params['renamed_post_data'] == {
+        'ContactNom': 'john',
+        'ContactTel': '01'
+    }
-- 
2.20.1

