Exemplo n.º 1
0
def test_encoding_octet_stream(empty_open_api_3_schema, openapi3_base_url):
    # See: GH-1134
    # When the operation contains the `application/octet-stream` media type
    # And has no `format: binary` in its schema
    empty_open_api_3_schema["paths"] = {
        "/data": {
            "post": {
                "requestBody": {
                    "required": True,
                    "content": {
                        "application/octet-stream": {
                            "schema": {
                                "type": "string",
                            },
                        },
                    },
                },
                "responses": {"200": {"description": "OK"}},
            }
        }
    }
    schema = oas_loaders.from_dict(empty_open_api_3_schema, base_url=openapi3_base_url)
    initialized, before, after, finished = from_schema(schema).execute()
    # Then the test outcomes should not contain errors
    # And it should not lead to encoding errors
    assert after.status == Status.success
    assert not finished.has_failures
    assert not finished.has_errors
Exemplo n.º 2
0
def test_skip_operations_with_recursive_references(schema_with_recursive_references):
    # When the test schema contains recursive references
    schema = oas_loaders.from_dict(schema_with_recursive_references)
    *_, after, finished = from_schema(schema).execute()
    # Then it causes an error with a proper error message
    assert after.status == Status.error
    assert RECURSIVE_REFERENCE_ERROR_MESSAGE in after.result.errors[0].exception
Exemplo n.º 3
0
def test_force_open_api_version(version, expected):
    schema = {
        # Invalid schema, but it happens in real applications
        "swagger": "2.0",
        "openapi": "3.0.0",
    }
    loaded = loaders.from_dict(schema,
                               force_schema_version=version,
                               validate_schema=False)
    assert isinstance(loaded, expected)
Exemplo n.º 4
0
def test_hypothesis_errors_propagation(empty_open_api_3_schema, openapi3_base_url):
    # See: GH-1046
    # When the operation contains a media type, that Schemathesis can't serialize
    # And there is still a supported media type
    empty_open_api_3_schema["paths"] = {
        "/data": {
            "post": {
                "requestBody": {
                    "required": True,
                    "content": {
                        # This one is known
                        "application/json": {
                            "schema": {
                                "type": "array",
                            },
                        },
                        # This one is not
                        "application/xml": {
                            "schema": {
                                "type": "array",
                            }
                        },
                    },
                },
                "responses": {"200": {"description": "OK"}},
            }
        }
    }

    max_examples = 10
    schema = oas_loaders.from_dict(empty_open_api_3_schema, base_url=openapi3_base_url)
    initialized, before, after, finished = from_schema(
        schema, hypothesis_settings=hypothesis.settings(max_examples=max_examples, deadline=None)
    ).execute()
    # Then the test outcomes should not contain errors
    assert after.status == Status.success
    # And there should be requested amount of test examples
    assert len(after.result.checks) == max_examples
    assert not finished.has_failures
    assert not finished.has_errors
Exemplo n.º 5
0
def test_unsatisfiable_example(empty_open_api_3_schema):
    # See GH-904
    # When filling missing properties during examples generation leads to unsatisfiable schemas
    empty_open_api_3_schema["paths"] = {
        "/success": {
            "post": {
                "parameters": [
                    # This parameter is not satisfiable
                    {
                        "name": "key",
                        "in": "query",
                        "required": True,
                        "schema": {"type": "integer", "minimum": 5, "maximum": 4},
                    }
                ],
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "foo": {"type": "string", "example": "foo example string"},
                                },
                            },
                        }
                    }
                },
                "responses": {"200": {"description": "OK"}},
            }
        }
    }
    # Then the testing process should not raise an internal error
    schema = oas_loaders.from_dict(empty_open_api_3_schema)
    *_, after, finished = from_schema(
        schema, hypothesis_settings=hypothesis.settings(max_examples=1, deadline=None)
    ).execute()
    # And the tests are failing because of the unsatisfiable schema
    assert finished.has_errors
    assert "Unable to satisfy schema parameters for this API operation" in after.result.errors[0].exception
Exemplo n.º 6
0
def test_unsupported_type():
    # When Schemathesis can't detect the Open API spec version
    with pytest.raises(ValueError, match="^Unsupported schema type$"):
        # Then it raises an error
        loaders.from_dict({})