11import uuid
22import pytest
3+ import os
34
45from labelbox .schema .timeunit import TimeUnit
56from labelbox .schema .api_key import ApiKey
67from lbox .exceptions import LabelboxError
78# The creation of API keys requires a feature flag to be enabled.
89
910
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+
1054def test_create_api_key_failed (client ):
1155 # Test with invalid role
1256 with pytest .raises (ValueError ) as excinfo :
@@ -132,7 +176,7 @@ def test_create_api_key_invalid_validity_values(client):
132176 validity = 0 ,
133177 time_unit = TimeUnit .MINUTE ,
134178 )
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 ()
136180
137181 # Days (exceeding 6 months)
138182 with pytest .raises (ValueError ) as excinfo :
@@ -185,10 +229,16 @@ def test_create_api_key_invalid_time_unit(client):
185229 assert "valid TimeUnit" in str (excinfo .value )
186230
187231
232+ @pytest .mark .skipif (
233+ condition = os .environ ["LABELBOX_TEST_ENVIRON" ] == "prod" ,
234+ reason = "Accounts with sdmin permission can create API keys" ,
235+ )
188236def test_create_api_key_insufficient_permissions (client ):
189237 """Test that creating an API key fails when the user has insufficient permissions."""
190238 user_email = client .get_user ().email
191239
240+ assert client .get_user ().org_role ().name == "Admin"
241+
192242 # Attempt to create another API key using the limited permissions client
193243 # This should fail due to insufficient permissions
194244 with pytest .raises (LabelboxError ) as excinfo :
@@ -200,5 +250,4 @@ def test_create_api_key_insufficient_permissions(client):
200250 time_unit = TimeUnit .MINUTE ,
201251 )
202252
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