1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
"""Fixtures and utilities for group-related tests"""
import uuid
import pytest
from gn_auth.auth.db import sqlite3 as db
from gn_auth.auth.authorisation.resources.groups import Group
from gn_auth.auth.authorisation.resources import Resource, ResourceCategory
from .role_fixtures import RESOURCE_EDITOR_ROLE
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)
SYSTEM_CATEGORY = ResourceCategory(
uuid.UUID("aa3d787f-af6a-44fa-9b0b-c82d40e54ad2"),
"system",
"The overall system.")
SYSTEM_RESOURCE = Resource(
uuid.UUID("0248b289-b277-4eaa-8c94-88a434d14b6e"),
"GeneNetwork System",
SYSTEM_CATEGORY,
True)
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": res_id,
"resource_name": group.group_name,
"category_id": str(GROUP_CATEGORY.resource_category_id),
"public": "0"
} for res_id, group in zip(
("38d1807d-105f-44a7-8327-7e2d973b6d8d",
"89458ef6-e090-4b53-8c2c-59eaf2785f11"),
TEST_GROUPS))
GROUP_RESOURCES = tuple(
Resource(uuid.UUID(row["resource_id"]),
row["resource_name"],
GROUP_CATEGORY,
False)
for row in GROUPS_AS_RESOURCES)
TEST_RESOURCES_GROUP_01 = (
Resource(uuid.UUID("26ad1668-29f5-439d-b905-84d551f85955"),
"ResourceG01R01",
ResourceCategory(uuid.UUID("48056f84-a2a6-41ac-8319-0e1e212cba2a"),
"genotype", "Genotype Dataset"),
True),
Resource(uuid.UUID("2130aec0-fefd-434d-92fd-9ca342348b2d"),
"ResourceG01R02",
ResourceCategory(uuid.UUID("548d684b-d4d1-46fb-a6d3-51a56b7da1b3"),
"phenotype", "Phenotype (Publish) Dataset"),
False),
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(uuid.UUID("14496a1c-c234-49a2-978c-8859ea274054"),
"ResourceG02R01",
ResourceCategory(uuid.UUID("48056f84-a2a6-41ac-8319-0e1e212cba2a"),
"genotype", "Genotype Dataset"),
False),
Resource(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 = (
SYSTEM_RESOURCE, TEST_RESOURCES_GROUP_01[0], TEST_RESOURCES_GROUP_02[1])
def __gtuple__(cursor):
return tuple(dict(row) for row in cursor.fetchall())
@pytest.fixture(scope="function")
def fxtr_group(conn_after_auth_migrations):# pylint: disable=[redefined-outer-name]
"""Fixture: setup a test group."""
with db.cursor(conn_after_auth_migrations) as cursor:
cursor.executemany(
"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))
@pytest.fixture(scope="function")
def fxtr_users_in_group(fxtr_group, fxtr_users):# pylint: disable=[redefined-outer-name, unused-argument]
"""Link the users to the groups."""
conn, all_users = fxtr_users
users = tuple(
user for user in all_users if user.email not in ("unaff@iliated.user",))
query_params = tuple(
(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, 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 fxtr_group_user_roles(fxtr_users_in_group, fxtr_resources, fxtr_resource_roles):#pylint: disable=[redefined-outer-name,unused-argument]
"""Assign roles to users."""
_conn, _group, group_users = fxtr_users_in_group
_conn, group_resources = fxtr_resources
conn, _groups, resource_roles = fxtr_resource_roles
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:
params = tuple({
"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 user_roles "
"VALUES (:user_id, :role_id, :resource_id)"),
params)
yield conn, group_users, resource_roles, group_resources
with db.cursor(conn) as cursor:
cursor.executemany(
("DELETE FROM user_roles WHERE "
"user_id=:user_id AND role_id=:role_id AND "
"resource_id=:resource_id"),
params)
|