diff options
author | Frederick Muriuki Muriithi | 2023-02-10 14:07:41 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2023-02-10 14:07:41 +0300 |
commit | a93b9599d6e342f8fd588022ca14336465f7ff7c (patch) | |
tree | 63caea29caf15f723341341026706a4601b0a7b3 | |
parent | 0d2a9c130531a2786b2dd01adc172e5005f13f59 (diff) | |
download | genenetwork3-a93b9599d6e342f8fd588022ca14336465f7ff7c.tar.gz |
Return empty tuple rather than Nothing
When user has no roles assigned, return an empty tuple rather than
pymonad.maybe.Nothing to ease maintenance.
-rw-r--r-- | gn3/auth/authorisation/roles/models.py | 11 | ||||
-rw-r--r-- | gn3/auth/authorisation/roles/views.py | 2 | ||||
-rw-r--r-- | gn3/auth/authorisation/users/views.py | 4 | ||||
-rw-r--r-- | tests/unit/auth/test_roles.py | 2 |
4 files changed, 7 insertions, 12 deletions
diff --git a/gn3/auth/authorisation/roles/models.py b/gn3/auth/authorisation/roles/models.py index 26b8f0a..bb7ea73 100644 --- a/gn3/auth/authorisation/roles/models.py +++ b/gn3/auth/authorisation/roles/models.py @@ -3,7 +3,6 @@ from uuid import UUID, uuid4 from functools import reduce from typing import Any, Sequence, Iterable, NamedTuple -from pymonad.maybe import Just, Maybe, Nothing from pymonad.either import Left, Right, Either from gn3.auth import db @@ -80,7 +79,7 @@ def __organise_privileges__(roles_dict, privilege_row): privilege_row["privilege_description"]),)) } -def user_roles(conn: db.DbConnection, user: User) -> Maybe[Sequence[Role]]: +def user_roles(conn: db.DbConnection, user: User) -> Sequence[Role]: """Retrieve non-resource roles assigned to the user.""" with db.cursor(conn) as cursor: cursor.execute( @@ -90,11 +89,9 @@ def user_roles(conn: db.DbConnection, user: User) -> Maybe[Sequence[Role]]: "ON rp.privilege_id=p.privilege_id WHERE ur.user_id=?", (str(user.user_id),)) - results = cursor.fetchall() - if results: - return Just(tuple( - reduce(__organise_privileges__, results, {}).values())) - return Nothing + return tuple( + reduce(__organise_privileges__, cursor.fetchall(), {}).values()) + return tuple() def user_role(conn: db.DbConnection, user: User, role_id: UUID) -> Either: """Retrieve a specific non-resource role assigned to the user.""" diff --git a/gn3/auth/authorisation/roles/views.py b/gn3/auth/authorisation/roles/views.py index 975fb19..3670aab 100644 --- a/gn3/auth/authorisation/roles/views.py +++ b/gn3/auth/authorisation/roles/views.py @@ -13,7 +13,7 @@ from ...authentication.oauth2.resource_server import require_oauth roles = Blueprint("roles", __name__) @roles.route("/view/<uuid:role_id>", methods=["GET"]) -@require_oauth("role") +@require_oauth("profile role") def view_role(role_id: uuid.UUID) -> Response: """Retrieve a user role with id `role_id`""" def __error__(exc: Exception): diff --git a/gn3/auth/authorisation/users/views.py b/gn3/auth/authorisation/users/views.py index 8a29fea..c592a3f 100644 --- a/gn3/auth/authorisation/users/views.py +++ b/gn3/auth/authorisation/users/views.py @@ -46,9 +46,7 @@ def user_roles() -> Response: with require_oauth.acquire("role") as token: with db.connection(current_app.config["AUTH_DB"]) as conn: return jsonify(tuple( - dictify(role) for role in - _user_roles(conn, token.user).maybe(# type: ignore[misc] - tuple(), lambda roles: roles))) + dictify(role) for role in _user_roles(conn, token.user))) def __email_valid__(email: str) -> Tuple[bool, Optional[str]]: """Validate the email address.""" diff --git a/tests/unit/auth/test_roles.py b/tests/unit/auth/test_roles.py index 30b7f43..0914b54 100644 --- a/tests/unit/auth/test_roles.py +++ b/tests/unit/auth/test_roles.py @@ -118,4 +118,4 @@ def test_user_roles(fxtr_group_user_roles, user, expected): THEN: return **ALL** the privileges attached to the user """ conn, *_others = fxtr_group_user_roles - assert user_roles(conn, user).maybe(tuple(), lambda rls: rls) == expected + assert user_roles(conn, user) == expected |