Exemplo n.º 1
0
async def test_blueprint_renaming() -> None:
    app = Quart(__name__)

    bp = Blueprint("bp", __name__)
    bp2 = Blueprint("bp2", __name__)

    @bp.get("/")
    async def index() -> str:
        return request.endpoint

    @bp.get("/error")
    async def error() -> str:
        abort(403)

    @bp.errorhandler(403)
    async def forbidden(_: Exception) -> ResponseReturnValue:
        return "Error", 403

    @bp2.get("/")
    async def index2() -> str:
        return request.endpoint

    bp.register_blueprint(bp2, url_prefix="/a", name="sub")
    app.register_blueprint(bp, url_prefix="/a")
    app.register_blueprint(bp, url_prefix="/b", name="alt")

    client = app.test_client()

    assert (await (await client.get("/a/")).get_data()) == b"bp.index"  # type: ignore
    assert (await (await client.get("/b/")).get_data()) == b"alt.index"  # type: ignore
    assert (await (await client.get("/a/a/")).get_data()) == b"bp.sub.index2"  # type: ignore
    assert (await (await client.get("/b/a/")).get_data()) == b"alt.sub.index2"  # type: ignore
    assert (await (await client.get("/a/error")).get_data()) == b"Error"  # type: ignore
    assert (await (await client.get("/b/error")).get_data()) == b"Error"  # type: ignore
Exemplo n.º 2
0
async def test_nesting_and_sibling() -> None:
    app = Quart(__name__)

    parent = Blueprint("parent", __name__, url_prefix="/parent")
    child = Blueprint("child", __name__, url_prefix="/child")

    @child.route("/")
    def index() -> ResponseReturnValue:
        return "index"

    parent.register_blueprint(child)
    app.register_blueprint(parent)
    app.register_blueprint(child, url_prefix="/sibling")

    test_client = app.test_client()
    response = await test_client.get("/parent/child/")
    assert response.status_code == 200
    response = await test_client.get("/sibling/")
    assert response.status_code == 200
Exemplo n.º 3
0
async def test_nesting_url_prefixes(
    parent_init: Optional[str],
    child_init: Optional[str],
    parent_registration: Optional[str],
    child_registration: Optional[str],
) -> None:
    app = Quart(__name__)

    parent = Blueprint("parent", __name__, url_prefix=parent_init)
    child = Blueprint("child", __name__, url_prefix=child_init)

    @child.route("/")
    def index() -> ResponseReturnValue:
        return "index"

    parent.register_blueprint(child, url_prefix=child_registration)
    app.register_blueprint(parent, url_prefix=parent_registration)

    test_client = app.test_client()
    response = await test_client.get("/parent/child/")
    assert response.status_code == 200
Exemplo n.º 4
0
async def test_nested_blueprint() -> None:
    app = Quart(__name__)

    parent = Blueprint("parent", __name__, url_prefix="/parent")
    child = Blueprint("child", __name__)
    grandchild = Blueprint("grandchild", __name__)
    sibling = Blueprint("sibling", __name__)

    @parent.errorhandler(403)
    async def forbidden(_: Exception) -> ResponseReturnValue:
        return "Parent no", 403

    @parent.route("/")
    async def parent_index() -> ResponseReturnValue:
        return "Parent yes"

    @parent.route("/no")
    async def parent_no() -> ResponseReturnValue:
        abort(403)

    @child.route("/")
    async def child_index() -> ResponseReturnValue:
        return "Child yes"

    @child.route("/no")
    async def child_no() -> ResponseReturnValue:
        abort(403)

    @grandchild.errorhandler(403)
    async def grandchild_forbidden(_: Exception) -> ResponseReturnValue:
        return "Grandchild no", 403

    @grandchild.route("/")
    async def grandchild_index() -> ResponseReturnValue:
        return "Grandchild yes"

    @grandchild.route("/no")
    async def grandchild_no() -> ResponseReturnValue:
        abort(403)

    @sibling.route("/sibling")
    async def sibling_index() -> ResponseReturnValue:
        return "Sibling yes"

    child.register_blueprint(grandchild, url_prefix="/grandchild")
    parent.register_blueprint(child, url_prefix="/child")
    parent.register_blueprint(sibling)
    app.register_blueprint(parent)
    app.register_blueprint(parent, url_prefix="/alt", name="alt")

    client = app.test_client()

    assert (await (await client.get("/parent/")).get_data()) == b"Parent yes"  # type: ignore
    assert (await (await client.get("/parent/child/")).get_data()) == b"Child yes"  # type: ignore
    assert (await (await client.get("/parent/sibling")).get_data()) == b"Sibling yes"  # type: ignore  # noqa: E501
    assert (await (await client.get("/alt/sibling")).get_data()) == b"Sibling yes"  # type: ignore
    assert (await (await client.get("/parent/child/grandchild/")).get_data()) == b"Grandchild yes"  # type: ignore  # noqa: E501
    assert (await (await client.get("/parent/no")).get_data()) == b"Parent no"  # type: ignore
    assert (await (await client.get("/parent/child/no")).get_data()) == b"Parent no"  # type: ignore
    assert (await (await client.get("/parent/child/grandchild/no")).get_data()) == b"Grandchild no"  # type: ignore  # noqa: E501
Exemplo n.º 5
0
async def test_nested_callback_order() -> None:
    app = Quart(__name__)

    parent = Blueprint("parent", __name__)
    child = Blueprint("child", __name__)

    @app.before_request
    async def app_before1() -> None:
        g.setdefault("seen", []).append("app_1")

    @app.teardown_request
    async def app_teardown1(exc: Optional[BaseException] = None) -> None:
        assert g.seen.pop() == "app_1"

    @app.before_request
    async def app_before2() -> None:
        g.setdefault("seen", []).append("app_2")

    @app.teardown_request
    async def app_teardown2(exc: Optional[BaseException] = None) -> None:
        assert g.seen.pop() == "app_2"

    @app.context_processor
    async def app_ctx() -> dict:
        return dict(key="app")

    @parent.before_request
    async def parent_before1() -> None:
        g.setdefault("seen", []).append("parent_1")

    @parent.teardown_request
    async def parent_teardown1(exc: Optional[BaseException] = None) -> None:
        assert g.seen.pop() == "parent_1"

    @parent.before_request
    async def parent_before2() -> None:
        g.setdefault("seen", []).append("parent_2")

    @parent.teardown_request
    async def parent_teardown2(exc: Optional[BaseException] = None) -> None:
        assert g.seen.pop() == "parent_2"

    @parent.context_processor
    async def parent_ctx() -> dict:
        return dict(key="parent")

    @child.before_request
    async def child_before1() -> None:
        g.setdefault("seen", []).append("child_1")

    @child.teardown_request
    async def child_teardown1(exc: Optional[BaseException] = None) -> None:
        assert g.seen.pop() == "child_1"

    @child.before_request
    async def child_before2() -> None:
        g.setdefault("seen", []).append("child_2")

    @child.teardown_request
    async def child_teardown2(exc: Optional[BaseException] = None) -> None:
        assert g.seen.pop() == "child_2"

    @child.context_processor
    async def child_ctx() -> dict:
        return dict(key="child")

    @child.route("/a")
    async def a() -> str:
        return ", ".join(g.seen)

    @child.route("/b")
    async def b() -> str:
        return await render_template_string("{{ key }}")

    parent.register_blueprint(child)
    app.register_blueprint(parent)

    client = app.test_client()
    assert (
        await (await client.get("/a")).get_data()
    ) == b"app_1, app_2, parent_1, parent_2, child_1, child_2"  # type: ignore
    assert (await (await client.get("/b")).get_data()) == b"child"  # type: ignore
Exemplo n.º 6
0
def test_self_registration() -> None:
    bp = Blueprint("bp", __name__)

    with pytest.raises(ValueError):
        bp.register_blueprint(bp)