示例#1
0
def test_hidden_failure_app(request, factory_name, open_api_3):
    factory = request.getfixturevalue(factory_name)
    app = factory(operations=("create_user", "get_user", "update_user"),
                  version=open_api_3)

    if factory_name == "asgi_app_factory":
        schema = schemathesis.from_asgi("/openapi.json", app=app)
        schema.add_link(
            source=schema["/users/"]["POST"],
            target=schema["/users/{user_id}"]["GET"],
            status_code="201",
            parameters={
                "path.user_id": "$response.body#/id",
                "query.uid": "$response.body#/id",
            },
        )
        schema.add_link(
            source=schema["/users/"]["POST"],
            target=schema["/users/{user_id}"]["PATCH"],
            status_code="201",
            parameters={"user_id": "$response.body#/id"},
        )
        schema.add_link(
            source=schema["/users/{user_id}"]["GET"],
            target="#/paths/~1users~1{user_id}/patch",
            status_code="200",
            parameters={"user_id": "$response.body#/id"},
            request_body={
                "first_name": "foo",
                "last_name": "bar"
            },
        )
    else:
        schema = schemathesis.from_wsgi("/schema.yaml", app=app)

    state_machine = schema.as_state_machine()

    with pytest.raises(CheckFailed,
                       match="Received a response with 5xx status code: 500"):
        run_state_machine_as_test(
            state_machine,
            settings=settings(
                max_examples=1000,
                deadline=None,
                suppress_health_check=HealthCheck.all(),
                stateful_step_count=3,
            ),
        )
示例#2
0
import schemathesis
from fastapi.testclient import TestClient
from schemathesis.checks import (
    not_a_server_error,
    content_type_conformance,
    response_headers_conformance,
    response_schema_conformance,
)
from hypothesis import settings, HealthCheck

from dispatch.main import app


schemathesis.fixups.install(["fast_api"])

schema = schemathesis.from_asgi("/api/v1/docs/openapi.json", app, base_url="/api/v1")


def before_generate_body(context, strategy):
    return strategy.filter(lambda value: "\\x00" not in str(value))


@pytest.fixture(scope="session")
def token():
    client = TestClient(app)
    response = client.post(
        "/api/v1/default/auth/register", json={"email": "*****@*****.**", "password": "******"}
    )
    assert response.status_code == 200
    return response.json()["token"]
from urllib.parse import unquote

from hypothesis import settings as hypsettings
from hypothesis import HealthCheck
import pytest
import schemathesis

from solarperformanceinsight_api.main import app

pytestmark = pytest.mark.usefixtures("add_example_db_data")
schemathesis.fixups.install()
schema = schemathesis.from_asgi("/openapi.json", app)


@pytest.fixture(params=[True, False])
def with_auth(request):
    return request.param


def before_generate_path_parameters(context, strategy):
    return strategy.filter(
        lambda x: not unquote(x.get("system_id", "a")).startswith("\n"))


@pytest.mark.usefixtures("nocommit_transaction")
@schema.hooks.apply(before_generate_path_parameters)
@schema.parametrize(method=["GET", "DELETE"])
def test_api(case, with_auth, auth_token):
    if with_auth:
        upd = {"Authorization": f"Bearer {auth_token}"}
        if case.headers is None: