|
12 | 12 | # License for the specific language governing permissions and limitations |
13 | 13 | # under the License. |
14 | 14 |
|
15 | | -from pytest import mark |
16 | 15 | from typing import List |
| 16 | + |
| 17 | +from fastapi import FastAPI |
| 18 | +from fastapi.requests import Request |
| 19 | +from fastapi.testclient import TestClient |
| 20 | +from pytest import fixture, mark |
| 21 | + |
17 | 22 | from supertokens_python import InputAppInfo, SupertokensConfig, init |
| 23 | +from supertokens_python.framework.fastapi.fastapi_middleware import get_middleware |
18 | 24 | from supertokens_python.process_state import AllowedProcessStates, ProcessState |
19 | 25 | from supertokens_python.recipe import session |
20 | 26 | from supertokens_python.recipe.session import SessionRecipe |
| 27 | +from supertokens_python.recipe.session.asyncio import ( |
| 28 | + create_new_session as asyncio_create_new_session, |
| 29 | +) |
21 | 30 | from supertokens_python.recipe.session.asyncio import ( |
22 | 31 | get_all_session_handles_for_user, |
23 | 32 | get_session_information, |
| 33 | + regenerate_access_token, |
| 34 | +) |
| 35 | +from supertokens_python.recipe.session.asyncio import ( |
| 36 | + revoke_session as asyncio_revoke_session, |
| 37 | +) |
| 38 | +from supertokens_python.recipe.session.asyncio import ( |
24 | 39 | update_access_token_payload, |
25 | 40 | update_session_data, |
26 | | - regenerate_access_token, |
27 | 41 | ) |
28 | 42 | from supertokens_python.recipe.session.recipe_implementation import RecipeImplementation |
29 | 43 | from supertokens_python.recipe.session.session_functions import ( |
|
32 | 46 | refresh_session, |
33 | 47 | revoke_session, |
34 | 48 | ) |
35 | | - |
36 | 49 | from tests.utils import clean_st, reset, setup_st, start_st |
37 | 50 |
|
38 | 51 |
|
@@ -211,3 +224,70 @@ async def test_creating_many_sessions_for_one_user_and_looping(): |
211 | 224 | assert is_updated is False |
212 | 225 | is_updated = await update_session_data("invalidHandle", {"foo": "bar"}) |
213 | 226 | assert is_updated is False |
| 227 | + |
| 228 | + |
| 229 | +@fixture(scope="function") |
| 230 | +async def driver_config_client(): |
| 231 | + app = FastAPI() |
| 232 | + app.add_middleware(get_middleware()) |
| 233 | + |
| 234 | + @app.get("/create") |
| 235 | + async def create(request: Request): # type: ignore |
| 236 | + session = await asyncio_create_new_session(request, "", {}, {}) |
| 237 | + session_handle = session.get_handle() |
| 238 | + return {"session_handle": session_handle, "access_token": session.access_token} |
| 239 | + |
| 240 | + @app.get("/revoke") |
| 241 | + async def revoke(request: Request): # type: ignore |
| 242 | + # session_handle = request.json()["session_handle"] |
| 243 | + # await asyncio_get_session(request, True, True) |
| 244 | + session = await asyncio_create_new_session(request, "", {}, {}) |
| 245 | + session_handle = session.get_handle() |
| 246 | + return {"session_handle": session_handle, "access_token": session.access_token} |
| 247 | + |
| 248 | + return TestClient(app) |
| 249 | + |
| 250 | + |
| 251 | +@mark.asyncio |
| 252 | +async def test_signout_api_works_even_if_session_is_deleted_after_creation( |
| 253 | + driver_config_client: TestClient, |
| 254 | +): |
| 255 | + init( |
| 256 | + supertokens_config=SupertokensConfig("http://localhost:3567"), |
| 257 | + app_info=InputAppInfo( |
| 258 | + app_name="SuperTokens Demo", |
| 259 | + api_domain="https://api.supertokens.io", |
| 260 | + website_domain="supertokens.io", |
| 261 | + ), |
| 262 | + framework="fastapi", |
| 263 | + recipe_list=[session.init(anti_csrf="VIA_TOKEN")], |
| 264 | + ) |
| 265 | + start_st() |
| 266 | + |
| 267 | + s = SessionRecipe.get_instance() |
| 268 | + if not isinstance(s.recipe_implementation, RecipeImplementation): |
| 269 | + raise Exception("Should never come here") |
| 270 | + user_id = "user_id" |
| 271 | + |
| 272 | + response = await create_new_session(s.recipe_implementation, user_id, {}, {}) |
| 273 | + |
| 274 | + session_handle = response["session"]["handle"] |
| 275 | + |
| 276 | + revoked = await asyncio_revoke_session(session_handle) |
| 277 | + assert revoked |
| 278 | + |
| 279 | + signout_response = driver_config_client.post( |
| 280 | + url="/auth/signout", |
| 281 | + cookies={ |
| 282 | + "sAccessToken": response["accessToken"]["token"], |
| 283 | + "sIdRefreshToken": response["idRefreshToken"]["token"], |
| 284 | + }, |
| 285 | + headers={"anti-csrf": response.get("antiCsrfToken", "")}, |
| 286 | + ) |
| 287 | + |
| 288 | + assert signout_response.json() == {"status": "OK"} |
| 289 | + |
| 290 | + assert ( |
| 291 | + signout_response.headers["set-cookie"] |
| 292 | + == """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""" |
| 293 | + ) |
0 commit comments