Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/update admin decorator #1190

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions fence/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from fence.user import get_current_user
from fence.utils import clear_cookies
from fence.config import config
from fence.authz.auth import check_arborist_auth

logger = get_logger(__name__)

Expand Down Expand Up @@ -275,25 +276,9 @@ def get_user_from_claims(claims):
)


def admin_required(f):
"""
Require user to be an admin user.
"""

@wraps(f)
def wrapper(*args, **kwargs):
if not flask.g.user:
raise Unauthorized("Require login")
if flask.g.user.is_admin is not True:
raise Unauthorized("Require admin user")
return f(*args, **kwargs)

return wrapper


def admin_login_required(function):
"""Compose the login required and admin required decorators."""
return login_required({"admin"})(admin_required(function))
"""Use the check_arborist_auth decorator checking on admin authorization."""
return check_arborist_auth(["/services/fence/admin"], "*")(function)


def _update_users_email(user, email):
Expand Down
14 changes: 13 additions & 1 deletion tests/admin/test_admin_users_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

@pytest.fixture(autouse=True)
def mock_arborist(mock_arborist_requests):
mock_arborist_requests()
mock_arborist_requests({"arborist/auth/request": {"POST": ({"auth": True}, 200)}})


# TODO: Not yet tested: PUT,DELETE /users/<username>/projects
Expand Down Expand Up @@ -186,6 +186,18 @@ def test_get_user_username(
assert r.json["username"] == "test_a"


def test_get_user_username_no_admin_auth(
client, encoded_admin_jwt, mock_arborist_requests
):
"""GET /users/<username>: [get_user]: rainy path where arborist authorization check fails"""
mock_arborist_requests({"arborist/auth/request": {"POST": ({"auth": False}, 200)}})
r = client.get(
"/admin/users/test_a", headers={"Authorization": "Bearer " + encoded_admin_jwt}
)
assert r.status_code == 403
assert "user does not have privileges to access this endpoint" in r.text


def test_get_user_long_username(
client, admin_user, encoded_admin_jwt, db_session, test_user_long
):
Expand Down
Loading