def test_get_operation_via_remote_reference(openapi_version, schema_url):
    schema = schemathesis.from_uri(schema_url)
    resolved = schema.get_operation_by_reference(
        f"{schema_url}#/paths/~1users~1{{user_id}}/patch")
    assert isinstance(resolved, APIOperation)
    assert resolved.path == "/users/{user_id}"
    assert resolved.method.upper() == "PATCH"
    # Via common parameters for all methods
    if openapi_version.is_openapi_2:
        assert resolved.query == ParameterSet([
            OpenAPI20Parameter({
                "in": "query",
                "name": "common",
                "required": True,
                "type": "integer"
            })
        ])
    if openapi_version.is_openapi_3:
        assert resolved.query == ParameterSet([
            OpenAPI30Parameter({
                "in": "query",
                "name": "common",
                "required": True,
                "schema": {
                    "type": "integer"
                }
            })
        ])
Exemple #2
0
def test_get_examples(name, swagger_20):
    if name == "body":
        # In Open API 2.0, the `body` parameter has a name, which is ignored
        # But we'd like to use this object as a payload; therefore, we put one extra level of nesting
        example = expected = {"name": "John"}
        media_type = "application/json"
        cls = PayloadAlternatives
    else:
        example = "John"
        expected = {"name": example}
        media_type = None  # there is no payload
        cls = ParameterSet
    endpoint = make_endpoint(
        swagger_20,
        **{
            name: cls(
                [
                    OpenAPI20Parameter(
                        {
                            "in": name,
                            "name": "name",
                            "required": True,
                            "type": "string",
                            "x-example": example,
                        }
                    )
                ]
            )
        },
    )
    strategies = endpoint.get_strategies_from_examples()
    assert len(strategies) == 1
    assert strategies[0].example() == Case(endpoint, media_type=media_type, **{name: expected})
Exemple #3
0
def test_headers_open_api_2(assert_parameters, make_openapi_2_schema):
    header = {"in": "header", "name": "id", "required": True, "type": "string"}
    schema = make_openapi_2_schema([], [header])
    assert_parameters(schema,
                      ParameterSet([OpenAPI20Parameter(definition=header)]),
                      [{
                          "type": "string"
                      }], "headers")
Exemple #4
0
def test_valid_headers(openapi2_base_url, swagger_20, definition):
    endpoint = Endpoint(
        "/api/success",
        "GET",
        definition=EndpointDefinition({}, {}, "foo", []),
        schema=swagger_20,
        base_url=openapi2_base_url,
        headers=ParameterSet([OpenAPI20Parameter(definition)]),
    )

    @given(case=get_case_strategy(endpoint))
    @settings(suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow], deadline=None, max_examples=10)
    def inner(case):
        case.call()
Exemple #5
0
def test_custom_strategies(swagger_20):
    register_string_format("even_4_digits", st.from_regex(r"\A[0-9]{4}\Z").filter(lambda x: int(x) % 2 == 0))
    endpoint = make_endpoint(
        swagger_20,
        query=ParameterSet(
            [
                OpenAPI20Parameter(
                    {"name": "id", "in": "query", "required": True, "type": "string", "format": "even_4_digits"}
                )
            ]
        ),
    )
    result = get_case_strategy(endpoint).example()
    assert len(result.query["id"]) == 4
    assert int(result.query["id"]) % 2 == 0
Exemple #6
0
def test_forms_open_api_2(consumes, assert_parameters, make_openapi_2_schema,
                          user_jsonschema, open_api_2_user_form_parameters):
    # In Open API 2.0, forms are separate "formData" parameters
    schema = make_openapi_2_schema(consumes, open_api_2_user_form_parameters)
    assert_parameters(
        schema,
        PayloadAlternatives([
            # They are represented as a single "composite" body for each media type
            OpenAPI20CompositeBody(
                definition=[
                    OpenAPI20Parameter(parameter)
                    for parameter in open_api_2_user_form_parameters
                ],
                media_type=value,
            ) for value in consumes
        ]),
        # Each converted schema should correspond to the default User schema.
        [user_jsonschema] * len(consumes),
    )
Exemple #7
0
def test_no_body_in_get(swagger_20):
    operation = APIOperation(
        path="/api/success",
        method="GET",
        definition=OperationDefinition({}, {}, "foo", []),
        schema=swagger_20,
        query=ParameterSet([
            OpenAPI20Parameter({
                "required": True,
                "in": "query",
                "type": "string",
                "name": "key",
                "x-example": "John",
            })
        ]),
    )
    strategies = operation.get_strategies_from_examples()
    assert len(strategies) == 1
    assert strategies[0].example().body is NOT_SET
Exemple #8
0
def test_multipart_form_open_api_2(
    consumes,
    assert_parameters,
    make_openapi_2_schema,
    user_jsonschema_with_file,
    open_api_2_user_form_with_file_parameters,
):
    # Multipart forms are represented as a list of "formData" parameters
    schema = make_openapi_2_schema(consumes,
                                   open_api_2_user_form_with_file_parameters)
    assert_parameters(
        schema,
        PayloadAlternatives([
            # Is represented with a "composite" body
            OpenAPI20CompositeBody(
                definition=[
                    OpenAPI20Parameter(parameter)
                    for parameter in open_api_2_user_form_with_file_parameters
                ],
                media_type="multipart/form-data",
            )
        ]),
        [user_jsonschema_with_file],
    )
Exemple #9
0
            {
                "definition": {
                    "schema": {
                        "type": "string"
                    }
                },
                "description": DESCRIPTION,
                "media_type": "application/json",
            },
            DESCRIPTION,
        ),
        (
            OpenAPI20CompositeBody,
            {
                "definition": [
                    OpenAPI20Parameter({
                        "in": "formData",
                        "name": "foo",
                        "type": "string"
                    })
                ],
                "media_type":
                "application/x-www-form-urlencoded",
            },
            None,
        ),
    ),
)
def test_description(cls, kwargs, expected):
    assert cls(**kwargs).description == expected