Esempio n. 1
0
def test_invalid_body_in_get_disable_validation(simple_schema):
    schema = schemathesis.from_dict(simple_schema, validate_schema=False)
    operation = APIOperation(
        path="/foo",
        method="GET",
        definition=OperationDefinition({}, {}, "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(operation)

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

    test()
Esempio n. 2
0
def test_get_container_invalid_location():
    operation = APIOperation(
        path="/users/{user_id}",
        method="get",
        schema=None,
        definition=OperationDefinition(
            raw={},
            resolved={},
            scope="",
            parameters=[
                OpenAPI30Parameter({
                    "in": "query",
                    "name": "code",
                    "type": "integer"
                }),
                OpenAPI30Parameter({
                    "in": "query",
                    "name": "user_id",
                    "type": "integer"
                }),
                OpenAPI30Parameter({
                    "in": "query",
                    "name": "common",
                    "type": "integer"
                }),
            ],
        ),
    )
    case = operation.make_case()
    with pytest.raises(
            ValueError,
            match=
            "Parameter `unknown` is not defined in API operation `GET /users/{user_id}`"
    ):
        get_container(case, None, "unknown")
Esempio n. 3
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()
Esempio n. 4
0
def make_case(schema: BaseSchema, definition: Dict[str, Any]) -> models.Case:
    operation = models.APIOperation("/path",
                                    "GET",
                                    definition=OperationDefinition(
                                        definition, definition, None, []),
                                    schema=schema)
    return models.Case(operation)
Esempio n. 5
0
def test_valid_headers(openapi2_base_url, swagger_20, definition):
    operation = APIOperation(
        "/api/success",
        "GET",
        definition=OperationDefinition({}, {}, "foo", []),
        schema=swagger_20,
        base_url=openapi2_base_url,
        headers=ParameterSet([OpenAPI20Parameter(definition)]),
    )

    @given(case=get_case_strategy(operation))
    @settings(suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow], deadline=None, max_examples=10)
    def inner(case):
        case.call()
Esempio n. 6
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
Esempio n. 7
0
def make_operation(schema, **kwargs) -> APIOperation:
    return APIOperation("/users", "POST", definition=OperationDefinition({}, {}, "foo", []), schema=schema, **kwargs)