1010from langfuse .callback import CallbackHandler
1111
1212from langfuse .client import Langfuse
13+ from langfuse .model import CreateTrace
1314from tests .test_task_manager import get_host
14- from langfuse .openai import _is_openai_v1 , openai
15+ from langfuse .openai import _is_openai_v1 , auth_check , openai
1516
1617chat_func = openai .chat .completions .create if _is_openai_v1 () else openai .ChatCompletion .create
1718
@@ -226,6 +227,27 @@ def test_openai_default():
226227 os .environ ["LANGFUSE_HOST" ] = host
227228
228229
230+ def test_openai_auth_check ():
231+ assert auth_check () is True
232+
233+
234+ def test_openai_auth_check_failing_key ():
235+ secret_key = os .environ ["LANGFUSE_SECRET_KEY" ]
236+ os .environ .pop ("LANGFUSE_SECRET_KEY" )
237+
238+ importlib .reload (langfuse )
239+ importlib .reload (langfuse .openai )
240+
241+ from langfuse .openai import openai
242+
243+ openai .langfuse_secret_key = "test"
244+
245+ with pytest .raises (UnauthorizedError ):
246+ auth_check ()
247+
248+ os .environ ["LANGFUSE_SECRET_KEY" ] = secret_key
249+
250+
229251def test_openai_configured (httpserver : HTTPServer ):
230252 httpserver .expect_request ("/api/public/ingestion" , method = "POST" ).respond_with_response (Response (status = 200 ))
231253 host = get_host (httpserver .url_for ("/api/public/ingestion" ))
@@ -270,6 +292,7 @@ def test_openai_configured(httpserver: HTTPServer):
270292
271293def test_client_init_workers_5 ():
272294 langfuse = Langfuse (threads = 5 )
295+ langfuse .flush ()
273296
274297 assert langfuse .task_manager ._threads == 5
275298
@@ -287,16 +310,54 @@ def test_auth_check():
287310
288311 assert langfuse .auth_check () is True
289312
313+ langfuse .flush ()
314+
290315
291316def test_wrong_key_auth_check ():
292317 langfuse = Langfuse (debug = False , secret_key = "test" )
293318
294319 with pytest .raises (UnauthorizedError ):
295320 langfuse .auth_check ()
296321
322+ langfuse .flush ()
323+
324+
325+ def test_auth_check_callback ():
326+ langfuse = CallbackHandler (debug = False )
327+
328+ assert langfuse .auth_check () is True
329+ langfuse .flush ()
330+
331+
332+ def test_auth_check_callback_stateful ():
333+ langfuse = Langfuse (debug = False )
334+ trace = langfuse .trace (CreateTrace (name = "name" ))
335+ handler = trace .get_langchain_handler ()
336+
337+ assert handler .auth_check () is True
338+ handler .flush ()
339+
340+
341+ def test_wrong_key_auth_check_callback ():
342+ langfuse = CallbackHandler (debug = False , secret_key = "test" )
343+
344+ with pytest .raises (UnauthorizedError ):
345+ langfuse .auth_check ()
346+ langfuse .flush ()
347+
297348
298349def test_wrong_url_auth_check ():
299350 langfuse = Langfuse (debug = False , host = "http://localhost:4000/" )
300351
301352 with pytest .raises (httpx .ConnectError ):
302353 langfuse .auth_check ()
354+
355+ langfuse .flush ()
356+
357+
358+ def test_wrong_url_auth_check_callback ():
359+ langfuse = CallbackHandler (debug = False , host = "http://localhost:4000/" )
360+
361+ with pytest .raises (httpx .ConnectError ):
362+ langfuse .auth_check ()
363+ langfuse .flush ()
0 commit comments