Skip to content

Commit b49c553

Browse files
committed
Add test for get_request_from_user_context
1 parent 8e75570 commit b49c553

File tree

1 file changed

+114
-3
lines changed

1 file changed

+114
-3
lines changed

tests/test_user_context.py

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
# under the License.
1414
from typing import Any, Dict, List, Optional
1515

16+
from fastapi import FastAPI
17+
from fastapi.testclient import TestClient
1618
from pytest import fixture, mark
19+
1720
from supertokens_python import InputAppInfo, SupertokensConfig, init
21+
from supertokens_python.asyncio import get_request_from_user_context
1822
from supertokens_python.framework.fastapi import get_middleware
1923
from supertokens_python.recipe import emailpassword, session
2024
from supertokens_python.recipe.emailpassword.asyncio import sign_up
@@ -28,9 +32,6 @@
2832
RecipeInterface as SRecipeInterface,
2933
)
3034

31-
from fastapi import FastAPI
32-
from fastapi.testclient import TestClient
33-
3435
from .utils import clean_st, reset, setup_st, sign_in_request, start_st
3536

3637
works = False
@@ -277,3 +278,113 @@ async def create_new_session(
277278
create_new_session_context_works,
278279
]
279280
)
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

Comments
 (0)