コード例 #1
0
def test_invalid_body_in_get_disable_validation(simple_schema):
    schema = schemathesis.from_dict(simple_schema, validate_schema=False)
    endpoint = Endpoint(
        path="/foo",
        method="GET",
        definition=EndpointDefinition({}, {}, "foo", []),
        schema=schema,
        body=PayloadAlternatives(
            [
                OpenAPI20Body(
                    {
                        "name": "attributes",
                        "in": "body",
                        "required": True,
                        "schema": {"required": ["foo"], "type": "object", "properties": {"foo": {"type": "string"}}},
                    },
                    media_type="application/json",
                )
            ]
        ),
    )
    strategy = get_case_strategy(endpoint)

    @given(strategy)
    @settings(max_examples=1)
    def test(case):
        assert case.body is not None

    test()
コード例 #2
0
def test_invalid_body_in_get(swagger_20):
    operation = APIOperation(
        path="/foo",
        method="GET",
        definition=OperationDefinition({}, {}, "foo", []),
        schema=swagger_20,
        body=PayloadAlternatives([
            OpenAPI20Body(
                {
                    "name": "attributes",
                    "in": "body",
                    "required": True,
                    "schema": {
                        "required": ["foo"],
                        "type": "object",
                        "properties": {
                            "foo": {
                                "type": "string"
                            }
                        }
                    },
                },
                media_type="application/json",
            )
        ]),
    )
    with pytest.raises(
            InvalidSchema,
            match=r"^Body parameters are defined for GET request.$"):
        get_case_strategy(operation).example()
コード例 #3
0
def test_resolving_multiple_files():
    raw_schema = {
        "swagger": "2.0",
        "info": {"title": "Example API", "description": "An API to test Schemathesis", "version": "1.0.0"},
        "host": "127.0.0.1:8888",
        "basePath": "/api",
        "schemes": ["http"],
        "produces": ["application/json"],
        "paths": {
            "/teapot": {
                "post": {
                    "parameters": [
                        {
                            "schema": {"$ref": "test/data/petstore_v2.yaml#/definitions/User"},
                            "in": "body",
                            "name": "user",
                            "required": True,
                        }
                    ],
                    "responses": {"200": {"description": "OK"}},
                }
            }
        },
    }
    schema = schemathesis.from_dict(raw_schema)
    assert schema["/teapot"]["post"].body == PayloadAlternatives(
        [
            OpenAPI20Body(
                {
                    "schema": {
                        "type": "object",
                        "properties": {
                            "id": {"type": "integer", "format": "int64"},
                            "username": {"type": "string"},
                            "firstName": {"type": "string"},
                            "lastName": {"type": "string"},
                            "email": {"type": "string"},
                            "password": {"type": "string"},
                            "phone": {"type": "string"},
                            "userStatus": {"type": "integer", "format": "int32", "description": "User Status"},
                        },
                        "xml": {"name": "User"},
                    },
                    "in": "body",
                    "name": "user",
                    "required": True,
                },
                media_type="application/json",
            )
        ]
    )
コード例 #4
0
def test_default_strategies_bytes(swagger_20):
    endpoint = make_endpoint(
        swagger_20,
        body=PayloadAlternatives(
            [
                OpenAPI20Body(
                    {"in": "body", "name": "byte", "required": True, "schema": {"type": "string", "format": "byte"}},
                    media_type="text/plain",
                )
            ]
        ),
    )
    result = get_case_strategy(endpoint).example()
    assert isinstance(result.body, str)
    b64decode(result.body)
コード例 #5
0
def test_payload_open_api_2(
    consumes,
    assert_parameters,
    make_openapi_2_schema,
    open_api_2_user_form_with_file_parameters,
    open_api_2_user_in_body,
    user_jsonschema,
):
    # A single "body" parameter is used for all payload variants
    schema = make_openapi_2_schema(consumes, [open_api_2_user_in_body])
    assert_parameters(
        schema,
        PayloadAlternatives([
            OpenAPI20Body(definition=open_api_2_user_in_body, media_type=value)
            for value in consumes
        ]),
        # For each one the schema is extracted from the parameter definition and transformed to the proper JSON Schema
        [user_jsonschema] * len(consumes),
    )
コード例 #6
0
    "properties": {
        "foo": {
            "type": "string"
        }
    }
}


@pytest.mark.parametrize(
    "body",
    (
        OpenAPI20Body(
            {
                "name": "attributes",
                "in": "body",
                "required": True,
                "schema": BODY_SCHEMA,
            },
            media_type="application/json",
        ),
        OpenAPI30Body(definition={"schema": BODY_SCHEMA},
                      media_type="application/json",
                      required=True),
    ),
)
def test_make_operation_body(body):
    # See GH-1069
    # When `requestBody` is present in the link definition
    # And in the target operation
    operation = APIOperation(
        path="/users/",