def test_configure_permissions_wraps_principal_callable(mocker):
    """ test if active_principle_funcs parameter is wrapped in "Depends" """

    mocker.patch("fastapi_permissions.Depends")

    from fastapi_permissions import Depends, configure_permissions

    configure_permissions(dummy_principal_callable)

    assert Depends.call_count == 1
    assert Depends.call_args == mocker.call(dummy_principal_callable)
def test_configure_permissions_parameters(mocker):
    """ test the configuration options of configure_permissions """

    mocker.patch("fastapi_permissions.Depends")

    from fastapi_permissions import configure_permissions

    partial_func = configure_permissions(
        dummy_principal_callable, permission_exception="exception option")
    parameters = inspect.signature(partial_func).parameters

    assert parameters["permission_exception"].default == "exception option"
def test_configure_permissions_returns_correct_signature(mocker):
    """ check the return value signature of configure_permissions """

    mocker.patch("fastapi_permissions.Depends")

    from fastapi_permissions import (
        Depends,
        permission_exception,
        configure_permissions,
        permission_dependency_factory,
    )

    partial_func = configure_permissions(dummy_principal_callable)
    parameters = inspect.signature(partial_func).parameters

    assert partial_func.func == permission_dependency_factory
    assert len(parameters) == 4
    assert parameters["permission"].default == inspect.Parameter.empty
    assert parameters["resource"].default == inspect.Parameter.empty
    assert parameters["active_principals_func"].default == Depends(
        dummy_principal_callable)
    assert parameters["permission_exception"].default == permission_exception
Beispiel #4
0
    db_session.add(user)
    db_session.commit()
    return user


def get_current_user(*,
                     db_session: Session = Depends(get_db),
                     request: Request) -> DispatchUser:
    """Attempts to get the current user depending on the configured authentication provider."""
    if DISPATCH_AUTHENTICATION_PROVIDER_SLUG:
        auth_plugin = plugins.get(DISPATCH_AUTHENTICATION_PROVIDER_SLUG)
        user_email = auth_plugin.get_current_user(request)
    else:
        log.debug("No authentication provider. Default user will be used")
        user_email = DISPATCH_AUTHENTICATION_DEFAULT_USER

    return get_or_create(db_session=db_session,
                         user_in=UserRegister(email=user_email))


def get_active_principals(user: DispatchUser = Depends(
    get_current_user)) -> List[str]:
    """Fetches the current participants for a given user."""
    principals = [Authenticated]
    principals.extend(getattr(user, "principals", []))
    return principals


Permission = configure_permissions(get_active_principals)