12
12
# License for the specific language governing permissions and limitations
13
13
# under the License.
14
14
15
- from typing import List
15
+ from typing import List , Dict , Any
16
+ from unittest .mock import MagicMock
16
17
17
18
from fastapi import FastAPI
18
19
from fastapi .requests import Request
23
24
from supertokens_python .framework .fastapi .fastapi_middleware import get_middleware
24
25
from supertokens_python .process_state import AllowedProcessStates , ProcessState
25
26
from supertokens_python .recipe import session
26
- from supertokens_python .recipe .session import SessionRecipe
27
+ from supertokens_python .recipe .session import SessionRecipe , InputOverrideConfig
27
28
from supertokens_python .recipe .session .asyncio import (
28
29
get_all_session_handles_for_user ,
29
30
get_session_information ,
36
37
update_access_token_payload ,
37
38
update_session_data ,
38
39
)
40
+ from supertokens_python .recipe .session .interfaces import RecipeInterface
39
41
from supertokens_python .recipe .session .recipe_implementation import RecipeImplementation
40
42
from supertokens_python .recipe .session .session_functions import (
41
43
create_new_session ,
42
44
get_session ,
43
45
refresh_session ,
44
46
revoke_session ,
45
47
)
48
+ from supertokens_python .recipe .session .asyncio import (
49
+ create_new_session as async_create_new_session ,
50
+ )
46
51
from tests .utils import clean_st , reset , setup_st , start_st
47
52
53
+ pytestmark = mark .asyncio
54
+
48
55
49
56
def setup_function (_ ):
50
57
reset ()
@@ -57,7 +64,6 @@ def teardown_function(_):
57
64
clean_st ()
58
65
59
66
60
- @mark .asyncio
61
67
async def test_that_once_the_info_is_loaded_it_doesnt_query_again ():
62
68
init (
63
69
supertokens_config = SupertokensConfig ("http://localhost:3567" ),
@@ -152,7 +158,6 @@ async def test_that_once_the_info_is_loaded_it_doesnt_query_again():
152
158
assert response5 is True
153
159
154
160
155
- @mark .asyncio
156
161
async def test_creating_many_sessions_for_one_user_and_looping ():
157
162
init (
158
163
supertokens_config = SupertokensConfig ("http://localhost:3567" ),
@@ -235,7 +240,6 @@ async def home(_request: Request): # type: ignore
235
240
return TestClient (app )
236
241
237
242
238
- @mark .asyncio
239
243
async def test_signout_api_works_even_if_session_is_deleted_after_creation (
240
244
driver_config_client : TestClient ,
241
245
):
@@ -278,3 +282,50 @@ async def test_signout_api_works_even_if_session_is_deleted_after_creation(
278
282
signout_response .headers ["set-cookie" ]
279
283
== """sAccessToken=""; expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Path=/; SameSite=lax; Secure, sIdRefreshToken=""; expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Path=/; SameSite=lax; Secure, sRefreshToken=""; expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Path=/auth/session/refresh; SameSite=lax; Secure"""
280
284
)
285
+
286
+
287
+ async def test_should_use_override_functions_in_session_container_methods ():
288
+ def override_session_functions (oi : RecipeInterface ) -> RecipeInterface :
289
+ oi_get_session_information = oi .get_session_information
290
+
291
+ async def get_session_information (
292
+ session_handle : str , user_context : Dict [str , Any ]
293
+ ):
294
+ info = await oi_get_session_information (session_handle , user_context )
295
+ assert info is not None
296
+ info .session_data ["foo" ] = "bar"
297
+ return info
298
+
299
+ oi .get_session_information = get_session_information
300
+
301
+ return oi
302
+
303
+ init (
304
+ supertokens_config = SupertokensConfig ("http://localhost:3567" ),
305
+ app_info = InputAppInfo (
306
+ app_name = "SuperTokens Demo" ,
307
+ api_domain = "https://api.supertokens.io" ,
308
+ website_domain = "supertokens.io" ,
309
+ ),
310
+ framework = "fastapi" ,
311
+ recipe_list = [
312
+ session .init (
313
+ anti_csrf = "VIA_TOKEN" ,
314
+ override = InputOverrideConfig (
315
+ functions = override_session_functions ,
316
+ ),
317
+ )
318
+ ],
319
+ )
320
+ start_st ()
321
+
322
+ s = SessionRecipe .get_instance ()
323
+ if not isinstance (s .recipe_implementation , RecipeImplementation ):
324
+ raise Exception ("Should never come here" )
325
+
326
+ mock_response = MagicMock ()
327
+
328
+ my_session = await async_create_new_session (mock_response , "test_id" )
329
+ data = await my_session .get_session_data ()
330
+
331
+ assert data == {"foo" : "bar" }
0 commit comments