misc: add parameters transformation capabilities (#34178)
This commit is contained in:
parent
b4aad9cd5f
commit
99b5fa1c40
|
@ -35,6 +35,7 @@ from django.views.generic.detail import SingleObjectMixin
|
|||
from django.conf import settings
|
||||
from django.shortcuts import resolve_url
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.utils import six
|
||||
from django.utils.timezone import make_aware
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.encoding import force_text
|
||||
|
@ -329,7 +330,32 @@ class GenericEndpointView(GenericConnectorMixin, SingleObjectMixin, View):
|
|||
validate(data, json_schema)
|
||||
except ValidationError as e:
|
||||
raise APIError(e.message, http_status=400)
|
||||
d['post_data'] = data
|
||||
request.orig_post_data = data
|
||||
|
||||
post_data = {}
|
||||
for p_name, p_value in json_schema.get('properties', {}).items():
|
||||
p_data = data.get(p_name)
|
||||
if p_data is not None:
|
||||
transform = p_value.get('transform')
|
||||
if not transform:
|
||||
post_data[p_name] = p_data
|
||||
continue
|
||||
if isinstance(transform, six.text_type) and transform == 'ignore':
|
||||
continue
|
||||
if callable(transform):
|
||||
post_data[p_name] = transform(p_data)
|
||||
continue
|
||||
|
||||
t0 = transform[0]
|
||||
if callable(t0):
|
||||
args = [p_data] + list(transform[1:])
|
||||
post_data[p_name] = t0(*args)
|
||||
continue
|
||||
if isinstance(t0, six.text_type) and t0 == 'rename':
|
||||
p_new_name = transform[1]
|
||||
post_data[p_new_name] = p_data
|
||||
|
||||
d['post_data'] = post_data
|
||||
|
||||
return d
|
||||
|
||||
|
|
|
@ -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,102 @@ 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'
|
||||
},
|
||||
'contact_email': {
|
||||
'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',
|
||||
'contact_email': 'foo@localhost'
|
||||
}
|
||||
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)
|
||||
|
||||
def foo(x):
|
||||
return 'foo'
|
||||
|
||||
# Schema with rename parameter
|
||||
schema = {
|
||||
'$schema': 'http://json-schema.org/draft-03/schema#',
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'contact_nom': {
|
||||
'type': 'string',
|
||||
'required': True,
|
||||
'transform': ('rename', 'ContactNom')
|
||||
},
|
||||
'contact_tel': {
|
||||
'type': 'string',
|
||||
'transform': 'ignore'
|
||||
},
|
||||
'contact_email': {
|
||||
'type': 'string',
|
||||
'transform': (foo,)
|
||||
}
|
||||
}
|
||||
}
|
||||
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',
|
||||
'contact_email': 'foo@localhost'
|
||||
}
|
||||
request = RequestFactory().post('/someurl', json.dumps(data), content_type='application/json')
|
||||
params = view.get_params(request)
|
||||
assert request.orig_post_data == data
|
||||
assert params['post_data'] == {
|
||||
'ContactNom': 'john',
|
||||
'contact_email': 'foo'
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue