Projet

Général

Profil

0001-misc-add-parameters-transformation-capabilities-3417.patch

Emmanuel Cazenave, 24 juin 2019 15:23

Télécharger (6,17 ko)

Voir les différences:

Subject: [PATCH] misc: add parameters transformation capabilities (#34178)

 passerelle/views.py            |  28 ++++++++-
 tests/test_generic_endpoint.py | 103 +++++++++++++++++++++++++++++++++
 2 files changed, 130 insertions(+), 1 deletion(-)
passerelle/views.py
35 35
from django.conf import settings
36 36
from django.shortcuts import resolve_url
37 37
from django.core.urlresolvers import reverse
38
from django.utils import six
38 39
from django.utils.timezone import make_aware
39 40
from django.utils.translation import ugettext_lazy as _
40 41
from django.utils.encoding import force_text
......
329 330
                    validate(data, json_schema)
330 331
                except ValidationError as e:
331 332
                    raise APIError(e.message, http_status=400)
332
                d['post_data'] = data
333
                request.orig_post_data = data
334

  
335
                post_data = {}
336
                for p_name, p_value in json_schema.get('properties', {}).items():
337
                    p_data = data.get(p_name)
338
                    if p_data:
339
                        transform = p_value.get('transform')
340
                        if not transform:
341
                            post_data[p_name] = p_data
342
                            continue
343
                        if isinstance(transform, six.text_type) and transform == 'ignore':
344
                            continue
345
                        if callable(transform):
346
                            post_data[p_name] = transform(p_data)
347
                            continue
348

  
349
                        t0 = transform[0]
350
                        if callable(t0):
351
                            args = [p_data] + list(transform[1:])
352
                            post_data[p_name] = t0(*args)
353
                            continue
354
                        if isinstance(t0, six.text_type) and t0 == 'rename':
355
                            p_new_name = transform[1]
356
                            post_data[p_new_name] = p_data
357

  
358
                d['post_data'] = post_data
333 359

  
334 360
        return d
335 361

  
tests/test_generic_endpoint.py
22 22
import random
23 23
import warnings
24 24

  
25
from django.test import RequestFactory
26
import json
25 27
import mock
26 28
import pytest
27 29

  
......
31 33
from passerelle.base.models import ResourceLog, ProxyLogger, BaseResource, HTTPResource
32 34
from passerelle.contrib.mdel.models import MDEL
33 35
from passerelle.contrib.stub_invoices.models import StubInvoicesConnector
36
from passerelle.views import GenericEndpointView
34 37
from passerelle.utils.api import endpoint
38
from passerelle.utils.jsonresponse import APIError
35 39

  
36 40

  
37 41
@pytest.fixture
......
344 348
    with warnings.catch_warnings():
345 349
        warnings.simplefilter('error')
346 350
        resource.requests.get(httpbin_secure.join('/get/'))
351

  
352

  
353
class FakeEndpoint(object):
354

  
355
    def __init__(self, endpoint_info):
356
        self.endpoint_info = endpoint_info
357

  
358

  
359
def test_get_params_json_schema(app, db, monkeypatch):
360
    schema = {
361
        '$schema': 'http://json-schema.org/draft-03/schema#',
362
        'type': 'object',
363
        'properties': {
364
            'contact_nom': {
365
                'type': 'string',
366
                'required': True
367
            },
368
            'contact_tel': {
369
                'type': 'string'
370
            },
371
            'contact_email': {
372
                'type': 'string'
373
            }
374
        }
375
    }
376
    endpoint_info = endpoint(
377
        post={
378
            'request_body': {
379
                'schema': {
380
                    'application/json': schema
381
                }
382
            }
383
        })
384
    endpoint_obj = FakeEndpoint(endpoint_info)
385
    view = GenericEndpointView(endpoint=endpoint_obj)
386

  
387
    data = {
388
        'contact_nom': 'john',
389
        'contact_tel': '01',
390
        'contact_email': 'foo@localhost'
391
    }
392
    request = RequestFactory().post('/someurl', json.dumps(data), content_type='application/json')
393
    params = view.get_params(request)
394
    assert params['post_data'] == data
395

  
396
    # Missing required parameter
397
    data = {
398
        'contact_tel': '01'
399
    }
400
    request = RequestFactory().post('/someurl', json.dumps(data), content_type='application/json')
401
    with pytest.raises(APIError):
402
        params = view.get_params(request)
403

  
404
    def foo(x):
405
        return 'foo'
406

  
407
    # Schema with rename parameter
408
    schema = {
409
        '$schema': 'http://json-schema.org/draft-03/schema#',
410
        'type': 'object',
411
        'properties': {
412
            'contact_nom': {
413
                'type': 'string',
414
                'required': True,
415
                'transform': ('rename', 'ContactNom')
416
            },
417
            'contact_tel': {
418
                'type': 'string',
419
                'transform': 'ignore'
420
            },
421
            'contact_email': {
422
                'type': 'string',
423
                'transform': (foo,)
424
            }
425
        }
426
    }
427
    endpoint_info = endpoint(
428
        post={
429
            'request_body': {
430
                'schema': {
431
                    'application/json': schema
432
                }
433
            }
434
        })
435
    endpoint_obj = FakeEndpoint(endpoint_info)
436
    view = GenericEndpointView(endpoint=endpoint_obj)
437

  
438
    data = {
439
        'contact_nom': 'john',
440
        'contact_tel': '01',
441
        'contact_email': 'foo@localhost'
442
    }
443
    request = RequestFactory().post('/someurl', json.dumps(data), content_type='application/json')
444
    params = view.get_params(request)
445
    assert request.orig_post_data == data
446
    assert params['post_data'] == {
447
        'ContactNom': 'john',
448
        'contact_email': 'foo'
449
    }
347
-