~homeworkprod/byceps

166c2c80612196f682676dd53517c4857f5abc90 — Jochen Kupperschmidt 10 months ago 1e544cc
Introduce functions `has_current_user_[any_]permission`
M byceps/util/authorization.py => byceps/util/authorization.py +11 -1
@@ 9,7 9,7 @@ byceps.util.authorization
from enum import Enum
from typing import List, Set

from flask import current_app
from flask import current_app, g

from ..services.authorization import service as authorization_service
from ..typing import UserID


@@ 94,3 94,13 @@ class PermissionRegistry:


permission_registry = PermissionRegistry()


def has_current_user_permission(permission: Enum) -> bool:
    """Return `True` if the current user has this permission."""
    return permission in g.user.permissions


def has_current_user_any_permission(*permissions: Set[Enum]) -> bool:
    """Return `True` if the current user has any of these permissions."""
    return any(map(has_current_user_permission, permissions))

A tests/integration/util/__init__.py => tests/integration/util/__init__.py +0 -0
A tests/integration/util/test_authorization.py => tests/integration/util/test_authorization.py +104 -0
@@ 0,0 1,104 @@
"""
:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from enum import Enum
from typing import Set

from flask import g
import pytest

from byceps.util.authorization import create_permission_enum, PermissionRegistry
from byceps.util.authorization import (
    has_current_user_any_permission,
    has_current_user_permission,
)


ChillPermission = create_permission_enum(
    'chill', ['browse_the_web', 'play_videogames', 'watch_movies']
)


class CurrentUserMock:
    def __init__(self, permissions: Set[Enum]) -> None:
        self.permissions = permissions


@pytest.mark.parametrize(
    'permissions_assigned, permission_requested, expected',
    [
        (
            {},
            ChillPermission.browse_the_web,
            False,
        ),
        (
            {ChillPermission.watch_movies},
            ChillPermission.play_videogames,
            False,
        ),
        (
            {ChillPermission.watch_movies},
            ChillPermission.watch_movies,
            True,
        ),
        (
            {
                ChillPermission.browse_the_web,
                ChillPermission.play_videogames,
            },
            ChillPermission.watch_movies,
            False,
        ),
        (
            {
                ChillPermission.browse_the_web,
                ChillPermission.play_videogames,
            },
            ChillPermission.play_videogames,
            True,
        ),
    ],
)
def test_has_current_user_permission(
    site_app, permissions_assigned, permission_requested, expected
):
    g.user = CurrentUserMock(permissions_assigned)
    assert has_current_user_permission(permission_requested) == expected


@pytest.mark.parametrize(
    'permissions_assigned, permissions_requested, expected',
    [
        (
            {},
            {
                ChillPermission.browse_the_web,
            },
            False,
        ),
        (
            {ChillPermission.watch_movies},
            {
                ChillPermission.browse_the_web,
                ChillPermission.play_videogames,
            },
            False,
        ),
        (
            {ChillPermission.watch_movies},
            {
                ChillPermission.play_videogames,
                ChillPermission.watch_movies,
            },
            True,
        ),
    ],
)
def test_has_current_user_any_permission(
    site_app, permissions_assigned, permissions_requested, expected
):
    g.user = CurrentUserMock(permissions_assigned)
    assert has_current_user_any_permission(*permissions_requested) == expected