Example #1
0
def test_path_summary():
    app = Starlette()
    plugin = StarlettePlugin(app)
    spec = APISpec(title="Test API",
                   version="0.0.1",
                   openapi_version="2.0",
                   plugins=[plugin])

    @app.route("/test")
    def test_endpoint(request):
        """This is the expected summary"""
        pass  # pragma: no cover

    @app.route("/test_with_doc")
    def test_endpoint_with_doc(request):
        """
        This is the expected summary 2
        ---
        responses:
            200:
                description: "ok"
        """
        pass  # pragma: no cover

    for endpoint in plugin.endpoints():
        spec.path(endpoint.path, endpoint=endpoint)

    assert spec.to_dict() == {
        "info": {
            "title": "Test API",
            "version": "0.0.1"
        },
        "paths": {
            "/test": {
                "get": {
                    "operationId": "get_test_endpoint",
                    "summary": "This is the expected summary",
                }
            },
            "/test_with_doc": {
                "get": {
                    "summary": "This is the expected summary 2",
                    "operationId": "get_test_endpoint_with_doc",
                    "responses": {
                        "200": {
                            "description": "ok"
                        }
                    },
                }
            },
        },
        "swagger": "2.0",
    }
Example #2
0
def test_path_mixed_operations():
    app = Starlette()
    plugin = StarlettePlugin(app)
    spec = APISpec(title="Test API",
                   version="0.0.1",
                   openapi_version="2.0",
                   plugins=[plugin])
    document_endpoint_oauth2_authentication(
        spec,
        endpoint="/test",
        method="get",
        required_scopes=["scope1", "scope2"],
        unauthorized_status_code=400,
        forbidden_status_code=402,
    )

    @app.route("/test")
    def endpoint():
        """
        responses:
            200:
                description: ok
                type: string
        """
        pass  # pragma: no cover

    for endpoint in plugin.endpoints():
        spec.path(endpoint.path, endpoint=endpoint)

    assert spec.to_dict() == {
        "info": {
            "title": "Test API",
            "version": "0.0.1"
        },
        "paths": {
            "/test": {
                "get": {
                    "operationId": "get_endpoint",
                    "responses": {
                        "200": {
                            "description": "ok",
                            "type": "string"
                        },
                        "400": {
                            "description":
                            "No "
                            "permission "
                            "-- "
                            "see "
                            "authorization "
                            "schemes",
                            "schema": {
                                "type": "string"
                            },
                        },
                        "402": {
                            "description":
                            "Request "
                            "forbidden "
                            "-- "
                            "authorization "
                            "will "
                            "not "
                            "help",
                            "schema": {
                                "type": "string"
                            },
                        },
                    },
                    "security": [{
                        "oauth2": ["scope1", "scope2"]
                    }],
                }
            }
        },
        "swagger": "2.0",
    }
Example #3
0
def test_path_operations():
    app = Starlette()
    plugin = StarlettePlugin(app)
    spec = APISpec(title="Test API",
                   version="0.0.1",
                   openapi_version="2.0",
                   plugins=[plugin])
    spec.path(
        "/test",
        operations={"get": {
            "responses": {
                "200": {
                    "description": "ok"
                }
            }
        }})
    spec.path(
        "/test_overriden",
        operations={"get": {
            "responses": {
                "200": {
                    "description": "ok2"
                }
            }
        }},
    )

    @app.route("/test")
    def test_endpoint(request):
        pass  # pragma: no cover

    @app.route("/test_without_response")
    def test_endpoint_without_response(request):
        pass  # pragma: no cover

    @app.route("/test_overriden")
    def test_endpoint_with_overriden_response(request):
        """
        responses:
            200:
                description: "non ok"
        """
        pass  # pragma: no cover

    for endpoint in plugin.endpoints():
        spec.path(endpoint.path, endpoint=endpoint)

    assert spec.to_dict() == {
        "info": {
            "title": "Test API",
            "version": "0.0.1"
        },
        "paths": {
            "/test": {
                "get": {
                    "operationId": "get_test_endpoint",
                    "responses": {
                        "200": {
                            "description": "ok"
                        }
                    },
                }
            },
            "/test_overriden": {
                "get": {
                    "operationId": "get_test_endpoint_with_overriden_response",
                    "responses": {
                        "200": {
                            "description": "ok2"
                        }
                    },
                }
            },
            "/test_without_response": {
                "get": {
                    "operationId": "get_test_endpoint_without_response"
                }
            },
        },
        "swagger": "2.0",
    }