-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathtest_users.py
More file actions
304 lines (242 loc) · 13.7 KB
/
test_users.py
File metadata and controls
304 lines (242 loc) · 13.7 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import pytest
from _pytest.fixtures import SubRequest
import weaviate
from integration.conftest import ClientFactory, _sanitize_collection_name
from weaviate.auth import Auth
from weaviate.rbac.models import Role, RoleBase, UserTypes
RBAC_PORTS = (8092, 50063)
RBAC_AUTH_CREDS = Auth.api_key("admin-key")
def _unique_user_id(name: str) -> str:
return _sanitize_collection_name(name).lower()
def test_own_user(client_factory: ClientFactory) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=RBAC_AUTH_CREDS) as client:
if client._connection._weaviate_version.is_lower_than(1, 28, 0):
pytest.skip("This test requires Weaviate 1.28.0 or higher")
user = client.users.get_my_user()
assert len(user.roles) > 0
assert user.user_id == "admin-user"
def test_get_user_roles_deprecated(client_factory: ClientFactory) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=RBAC_AUTH_CREDS) as client:
if client._connection._weaviate_version.is_lower_than(1, 28, 0):
pytest.skip("This test requires Weaviate 1.28.0 or higher")
roles = client.users.get_assigned_roles("admin-user")
assert len(roles) > 0
def test_get_user_roles_db(client_factory: ClientFactory) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=RBAC_AUTH_CREDS) as client:
if client._connection._weaviate_version.is_lower_than(1, 30, 0):
pytest.skip("This test requires Weaviate 1.30.0 or higher")
roles_base = client.users.db.get_assigned_roles(user_id="admin-user")
names = list(roles_base.keys())
assert len(roles_base) > 0
assert isinstance(roles_base[names[0]], RoleBase)
roles = client.users.db.get_assigned_roles(user_id="admin-user", include_permissions=True)
assert len(roles) > 0
assert isinstance(roles[names[0]], Role)
def test_get_user_roles_oidc(client_factory: ClientFactory) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=RBAC_AUTH_CREDS) as client:
if client._connection._weaviate_version.is_lower_than(1, 30, 0):
pytest.skip("This test requires Weaviate 1.30.0 or higher")
roles_base = client.users.oidc.get_assigned_roles(user_id="admin-user")
names = list(roles_base.keys())
assert len(roles_base) > 0
assert isinstance(roles_base[names[0]], RoleBase)
roles = client.users.oidc.get_assigned_roles(user_id="admin-user", include_permissions=True)
assert len(roles) > 0
assert isinstance(roles[names[0]], Role)
def test_get_user_with_no_roles(client_factory: ClientFactory) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=Auth.api_key("custom-key")) as client:
if client._connection._weaviate_version.is_lower_than(1, 28, 0):
pytest.skip("This test requires Weaviate 1.28.0 or higher")
user = client.users.get_my_user()
assert len(user.roles) == 0
def test_get_static_db_user(client_factory: ClientFactory) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=RBAC_AUTH_CREDS) as client:
if client._connection._weaviate_version.is_lower_than(1, 30, 0):
pytest.skip("This test requires Weaviate 1.30.0 or higher")
user = client.users.db.get(user_id="admin-user")
assert len(user.role_names) > 0
assert user.user_id == "admin-user"
assert user.active
assert user.user_type == UserTypes.DB_STATIC
def test_create_user_and_get(client_factory: ClientFactory, request: SubRequest) -> None:
with client_factory(ports=(RBAC_PORTS), auth_credentials=Auth.api_key("admin-key")) as client:
if client._connection._weaviate_version.is_lower_than(1, 30, 0):
pytest.skip("This test requires Weaviate 1.30.0 or higher")
randomUserName = _unique_user_id(request.node.name)
apiKey = client.users.db.create(user_id=randomUserName)
with weaviate.connect_to_local(
port=RBAC_PORTS[0], grpc_port=RBAC_PORTS[1], auth_credentials=Auth.api_key(apiKey)
) as client2:
user = client2.users.get_my_user()
assert user.user_id == randomUserName
dynamicUser = client.users.db.get(user_id=randomUserName)
assert dynamicUser is not None
assert dynamicUser.user_id == randomUserName
assert dynamicUser.user_type == UserTypes.DB_DYNAMIC
assert client.users.db.delete(user_id=randomUserName)
def test_delete_user(client_factory: ClientFactory, request: SubRequest) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=Auth.api_key("admin-key")) as client:
if client._connection._weaviate_version.is_lower_than(1, 30, 0):
pytest.skip("This test requires Weaviate 1.30.0 or higher")
randomUserName = _unique_user_id(request.node.name)
client.users.db.create(user_id=randomUserName)
assert client.users.db.delete(user_id=randomUserName)
assert not client.users.db.delete(user_id="I-do-not-exist")
def test_rotate_user_key(client_factory: ClientFactory, request: SubRequest) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=Auth.api_key("admin-key")) as client:
if client._connection._weaviate_version.is_lower_than(1, 30, 0):
pytest.skip("This test requires Weaviate 1.30.0 or higher")
randomUserName = _unique_user_id(request.node.name)
apiKey = client.users.db.create(user_id=randomUserName)
with weaviate.connect_to_local(
port=RBAC_PORTS[0], grpc_port=RBAC_PORTS[1], auth_credentials=Auth.api_key(apiKey)
) as client2:
user = client2.users.get_my_user()
assert user.user_id == randomUserName
apiKeyNew = client.users.db.rotate_key(user_id=randomUserName)
with weaviate.connect_to_local(
port=RBAC_PORTS[0], grpc_port=RBAC_PORTS[1], auth_credentials=Auth.api_key(apiKeyNew)
) as client2:
user = client2.users.get_my_user()
assert user.user_id == randomUserName
assert client.users.db.delete(user_id=randomUserName)
def test_de_activate(client_factory: ClientFactory, request: SubRequest) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=Auth.api_key("admin-key")) as client:
if client._connection._weaviate_version.is_lower_than(1, 30, 0):
pytest.skip("This test requires Weaviate 1.30.0 or higher")
randomUserName = _unique_user_id(request.node.name)
client.users.db.create(user_id=randomUserName)
assert client.users.db.deactivate(user_id=randomUserName)
assert not client.users.db.deactivate(
user_id=randomUserName
) # second deactivation returns a conflict => false
user = client.users.db.get(user_id=randomUserName)
assert not user.active
assert client.users.db.activate(user_id=randomUserName)
assert not client.users.db.activate(
user_id=randomUserName
) # second activation returns a conflict => false
user = client.users.db.get(user_id=randomUserName)
assert user.active
client.users.db.delete(user_id=randomUserName)
def test_deactivate_and_revoke(client_factory: ClientFactory, request: SubRequest) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=Auth.api_key("admin-key")) as client:
if client._connection._weaviate_version.is_lower_than(1, 30, 0):
pytest.skip("This test requires Weaviate 1.30.0 or higher")
randomUserName = _unique_user_id(request.node.name)
apiKeyOld = client.users.db.create(user_id=randomUserName)
assert client.users.db.deactivate(user_id=randomUserName, revoke_key=True)
with pytest.raises(weaviate.exceptions.UnexpectedStatusCodeError):
weaviate.connect_to_local(
port=RBAC_PORTS[0],
grpc_port=RBAC_PORTS[1],
auth_credentials=Auth.api_key(apiKeyOld),
)
# re-activating is not enough
assert client.users.db.activate(user_id=randomUserName)
with pytest.raises(weaviate.exceptions.UnexpectedStatusCodeError):
weaviate.connect_to_local(
port=RBAC_PORTS[0],
grpc_port=RBAC_PORTS[1],
auth_credentials=Auth.api_key(apiKeyOld),
)
apiKeyNew = client.users.db.rotate_key(user_id=randomUserName)
with weaviate.connect_to_local(
port=RBAC_PORTS[0], grpc_port=RBAC_PORTS[1], auth_credentials=Auth.api_key(apiKeyNew)
) as client2:
user = client2.users.get_my_user()
assert user.user_id == randomUserName
client.users.db.delete(user_id=randomUserName)
def test_deprecated_syntax(client_factory: ClientFactory, request: SubRequest) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=Auth.api_key("admin-key")) as client:
if client._connection._weaviate_version.is_lower_than(1, 30, 0):
pytest.skip("This test requires Weaviate 1.30.0 or higher")
randomUserName = _unique_user_id(request.node.name)
client.users.db.create(user_id=randomUserName)
roles = client.users.db.get_assigned_roles(user_id=randomUserName)
assert len(roles) == 0
client.users.db.delete(user_id=randomUserName)
def test_list_all_users(client_factory: ClientFactory) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=Auth.api_key("admin-key")) as client:
if client._connection._weaviate_version.is_lower_than(1, 30, 0):
pytest.skip("This test requires Weaviate 1.30.0 or higher")
for i in range(5):
client.users.db.delete(user_id=f"list-all-user-{i}")
client.users.db.create(user_id=f"list-all-user-{i}")
users = client.users.db.list_all()
dynamic_users = [user for user in users if user.user_id.startswith("list-all-")]
assert len(dynamic_users) == 5
for i in range(5):
client.users.db.delete(user_id=f"list-all-{i}")
def test_get_user_created_at_and_api_key_first_letters(
client_factory: ClientFactory, request: SubRequest
) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=Auth.api_key("admin-key")) as client:
if client._connection._weaviate_version.is_lower_than(1, 30, 0):
pytest.skip("This test requires Weaviate 1.30.0 or higher")
randomUserName = _unique_user_id(request.node.name)
client.users.db.create(user_id=randomUserName)
try:
user = client.users.db.get(user_id=randomUserName)
assert user is not None
assert user.created_at is not None
assert user.api_key_first_letters is not None
assert len(user.api_key_first_letters) > 0
finally:
client.users.db.delete(user_id=randomUserName)
def test_get_user_include_last_used_time(
client_factory: ClientFactory, request: SubRequest
) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=Auth.api_key("admin-key")) as client:
if client._connection._weaviate_version.is_lower_than(1, 30, 0):
pytest.skip("This test requires Weaviate 1.30.0 or higher")
randomUserName = _unique_user_id(request.node.name)
apiKey = client.users.db.create(user_id=randomUserName)
try:
# log in with the new user to generate a lastUsedAt timestamp
with weaviate.connect_to_local(
port=RBAC_PORTS[0],
grpc_port=RBAC_PORTS[1],
auth_credentials=Auth.api_key(apiKey),
) as client2:
assert client2.users.get_my_user().user_id == randomUserName
# without include_last_used_time, last_used_time should be None
user = client.users.db.get(user_id=randomUserName)
assert user is not None
assert user.last_used_time is None
# with include_last_used_time=True, last_used_time should be populated
user = client.users.db.get(user_id=randomUserName, include_last_used_time=True)
assert user is not None
assert user.last_used_time is not None
finally:
client.users.db.delete(user_id=randomUserName)
def test_list_all_include_last_used_time(
client_factory: ClientFactory, request: SubRequest
) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=Auth.api_key("admin-key")) as client:
if client._connection._weaviate_version.is_lower_than(1, 30, 0):
pytest.skip("This test requires Weaviate 1.30.0 or higher")
randomUserName = _unique_user_id(request.node.name)
apiKey = client.users.db.create(user_id=randomUserName)
try:
# log in with the new user to generate a lastUsedAt timestamp
with weaviate.connect_to_local(
port=RBAC_PORTS[0],
grpc_port=RBAC_PORTS[1],
auth_credentials=Auth.api_key(apiKey),
) as client2:
assert client2.users.get_my_user().user_id == randomUserName
# without include_last_used_time, last_used_time should be None
users = client.users.db.list_all()
target = next((u for u in users if u.user_id == randomUserName), None)
assert target is not None
assert target.created_at is not None
assert target.api_key_first_letters is not None
assert target.last_used_time is None
# with include_last_used_time=True, last_used_time should be populated
users = client.users.db.list_all(include_last_used_time=True)
target = next((u for u in users if u.user_id == randomUserName), None)
assert target is not None
assert target.last_used_time is not None
finally:
client.users.db.delete(user_id=randomUserName)