def make_case(schema: BaseSchema, definition: Dict[str, Any]) -> models.Case: endpoint = models.Endpoint("/path", "GET", definition=EndpointDefinition( definition, definition, None, []), schema=schema) return models.Case(endpoint)
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={ "properties": { "api_key": definition }, "additionalProperties": False, "type": "object", "required": ["api_key"], }, ) @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()
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()
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={ "required": ["foo"], "type": "object", "properties": { "foo": { "type": "string" } } }, ) strategy = get_case_strategy(endpoint) @given(strategy) @settings(max_examples=1) def test(case): assert case.body is not None test()
def test_get_container_invalid_location(): endpoint = Endpoint( path="/users/{user_id}", method="get", schema=None, definition=EndpointDefinition( raw={}, resolved={}, scope="", parameters=[ { "in": "query", "name": "code", "type": "integer" }, { "in": "query", "name": "user_id", "type": "integer" }, { "in": "query", "name": "common", "type": "integer" }, ], ), ) case = endpoint.make_case() with pytest.raises( ValueError, match= "Parameter `unknown` is not defined in endpoint `GET /users/{user_id}`" ): get_container(case, None, "unknown")
def f( path=None, method=None, definition=None, schema=None, app=None, base_url=None, path_parameters=None, headers=None, cookies=None, query=None, body=None, form_data=None, ) -> Endpoint: return Endpoint( path, method, EndpointDefinition(raw=definition, resolved=definition, scope="global"), schema, app, base_url, path_parameters, headers, cookies, query, body, form_data, )
def test_valid_headers(base_url, swagger_20): endpoint = Endpoint( "/api/success", "GET", definition=EndpointDefinition({}, {}, "foo"), schema=swagger_20, base_url=base_url, headers={ "properties": { "api_key": { "name": "api_key", "in": "header", "type": "string" } }, "additionalProperties": False, "type": "object", "required": ["api_key"], }, ) @given(case=get_case_strategy(endpoint)) @settings(suppress_health_check=[HealthCheck.filter_too_much], deadline=None) def inner(case): case.call()
def test_invalid_body_in_get(swagger_20): endpoint = Endpoint( path="/foo", method="GET", definition=EndpointDefinition({}, {}, "foo"), schema=swagger_20, body={"required": ["foo"], "type": "object", "properties": {"foo": {"type": "string"}}}, ) with pytest.raises(InvalidSchema, match=r"^Body parameters are defined for GET request.$"): get_case_strategy(endpoint)
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()
def test_no_body_in_get(swagger_20): endpoint = Endpoint( path="/api/success", method="GET", definition=EndpointDefinition({}, {}, "foo"), schema=swagger_20, query={ "required": ["name"], "type": "object", "additionalProperties": False, "properties": {"name": {"type": "string"}}, "example": {"name": "John"}, }, ) strategies = endpoint.get_strategies_from_examples() assert len(strategies) == 1 assert strategies[0].example().body is None
def test_invalid_body_in_get(swagger_20): endpoint = Endpoint( path="/foo", method="GET", definition=EndpointDefinition({}, {}, "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(endpoint).example()
def test_no_body_in_get(swagger_20): endpoint = Endpoint( path="/api/success", method="GET", definition=EndpointDefinition({}, {}, "foo", []), schema=swagger_20, query=ParameterSet( [ OpenAPI20Parameter( { "required": True, "in": "query", "type": "string", "name": "key", "x-example": "John", } ) ] ), ) strategies = endpoint.get_strategies_from_examples() assert len(strategies) == 1 assert strategies[0].example().body is NOT_SET
def make_endpoint(schema, **kwargs) -> Endpoint: return Endpoint("/users", "POST", definition=EndpointDefinition({}, {}, "foo", []), schema=schema, **kwargs)
def test_complex_dereference(testdir, complex_schema): schema = schemathesis.from_path(complex_schema) path = Path(str(testdir)) body = OpenAPI30Body( { "schema": { "additionalProperties": False, "description": "Test", "properties": { "profile": { "additionalProperties": False, "description": "Test", "properties": {"id": {"type": "integer"}}, "required": ["id"], "type": "object", }, "username": {"type": "string"}, }, "required": ["username", "profile"], "type": "object", } }, required=True, media_type="application/json", ) assert schema.endpoints["/teapot"]["POST"] == Endpoint( base_url="file:///", path="/teapot", method="post", definition=EndpointDefinition( { "requestBody": { "content": { "application/json": {"schema": {"$ref": "../schemas/teapot/create.yaml#/TeapotCreateRequest"}} }, "description": "Test.", "required": True, }, "responses": {"default": {"$ref": "../../common/responses.yaml#/DefaultError"}}, "summary": "Test", "tags": ["ancillaries"], }, { "requestBody": { "content": { "application/json": { "schema": { "additionalProperties": False, "description": "Test", "properties": { "profile": { "additionalProperties": False, "description": "Test", "properties": {"id": {"type": "integer"}}, "required": ["id"], "type": "object", }, "username": {"type": "string"}, }, "required": ["username", "profile"], "type": "object", } } }, "description": "Test.", "required": True, }, "responses": { "default": { "content": { "application/json": { "schema": { "additionalProperties": False, "properties": { "key": {"anyOf": [{"type": "string"}, {"type": "null"}]}, "referenced": {"anyOf": [{"type": "string"}, {"type": "null"}]}, }, "required": ["key", "referenced"], "type": "object", } } }, "description": "Probably an error", } }, "summary": "Test", "tags": ["ancillaries"], }, scope=f"{path.as_uri()}/root/paths/teapot.yaml#/TeapotCreatePath", parameters=[body], ), body=PayloadAlternatives([body]), schema=schema, )