コード例 #1
0
def test_permission_dependency_factory_wraps_callable_resource(mocker):
    mocker.patch("fastapi_permissions.Depends")

    from fastapi_permissions import Depends, permission_dependency_factory

    permission_dependency_factory(
        "view",
        dummy_resource_callable,
        "active_principals_func",
        "permisssion_exception",
    )

    assert Depends.call_count == 2
    assert Depends.call_args_list[0] == mocker.call(dummy_resource_callable)
コード例 #2
0
def test_permission_dependency_raises_exception(mocker):
    """ If a user dosen't have a permission, a exception should be raised """
    mocker.patch("fastapi_permissions.has_permission", return_value=False)
    mocker.patch("fastapi_permissions.Depends")

    from fastapi import HTTPException

    from fastapi_permissions import (
        Depends,
        permission_exception,
        permission_dependency_factory,
    )

    # since the resulting permission function is wrapped in Depends()
    # we need to extract it from the mock
    permission_func = permission_dependency_factory(
        "view",
        dummy_resource_callable,
        "active_principals_func",
        permission_exception,
    )
    assert Depends.call_count == 2
    args, kwargs = Depends.call_args_list[1]
    permission_func = args[0]

    with pytest.raises(HTTPException):
        permission_func()
コード例 #3
0
def test_permission_dependency_returns_requested_resource(mocker):
    """ If a user has a permission, the resource should be returned """
    mocker.patch("fastapi_permissions.has_permission", return_value=True)
    mocker.patch("fastapi_permissions.Depends")

    from fastapi_permissions import Depends, permission_dependency_factory

    # since the resulting permission function is wrapped in Depends()
    # we need to extract it from the mock
    permission_dependency_factory(
        "view",
        dummy_resource_callable,
        "active_principals_func",
        "permisssion_exception",
    )
    assert Depends.call_count == 2
    args, kwargs = Depends.call_args_list[1]
    permission_func = args[0]

    result = permission_func()
    assert result == Depends(dummy_resource_callable)
コード例 #4
0
def test_permission_dependency_factory_returns_correct_signature(mocker):
    mocker.patch("fastapi_permissions.Depends")

    from fastapi_permissions import Depends, permission_dependency_factory

    permission_func = permission_dependency_factory(
        "view",
        dummy_resource_callable,
        "active_principals_func",
        "permisssion_exception",
    )

    assert Depends.call_count == 2
    args, kwargs = Depends.call_args_list[1]
    permission_func = args[0]
    assert callable(permission_func)

    parameters = inspect.signature(permission_func).parameters
    print(parameters)
    assert len(parameters) == 2
    assert parameters["resource"].default == Depends(dummy_resource_callable)
    assert parameters["principals"].default == "active_principals_func"