Exemplo n.º 1
0
def test_bad_default():
    with pytest.raises(InvalidSpecification) as exc_info:
        Operation(method='GET', path='endpoint', operation=OPERATION6, app_produces=['application/json'],
                  app_security=[], security_definitions={}, definitions=DEFINITIONS, parameter_definitions=PARAMETER_DEFINITIONS,
                  resolver=Resolver())
    exception = exc_info.value
    assert str(exception) == "<InvalidSpecification: The parameter 'stack_version' has a default value which " \
                             "is not of type 'number'>"
    assert repr(exception) == "<InvalidSpecification: The parameter 'stack_version' has a default value which " \
                              "is not of type 'number'>"

    with pytest.raises(InvalidSpecification) as exc_info:
        Operation(method='GET', path='endpoint', operation=OPERATION7, app_produces=['application/json'],
                  app_security=[], security_definitions={}, definitions=DEFINITIONS, parameter_definitions={},
                  resolver=Resolver())
    exception = exc_info.value
    assert str(exception) == "<InvalidSpecification: The parameter 'new_stack' has a default value which " \
                             "is not of type 'integer'>"
    assert repr(exception) == "<InvalidSpecification: The parameter 'new_stack' has a default value which " \
                              "is not of type 'integer'>"

    with pytest.raises(InvalidSpecification) as exc_info:
        Operation(
                method='GET', path='endpoint', operation=OPERATION8, app_produces=['application/json'],
                app_security=[], security_definitions={}, definitions=DEFINITIONS, parameter_definitions={},
                resolver=Resolver()
        )
    exception = exc_info.value
    assert str(exception) == "<InvalidSpecification: The parameter 'new_stack' has a default value which " \
                             "is not of type 'object'>"
    assert repr(exception) == "<InvalidSpecification: The parameter 'new_stack' has a default value which " \
                              "is not of type 'object'>"
Exemplo n.º 2
0
def test_default(api):
    op = OPERATION6.copy()
    op['parameters'][1]['default'] = 1
    Operation(api=api,
              method='GET',
              path='endpoint',
              path_parameters=[],
              operation=op,
              app_produces=['application/json'],
              app_consumes=['application/json'],
              app_security=[],
              security_definitions={},
              definitions=DEFINITIONS,
              parameter_definitions=PARAMETER_DEFINITIONS,
              resolver=Resolver())
    op = OPERATION8.copy()
    op['parameters'][0]['default'] = {
        'keep_stacks': 1,
        'image_version': 'one',
        'senza_yaml': 'senza.yaml',
        'new_traffic': 100
    }
    Operation(api=api,
              method='POST',
              path='endpoint',
              path_parameters=[],
              operation=op,
              app_produces=['application/json'],
              app_consumes=['application/json'],
              app_security=[],
              security_definitions={},
              definitions=DEFINITIONS,
              parameter_definitions={},
              resolver=Resolver())
Exemplo n.º 3
0
def test_get_path_parameter_types(api):
    op = OPERATION1.copy()
    op['parameters'] = [{
        'in': 'path',
        'type': 'int',
        'name': 'int_path'
    }, {
        'in': 'path',
        'type': 'string',
        'name': 'string_path'
    }, {
        'in': 'path',
        'type': 'string',
        'format': 'path',
        'name': 'path_path'
    }]

    operation = Operation(api=api,
                          method='GET',
                          path='endpoint',
                          path_parameters=[],
                          operation=op,
                          app_produces=['application/json'],
                          app_consumes=['application/json'],
                          resolver=Resolver())

    assert {
        'int_path': 'int',
        'string_path': 'string',
        'path_path': 'path'
    } == operation.get_path_parameter_types()
Exemplo n.º 4
0
def test_get_path_parameter_types(api):
    op = OPERATION1.copy()
    op['parameters'] = [{'in': 'path', 'type': 'int', 'name': 'int_path'},
                        {'in': 'path', 'type': 'string', 'name': 'string_path'},
                        {'in': 'path', 'type': 'string', 'format': 'path', 'name': 'path_path'}]

    operation = Operation(api=api, method='GET', path='endpoint', path_parameters=[], operation=op,
                          app_produces=['application/json'], app_consumes=['application/json'], resolver=Resolver())

    assert {'int_path': 'int', 'string_path': 'string', 'path_path': 'path'} == operation.get_path_parameter_types()
Exemplo n.º 5
0
def test_mock_resolver_no_examples():
    resolver = MockResolver(mock_all=True)

    responses = {
        '418': {}
    }

    operation = Operation(api=None,
                          method='GET',
                          path='endpoint',
                          path_parameters=[],
                          operation={
                              'responses': responses
                          },
                          app_produces=['application/json'],
                          app_consumes=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions={},
                          resolver=resolver)
    assert operation.operation_id == 'mock-1'

    response, status_code = resolver.mock_operation(operation)
    assert status_code == 418
    assert response == 'No example response was defined.'
Exemplo n.º 6
0
def test_object_controller_resolve():

    controller = GreetingController()

    resolver = ObjectResolver()
    resolver.add_controller(controller)

    route_handler = resolver.resolve_function_from_operation_id(
        'greeting.post_greeting')

    assert type(route_handler) is not None
    assert route_handler("Test")['greeting'] == "Hello Test"

    operation = Operation(api=None,
                          method='POST',
                          path='/hello',
                          path_parameters=[],
                          operation={
                              'x-swagger-router-controller': 'greeting',
                              'operationId': 'post_greeting',
                          },
                          app_produces=['application/json'],
                          app_consumes=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=resolver)
    #
    # # TODO: Test resolver was hit
    # # TODO: Test operation has right function

    assert operation.operation_id == 'greeting.post_greeting'
Exemplo n.º 7
0
def test_operation_composed_definition(api):
    operation = Operation(api=api,
                          method='GET',
                          path='endpoint',
                          path_parameters=[],
                          operation=OPERATION10,
                          app_produces=['application/json'],
                          app_consumes=['application/json'],
                          app_security=[],
                          security_definitions=SECURITY_DEFINITIONS,
                          definitions=DEFINITIONS,
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=Resolver())
    assert isinstance(operation.function, types.FunctionType)
    # security decorator should be a partial with verify_oauth as the function and token url and scopes as arguments.
    # See https://docs.python.org/2/library/functools.html#partial-objects
    assert operation.security_decorator.func is verify_oauth
    assert operation.security_decorator.args == (
        'https://oauth.example/token_info', set(['uid']))

    assert operation.method == 'GET'
    assert operation.produces == ['application/json']
    assert operation.consumes == ['application/json']
    assert operation.security == [{'oauth': ['uid']}]
    expected_body_schema = {
        '$ref': '#/definitions/composed',
        'definitions': DEFINITIONS
    }
    assert operation.body_schema == expected_body_schema
Exemplo n.º 8
0
def test_no_token_info(api):
    operation = Operation(api=api,
                          method='GET',
                          path='endpoint',
                          path_parameters=[],
                          operation=OPERATION1,
                          app_produces=['application/json'],
                          app_consumes=['application/json'],
                          app_security=SECURITY_DEFINITIONS_WO_INFO,
                          security_definitions=SECURITY_DEFINITIONS_WO_INFO,
                          definitions=DEFINITIONS,
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=Resolver())
    assert isinstance(operation.function, types.FunctionType)
    assert operation.security_decorator is security_passthrough

    assert operation.method == 'GET'
    assert operation.produces == ['application/json']
    assert operation.consumes == ['application/json']
    assert operation.security == [{'oauth': ['uid']}]

    expected_body_schema = {
        '$ref': '#/definitions/new_stack',
        'definitions': DEFINITIONS
    }
    assert operation.body_schema == expected_body_schema
Exemplo n.º 9
0
def test_mock_resolver():
    resolver = MockResolver(mock_all=True)

    responses = {
        'default': {
            'examples': {
                'application/json': {
                    'foo': 'bar'
                }
            }
        }
    }

    operation = Operation(api=None,
                          method='GET',
                          path='endpoint',
                          path_parameters=[],
                          operation={
                              'responses': responses
                          },
                          app_produces=['application/json'],
                          app_consumes=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions={},
                          resolver=resolver)
    assert operation.operation_id == 'mock-1'

    response, status_code = resolver.mock_operation(operation)
    assert status_code == 200
    assert response == {'foo': 'bar'}
Exemplo n.º 10
0
def test_resolve_invalid_reference():
    with pytest.raises(InvalidSpecification) as exc_info:
        Operation(method='GET', path='endpoint', operation=OPERATION5, app_produces=['application/json'],
                  app_security=[], security_definitions={}, definitions={}, parameter_definitions=PARAMETER_DEFINITIONS,
                  resolver=Resolver())

    exception = exc_info.value  # type: InvalidSpecification
    assert exception.reason == "GET endpoint  '$ref' needs to start with '#/'"
Exemplo n.º 11
0
def test_mock_resolver_notimplemented():
    resolver = MockResolver(mock_all=False)

    responses = {
        '418': {}
    }

    # do not mock the existent functions
    operation = Operation(api=None,
                          method='GET',
                          path='endpoint',
                          path_parameters=[],
                          operation={
                              'operationId': 'fakeapi.hello.get'
                          },
                          app_produces=['application/json'],
                          app_consumes=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions={},
                          resolver=resolver)
    assert operation.operation_id == 'fakeapi.hello.get'

    # mock only the nonexistent ones
    operation = Operation(api=None,
                          method='GET',
                          path='endpoint',
                          path_parameters=[],
                          operation={
                              'operationId': 'fakeapi.hello.nonexistent_function',
                              'responses': responses
                          },
                          app_produces=['application/json'],
                          app_consumes=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions={},
                          resolver=resolver)

    # check if it is using the mock function
    assert operation._Operation__undecorated_function() == ('No example response was defined.', 418)
Exemplo n.º 12
0
def test_detect_controller():
    operation = Operation(method='GET',
                          path='endpoint',
                          operation=OPERATION6,
                          app_produces=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=get_function_from_name)
    assert operation.operation_id == 'fakeapi.hello.post_greeting'
Exemplo n.º 13
0
def test_resty_resolve_with_default_module_name():
    operation = Operation(method='GET',
                          path='/hello/{id}',
                          operation={},
                          app_produces=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=RestyResolver('fakeapi'))
    assert operation.operation_id == 'fakeapi.hello.get'
Exemplo n.º 14
0
def test_parameter_reference():
    operation = Operation(method='GET',
                          path='endpoint',
                          operation=OPERATION4,
                          app_produces=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=Resolver())
    assert operation.parameters == [{'in': 'path', 'type': 'integer'}]
Exemplo n.º 15
0
def test_resty_resolve_with_default_module_name_will_translate_dashes_in_resource_name(
):
    operation = Operation(method='GET',
                          path='/foo-bar',
                          operation={},
                          app_produces=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=RestyResolver('fakeapi'))
    assert operation.operation_id == 'fakeapi.foo_bar.search'
Exemplo n.º 16
0
def test_resty_resolve_x_router_controller_without_operation_id():
    operation = Operation(
        method='GET',
        path='/hello/{id}',
        operation={'x-swagger-router-controller': 'fakeapi.hello'},
        app_produces=['application/json'],
        app_security=[],
        security_definitions={},
        definitions={},
        parameter_definitions=PARAMETER_DEFINITIONS,
        resolver=RestyResolver('fakeapi'))
    assert operation.operation_id == 'fakeapi.hello.get'
Exemplo n.º 17
0
def test_resty_resolve_with_default_module_name_will_resolve_resource_root_as_configured(
):
    operation = Operation(method='GET',
                          path='/hello',
                          operation={},
                          app_produces=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=RestyResolver('fakeapi', 'list'))
    assert operation.operation_id == 'fakeapi.hello.list'
Exemplo n.º 18
0
def test_resty_resolve_operation_id():
    operation = Operation(method='GET',
                          path='endpoint',
                          operation={
                              'operationId': 'fakeapi.hello.post_greeting',
                          },
                          app_produces=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=RestyResolver('fakeapi'))
    assert operation.operation_id == 'fakeapi.hello.post_greeting'
Exemplo n.º 19
0
def test_resty_resolve_with_default_module_name_will_resolve_resource_root_post_as_post():
    operation = Operation(api=None,
                          method='POST',
                          path='/hello',
                          path_parameters=[],
                          operation={},
                          app_produces=['application/json'],
                          app_consumes=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=RestyResolver('fakeapi'))
    assert operation.operation_id == 'fakeapi.hello.post'
Exemplo n.º 20
0
def test_resty_resolve_with_default_module_name_can_resolve_api_root():
    operation = Operation(api=None,
                          method='GET',
                          path='/',
                          path_parameters=[],
                          operation={},
                          app_produces=['application/json'],
                          app_consumes=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=RestyResolver('fakeapi'))
    assert operation.operation_id == 'fakeapi.get'
Exemplo n.º 21
0
def test_standard_resolve_x_router_controller():
    operation = Operation(method='GET',
                          path='endpoint',
                          operation={
                              'x-swagger-router-controller': 'fakeapi.hello',
                              'operationId': 'post_greeting',
                          },
                          app_produces=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=Resolver())
    assert operation.operation_id == 'fakeapi.hello.post_greeting'
Exemplo n.º 22
0
def test_resty_resolve_with_default_module_name_and_x_router_controller_will_resolve_resource_root_get_as_search(
):
    operation = Operation(method='GET',
                          path='/hello',
                          operation={
                              'x-swagger-router-controller': 'fakeapi.hello',
                          },
                          app_produces=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=RestyResolver('fakeapi'))
    assert operation.operation_id == 'fakeapi.hello.search'
Exemplo n.º 23
0
def test_mock_resolver_notimplemented():
    resolver = MockResolver(mock_all=False)

    operation = Operation(method='GET',
                          path='endpoint',
                          path_parameters=[],
                          operation={'operationId': 'fakeapi.hello.get'},
                          app_produces=['application/json'],
                          app_consumes=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions={},
                          resolver=resolver)
    assert operation.operation_id == 'fakeapi.hello.get'
Exemplo n.º 24
0
def test_non_existent_reference():
    with pytest.raises(InvalidSpecification) as exc_info:  # type: py.code.ExceptionInfo
        operation = Operation(method='GET',
                            path='endpoint',
                            operation=OPERATION1,
                            app_produces=['application/json'],
                            app_security=[],
                            security_definitions={},
                            definitions={},
                            parameter_definitions={},
                            resolver=Resolver())
        schema = operation.body_schema

    exception = exc_info.value
    assert str(exception) == "<InvalidSpecification: GET endpoint Definition 'new_stack' not found>"
    assert repr(exception) == "<InvalidSpecification: GET endpoint Definition 'new_stack' not found>"
Exemplo n.º 25
0
def test_multi_body():
    with pytest.raises(InvalidSpecification) as exc_info:  # type: py.code.ExceptionInfo
        operation = Operation(method='GET',
                            path='endpoint',
                            operation=OPERATION2,
                            app_produces=['application/json'],
                            app_security=[],
                            security_definitions={},
                            definitions=DEFINITIONS,
                            parameter_definitions=PARAMETER_DEFINITIONS,
                            resolver=Resolver())
        schema = operation.body_schema

    exception = exc_info.value
    assert str(exception) == "<InvalidSpecification: GET endpoint There can be one 'body' parameter at most>"
    assert repr(exception) == "<InvalidSpecification: GET endpoint There can be one 'body' parameter at most>"
Exemplo n.º 26
0
def test_invalid_reference():
    with pytest.raises(InvalidSpecification) as exc_info:  # type: py.code.ExceptionInfo
        operation = Operation(method='GET',
                            path='endpoint',
                            operation=OPERATION3,
                            app_produces=['application/json'],
                            app_security=[],
                            security_definitions={},
                            definitions=DEFINITIONS,
                            parameter_definitions=PARAMETER_DEFINITIONS,
                            resolver=Resolver())
        schema = operation.body_schema

    exception = exc_info.value
    assert str(exception) == "<InvalidSpecification: GET endpoint  '$ref' needs to point to definitions or parameters>"
    assert repr(exception) == "<InvalidSpecification: GET endpoint  '$ref' needs to point to definitions or parameters>"
Exemplo n.º 27
0
def test_no_token_info():
    operation = Operation(method='GET',
                          path='endpoint',
                          operation=OPERATION1,
                          app_produces=['application/json'],
                          app_security=SECURITY_DEFINITIONS_WO_INFO,
                          security_definitions=SECURITY_DEFINITIONS_WO_INFO,
                          definitions=DEFINITIONS,
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=Resolver())
    assert isinstance(operation.function, types.FunctionType)
    assert operation._Operation__security_decorator is security_passthrough

    assert operation.method == 'GET'
    assert operation.produces == ['application/json']
    assert operation.security == [{'oauth': ['uid']}]
    assert operation.body_schema == DEFINITIONS['new_stack']
Exemplo n.º 28
0
def test_invalid_reference(api):
    with pytest.raises(InvalidSpecification) as exc_info:  # type: py.code.ExceptionInfo
        operation = Operation(api=api,
                              method='GET',
                              path='endpoint',
                              path_parameters=[],
                              operation=OPERATION3,
                              app_produces=['application/json'],
                              app_consumes=['application/json'],
                              app_security=[],
                              security_definitions={},
                              definitions=DEFINITIONS,
                              parameter_definitions=PARAMETER_DEFINITIONS,
                              resolver=Resolver())
        operation.body_schema

    exception = exc_info.value
    assert str(exception).startswith("<InvalidSpecification: GET endpoint $ref")
    assert repr(exception).startswith("<InvalidSpecification: GET endpoint $ref")
Exemplo n.º 29
0
def test_object_controller_resolve_no_controller_operation():

    resolver = ObjectResolver()
    greetings = GreetingController()

    resolver.add_controller(greetings)

    operation = Operation(api=None,
                          method='POST',
                          path='/greeting',
                          path_parameters=[],
                          operation={},
                          app_produces=['application/json'],
                          app_consumes=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=resolver)

    assert operation.operation_id == "greeting.post_greeting"
Exemplo n.º 30
0
def test_operation():
    operation = Operation(method='GET',
                          path='endpoint',
                          operation=OPERATION1,
                          app_produces=['application/json'],
                          app_security=[],
                          security_definitions=SECURITY_DEFINITIONS,
                          definitions=DEFINITIONS,
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=get_function_from_name)
    assert isinstance(operation.function, types.FunctionType)
    # security decorator should be a partial with verify_oauth as the function and token url and scopes as arguments.
    # See https://docs.python.org/2/library/functools.html#partial-objects
    assert operation._Operation__security_decorator.func is verify_oauth
    assert operation._Operation__security_decorator.args == (
        'https://ouath.example/token_info', set(['uid']))

    assert operation.method == 'GET'
    assert operation.produces == ['application/json']
    assert operation.security == [{'oauth': ['uid']}]
    assert operation.body_schema == DEFINITIONS['new_stack']