diff options
-rw-r--r-- | tests/unit/auth/fixtures/group_fixtures.py | 52 | ||||
-rw-r--r-- | tests/unit/auth/fixtures/resource_fixtures.py | 30 | ||||
-rw-r--r-- | tests/unit/auth/fixtures/user_fixtures.py | 34 | ||||
-rw-r--r-- | tests/unit/auth/test_resources.py | 9 |
4 files changed, 96 insertions, 29 deletions
diff --git a/tests/unit/auth/fixtures/group_fixtures.py b/tests/unit/auth/fixtures/group_fixtures.py index c7775d9..8a4e79b 100644 --- a/tests/unit/auth/fixtures/group_fixtures.py +++ b/tests/unit/auth/fixtures/group_fixtures.py @@ -15,30 +15,42 @@ TEST_GROUP_02 = Group(uuid.UUID("e37d59d7-c05e-4d67-b479-81e627d8d634"), "AnotherTestGroup", {}) TEST_GROUPS = (TEST_GROUP_01, TEST_GROUP_02) +GROUP_CATEGORY = ResourceCategory( + uuid.UUID("1e0f70ee-add5-4358-8c6c-43de77fa4cce"), + "group", + "A group resource.") +GROUPS_AS_RESOURCES = tuple({ + "group_id": str(group.group_id), + "resource_id": str(uuid.uuid4()), + "resource_name": group.group_name, + "category_id": str(GROUP_CATEGORY.resource_category_id), + "public": "1" +} for group in TEST_GROUPS) + TEST_RESOURCES_GROUP_01 = ( - Resource(TEST_GROUPS[0], uuid.UUID("26ad1668-29f5-439d-b905-84d551f85955"), + Resource(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"), + Resource(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"), + Resource(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"), + Resource(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"), + Resource(uuid.UUID("04ad9e09-94ea-4390-8a02-11f92999806b"), "ResourceG02R02", ResourceCategory(uuid.UUID("fad071a3-2fc8-40b8-992b-cdefe7dcac79"), "mrna", "mRNA Dataset"), @@ -53,16 +65,32 @@ def __gtuple__(cursor): @pytest.fixture(scope="function") def fxtr_group(conn_after_auth_migrations):# pylint: disable=[redefined-outer-name] """Fixture: setup a test group.""" - query = "INSERT INTO groups(group_id, group_name) VALUES (?, ?)" with db.cursor(conn_after_auth_migrations) as cursor: cursor.executemany( - query, tuple( + "INSERT INTO groups(group_id, group_name) VALUES (?, ?)", + tuple( (str(group.group_id), group.group_name) for group in TEST_GROUPS)) + cursor.executemany( + "INSERT INTO resources " + "VALUES(:resource_id, :resource_name, :category_id, :public)", + GROUPS_AS_RESOURCES) + + cursor.executemany( + "INSERT INTO group_resources(resource_id, group_id) " + "VALUES(:resource_id, :group_id)", + GROUPS_AS_RESOURCES) + yield (conn_after_auth_migrations, TEST_GROUPS[0]) with db.cursor(conn_after_auth_migrations) as cursor: + resource_id_params = tuple( + (str(res["resource_id"]),) for res in GROUPS_AS_RESOURCES) + cursor.executemany("DELETE FROM group_resources WHERE resource_id=?", + resource_id_params) + cursor.executemany("DELETE FROM resources WHERE resource_id=?", + resource_id_params) cursor.executemany( "DELETE FROM groups WHERE group_id=?", ((str(group.group_id),) for group in TEST_GROUPS)) @@ -106,7 +134,6 @@ def fxtr_group_roles(fxtr_group, fxtr_roles):# pylint: disable=[redefined-outer- yield conn, groups, group_roles with db.cursor(conn) as cursor: - cursor.execute("SELECT * FROM group_user_roles_on_resources") cursor.executemany( ("DELETE FROM group_roles " "WHERE group_role_id=? AND group_id=? AND role_id=?"), @@ -127,21 +154,20 @@ def fxtr_group_user_roles(fxtr_resources, fxtr_group_roles, fxtr_users_in_group) for user in users if user.email == "group@mem.ber01") with db.cursor(conn) as cursor: params = tuple({ - "group_id": str(resource.group.group_id), "user_id": str(user.user_id), "role_id": str(role.role_id), "resource_id": str(resource.resource_id) } for user, role, resource in users_roles_resources) cursor.executemany( - ("INSERT INTO group_user_roles_on_resources " - "VALUES (:group_id, :user_id, :role_id, :resource_id)"), + ("INSERT INTO user_roles " + "VALUES (:user_id, :role_id, :resource_id)"), params) yield conn, group_users, group_roles, group_resources with db.cursor(conn) as cursor: cursor.executemany( - ("DELETE FROM group_user_roles_on_resources WHERE " - "group_id=:group_id AND user_id=:user_id AND role_id=:role_id AND " + ("DELETE FROM user_roles WHERE " + "user_id=:user_id AND role_id=:role_id AND " "resource_id=:resource_id"), params) diff --git a/tests/unit/auth/fixtures/resource_fixtures.py b/tests/unit/auth/fixtures/resource_fixtures.py index af22733..7f3c383 100644 --- a/tests/unit/auth/fixtures/resource_fixtures.py +++ b/tests/unit/auth/fixtures/resource_fixtures.py @@ -3,23 +3,43 @@ import pytest from gn_auth.auth.db import sqlite3 as db -from .group_fixtures import TEST_RESOURCES +from .group_fixtures import ( + TEST_RESOURCES, + TEST_GROUP_01, + TEST_GROUP_02, + TEST_RESOURCES_GROUP_01, + TEST_RESOURCES_GROUP_02) @pytest.fixture(scope="function") def fxtr_resources(fxtr_group):# pylint: disable=[redefined-outer-name] """fixture: setup test resources in the database""" conn, _group = fxtr_group + ownership = tuple({ + "group_id": str(TEST_GROUP_01.group_id), + "resource_id": str(res.resource_id) + } for res in TEST_RESOURCES_GROUP_01) + tuple({ + "group_id": str(TEST_GROUP_02.group_id), + "resource_id": str(res.resource_id) + } for res in TEST_RESOURCES_GROUP_02) + with db.cursor(conn) as cursor: cursor.executemany( - "INSERT INTO resources VALUES (?,?,?,?,?)", - ((str(res.group.group_id), str(res.resource_id), res.resource_name, + "INSERT INTO resources VALUES (?,?,?,?)", + ((str(res.resource_id), res.resource_name, str(res.resource_category.resource_category_id), 1 if res.public else 0) for res in TEST_RESOURCES)) + cursor.executemany( + "INSERT INTO resource_ownership(group_id, resource_id) " + "VALUES (:group_id, :resource_id)", + ownership) yield (conn, TEST_RESOURCES) with db.cursor(conn) as cursor: cursor.executemany( - "DELETE FROM resources WHERE group_id=? AND resource_id=?", - ((str(res.group.group_id), str(res.resource_id),) + "DELETE FROM resource_ownership " + "WHERE group_id=:group_id AND resource_id=:resource_id", + ownership) + cursor.executemany("DELETE FROM resources WHERE resource_id=?", + ((str(res.resource_id),) for res in TEST_RESOURCES)) diff --git a/tests/unit/auth/fixtures/user_fixtures.py b/tests/unit/auth/fixtures/user_fixtures.py index 531a321..b88d78a 100644 --- a/tests/unit/auth/fixtures/user_fixtures.py +++ b/tests/unit/auth/fixtures/user_fixtures.py @@ -6,6 +6,8 @@ import pytest from gn_auth.auth.db import sqlite3 as db from gn_auth.auth.authentication.users import User, hash_password +from .group_fixtures import TEST_GROUP_01 + TEST_USERS = ( User(uuid.UUID("ecb52977-3004-469e-9428-2a1856725c7f"), "group@lead.er", "Group Leader"), @@ -17,19 +19,35 @@ TEST_USERS = ( "unaff@iliated.user", "Unaffiliated User")) @pytest.fixture(scope="function") -def fxtr_users(conn_after_auth_migrations):# pylint: disable=[redefined-outer-name] +def fxtr_users(conn_after_auth_migrations, fxtr_group):# pylint: disable=[redefined-outer-name, unused-argument] """Fixture: setup test users.""" query = "INSERT INTO users(user_id, email, name) VALUES (?, ?, ?)" - query_user_roles = "INSERT INTO user_roles(user_id, role_id) VALUES (?, ?)" - test_user_roles = ( - ("ecb52977-3004-469e-9428-2a1856725c7f", - "a0e67630-d502-4b9f-b23f-6805d0f30e30"), - ("ecb52977-3004-469e-9428-2a1856725c7f", - "ade7e6b0-ba9c-4b51-87d0-2af7fe39a347")) with db.cursor(conn_after_auth_migrations) as cursor: cursor.executemany(query, ( (str(user.user_id), user.email, user.name) for user in TEST_USERS)) - cursor.executemany(query_user_roles, test_user_roles) + # setup user roles + cursor.execute("SELECT * FROM group_resources") + g01res_id = { + row["group_id"]: row["resource_id"] + for row in cursor.fetchall() + }[str(TEST_GROUP_01.group_id)] + cursor.execute("SELECT * FROM resources WHERE resource_name='GeneNetwork System'") + sysres_id = cursor.fetchone()["resource_id"] + test_user_roles = ( + { + "user_id": "ecb52977-3004-469e-9428-2a1856725c7f", + "role_id": "a0e67630-d502-4b9f-b23f-6805d0f30e30",# group-leader + "resource_id": g01res_id + }, + { + "user_id": "ecb52977-3004-469e-9428-2a1856725c7f", + "role_id": "ade7e6b0-ba9c-4b51-87d0-2af7fe39a347",# group-creator + "resource_id": sysres_id + }) + cursor.executemany( + "INSERT INTO user_roles(user_id, role_id, resource_id) " + "VALUES (:user_id, :role_id, :resource_id)", + test_user_roles) yield (conn_after_auth_migrations, TEST_USERS) diff --git a/tests/unit/auth/test_resources.py b/tests/unit/auth/test_resources.py index 7018e73..57ccc1d 100644 --- a/tests/unit/auth/test_resources.py +++ b/tests/unit/auth/test_resources.py @@ -28,7 +28,7 @@ create_resource_failure = { tuple(zip( conftest.TEST_USERS[0:1], (Resource( - group, uuid.UUID("d32611e3-07fc-4564-b56c-786c6db6de2b"), + uuid.UUID("d32611e3-07fc-4564-b56c-786c6db6de2b"), "test_resource", resource_category, False),)))) def test_create_resource(mocker, fxtr_users_in_group, user, expected): """Test that resource creation works as expected.""" @@ -43,11 +43,14 @@ def test_create_resource(mocker, fxtr_users_in_group, user, expected): with db.cursor(conn) as cursor: # Cleanup cursor.execute( - "DELETE FROM group_user_roles_on_resources WHERE resource_id=?", + "DELETE FROM user_roles WHERE resource_id=?", + (str(resource.resource_id),)) + cursor.execute( + "DELETE FROM resource_ownership WHERE resource_id=?", (str(resource.resource_id),)) cursor.execute( "DELETE FROM group_roles WHERE group_id=?", - (str(resource.group.group_id),)) + (str(group.group_id),)) cursor.execute( "DELETE FROM resources WHERE resource_id=?", (str(resource.resource_id),)) |