aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2022-12-08 06:22:23 +0300
committerFrederick Muriuki Muriithi2022-12-08 08:35:42 +0300
commit2be81e59ff416cb8764aaf041a3b8febae4d8875 (patch)
treebf1b8c76c817041fc627dbc8b319d65c69e3fefc
parentb939c1fc96398708accc84c3026ce9f48654d7a8 (diff)
downloadgenenetwork3-2be81e59ff416cb8764aaf041a3b8febae4d8875.tar.gz
auth: add function to retrieve a user's group
* gn3/auth/authorisation/groups.py: new `user_group` function * tests/unit/auth/test_groups.py: test `user_group` function
-rw-r--r--gn3/auth/authorisation/groups.py18
-rw-r--r--tests/unit/auth/test_groups.py21
2 files changed, 38 insertions, 1 deletions
diff --git a/gn3/auth/authorisation/groups.py b/gn3/auth/authorisation/groups.py
index 6496e87..743e812 100644
--- a/gn3/auth/authorisation/groups.py
+++ b/gn3/auth/authorisation/groups.py
@@ -106,3 +106,21 @@ def authenticated_user_group(conn) -> Maybe:
return Just(groups[0])
return Nothing
+
+def user_group(conn: db.DbConnection, user: User) -> Maybe:
+ """Returns the given user's group"""
+ with db.cursor(conn) as cursor:
+ cursor.execute(
+ ("SELECT groups.group_id, groups.group_name FROM group_users "
+ "INNER JOIN groups ON group_users.group_id=groups.group_id "
+ "WHERE group_users.user_id = ?"),
+ (str(user.user_id),))
+ groups = tuple(Group(UUID(row[0]), row[1]) for row in cursor.fetchall())
+
+ if len(groups) > 1:
+ raise MembershipError(user, groups)
+
+ if len(groups) == 1:
+ return Just(groups[0])
+
+ return Nothing
diff --git a/tests/unit/auth/test_groups.py b/tests/unit/auth/test_groups.py
index 22d90f4..79dc16f 100644
--- a/tests/unit/auth/test_groups.py
+++ b/tests/unit/auth/test_groups.py
@@ -2,13 +2,14 @@
from uuid import UUID
import pytest
+from pymonad.maybe import Nothing
from gn3.auth import db
from gn3.auth.authentication.users import User
from gn3.auth.authorisation.roles import Role
from gn3.auth.authorisation.privileges import Privilege
from gn3.auth.authorisation.groups import (
- Group, GroupRole, create_group, MembershipError, create_group_role)
+ Group, GroupRole, user_group, create_group, MembershipError, create_group_role)
from tests.unit.auth import conftest
@@ -98,3 +99,21 @@ def test_create_multiple_groups(mocker, test_app, test_users):
# subsequent attempts should fail
with pytest.raises(MembershipError):
create_group(conn, "another_test_group", user)
+
+@pytest.mark.unit_test
+@pytest.mark.parametrize(
+ "user,expected",
+ tuple(zip(
+ conftest.TEST_USERS,
+ (([Group(UUID("9988c21d-f02f-4d45-8966-22c968ac2fbf"), "TheTestGroup")] * 3)
+ + [Nothing]))))
+def test_user_group(test_users_in_group, user, expected):
+ """
+ GIVEN: A bunch of registered users, some of whom are members of a group, and
+ others are not
+ WHEN: a particular user's group is requested,
+ THEN: return a Maybe containing the group that the user belongs to, or
+ Nothing
+ """
+ conn, group, users = test_users_in_group
+ assert user_group(conn, user).maybe(Nothing, lambda val: val) == expected