-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_group.py
More file actions
289 lines (241 loc) · 10.4 KB
/
test_group.py
File metadata and controls
289 lines (241 loc) · 10.4 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
test_group.py
Tests that the Group class methods work as expected.
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch
Last updated: 08/12/2025
"""
import pytest
from unittest.mock import Mock, patch
import cs3.rpc.v1beta1.code_pb2 as cs3code
import cs3.identity.group.v1beta1.group_api_pb2 as cs3ig
import cs3.identity.group.v1beta1.resources_pb2 as cs3igr
from cs3client.group import Group
from cs3client.exceptions import (
AuthenticationException,
NotFoundException,
UnknownException,
)
from .fixtures import ( # noqa: F401 (they are used, the framework is not detecting it)
mock_config,
mock_logger,
mock_gateway,
mock_status_code_handler,
)
@pytest.fixture
def group_instance(mock_config, mock_logger, mock_gateway, mock_status_code_handler): # noqa: F811
"""
Fixture for creating a Group instance with mocked dependencies.
"""
from cs3client.group import Group
return Group(mock_config, mock_logger, mock_gateway, mock_status_code_handler)
# Test cases for the Group class
@pytest.mark.parametrize(
"status_code, status_message, expected_exception, group_data",
[
(cs3code.CODE_OK, None, None, Mock(id=Mock(idp="idp", opaque_id="opaque_id"))),
(cs3code.CODE_NOT_FOUND, "error", NotFoundException, None),
(-2, "error", UnknownException, None),
],
)
def test_get_group(
group_instance, status_code, status_message, expected_exception, group_data # noqa: F811 (not a redefinition)
):
idp = "idp"
opaque_id = "opaque_id"
mock_response = Mock()
mock_response.status.code = status_code
mock_response.status.message = status_message
mock_response.group = group_data
auth_token = ('x-access-token', "some_token")
with patch.object(group_instance._gateway, "GetGroup", return_value=mock_response):
if expected_exception:
with pytest.raises(expected_exception):
group_instance.get_group(auth_token, opaque_id, idp)
else:
result = group_instance.get_group(auth_token, opaque_id, idp)
assert result == group_data
@pytest.mark.parametrize(
"status_code, status_message, expected_exception, has_member",
[
(cs3code.CODE_OK, None, None, True),
(cs3code.CODE_OK, None, None, False),
(cs3code.CODE_NOT_FOUND, "error", NotFoundException, None),
(-2, "error", UnknownException, None),
],
)
def test_has_member(
group_instance, status_code, status_message, expected_exception, has_member # noqa: F811 (not a redefinition)
):
group_opaque_id = "group_opaque_id"
user_opaque_id = "user_opaque_id"
user_idp = "user_idp"
mock_response = Mock()
mock_response.status.code = status_code
mock_response.status.message = status_message
mock_response.ok = has_member
auth_token = ('x-access-token', "some_token")
with patch.object(group_instance._gateway, "HasMember", return_value=mock_response):
if expected_exception:
with pytest.raises(expected_exception):
group_instance.has_member(auth_token, group_opaque_id, user_opaque_id, user_idp)
else:
result = group_instance.has_member(auth_token, group_opaque_id, user_opaque_id, user_idp)
assert result == has_member
@pytest.mark.parametrize(
"status_code, status_message, expected_exception, groups",
[
(cs3code.CODE_OK, None, None, ["member1", "member2"]),
(cs3code.CODE_NOT_FOUND, "error", NotFoundException, None),
(-2, "error", UnknownException, None),
],
)
def test_get_members(
group_instance, status_code, status_message, expected_exception, groups # noqa: F811 (not a redefinition)
):
idp = "idp"
opaque_id = "opaque_id"
mock_response = Mock()
mock_response.status.code = status_code
mock_response.status.message = status_message
mock_response.groups = groups
auth_token = ('x-access-token', "some_token")
with patch.object(group_instance._gateway, "GetUserGroups", return_value=mock_response):
if expected_exception:
with pytest.raises(expected_exception):
group_instance.get_members(auth_token, opaque_id, idp)
else:
result = group_instance.get_members(auth_token, opaque_id, idp)
assert result == groups
@pytest.mark.parametrize(
"status_code, status_message, expected_exception, groups",
[
(cs3code.CODE_OK, None, None, [Mock(), Mock()]),
(cs3code.CODE_NOT_FOUND, "error", NotFoundException, None),
(cs3code.CODE_UNAUTHENTICATED, "error", AuthenticationException, None),
(-2, "error", UnknownException, None),
],
)
def test_find_groups(
group_instance, status_code, status_message, expected_exception, groups # noqa: F811 (not a redefinition)
):
filters = [
cs3ig.Filter(
type=cs3igr.GroupType.GROUP_TYPE_FEDERATED,
)
]
mock_response = Mock()
mock_response.status.code = status_code
mock_response.status.message = status_message
mock_response.groups = groups
auth_token = ('x-access-token', "some_token")
with patch.object(group_instance._gateway, "FindGroups", return_value=mock_response):
if expected_exception:
with pytest.raises(expected_exception):
group_instance.find_groups(auth_token, filters)
else:
result = group_instance.find_groups(auth_token, filters)
assert result == groups
@pytest.mark.parametrize(
"status_code, status_message, expected_exception, group_data",
[
(cs3code.CODE_OK, None, None, Mock(idp="idp", opaque_id="opaque_id")),
(cs3code.CODE_NOT_FOUND, "error", NotFoundException, None),
(-2, "error", UnknownException, None),
],
)
def test_get_group_by_claim(
group_instance, status_code, status_message, expected_exception, group_data # noqa: F811 (not a redefinition)
):
claim = "claim"
value = "value"
mock_response = Mock()
mock_response.status.code = status_code
mock_response.status.message = status_message
mock_response.group = group_data
auth_token = ('x-access-token', "some_token")
with patch.object(group_instance._gateway, "GetGroupByClaim", return_value=mock_response):
if expected_exception:
with pytest.raises(expected_exception):
group_instance.get_group_by_claim(auth_token, claim, value)
else:
result = group_instance.get_group_by_claim(auth_token, claim, value)
assert result == group_data
@pytest.mark.parametrize(
"filter_type, query, group_type, expected_exception",
[
("TYPE_QUERY", "test_group", None, None),
("TYPE_GROUPTYPE", None, "GROUP_TYPE_FEDERATED", None),
("TYPE_GROUPTYPE", None, "GROUP_TYPE_REGULAR", None),
("TYPE_GROUPTYPE", None, None, ValueError),
("TYPE_INVALID", "test", None, ValueError),
],
)
def test_create_find_group_filter(filter_type, query, group_type, expected_exception):
"""Test the create_find_group_filter classmethod."""
if expected_exception:
with pytest.raises(expected_exception):
Group.create_find_group_filter(filter_type, query, group_type)
else:
result = Group.create_find_group_filter(filter_type, query, group_type)
assert result is not None
assert isinstance(result, cs3ig.Filter)
@pytest.mark.parametrize(
"status_code, status_message, expected_exception, groups, filter_type, query, group_type",
[
(cs3code.CODE_OK, None, None, [Mock(), Mock()], "TYPE_QUERY", "test_group", None),
(cs3code.CODE_OK, None, None, [Mock()], "TYPE_GROUPTYPE", None, "GROUP_TYPE_FEDERATED"),
(cs3code.CODE_OK, None, None, [Mock()], "TYPE_GROUPTYPE", None, "GROUP_TYPE_REGULAR"),
(cs3code.CODE_NOT_FOUND, "error", NotFoundException, None, "TYPE_QUERY", "nonexistent", None),
(cs3code.CODE_UNAUTHENTICATED, "error", AuthenticationException, None, "TYPE_QUERY", "test", None),
(-2, "error", UnknownException, None, "TYPE_GROUPTYPE", None, "GROUP_TYPE_REGULAR"),
],
)
def test_find_groups_with_filter_creation(
group_instance, status_code, status_message, expected_exception, groups, filter_type, query, group_type # noqa: F811 (not a redefinition)
):
"""Test find_groups using the create_find_group_filter classmethod."""
# Create filter using the classmethod
group_filter = Group.create_find_group_filter(filter_type, query, group_type)
filters = [group_filter]
mock_response = Mock()
mock_response.status.code = status_code
mock_response.status.message = status_message
mock_response.groups = groups
auth_token = ('x-access-token', "some_token")
with patch.object(group_instance._gateway, "FindGroups", return_value=mock_response):
if expected_exception:
with pytest.raises(expected_exception):
group_instance.find_groups(auth_token, filters)
else:
result = group_instance.find_groups(auth_token, filters)
assert result == groups
@pytest.mark.parametrize(
"status_code, status_message, expected_exception, groups",
[
(cs3code.CODE_OK, None, None, [Mock(), Mock()]),
(cs3code.CODE_NOT_FOUND, "error", NotFoundException, None),
(cs3code.CODE_UNAUTHENTICATED, "error", AuthenticationException, None),
(-2, "error", UnknownException, None),
],
)
def test_find_groups_with_multiple_filters(
group_instance, status_code, status_message, expected_exception, groups # noqa: F811 (not a redefinition)
):
"""Test find_groups with multiple filters using the create_find_group_filter classmethod."""
# Create multiple filters using the classmethod
filter1 = Group.create_find_group_filter("TYPE_QUERY", "test", None)
filter2 = Group.create_find_group_filter("TYPE_GROUPTYPE", None, "GROUP_TYPE_FEDERATED")
filters = [filter1, filter2]
mock_response = Mock()
mock_response.status.code = status_code
mock_response.status.message = status_message
mock_response.groups = groups
auth_token = ('x-access-token', "some_token")
with patch.object(group_instance._gateway, "FindGroups", return_value=mock_response):
if expected_exception:
with pytest.raises(expected_exception):
group_instance.find_groups(auth_token, filters)
else:
result = group_instance.find_groups(auth_token, filters)
assert result == groups