|
13 | 13 | # under the License. |
14 | 14 | from typing import Any, Dict, List, Optional |
15 | 15 |
|
| 16 | +from fastapi import FastAPI |
| 17 | +from fastapi.testclient import TestClient |
16 | 18 | from pytest import fixture, mark |
17 | | -from supertokens_python import InputAppInfo, SupertokensConfig, init |
| 19 | + |
| 20 | +from supertokens_python import ( |
| 21 | + InputAppInfo, |
| 22 | + SupertokensConfig, |
| 23 | + get_request_from_user_context, |
| 24 | + init, |
| 25 | +) |
18 | 26 | from supertokens_python.framework.fastapi import get_middleware |
19 | 27 | from supertokens_python.recipe import emailpassword, session |
20 | 28 | from supertokens_python.recipe.emailpassword.asyncio import sign_up |
|
28 | 36 | RecipeInterface as SRecipeInterface, |
29 | 37 | ) |
30 | 38 |
|
31 | | -from fastapi import FastAPI |
32 | | -from fastapi.testclient import TestClient |
33 | | - |
34 | 39 | from .utils import clean_st, reset, setup_st, sign_in_request, start_st |
35 | 40 |
|
36 | 41 | works = False |
@@ -277,3 +282,121 @@ async def create_new_session( |
277 | 282 | create_new_session_context_works, |
278 | 283 | ] |
279 | 284 | ) |
| 285 | + |
| 286 | + |
| 287 | +@mark.asyncio |
| 288 | +async def test_get_request_from_user_context(driver_config_client: TestClient): |
| 289 | + signin_api_context_works, signin_context_works, create_new_session_context_works = ( |
| 290 | + False, |
| 291 | + False, |
| 292 | + False, |
| 293 | + ) |
| 294 | + |
| 295 | + def apis_override_email_password(param: APIInterface): |
| 296 | + og_sign_in_post = param.sign_in_post |
| 297 | + |
| 298 | + async def sign_in_post( |
| 299 | + form_fields: List[FormField], |
| 300 | + api_options: APIOptions, |
| 301 | + user_context: Dict[str, Any], |
| 302 | + ): |
| 303 | + req = get_request_from_user_context(user_context) |
| 304 | + if req: |
| 305 | + assert req.method() == "POST" |
| 306 | + assert req.get_path() == "/auth/signin" |
| 307 | + nonlocal signin_api_context_works |
| 308 | + signin_api_context_works = True |
| 309 | + |
| 310 | + return await og_sign_in_post(form_fields, api_options, user_context) |
| 311 | + |
| 312 | + param.sign_in_post = sign_in_post |
| 313 | + return param |
| 314 | + |
| 315 | + def functions_override_email_password(param: RecipeInterface): |
| 316 | + og_sign_in = param.sign_in |
| 317 | + |
| 318 | + async def sign_in(email: str, password: str, user_context: Dict[str, Any]): |
| 319 | + req = get_request_from_user_context(user_context) |
| 320 | + if req: |
| 321 | + assert req.method() == "POST" |
| 322 | + assert req.get_path() == "/auth/signin" |
| 323 | + nonlocal signin_context_works |
| 324 | + signin_context_works = True |
| 325 | + |
| 326 | + orginal_request = req |
| 327 | + user_context["_default"]["request"] = None |
| 328 | + |
| 329 | + newReq = get_request_from_user_context(user_context) |
| 330 | + assert newReq is None |
| 331 | + |
| 332 | + user_context["_default"]["request"] = orginal_request |
| 333 | + |
| 334 | + return await og_sign_in(email, password, user_context) |
| 335 | + |
| 336 | + param.sign_in = sign_in |
| 337 | + return param |
| 338 | + |
| 339 | + def functions_override_session(param: SRecipeInterface): |
| 340 | + og_create_new_session = param.create_new_session |
| 341 | + |
| 342 | + async def create_new_session( |
| 343 | + user_id: str, |
| 344 | + access_token_payload: Optional[Dict[str, Any]], |
| 345 | + session_data_in_database: Optional[Dict[str, Any]], |
| 346 | + disable_anti_csrf: Optional[bool], |
| 347 | + user_context: Dict[str, Any], |
| 348 | + ): |
| 349 | + req = get_request_from_user_context(user_context) |
| 350 | + if req: |
| 351 | + assert req.method() == "POST" |
| 352 | + assert req.get_path() == "/auth/signin" |
| 353 | + nonlocal create_new_session_context_works |
| 354 | + create_new_session_context_works = True |
| 355 | + |
| 356 | + response = await og_create_new_session( |
| 357 | + user_id, |
| 358 | + access_token_payload, |
| 359 | + session_data_in_database, |
| 360 | + disable_anti_csrf, |
| 361 | + user_context, |
| 362 | + ) |
| 363 | + return response |
| 364 | + |
| 365 | + param.create_new_session = create_new_session |
| 366 | + return param |
| 367 | + |
| 368 | + init( |
| 369 | + supertokens_config=SupertokensConfig("http://localhost:3567"), |
| 370 | + app_info=InputAppInfo( |
| 371 | + app_name="SuperTokens Demo", |
| 372 | + api_domain="http://api.supertokens.io", |
| 373 | + website_domain="http://supertokens.io", |
| 374 | + ), |
| 375 | + framework="fastapi", |
| 376 | + recipe_list=[ |
| 377 | + emailpassword.init( |
| 378 | + override=emailpassword.InputOverrideConfig( |
| 379 | + apis=apis_override_email_password, |
| 380 | + functions=functions_override_email_password, |
| 381 | + ) |
| 382 | + ), |
| 383 | + session.init( |
| 384 | + override=session.InputOverrideConfig( |
| 385 | + functions=functions_override_session |
| 386 | + ) |
| 387 | + ), |
| 388 | + ], |
| 389 | + ) |
| 390 | + start_st() |
| 391 | + |
| 392 | + await sign_up( "[email protected]", "validpass123", { "manualCall": True}) |
| 393 | + res = sign_in_request( driver_config_client, "[email protected]", "validpass123") |
| 394 | + |
| 395 | + assert res.status_code == 200 |
| 396 | + assert all( |
| 397 | + [ |
| 398 | + signin_api_context_works, |
| 399 | + signin_context_works, |
| 400 | + create_new_session_context_works, |
| 401 | + ] |
| 402 | + ) |
0 commit comments