Skip to content

test: Ensure that override work as expected for Session Container methods #216

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 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

- Add to test to ensure that overrides are applying correctly in methods called on SessionContainer instances

## [0.10.2] - 2022-07-14
### Bug fix
- Make `user_context` optional in userroles recipe syncio functions.
Expand Down
61 changes: 56 additions & 5 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.

from typing import List
from typing import List, Dict, Any
from unittest.mock import MagicMock

from fastapi import FastAPI
from fastapi.requests import Request
Expand All @@ -23,7 +24,7 @@
from supertokens_python.framework.fastapi.fastapi_middleware import get_middleware
from supertokens_python.process_state import AllowedProcessStates, ProcessState
from supertokens_python.recipe import session
from supertokens_python.recipe.session import SessionRecipe
from supertokens_python.recipe.session import SessionRecipe, InputOverrideConfig
from supertokens_python.recipe.session.asyncio import (
get_all_session_handles_for_user,
get_session_information,
Expand All @@ -36,15 +37,21 @@
update_access_token_payload,
update_session_data,
)
from supertokens_python.recipe.session.interfaces import RecipeInterface
from supertokens_python.recipe.session.recipe_implementation import RecipeImplementation
from supertokens_python.recipe.session.session_functions import (
create_new_session,
get_session,
refresh_session,
revoke_session,
)
from supertokens_python.recipe.session.asyncio import (
create_new_session as async_create_new_session,
)
from tests.utils import clean_st, reset, setup_st, start_st

pytestmark = mark.asyncio


def setup_function(_):
reset()
Expand All @@ -57,7 +64,6 @@ def teardown_function(_):
clean_st()


@mark.asyncio
async def test_that_once_the_info_is_loaded_it_doesnt_query_again():
init(
supertokens_config=SupertokensConfig("http://localhost:3567"),
Expand Down Expand Up @@ -152,7 +158,6 @@ async def test_that_once_the_info_is_loaded_it_doesnt_query_again():
assert response5 is True


@mark.asyncio
async def test_creating_many_sessions_for_one_user_and_looping():
init(
supertokens_config=SupertokensConfig("http://localhost:3567"),
Expand Down Expand Up @@ -235,7 +240,6 @@ async def home(_request: Request): # type: ignore
return TestClient(app)


@mark.asyncio
async def test_signout_api_works_even_if_session_is_deleted_after_creation(
driver_config_client: TestClient,
):
Expand Down Expand Up @@ -278,3 +282,50 @@ async def test_signout_api_works_even_if_session_is_deleted_after_creation(
signout_response.headers["set-cookie"]
== """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"""
)


async def test_should_use_override_functions_in_session_container_methods():
def override_session_functions(oi: RecipeInterface) -> RecipeInterface:
oi_get_session_information = oi.get_session_information

async def get_session_information(
session_handle: str, user_context: Dict[str, Any]
):
info = await oi_get_session_information(session_handle, user_context)
assert info is not None
info.session_data["foo"] = "bar"
return info

oi.get_session_information = get_session_information

return oi

init(
supertokens_config=SupertokensConfig("http://localhost:3567"),
app_info=InputAppInfo(
app_name="SuperTokens Demo",
api_domain="https://api.supertokens.io",
website_domain="supertokens.io",
),
framework="fastapi",
recipe_list=[
session.init(
anti_csrf="VIA_TOKEN",
override=InputOverrideConfig(
functions=override_session_functions,
),
)
],
)
start_st()

s = SessionRecipe.get_instance()
if not isinstance(s.recipe_implementation, RecipeImplementation):
raise Exception("Should never come here")

mock_response = MagicMock()

my_session = await async_create_new_session(mock_response, "test_id")
data = await my_session.get_session_data()

assert data == {"foo": "bar"}