Projet

Général

Profil

0001-misc-add-parameters-rename-capabilities-34178.patch

Emmanuel Cazenave, 20 juin 2019 12:29

Télécharger (4,58 ko)

Voir les différences:

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

 passerelle/views.py            |  9 ++++
 tests/test_generic_endpoint.py | 92 ++++++++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+)
passerelle/views.py
331 331
                    raise APIError(e.message, http_status=400)
332 332
                d['post_data'] = data
333 333

  
334
                renamed_post_data = {}
335
                for p_name, p_value in json_schema.get('properties', {}).items():
336
                    p_new_name = p_value.get('rename')
337
                    if p_new_name and p_name in data:
338
                        renamed_post_data[p_new_name] = data[p_name]
339

  
340
                if renamed_post_data:
341
                    d['renamed_post_data'] = renamed_post_data
342

  
334 343
        return d
335 344

  
336 345
    @csrf_exempt
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
        }
372
    }
373
    endpoint_info = endpoint(
374
        post={
375
            'request_body': {
376
                'schema': {
377
                    'application/json': schema
378
                }
379
            }
380
        })
381
    endpoint_obj = FakeEndpoint(endpoint_info)
382
    view = GenericEndpointView(endpoint=endpoint_obj)
383

  
384
    data = {
385
        'contact_nom': 'john',
386
        'contact_tel': '01'
387
    }
388
    request = RequestFactory().post('/someurl', json.dumps(data), content_type='application/json')
389
    params = view.get_params(request)
390
    assert params['post_data'] == data
391

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

  
400
    # Schema with rename parameter
401
    schema = {
402
        '$schema': 'http://json-schema.org/draft-03/schema#',
403
        'type': 'object',
404
        'properties': {
405
            'contact_nom': {
406
                'type': 'string',
407
                'required': True,
408
                'rename': 'ContactNom'
409
            },
410
            'contact_tel': {
411
                'type': 'string',
412
                'rename': 'ContactTel'
413
            }
414
        }
415
    }
416
    endpoint_info = endpoint(
417
        post={
418
            'request_body': {
419
                'schema': {
420
                    'application/json': schema
421
                }
422
            }
423
        })
424
    endpoint_obj = FakeEndpoint(endpoint_info)
425
    view = GenericEndpointView(endpoint=endpoint_obj)
426

  
427
    data = {
428
        'contact_nom': 'john',
429
        'contact_tel': '01'
430
    }
431
    request = RequestFactory().post('/someurl', json.dumps(data), content_type='application/json')
432
    params = view.get_params(request)
433
    assert params['post_data'] == data
434

  
435
    assert params['renamed_post_data'] == {
436
        'ContactNom': 'john',
437
        'ContactTel': '01'
438
    }
347
-