From 5269d0e9927a18f7266d53ecb67a81c7eadf70b9 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 12 Dec 2022 13:33:13 +0300 Subject: tests: Update fixtures to use for testing resources functions --- tests/unit/auth/fixtures/group_fixtures.py | 104 ++++++++++++++++++++++++++--- 1 file changed, 93 insertions(+), 11 deletions(-) (limited to 'tests/unit/auth/fixtures/group_fixtures.py') diff --git a/tests/unit/auth/fixtures/group_fixtures.py b/tests/unit/auth/fixtures/group_fixtures.py index a106ef4..d17d5cb 100644 --- a/tests/unit/auth/fixtures/group_fixtures.py +++ b/tests/unit/auth/fixtures/group_fixtures.py @@ -4,11 +4,46 @@ import uuid import pytest from gn3.auth import db -from gn3.auth.authorisation.groups import Group +from gn3.auth.authorisation.groups import Group, GroupRole +from gn3.auth.authorisation.resources import Resource, ResourceCategory -TEST_GROUPS = ( - Group(uuid.UUID("9988c21d-f02f-4d45-8966-22c968ac2fbf"), "TheTestGroup"), - Group(uuid.UUID("e37d59d7-c05e-4d67-b479-81e627d8d634"), "TheTestGroup")) +TEST_GROUP_01 = Group(uuid.UUID("9988c21d-f02f-4d45-8966-22c968ac2fbf"), + "TheTestGroup") +TEST_GROUP_02 = Group(uuid.UUID("e37d59d7-c05e-4d67-b479-81e627d8d634"), + "AnotherTestGroup") +TEST_GROUPS = (TEST_GROUP_01, TEST_GROUP_02) + +TEST_RESOURCES_GROUP_01 = ( + Resource(TEST_GROUPS[0], uuid.UUID("26ad1668-29f5-439d-b905-84d551f85955"), + "ResourceG01R01", + ResourceCategory(uuid.UUID("48056f84-a2a6-41ac-8319-0e1e212cba2a"), + "genotype", "Genotype Dataset"), + True), + Resource(TEST_GROUPS[0], uuid.UUID("2130aec0-fefd-434d-92fd-9ca342348b2d"), + "ResourceG01R02", + ResourceCategory(uuid.UUID("548d684b-d4d1-46fb-a6d3-51a56b7da1b3"), + "phenotype", "Phenotype (Publish) Dataset"), + False), + Resource(TEST_GROUPS[0], uuid.UUID("e9a1184a-e8b4-49fb-b713-8d9cbeea5b83"), + "ResourceG01R03", + ResourceCategory(uuid.UUID("fad071a3-2fc8-40b8-992b-cdefe7dcac79"), + "mrna", "mRNA Dataset"), + False)) + +TEST_RESOURCES_GROUP_02 = ( + Resource(TEST_GROUPS[1], uuid.UUID("14496a1c-c234-49a2-978c-8859ea274054"), + "ResourceG02R01", + ResourceCategory(uuid.UUID("48056f84-a2a6-41ac-8319-0e1e212cba2a"), + "genotype", "Genotype Dataset"), + False), + Resource(TEST_GROUPS[1], uuid.UUID("04ad9e09-94ea-4390-8a02-11f92999806b"), + "ResourceG02R02", + ResourceCategory(uuid.UUID("fad071a3-2fc8-40b8-992b-cdefe7dcac79"), + "mrna", "mRNA Dataset"), + True)) + +TEST_RESOURCES = TEST_RESOURCES_GROUP_01 + TEST_RESOURCES_GROUP_02 +TEST_RESOURCES_PUBLIC = (TEST_RESOURCES_GROUP_01[0], TEST_RESOURCES_GROUP_02[1]) @pytest.fixture(scope="function") def test_group(conn_after_auth_migrations):# pylint: disable=[redefined-outer-name] @@ -23,22 +58,69 @@ def test_group(conn_after_auth_migrations):# pylint: disable=[redefined-outer-na yield (conn_after_auth_migrations, TEST_GROUPS[0]) @pytest.fixture(scope="function") -def test_users_in_group(test_group, test_users):# pylint: disable=[redefined-outer-name] +def test_users_in_group(test_group, test_users):# pylint: disable=[redefined-outer-name, unused-argument] """Link the users to the groups.""" - conn = test_group[0] - group = test_group[1] - users = test_users[1] + conn, all_users = test_users + users = tuple( + user for user in all_users if user.email not in ("unaff@iliated.user",)) query_params = tuple( - (str(group.group_id), str(user.user_id)) for user in users - if user.email not in ("unaff@iliated.user",)) + (str(TEST_GROUP_01.group_id), str(user.user_id)) for user in users) with db.cursor(conn) as cursor: cursor.executemany( "INSERT INTO group_users(group_id, user_id) VALUES (?, ?)", query_params) - yield (conn, group, users) + yield (conn, TEST_GROUP_01, users) with db.cursor(conn) as cursor: cursor.executemany( "DELETE FROM group_users WHERE group_id=? AND user_id=?", query_params) + +@pytest.fixture(scope="function") +def fixture_group_roles(test_group):# pylint: disable=[redefined-outer-name] + """Link roles to group""" + from .role_fixtures import RESOURCE_EDITOR_ROLE, RESOURCE_READER_ROLE# pylint: disable=[import-outside-toplevel] + group_roles = ( + GroupRole(uuid.UUID("9c25efb2-b477-4918-a95c-9914770cbf4d"), + TEST_GROUP_01, RESOURCE_EDITOR_ROLE), + GroupRole(uuid.UUID("82aed039-fe2f-408c-ab1e-81cd1ba96630"), + TEST_GROUP_02, RESOURCE_READER_ROLE)) + conn, groups = test_group + with db.cursor(conn) as cursor: + cursor.executemany( + "INSERT INTO group_roles VALUES (?, ?, ?)", + ((str(role.group_role_id), str(role.group.group_id), + str(role.role.role_id)) + for role in group_roles)) + + yield conn, groups, group_roles + +@pytest.fixture(scope="function") +def fixture_group_user_roles(test_users_in_group, fixture_group_roles, fixture_resources):#pylint: disable=[redefined-outer-name,unused-argument] + """Assign roles to users.""" + from .role_fixtures import RESOURCE_EDITOR_ROLE # pylint: disable=[import-outside-toplevel] + conn, _groups, _group_roles = fixture_group_roles + _conn, _group, group_users = test_users_in_group + users = tuple(user for user in group_users if user.email + not in ("unaff@iliated.user", "group@lead.er")) + users_roles_resources = ( + (user, RESOURCE_EDITOR_ROLE, TEST_RESOURCES_GROUP_01[1]) + for user in users if user.email == "group@mem.ber01") + with db.cursor(conn) as cursor: + cursor.executemany( + ("INSERT INTO group_user_roles_on_resources VALUES (?, ?, ?, ?)"), + ((str(TEST_GROUP_01.group_id), str(user.user_id), str(role.role_id), + str(resource.resource_id)) + for user, role, resource in users_roles_resources)) + + yield conn + + with db.cursor(conn) as cursor: + cursor.executemany( + ("DELETE FROM group_user_roles_on_resources WHERE " + "group_id=? AND user_id=? AND role_id=? AND " + "resource_id=?"), + ((str(TEST_GROUP_01.group_id), str(user.user_id), str(role.role_id), + str(resource.resource_id)) + for user, role, resource in users_roles_resources)) -- cgit v1.2.3