1
1
import uuid
2
2
import pytest
3
+ import os
3
4
4
5
from labelbox .schema .timeunit import TimeUnit
5
6
from labelbox .schema .api_key import ApiKey
6
7
from lbox .exceptions import LabelboxError
7
8
# The creation of API keys requires a feature flag to be enabled.
8
9
9
10
11
+ @pytest .mark .skipif (
12
+ condition = os .environ ["LABELBOX_TEST_ENVIRON" ] != "prod" ,
13
+ reason = "Admin permissions are required to create API keys" ,
14
+ )
15
+ def test_create_api_key_success (client ):
16
+ # Create a test API key
17
+ key_name = f"Test Key { uuid .uuid4 ()} "
18
+ user_email = client .get_user ().email
19
+
20
+ assert (
21
+ client .get_user ().org_role ().name == "Admin"
22
+ ), "User must be an admin to create API keys"
23
+
24
+ # Get available roles and use the first one
25
+ available_roles = ApiKey ._get_available_api_key_roles (client )
26
+ assert (
27
+ len (available_roles ) > 0
28
+ ), "No available roles found for API key creation"
29
+
30
+ # Create the API key with a short validity period
31
+ api_key_result = client .create_api_key (
32
+ name = key_name ,
33
+ user = user_email ,
34
+ role = available_roles [0 ],
35
+ validity = 5 ,
36
+ time_unit = TimeUnit .MINUTE ,
37
+ )
38
+
39
+ # Verify the response format
40
+ assert isinstance (
41
+ api_key_result , dict
42
+ ), "API key result should be a dictionary"
43
+ assert "id" in api_key_result , "API key result should contain an 'id' field"
44
+ assert (
45
+ "jwt" in api_key_result
46
+ ), "API key result should contain a 'jwt' field"
47
+
48
+ # Verify the JWT token format (should be a JWT string)
49
+ jwt = api_key_result ["jwt" ]
50
+ assert isinstance (jwt , str ), "JWT should be a string"
51
+ assert jwt .count ("." ) == 2 , "JWT should have three parts separated by dots"
52
+
53
+
10
54
def test_create_api_key_failed (client ):
11
55
# Test with invalid role
12
56
with pytest .raises (ValueError ) as excinfo :
@@ -132,7 +176,7 @@ def test_create_api_key_invalid_validity_values(client):
132
176
validity = 0 ,
133
177
time_unit = TimeUnit .MINUTE ,
134
178
)
135
- assert "validity must be a positive integer " in str (excinfo .value ).lower ()
179
+ assert "minimum validity period is 1 minute " in str (excinfo .value ).lower ()
136
180
137
181
# Days (exceeding 6 months)
138
182
with pytest .raises (ValueError ) as excinfo :
@@ -185,10 +229,16 @@ def test_create_api_key_invalid_time_unit(client):
185
229
assert "valid TimeUnit" in str (excinfo .value )
186
230
187
231
232
+ @pytest .mark .skipif (
233
+ condition = os .environ ["LABELBOX_TEST_ENVIRON" ] == "prod" ,
234
+ reason = "Accounts with sdmin permission can create API keys" ,
235
+ )
188
236
def test_create_api_key_insufficient_permissions (client ):
189
237
"""Test that creating an API key fails when the user has insufficient permissions."""
190
238
user_email = client .get_user ().email
191
239
240
+ assert client .get_user ().org_role ().name == "Admin"
241
+
192
242
# Attempt to create another API key using the limited permissions client
193
243
# This should fail due to insufficient permissions
194
244
with pytest .raises (LabelboxError ) as excinfo :
@@ -200,5 +250,4 @@ def test_create_api_key_insufficient_permissions(client):
200
250
time_unit = TimeUnit .MINUTE ,
201
251
)
202
252
203
- # Check for the exact "Permission denied" error message
204
- assert "Permission denied" in str (excinfo .value )
253
+ assert "192" in str (excinfo .value )
0 commit comments