-
Notifications
You must be signed in to change notification settings - Fork 42
feat: Add a helper function to read the original request from the user context inside overrides #328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: Add a helper function to read the original request from the user context inside overrides #328
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8e75570
Add helper function to get the original request from user context pro…
nkshah2 b49c553
Add test for get_request_from_user_context
nkshah2 fb1286f
Update package version and CHANGELOG
nkshah2 42539be
Refactor based on PR reviews
nkshah2 6e55f1e
Make test better
nkshah2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,7 @@ | |
|
||
setup( | ||
name="supertokens_python", | ||
version="0.14.0", | ||
version="0.14.1", | ||
author="SuperTokens", | ||
license="Apache 2.0", | ||
author_email="[email protected]", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,16 @@ | |
# under the License. | ||
from typing import Any, Dict, List, Optional | ||
|
||
from fastapi import FastAPI | ||
from fastapi.testclient import TestClient | ||
from pytest import fixture, mark | ||
from supertokens_python import InputAppInfo, SupertokensConfig, init | ||
|
||
from supertokens_python import ( | ||
InputAppInfo, | ||
SupertokensConfig, | ||
get_request_from_user_context, | ||
init, | ||
) | ||
from supertokens_python.framework.fastapi import get_middleware | ||
from supertokens_python.recipe import emailpassword, session | ||
from supertokens_python.recipe.emailpassword.asyncio import sign_up | ||
|
@@ -28,9 +36,6 @@ | |
RecipeInterface as SRecipeInterface, | ||
) | ||
|
||
from fastapi import FastAPI | ||
from fastapi.testclient import TestClient | ||
|
||
from .utils import clean_st, reset, setup_st, sign_in_request, start_st | ||
|
||
works = False | ||
|
@@ -277,3 +282,121 @@ async def create_new_session( | |
create_new_session_context_works, | ||
] | ||
) | ||
|
||
|
||
@mark.asyncio | ||
async def test_get_request_from_user_context(driver_config_client: TestClient): | ||
signin_api_context_works, signin_context_works, create_new_session_context_works = ( | ||
False, | ||
False, | ||
False, | ||
) | ||
|
||
def apis_override_email_password(param: APIInterface): | ||
og_sign_in_post = param.sign_in_post | ||
|
||
async def sign_in_post( | ||
form_fields: List[FormField], | ||
api_options: APIOptions, | ||
user_context: Dict[str, Any], | ||
): | ||
req = get_request_from_user_context(user_context) | ||
if req: | ||
assert req.method() == "POST" | ||
assert req.get_path() == "/auth/signin" | ||
nonlocal signin_api_context_works | ||
signin_api_context_works = True | ||
|
||
return await og_sign_in_post(form_fields, api_options, user_context) | ||
|
||
param.sign_in_post = sign_in_post | ||
return param | ||
|
||
def functions_override_email_password(param: RecipeInterface): | ||
og_sign_in = param.sign_in | ||
|
||
async def sign_in(email: str, password: str, user_context: Dict[str, Any]): | ||
req = get_request_from_user_context(user_context) | ||
if req: | ||
assert req.method() == "POST" | ||
assert req.get_path() == "/auth/signin" | ||
nonlocal signin_context_works | ||
signin_context_works = True | ||
|
||
orginal_request = req | ||
user_context["_default"]["request"] = None | ||
|
||
newReq = get_request_from_user_context(user_context) | ||
assert newReq is None | ||
|
||
user_context["_default"]["request"] = orginal_request | ||
|
||
return await og_sign_in(email, password, user_context) | ||
|
||
param.sign_in = sign_in | ||
return param | ||
|
||
def functions_override_session(param: SRecipeInterface): | ||
og_create_new_session = param.create_new_session | ||
|
||
async def create_new_session( | ||
user_id: str, | ||
access_token_payload: Optional[Dict[str, Any]], | ||
session_data_in_database: Optional[Dict[str, Any]], | ||
disable_anti_csrf: Optional[bool], | ||
user_context: Dict[str, Any], | ||
): | ||
req = get_request_from_user_context(user_context) | ||
if req: | ||
assert req.method() == "POST" | ||
assert req.get_path() == "/auth/signin" | ||
nonlocal create_new_session_context_works | ||
create_new_session_context_works = True | ||
|
||
response = await og_create_new_session( | ||
user_id, | ||
access_token_payload, | ||
session_data_in_database, | ||
disable_anti_csrf, | ||
user_context, | ||
) | ||
return response | ||
|
||
param.create_new_session = create_new_session | ||
return param | ||
|
||
init( | ||
supertokens_config=SupertokensConfig("http://localhost:3567"), | ||
app_info=InputAppInfo( | ||
app_name="SuperTokens Demo", | ||
api_domain="http://api.supertokens.io", | ||
website_domain="http://supertokens.io", | ||
), | ||
framework="fastapi", | ||
recipe_list=[ | ||
emailpassword.init( | ||
override=emailpassword.InputOverrideConfig( | ||
apis=apis_override_email_password, | ||
functions=functions_override_email_password, | ||
) | ||
), | ||
session.init( | ||
override=session.InputOverrideConfig( | ||
functions=functions_override_session | ||
) | ||
), | ||
], | ||
) | ||
start_st() | ||
|
||
await sign_up("[email protected]", "validpass123", {"manualCall": True}) | ||
res = sign_in_request(driver_config_client, "[email protected]", "validpass123") | ||
|
||
assert res.status_code == 200 | ||
assert all( | ||
[ | ||
signin_api_context_works, | ||
signin_context_works, | ||
create_new_session_context_works, | ||
] | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make test better