1717 emailpassword ,
1818 emailverification ,
1919 dashboard ,
20+ thirdparty ,
2021)
2122from supertokens_python import InputAppInfo
22- from supertokens_python .recipe .emailpassword .asyncio import get_user_by_id
23+ from supertokens_python .recipe .emailpassword .asyncio import get_user_by_id , sign_up
24+ from supertokens_python .recipe .thirdparty .asyncio import (
25+ get_user_by_id as tp_get_user_by_id ,
26+ )
2327import asyncio
2428import respx
2529import httpx
@@ -227,6 +231,7 @@ def intercept(
227231 session .init (),
228232 emailpassword .init (),
229233 dashboard .init (),
234+ thirdparty .init (),
230235 ],
231236 ) # type: ignore
232237 start_st ()
@@ -241,3 +246,230 @@ def intercept(
241246 user = await get_user_by_id ("random" , user_context )
242247 assert user is None
243248 assert not called_core
249+
250+ user = await tp_get_user_by_id ("random" , user_context )
251+
252+ assert user is None
253+ assert called_core
254+
255+ called_core = False
256+
257+ user = await tp_get_user_by_id ("random" , user_context )
258+ assert user is None
259+ assert not called_core
260+
261+ user = await get_user_by_id ("random" , user_context )
262+ assert user is None
263+ assert not called_core
264+
265+
266+ async def test_caching_gets_clear_with_non_get ():
267+
268+ called_core = False
269+
270+ def intercept (
271+ url : str ,
272+ method : str ,
273+ headers : Dict [str , Any ],
274+ params : Optional [Dict [str , Any ]],
275+ body : Optional [Dict [str , Any ]],
276+ _ : Optional [Dict [str , Any ]],
277+ ):
278+ nonlocal called_core
279+ called_core = True
280+ return url , method , headers , params , body
281+
282+ init (
283+ supertokens_config = SupertokensConfig (
284+ connection_uri = "http://localhost:3567" , network_interceptor = intercept
285+ ),
286+ app_info = InputAppInfo (
287+ app_name = "ST" ,
288+ api_domain = "http://api.supertokens.io" ,
289+ website_domain = "http://supertokens.io" ,
290+ api_base_path = "/auth" ,
291+ ),
292+ framework = "fastapi" ,
293+ mode = "asgi" ,
294+ recipe_list = [
295+ session .init (),
296+ emailpassword .init (),
297+ dashboard .init (),
298+ ],
299+ ) # type: ignore
300+ start_st ()
301+ user_context : Dict [str , Any ] = {}
302+ user = await get_user_by_id ("random" , user_context )
303+
304+ assert user is None
305+ assert called_core
306+
307+ await sign_up (
"public" ,
"[email protected] " ,
"abcd1234" ,
user_context )
308+
309+ called_core = False
310+
311+ user = await get_user_by_id ("random" , user_context )
312+ assert user is None
313+ assert called_core
314+
315+ called_core = False
316+
317+ user = await get_user_by_id ("random" , user_context )
318+ assert user is None
319+ assert not called_core
320+
321+
322+ async def test_no_caching_if_disabled_by_user ():
323+
324+ called_core = False
325+
326+ def intercept (
327+ url : str ,
328+ method : str ,
329+ headers : Dict [str , Any ],
330+ params : Optional [Dict [str , Any ]],
331+ body : Optional [Dict [str , Any ]],
332+ _ : Optional [Dict [str , Any ]],
333+ ):
334+ nonlocal called_core
335+ called_core = True
336+ return url , method , headers , params , body
337+
338+ init (
339+ supertokens_config = SupertokensConfig (
340+ connection_uri = "http://localhost:3567" ,
341+ network_interceptor = intercept ,
342+ disable_core_call_cache = True ,
343+ ),
344+ app_info = InputAppInfo (
345+ app_name = "ST" ,
346+ api_domain = "http://api.supertokens.io" ,
347+ website_domain = "http://supertokens.io" ,
348+ api_base_path = "/auth" ,
349+ ),
350+ framework = "fastapi" ,
351+ mode = "asgi" ,
352+ recipe_list = [
353+ session .init (),
354+ emailpassword .init (),
355+ dashboard .init (),
356+ ],
357+ ) # type: ignore
358+ start_st ()
359+ user_context : Dict [str , Any ] = {}
360+ user = await get_user_by_id ("random" , user_context )
361+
362+ assert user is None
363+ assert called_core
364+
365+ called_core = False
366+
367+ user = await get_user_by_id ("random" , user_context )
368+ assert user is None
369+ assert called_core
370+
371+
372+ async def test_no_caching_if_headers_are_different ():
373+
374+ called_core = False
375+
376+ def intercept (
377+ url : str ,
378+ method : str ,
379+ headers : Dict [str , Any ],
380+ params : Optional [Dict [str , Any ]],
381+ body : Optional [Dict [str , Any ]],
382+ _ : Optional [Dict [str , Any ]],
383+ ):
384+ nonlocal called_core
385+ called_core = True
386+ return url , method , headers , params , body
387+
388+ init (
389+ supertokens_config = SupertokensConfig (
390+ connection_uri = "http://localhost:3567" ,
391+ network_interceptor = intercept ,
392+ ),
393+ app_info = InputAppInfo (
394+ app_name = "ST" ,
395+ api_domain = "http://api.supertokens.io" ,
396+ website_domain = "http://supertokens.io" ,
397+ api_base_path = "/auth" ,
398+ ),
399+ framework = "fastapi" ,
400+ mode = "asgi" ,
401+ recipe_list = [
402+ session .init (),
403+ emailpassword .init (),
404+ dashboard .init (),
405+ thirdparty .init (),
406+ ],
407+ ) # type: ignore
408+ start_st ()
409+ user_context : Dict [str , Any ] = {}
410+ user = await get_user_by_id ("random" , user_context )
411+
412+ assert user is None
413+ assert called_core
414+
415+ called_core = False
416+
417+ user = await get_user_by_id ("random" , user_context )
418+ assert user is None
419+ assert not called_core
420+
421+ called_core = False
422+
423+ user = await tp_get_user_by_id ("random" , user_context )
424+ assert user is None
425+ assert called_core
426+
427+
428+ async def test_caching_gets_clear_when_query_without_user_context ():
429+
430+ called_core = False
431+
432+ def intercept (
433+ url : str ,
434+ method : str ,
435+ headers : Dict [str , Any ],
436+ params : Optional [Dict [str , Any ]],
437+ body : Optional [Dict [str , Any ]],
438+ _ : Optional [Dict [str , Any ]],
439+ ):
440+ nonlocal called_core
441+ called_core = True
442+ return url , method , headers , params , body
443+
444+ init (
445+ supertokens_config = SupertokensConfig (
446+ connection_uri = "http://localhost:3567" , network_interceptor = intercept
447+ ),
448+ app_info = InputAppInfo (
449+ app_name = "ST" ,
450+ api_domain = "http://api.supertokens.io" ,
451+ website_domain = "http://supertokens.io" ,
452+ api_base_path = "/auth" ,
453+ ),
454+ framework = "fastapi" ,
455+ mode = "asgi" ,
456+ recipe_list = [
457+ session .init (),
458+ emailpassword .init (),
459+ dashboard .init (),
460+ ],
461+ ) # type: ignore
462+ start_st ()
463+ user_context : Dict [str , Any ] = {}
464+ user = await get_user_by_id ("random" , user_context )
465+
466+ assert user is None
467+ assert called_core
468+
469+ await sign_up (
"public" ,
"[email protected] " ,
"abcd1234" )
470+
471+ called_core = False
472+
473+ user = await get_user_by_id ("random" , user_context )
474+ assert user is None
475+ assert called_core
0 commit comments