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