Ejemplo n.º 1
0
def test_mock_resolver_notimplemented():
    resolver = MockResolver(mock_all=False)

    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'
Ejemplo n.º 2
0
def test_resty_resolve_operation_id():
    operation = Operation(api=None,
                          method='GET',
                          path='endpoint',
                          path_parameters=[],
                          operation={
                              'operationId': 'fakeapi.hello.post_greeting',
                          },
                          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_greeting'
Ejemplo n.º 3
0
def test_resty_resolve_with_default_module_name_and_x_router_controller_will_resolve_resource_root_get_as_search():
    operation = Operation(api=None,
                          method='GET',
                          path='/hello',
                          path_parameters=[],
                          operation={
                              'x-swagger-router-controller': 'fakeapi.hello',
                          },
                          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.search'
Ejemplo n.º 4
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>"
Ejemplo n.º 5
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>"
Ejemplo n.º 6
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>"
Ejemplo n.º 7
0
def test_resolve_invalid_reference(api):
    with pytest.raises(InvalidSpecification) as exc_info:
        Operation(api=api,
                  method='GET',
                  path='endpoint',
                  path_parameters=[],
                  operation=OPERATION5,
                  app_produces=['application/json'],
                  app_consumes=['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 '#/'"
Ejemplo n.º 8
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']
Ejemplo n.º 9
0
def test_standard_resolve_x_router_controller():
    operation = Operation(api=None,
                          method='GET',
                          path='endpoint',
                          path_parameters=[],
                          operation={
                              'x-swagger-router-controller': 'fakeapi.hello',
                              'operationId': 'post_greeting',
                          },
                          app_produces=['application/json'],
                          app_consumes=['application/json'],
                          app_security=[],
                          security_definitions={},
                          definitions={},
                          parameter_definitions=PARAMETER_DEFINITIONS,
                          resolver=Resolver())
    assert operation.operation_id == 'fakeapi.hello.post_greeting'
Ejemplo n.º 10
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")
Ejemplo n.º 11
0
def test_mock_resolver_no_examples():
    resolver = MockResolver(mock_all=True)

    responses = {'418': {}}

    operation = Operation(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.'
Ejemplo n.º 12
0
def test_mock_resolver():
    resolver = MockResolver(mock_all=True)

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

    operation = Operation(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'}
Ejemplo n.º 13
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=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._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']
Ejemplo n.º 14
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"
Ejemplo n.º 15
0
def test_mock_resolver_inline_schema_example():
    resolver = MockResolver(mock_all=True)

    responses = {
        'default': {
            'schema': {
                'type': 'object',
                'properties': {
                    'foo': {
                        'type': 'string'
                    }
                },
                'example': {
                    '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'}