Exemplo n.º 1
0
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"
                }
            })
        ])
Exemplo n.º 2
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")
Exemplo n.º 3
0
def test_headers_open_api_3(assert_parameters, make_openapi_3_schema):
    # It is possible to omit "type" in the "schema" keyword
    header = {"in": "header", "name": "id", "required": True, "schema": {}}
    schema = make_openapi_3_schema(parameters=[header])
    # Schemathesis enforces `type=string` for headers
    assert_parameters(schema,
                      ParameterSet([OpenAPI30Parameter(definition=header)]),
                      [{
                          "type": "string"
                      }], "headers")
Exemplo n.º 4
0
def test_make_endpoint_single():
    endpoint = LINK.make_endpoint([ParsedData({"path.user_id": 1, "query.user_id": 2, "code": 7})])
    assert endpoint.path_parameters == ParameterSet(
        [OpenAPI30Parameter({"in": "path", "name": "user_id", "schema": {"enum": [1]}})]
    )
    for item in endpoint.query:
        schema = item.definition["schema"]
        if item.name == "code":
            assert schema == {"enum": [7]}
        elif item.name == "user_id":
            assert schema == {"enum": [2]}
        else:
            assert schema == {"type": "integer"}
Exemplo n.º 5
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()
Exemplo n.º 6
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
Exemplo n.º 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
Exemplo n.º 8
0
import schemathesis
from schemathesis.models import APIOperation, Case, OperationDefinition
from schemathesis.parameters import ParameterSet
from schemathesis.specs.openapi.links import Link, get_container
from schemathesis.specs.openapi.parameters import OpenAPI30Parameter
from schemathesis.stateful import ParsedData, Stateful

API_OPERATION = APIOperation(
    path="/users/{user_id}",
    method="get",
    definition=ANY,
    schema=ANY,
    base_url=ANY,
    path_parameters=ParameterSet(
        [
            OpenAPI30Parameter({"in": "path", "name": "user_id", "schema": {"type": "integer"}}),
        ]
    ),
    query=ParameterSet(
        [
            OpenAPI30Parameter({"in": "query", "name": "code", "schema": {"type": "integer"}}),
            OpenAPI30Parameter({"in": "query", "name": "user_id", "schema": {"type": "integer"}}),
            OpenAPI30Parameter({"in": "query", "name": "common", "schema": {"type": "integer"}}),
        ]
    ),
)
LINK = Link(
    name="GetUserByUserId",
    operation=API_OPERATION,
    parameters={"path.user_id": "$response.body#/id", "query.user_id": "$response.body#/id"},
)