def test_multiple_response_types(response_transmute_func):
    """ multiple response types should be indicated as such in
    the swagger documentation.
    """
    routes = SwaggerSpec()
    routes.add_func(response_transmute_func, default_context)
    definition = routes.swagger_definition()
    path = definition["paths"]["/api/v1/create_if_authorized/"]
    responses = path["get"]["responses"]
    assert responses["201"] == swagger_schema.Response({
        "description": "",
        "schema": {
            "type": "boolean"
        },
        "headers": {
            "location": {
                "type": "string"
            }
        }
    }).to_primitive()
    assert responses["401"] == swagger_schema.Response({
        "description": "unauthorized",
        "schema": {
            "type": "string"
        }
    }).to_primitive()
    assert "200" not in responses
    assert "400" in responses
Exemple #2
0
def _generate_swagger_json(app, context, **kwargs):
    spec = SwaggerSpec()
    for handler in _get_handlers(app):
        for m in METHODS:
            method = getattr(handler, m)
            if hasattr(method, "transmute_func"):
                spec.add_func(method.transmute_func, context)
    return spec.swagger_definition(**kwargs)
Exemple #3
0
def _generate_swagger_json(app, context, **kwargs):
    spec = SwaggerSpec()
    for domain, specs in app.handlers:
        for s in specs:
            handler = s.handler_class
            for m in METHODS:
                method = getattr(handler, m)
                if hasattr(method, "transmute_func"):
                    spec.add_func(method.transmute_func, context)
    return spec.swagger_definition(**kwargs)
def test_swagger_get_post(transmute_func, transmute_func_post):
    """
    adding different paths of diffrent methods should have both
    present in the spec.
    """
    routes = SwaggerSpec()
    routes.add_func(transmute_func, default_context)
    routes.add_func(transmute_func_post, default_context)
    spec = routes.swagger_definition()
    assert "get" in spec["paths"]["/api/v1/multiply"]
    assert "post" in spec["paths"]["/api/v1/multiply"]
Exemple #5
0
def test_swagger_transmute_func(transmute_func):
    """
    swagger routes should be ablo to generate a proper
    spec.
    """
    routes = SwaggerSpec()
    routes.add_func(transmute_func, default_context)
    assert routes.swagger_definition() == {
        "info": {"title": "example", "version": "1.0"},
        "paths": {
            "/api/v1/multiply": transmute_func.get_swagger_path(default_context).to_primitive(),
        },
        "swagger": "2.0",
        "basePath": "/"
    }
Exemple #6
0
def test_swagger_parameter_description():
    """
    if parameter descriptions are added to a function, they
    should appear in the swagger json.
    """
    parameter_descriptions = {
        "left": "the left operand",
        "right": "the right operand",
        "header": "the header",
        "path": "the path",
        "return": "the result",
    }

    @describe(
        paths="/api/v1/adopt/{path}",
        parameter_descriptions=parameter_descriptions,
        header_parameters=["header"],
    )
    @annotate({
        "left": int,
        "right": int,
        "header": str,
        "path": str,
        "return": int
    })
    def adopt(left, right, header, path):
        return left + right

    func = TransmuteFunction(adopt)

    routes = SwaggerSpec()
    routes.add_func(func, default_context)
    spec = routes.swagger_definition()

    params = spec["paths"]["/api/v1/adopt/{path}"]["get"]["parameters"]
    for param in spec["paths"]["/api/v1/adopt/{path}"]["get"]["parameters"]:
        assert parameter_descriptions[param["name"]] == param["description"]
    assert (parameter_descriptions["return"] == spec["paths"]
            ["/api/v1/adopt/{path}"]["get"]["responses"]["200"]["schema"]
            ["description"])