Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_response_conformance_invalid(args):
app, kwargs = args
# When endpoint returns a response that doesn't conform to the schema
# And "response_schema_conformance" is specified
results = execute(**kwargs, checks=(response_schema_conformance,), hypothesis_options={"max_examples": 1})
# Then there should be a failure
assert results.has_failures
lines = results.results[0].checks[-1].message.split("\n")
assert lines[0] == "The received response does not conform to the defined schema!"
assert lines[2] == "Details: "
assert lines[4] == "'success' is a required property"
def test_auth(args):
app, kwargs = args
# When auth is specified in `api_options` as a tuple of 2 strings
execute(**kwargs, api_options={"auth": ("test", "test")})
# Then each request should contain corresponding basic auth header
assert_incoming_requests_num(app, 3)
headers = {"Authorization": "Basic dGVzdDp0ZXN0"}
assert_request(app, 0, "GET", "/api/failure", headers)
assert_request(app, 1, "GET", "/api/failure", headers)
assert_request(app, 2, "GET", "/api/success", headers)
def test_execute_base_url_found(base_url, schema_url, app):
# When base_url is specified
execute(schema_url, loader_options={"base_url": base_url})
# Then it should be used by the runner
assert_incoming_requests_num(app, 3)
def test_unknown_content_type(args):
app, kwargs = args
# When endpoint returns a response with content type, not specified in "produces"
# And "content_type_conformance" is specified
results = execute(**kwargs, checks=(content_type_conformance,), hypothesis_options={"max_examples": 1})
# Then there should be a failure
assert results.has_failures
check = results.results[0].checks[0]
assert check.name == "content_type_conformance"
assert check.value == Status.failure
def test_unknown_response_code_with_default(args):
app, kwargs = args
# When endpoint returns a status code, that is not listed in "responses", but there is a "default" response
# And "status_code_conformance" is specified
results = execute(**kwargs, checks=(status_code_conformance,), hypothesis_options={"max_examples": 1})
# Then there should be no failure
assert not results.has_failures
check = results.results[0].checks[0]
assert check.name == "status_code_conformance"
assert check.value == Status.success
def test_hypothesis_deadline(args):
app, kwargs = args
# When `deadline` is passed in `hypothesis_options` in the `execute` call
execute(**kwargs, hypothesis_options={"deadline": 500})
assert_incoming_requests_num(app, 1)
assert_request(app, 0, "GET", "/api/slow")
def test_invalid_path_parameter(args):
app, kwargs = args
results = execute(**kwargs)
assert results.has_errors
error, _ = results.results[0].errors[0]
assert isinstance(error, InvalidSchema)
assert str(error) == "Missing required property `required: true`"
def test_flaky_exceptions(args, mocker):
app, kwargs = args
# GH: #236
error_idx = 0
def flaky(*args, **kwargs):
nonlocal error_idx
exception_class = [ValueError, TypeError, ZeroDivisionError, KeyError][error_idx % 4]
error_idx += 1
raise exception_class
# When there are many different exceptions during the test
# And Hypothesis consider this test as a flaky one
mocker.patch("schemathesis.Case.call", side_effect=flaky)
mocker.patch("schemathesis.Case.call_wsgi", side_effect=flaky)
results = execute(**kwargs, hypothesis_options={"max_examples": 3, "derandomize": True})
# Then the execution result should indicate errors
assert results.has_errors
assert results.results[0].errors[0][0].args[0].startswith("Tests on this endpoint produce unreliable results:")
def test_base_url(base_url, schema_url, app, converter):
base_url = converter(base_url)
# When `base_url` is specified explicitly with or without trailing slash
execute(schema_url, loader_options={"base_url": base_url})
# Then each request should reach the app in both cases
assert_incoming_requests_num(app, 3)
assert_request(app, 0, "GET", "/api/failure")
assert_request(app, 1, "GET", "/api/failure")
assert_request(app, 2, "GET", "/api/success")