Projet

Général

Profil

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

Emmanuel Cazenave, 19 juin 2019 18:59

Télécharger (4,72 ko)

Voir les différences:

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

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

  
334
                translated_post_data = {}
335
                for p_name, p_value in json_schema.get('properties', {}).items():
336
                    p_translated_name = p_value.get('translate')
337
                    if p_translated_name:
338
                        translated_post_data[p_translated_name] = data[p_name]
339

  
340
                if translated_post_data:
341
                    d['translated_post_data'] = translated_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
    view = GenericEndpointView()
374
    endpoint_info = endpoint(
375
        post={
376
            'request_body': {
377
                'schema': {
378
                    'application/json': schema
379
                }
380
            }
381
        })
382
    endpoint_obj = FakeEndpoint(endpoint_info)
383
    view = GenericEndpointView(endpoint=endpoint_obj)
384

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

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

  
401

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

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

  
438
    assert params['translated_post_data'] == {
439
        'ContactNom': 'john',
440
        'ContactTel': '01'
441
    }
347
-