def test_build_integer_valued_param():
    graph = create_object_graph(name="example", testing=True)
    ns = Namespace(
        subject=Person,
        version="v1",
        identifier_type="int",
    )
    configure_crud(graph, ns, PERSON_MAPPINGS)

    with graph.flask.test_request_context():
        operations = list(iter_endpoints(graph, match_function))
        swagger_schema = build_swagger(graph, ns, operations)

        assert_that(
            build_path_for_integer_param(ns, Operation.Update,
                                         set(["person_id"])),
            equal_to("/api/v1/person/{person_id}"),
        )

    assert_that(
        swagger_schema,
        has_entries(paths=has_entries(
            **{
                "/person/{person_id}":
                has_entries(patch=has_entries(parameters=has_items({
                    "required":
                    True,
                    "type":
                    "integer",
                    "name":
                    "person_id",
                    "in":
                    "path",
                }), ), ),
            }, ), ))
Beispiel #2
0
    def find_matching_endpoints(self, discovery_ns):
        """
        Compute current matching endpoints.

        Evaluated as a property to defer evaluation.

        """
        def match_func(operation, ns, rule):
            return operation in self.matching_operations

        return list(iter_endpoints(self.graph, match_func))
Beispiel #3
0
    def find_matching_endpoints(self, swagger_ns):
        """
        Compute current matching endpoints.

        Evaluated as a property to defer evaluation.

        """
        def match_func(operation, ns, rule):
            # only expose endpoints that have the correct path prefix and operation
            return (rule.rule.startswith(make_path(self.graph,
                                                   swagger_ns.path))
                    and operation in self.matching_operations)

        return list(iter_endpoints(self.graph, match_func))
    def find_matching_endpoints(self, swagger_ns):
        """
        Compute current matching endpoints.

        Evaluated as a property to defer evaluation.

        """
        def match_func(operation, ns, rule):
            # only expose endpoints that have the correct path prefix and operation
            return (
                rule.rule.startswith(self.graph.build_route_path(swagger_ns.path, swagger_ns.prefix)) and
                operation in self.matching_operations
            )

        return list(iter_endpoints(self.graph, match_func))
Beispiel #5
0
    def get_swagger_versions():
        """
        Finds all swagger conventions that are bound to the graph

        """
        versions = []

        def matches(operation, ns, rule):
            """
            Defines a condition to determine which endpoints are swagger type

            """
            if (ns.subject == graph.config.swagger_convention.name):
                return True
            return False

        for operation, ns, rule, func in iter_endpoints(graph, matches):
            versions.append(ns.version)

        return versions
    def get_swagger_versions():
        """
        Finds all swagger conventions that are bound to the graph

        """
        versions = []

        def matches(operation, ns, rule):
            """
            Defines a condition to determine which endpoints are swagger type

            """
            if(ns.subject == graph.config.swagger_convention.name):
                return True
            return False

        for operation, ns, rule, func in iter_endpoints(graph, matches):
            versions.append(ns.version)

        return versions
def test_no_prefix_no_version_path():
    loader = load_from_dict(
        dict(
            # We want our routes to come directly after the root /
            build_route_path=dict(prefix=""), ))
    graph = create_object_graph(name="example", testing=True, loader=loader)

    ns = Namespace(
        subject=Person,
        version="",
    )
    configure_crud(graph, ns, PERSON_MAPPINGS)

    with graph.flask.test_request_context():
        operations = list(iter_endpoints(graph, match_function))
        swagger_schema = build_swagger(graph, ns, operations)

    # Test that in a no prefix, no version case we still get a leading slash in our paths
    assert_that("/person" in swagger_schema["paths"])
    assert_that(swagger_schema["basePath"], equal_to("/"))
def test_no_prefix_no_version_path():
    loader = load_from_dict(dict(
        # We want our routes to come directly after the root /
        build_route_path=dict(
            prefix=""
        ),
    ))
    graph = create_object_graph(name="example", testing=True, loader=loader)

    ns = Namespace(
        subject=Person,
        version="",
    )
    configure_crud(graph, ns, PERSON_MAPPINGS)

    with graph.flask.test_request_context():
        operations = list(iter_endpoints(graph, match_function))
        swagger_schema = build_swagger(graph, ns, operations)

    # Test that in a no prefix, no version case we still get a leading slash in our paths
    assert_that("/person" in swagger_schema["paths"])
    assert_that(swagger_schema["basePath"], equal_to("/"))
Beispiel #9
0
def test_build_swagger():
    graph = create_object_graph(name="example", testing=True)
    ns = Namespace(
        subject=Person,
        version="v1",
    )
    configure_crud(graph, ns, PERSON_MAPPINGS)

    with graph.flask.test_request_context():
        operations = list(iter_endpoints(graph, match_function))
        swagger_schema = build_swagger(graph, ns, operations)

    assert_that(swagger_schema, is_(equal_to({
        "info": {
            "version": "v1",
            "title": "example",
        },
        "paths": {
            "/person": {
                "post": {
                    "tags": ["person"],
                    "responses": {
                        "default": {
                            "description": "An error occurred", "schema": {
                                "$ref": "#/definitions/Error",
                            }
                        },
                        "201": {
                            "description": "Create a new person",
                            "schema": {
                                "$ref": "#/definitions/Person",
                            },
                        },
                    },
                    "parameters": [
                        {
                            "in": "header",
                            "name": "X-Response-Skip-Null",
                            "required": False,
                            "type": "string",
                        },
                        {
                            "in": "body",
                            "name": "body",
                            "schema": {
                                "$ref": "#/definitions/NewPerson",
                            },
                        },
                    ],
                    "operationId": "create",
                },
            },
            "/person/{person_id}": {
                "patch": {
                    "tags": ["person"],
                    "responses": {
                        "default": {
                            "description": "An error occurred",
                            "schema": {
                                "$ref": "#/definitions/Error",
                            },
                        },
                        "200": {
                            "description": "Update some or all of a person by id",
                            "schema": {
                                "$ref": "#/definitions/Person",
                            },
                        },
                    },
                    "parameters": [
                        {
                            "required": False,
                            "type": "string",
                            "name": "X-Response-Skip-Null",
                            "in": "header",
                        },
                        {
                            "in": "body",
                            "name": "body",
                            "schema": {
                                "$ref": "#/definitions/UpdatePerson",
                            },
                        },
                        {
                            "required": True,
                            "type": "string",
                            "name": "person_id",
                            "in": "path",
                            "format": "uuid",
                        },
                    ],
                    "operationId": "update",
                },
            },
        },
        "produces": [
            "application/json",
        ],
        "definitions": {
            "NewPerson": {
                "required": [
                    "firstName",
                    "lastName",
                ],
                "type": "object",
                "properties": {
                    "lastName": {
                        "type": "string",
                    },
                    "firstName": {
                        "type": "string",
                    }
                }
            },
            "Person": {
                "required": [
                    "firstName",
                    "id",
                    "lastName",
                ],
                "type": "object",
                "properties": {
                    "lastName": {
                        "type": "string",
                    },
                    "_links": {
                        "type": "object"
                    },
                    "id": {
                        "type": "string",
                        "format": "uuid",
                    },
                    "firstName": {
                        "type": "string",
                    },
                },
            },
            "UpdatePerson": {
                "type": "object",
                "properties": {
                    "lastName": {
                        "type": "string",
                    },
                    "firstName": {
                        "type": "string",
                    }
                }
            },
            "ErrorContext": {
                "required": ["errors"],
                "type": "object",
                "properties": {
                    "errors": {
                        "items": {
                            "$ref": "#/definitions/SubError",
                        },
                        "type": "array",
                    },
                },
            },
            "SubError": {
                "required": ["message"],
                "type": "object",
                "properties": {
                    "message": {
                        "type": "string",
                    },
                },
            },
            "Error": {
                "required": [
                    "code",
                    "message",
                    "retryable",
                ],
                "type": "object",
                "properties": {
                    "message": {
                        "type": "string",
                        "default": "Unknown Error",
                    },
                    "code": {
                        "type": "integer",
                        "format": "int32",
                        "default": 500,
                    },
                    "context": {
                        "$ref": "#/definitions/ErrorContext",
                    },
                    "retryable": {
                        "type": "boolean",
                    },
                },
            },
        },
        "basePath": "/api/v1",
        "swagger": "2.0",
        "consumes": [
            "application/json",
        ],
    })))
def test_build_swagger():
    graph = create_object_graph(name="example", testing=True)
    ns = Namespace(
        subject=Person,
        version="v1",
    )
    configure_crud(graph, ns, PERSON_MAPPINGS)

    with graph.flask.test_request_context():
        operations = list(iter_endpoints(graph, match_function))
        swagger_schema = build_swagger(graph, ns, operations)

    assert_that(
        swagger_schema,
        has_entries(
            info={
                "version": "v1",
                "title": "example",
            },
            paths={
                "/person": {
                    "post": {
                        "tags": ["person"],
                        "responses": {
                            "default": {
                                "description": "An error occurred",
                                "schema": {
                                    "$ref": "#/definitions/Error",
                                }
                            },
                            "201": {
                                "description": "Create a new person",
                                "schema": {
                                    "$ref": "#/definitions/Person",
                                },
                            },
                        },
                        "parameters": [
                            {
                                "in": "header",
                                "name": "X-Response-Skip-Null",
                                "required": False,
                                "type": "string",
                            },
                            {
                                "in": "body",
                                "name": "body",
                                "schema": {
                                    "$ref": "#/definitions/NewPerson",
                                },
                            },
                        ],
                        "operationId":
                        "create",
                    },
                },
                "/person/{person_id}": {
                    "patch": {
                        "tags": ["person"],
                        "responses": {
                            "default": {
                                "description": "An error occurred",
                                "schema": {
                                    "$ref": "#/definitions/Error",
                                },
                            },
                            "200": {
                                "description":
                                "Update some or all of a person by id",
                                "schema": {
                                    "$ref": "#/definitions/Person",
                                },
                            },
                        },
                        "parameters": [
                            {
                                "required": False,
                                "type": "string",
                                "name": "X-Response-Skip-Null",
                                "in": "header",
                            },
                            {
                                "in": "body",
                                "name": "body",
                                "schema": {
                                    "$ref": "#/definitions/UpdatePerson",
                                },
                            },
                            {
                                "required": True,
                                "type": "string",
                                "name": "person_id",
                                "in": "path",
                                "format": "uuid",
                            },
                        ],
                        "operationId":
                        "update",
                    },
                },
            },
            produces=[
                "application/json",
            ],
            definitions=has_entries(
                NewPerson=has_entries(required=[
                    "firstName",
                    "lastName",
                ],
                                      type="object",
                                      properties={
                                          "email": {
                                              "type": "string",
                                              "format": "email",
                                          },
                                          "eyeColor": {
                                              "enum": [
                                                  "PURPLE",
                                                  "TEAL",
                                                  "RUBY",
                                              ],
                                              "format": "enum",
                                              "type": "string",
                                          },
                                          "lastName": {
                                              "type": "string",
                                          },
                                          "firstName": {
                                              "type": "string",
                                          }
                                      }),
                Person=has_entries(
                    required=[
                        "firstName",
                        "id",
                        "lastName",
                    ],
                    type="object",
                    properties={
                        "email": {
                            "type": "string",
                            "format": "email",
                        },
                        # Response-side enums are declared as strings
                        "eyeColor": {
                            "type": "string",
                        },
                        "lastName": {
                            "type": "string",
                        },
                        "_links": {
                            "type": "object"
                        },
                        "id": {
                            "type": "string",
                            "format": "uuid",
                        },
                        "firstName": {
                            "type": "string",
                        },
                    },
                ),
                PersonPubsubMessage=has_entries(
                    type="object",
                    properties={
                        "email": {
                            "format": "email",
                            "type": "string",
                        },
                        "firstName": {
                            "type": "string",
                        },
                    },
                    required=[
                        "firstName",
                    ],
                ),
                PersonFoo=has_entries(
                    type="object",
                    properties={
                        "email": {
                            "format": "email",
                            "type": "string",
                        },
                        "firstName": {
                            "type": "string",
                        },
                    },
                    required=[
                        "email",
                    ],
                ),
                UpdatePerson=dict(type="object",
                                  properties={
                                      "lastName": {
                                          "type": "string",
                                      },
                                      "firstName": {
                                          "type": "string",
                                      }
                                  }),
                ErrorContext=has_entries(
                    required=["errors"],
                    type="object",
                    properties={
                        "errors": {
                            "items": {
                                "$ref": "#/definitions/SubError",
                            },
                            "type": "array",
                        },
                    },
                ),
                SubError=has_entries(
                    required=["message"],
                    type="object",
                    properties={
                        "message": {
                            "type": "string",
                        },
                    },
                ),
                Error=has_entries(
                    required=[
                        "code",
                        "message",
                        "retryable",
                    ],
                    type="object",
                    properties={
                        "message": {
                            "type": "string",
                            "default": "Unknown Error",
                        },
                        "code": {
                            "type": "integer",
                            "format": "int32",
                            "default": 500,
                        },
                        "context": {
                            "$ref": "#/definitions/ErrorContext",
                        },
                        "retryable": {
                            "type": "boolean",
                        },
                    },
                ),
            ),
            basePath="/api/v1",
            swagger="2.0",
            consumes=[
                "application/json",
            ],
        ))