Esempio n. 1
0
def validate_action_params(parameters, dereferencer, app_name, action_name, action_func, event=''):
    seen = set()
    for parameter in parameters:
        parameter = deref(parameter, dereferencer)
        name = parameter['name']
        if name in seen:
            raise InvalidApi('Duplicate parameter {0} in api for {1} '
                             'for action {2}'.format(name, app_name, action_name))
        seen.add(name)

    if hasattr(action_func, '__arg_names'):
        method_params = list(action_func.__arg_names)
    else:
        method_params = get_function_arg_names(action_func)

    if method_params and method_params[0] == 'self':
        method_params.pop(0)

    if event:
        method_params.pop(0)

        if action_func.__event_name != event:
            logger.warning('In app {0} action {1}, event documented {2} does not match '
                           'event specified {3}'.format(app_name, action_name, event, action_func.__event_name))

    if not seen == set(method_params):
        only_in_api = seen - set(method_params)
        only_in_definition = set(method_params) - seen
        message = ('Discrepancy between defined parameters in API and in method definition '
                   'for app {0} action {1}.'.format(app_name, action_name))
        if only_in_api:
            message += ' Only in API: {0}.'.format(only_in_api)
        if only_in_definition:
            message += ' Only in definition: {0}'.format(only_in_definition)
        raise InvalidApi(message)
Esempio n. 2
0
def validate_action_params(parameters, dereferencer, app_name, action_name, action_func, event=''):
    seen = set()
    for parameter in parameters:
        parameter = deref(parameter, dereferencer)
        name = parameter['name']
        if name in seen:
            raise InvalidApi('Duplicate parameter {0} in api for {1} '
                             'for action {2}'.format(name, app_name, action_name))
        seen.add(name)

    if hasattr(action_func, '__arg_names'):
        method_params = list(action_func.__arg_names)
    else:
        method_params = get_function_arg_names(action_func)

    if method_params and method_params[0] == 'self':
        method_params.pop(0)

    if event:
        method_params.pop(0)

        if action_func.__event_name != event:
            logger.warning('In app {0} action {1}, event documented {2} does not match '
                           'event specified {3}'.format(app_name, action_name, event, action_func.__event_name))

    if not seen == set(method_params):
        only_in_api = seen - set(method_params)
        only_in_definition = set(method_params) - seen
        message = ('Discrepancy between defined parameters in API and in method definition '
                   'for app {0} action {1}.'.format(app_name, action_name))
        if only_in_api:
            message += ' Only in API: {0}.'.format(only_in_api)
        if only_in_definition:
            message += ' Only in definition: {0}'.format(only_in_definition)
        raise InvalidApi(message)
Esempio n. 3
0
def validate_action_params(parameters, dereferencer, app_name, action_name,
                           action_func):
    seen = set()
    for parameter in parameters:
        parameter = deref(parameter, dereferencer)
        name = parameter['name']
        if name in seen:
            raise InvalidApi('Duplicate parameter {0} in api for {1} '
                             'for action {2}'.format(name, app_name,
                                                     action_name))
        seen.add(name)
    if __new_inspection:
        method_params = list(getsignature(action_func).parameters.keys())
    else:
        method_params = getsignature(action_func).args
    if method_params and method_params[0] == 'self':
        method_params.pop(0)
    if not seen == set(method_params):
        only_in_api = seen - set(method_params)
        only_in_definition = set(method_params) - seen
        message = 'Discrepancy between defined parameters in API and in method definition.'
        if only_in_api:
            message += ' Only in API: {0}.'.format(only_in_api)
        if only_in_definition:
            message += ' Only in definition: {0}'.format(only_in_definition)
        raise InvalidApi(message)
Esempio n. 4
0
def test_ref():
    ref_dict = {'$ref': '#/definitions/Foo'}
    definitions = {
        'definitions': {
            'Foo': 'bar'
        }
    }
    assert deref(ref_dict, RefResolver('', definitions)) == 'bar'
def test_ref():
    ref_dict = {'$ref': '#/definitions/Foo'}
    definitions = {
        'definitions': {
            'Foo': 'bar'
        }
    }
    assert deref(ref_dict, RefResolver('', definitions)) == 'bar'
Esempio n. 6
0
def test_none():
    assert deref(None, Mock(spec=RefResolver)) is None
Esempio n. 7
0
def test_ref_not_found():
    ref_dict = {'$ref': '#/definitions/Foo'}
    definitions = {}
    with pytest.raises(RefResolutionError) as excinfo:
        deref(ref_dict, RefResolver('', definitions))
    assert 'Unresolvable JSON pointer' in str(excinfo.value)
Esempio n. 8
0
def test_not_ref():
    input = {'type': 'object'}
    assert deref(input, Mock(spec=RefResolver)) == input
Esempio n. 9
0
def test_not_dict():
    assert deref(1, Mock(spec=RefResolver)) == 1
Esempio n. 10
0
def test_invalid_ref():
    ref_dict = {'$ref': '#/definitions/Foo', 'x-nullable': True}
    definitions = {'definitions': {'Foo': 'bar'}}
    with warnings.catch_warnings(record=True) as warnings_list:
        deref(ref_dict, RefResolver('', definitions))
        assert warnings_list[0].category == SwaggerValidationWarning
Esempio n. 11
0
def test_none():
    assert deref(None, Mock(spec=RefResolver)) is None
Esempio n. 12
0
def test_ref_not_found():
    ref_dict = {'$ref': '#/definitions/Foo'}
    definitions = {}
    with pytest.raises(RefResolutionError) as excinfo:
        deref(ref_dict, RefResolver('', definitions))
    assert 'Unresolvable JSON pointer' in str(excinfo.value)
Esempio n. 13
0
def test_not_ref():
    input = {'type': 'object'}
    assert deref(input, Mock(spec=RefResolver)) == input
Esempio n. 14
0
def test_not_dict():
    assert deref(1, Mock(spec=RefResolver)) == 1