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(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()
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_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_default_strategies_binary(swagger_20): endpoint = make_endpoint( swagger_20, form_data={ "required": ["file"], "type": "object", "additionalProperties": False, "properties": {"file": {"type": "string", "format": "binary"}}, }, ) result = get_case_strategy(endpoint).example() assert isinstance(result.form_data["file"], bytes)
def test_default_strategies_bytes(swagger_20): endpoint = make_endpoint( swagger_20, body={ "required": ["byte"], "type": "object", "additionalProperties": False, "properties": {"byte": {"type": "string", "format": "byte"}}, }, ) result = get_case_strategy(endpoint).example() assert isinstance(result.body["byte"], str) b64decode(result.body["byte"])
def test_explicit_attributes(operation, values, expected): # When some Case's attribute is passed explicitly to the case strategy strategy = get_case_strategy(operation=operation, **values) @given(strategy) @settings(max_examples=1) def test(case): # Then it should appear in the final result for attr_name, expected_values in expected.items(): value = getattr(case, attr_name) assert value == expected_values test()
def test_explicit_attributes(endpoint, values): # When some Case's attribute is passed explicitly to the case strategy strategy = get_case_strategy(endpoint=endpoint, **values) @given(strategy) @settings(max_examples=1) def test(case): # Then it should be taken as is for attr_name, expected in values.items(): value = getattr(case, attr_name) assert value == expected test()
def test_custom_strategies(swagger_20): register_string_format("even_4_digits", strategies.from_regex(r"\A[0-9]{4}\Z").filter(lambda x: int(x) % 2 == 0)) endpoint = make_endpoint( swagger_20, query={ "required": ["id"], "type": "object", "additionalProperties": False, "properties": {"id": {"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
def test_default_strategies_bytes(swagger_20): endpoint = make_endpoint( swagger_20, body=PayloadAlternatives( [ OpenAPI20Body( {"in": "body", "name": "byte", "required": True, "schema": {"type": "string", "format": "byte"}}, media_type="text/plain", ) ] ), ) result = get_case_strategy(endpoint).example() assert isinstance(result.body, str) b64decode(result.body)
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
def test_default_strategies_binary(swagger_20): operation = make_operation( swagger_20, body=PayloadAlternatives([ OpenAPI20CompositeBody.from_parameters( { "name": "upfile", "in": "formData", "type": "file", "required": True, }, media_type="multipart/form-data", ) ]), ) result = get_case_strategy(operation).example() assert isinstance(result.body["upfile"], bytes)