88import uuid
99from enum import Enum
1010from types import SimpleNamespace
11- from typing import Type , List
11+ from typing import Type , List , Union
1212
1313import pytest
1414import requests
@@ -39,9 +39,10 @@ class Environ(Enum):
3939 ONPREM = 'onprem'
4040 CUSTOM = 'custom'
4141 STAGING_EU = 'staging-eu'
42- EPHEMERAL = 'ephemeral' # Used for testing PRs with ephemeral environments
43-
44- EPHEMERAL_BASE_URL = "http://lb-api-public"
42+ EPHEMERAL = 'ephemeral' # Used for testing PRs with ephemeral environments
43+
44+
45+ EPHEMERAL_BASE_URL = "http://lb-api-public"
4546
4647
4748@pytest .fixture (scope = "session" )
@@ -78,7 +79,7 @@ def graphql_url(environ: str) -> str:
7879 raise Exception (f"Missing LABELBOX_TEST_GRAPHQL_API_ENDPOINT" )
7980 return graphql_api_endpoint
8081 elif environ == Environ .EPHEMERAL :
81- return f"{ Environ . EPHEMERAL_BASE_URL } /graphql"
82+ return f"{ EPHEMERAL_BASE_URL } /graphql"
8283 return 'http://host.docker.internal:8080/graphql'
8384
8485
@@ -95,10 +96,17 @@ def rest_url(environ: str) -> str:
9596 raise Exception (f"Missing LABELBOX_TEST_REST_API_ENDPOINT" )
9697 return rest_api_endpoint
9798 elif environ == Environ .EPHEMERAL :
98- return f"{ Environ . EPHEMERAL_BASE_URL } /api/v1"
99+ return f"{ EPHEMERAL_BASE_URL } /api/v1"
99100 return 'http://host.docker.internal:8080/api/v1'
100101
101102
103+ def admin_url (environ : str ) -> Union [str , None ]:
104+ if environ == Environ .EPHEMERAL :
105+ return f"{ EPHEMERAL_BASE_URL } /admin/v1"
106+
107+ return 'http://host.docker.internal:8080/admin/v1'
108+
109+
102110def testing_api_key (environ : str ) -> str :
103111 if environ == Environ .PROD :
104112 return os .environ ["LABELBOX_TEST_API_KEY_PROD" ]
@@ -170,6 +178,8 @@ def __init__(self, environ: str) -> None:
170178 api_url = graphql_url (environ )
171179 api_key = testing_api_key (environ )
172180 rest_endpoint = rest_url (environ )
181+ self ._admin_endpoint = admin_url (environ )
182+
173183 super ().__init__ (api_key ,
174184 api_url ,
175185 enable_experimental = True ,
@@ -182,6 +192,98 @@ def execute(self, query=None, params=None, check_naming=True, **kwargs):
182192 self .queries .append ((query , params ))
183193 return super ().execute (query , params , ** kwargs )
184194
195+ def create_organization (self ) -> str :
196+ endpoint = f"{ self ._admin_endpoint } /organizations/"
197+ response = requests .post (
198+ endpoint ,
199+ headers = self .headers ,
200+ json = {"name" : f"Test Org { uuid .uuid4 ()} " },
201+ )
202+ if response .status_code != requests .codes .created :
203+ raise Exception ("Failed to create ephemeral org, message: " +
204+ str (response .json ()['message' ]))
205+
206+ return response .json ()['id' ]
207+
208+ def create_user (self , organization_id ) -> tuple [str , str ]:
209+ endpoint = f"{ self ._admin_endpoint } /user-identities/"
210+ identity_id = f"e2e+{ uuid .uuid4 ()} "
211+
212+ response = requests .post (
213+ endpoint ,
214+ headers = self .headers ,
215+ json = {
216+ "identityId" : identity_id ,
217+ "email" : "email@email.com" ,
218+ "name" : f"tester{ uuid .uuid4 ()} " ,
219+ "verificationStatus" : "VERIFIED" ,
220+ },
221+ )
222+ if response .status_code != requests .codes .created :
223+ raise Exception ("Failed to create ephemeral org, message: " +
224+ str (response .json ()['message' ]))
225+
226+ user_identity_id = response .json ()['identityId' ]
227+
228+ endpoint = f"{ self ._admin_endpoint } /organizations/{ organization_id } /users/"
229+ response = requests .post (
230+ endpoint ,
231+ headers = self .headers ,
232+ json = {
233+ "identityId" : user_identity_id ,
234+ "organizationRole" : "Admin"
235+ },
236+ )
237+ if response .status_code != requests .codes .created :
238+ raise Exception ("Failed to create ephemeral org, message: " +
239+ str (response .json ()['message' ]))
240+
241+ user_id = response .json ()['id' ]
242+
243+ endpoint = f"{ self ._admin_endpoint } /users/{ user_id } /token"
244+ response = requests .get (
245+ endpoint ,
246+ headers = self .headers ,
247+ )
248+ if response .status_code != requests .codes .created :
249+ raise Exception ("Failed to create ephemeral org, message: " +
250+ str (response .json ()['message' ]))
251+
252+ token = response ["token" ]
253+
254+ return user_id , token
255+
256+ def create_api_key_for_user (self , user_token ) -> str :
257+ key_name = f"test-key+{ uuid .uuid4 ()} "
258+ query = """
259+ mutation CreateApiKey($name: String!) {
260+ createApiKey(data: {name: $name}) {
261+ id
262+ jwt
263+ __typename
264+ }
265+ }
266+ """
267+ params = {"name" : key_name }
268+ req = self ._make_gql_request (query = query , params = params )
269+
270+ return req ["createApiKey" ]["jwt" ]
271+ mutation_name = "deleteDataRowsByQuery"
272+ query = """mutation DeleteDataRowsByQueryPyApi($searchQueryInput: SearchServiceQueryInput!) {
273+ %s(where: {searchQuery: $searchQueryInput})
274+ { taskId }
275+ }
276+ """ % (mutation_name )
277+ query_params = {
278+ "searchQueryInput" : {
279+ "query" : search_query ,
280+ "scope" : None
281+ }
282+ }
283+
284+ res = self .execute (query , query_params , error_log_key = "errors" )
285+ res = res [mutation_name ]
286+
185287
186288@pytest .fixture (scope = "session" )
187289def client (environ : str ):
0 commit comments