|
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 |
21 | 27 | from supertokens_python.recipe.session.asyncio import ( |
22 | 28 | get_all_session_handles_for_user, |
23 | 29 | get_session_information, |
| 30 | + regenerate_access_token, |
| 31 | +) |
| 32 | +from supertokens_python.recipe.session.asyncio import ( |
| 33 | + revoke_session as asyncio_revoke_session, |
| 34 | +) |
| 35 | +from supertokens_python.recipe.session.asyncio import ( |
24 | 36 | update_access_token_payload, |
25 | 37 | update_session_data, |
26 | | - regenerate_access_token, |
27 | 38 | ) |
28 | 39 | from supertokens_python.recipe.session.recipe_implementation import RecipeImplementation |
29 | 40 | from supertokens_python.recipe.session.session_functions import ( |
|
32 | 43 | refresh_session, |
33 | 44 | revoke_session, |
34 | 45 | ) |
35 | | - |
36 | 46 | from tests.utils import clean_st, reset, setup_st, start_st |
37 | 47 |
|
38 | 48 |
|
@@ -211,3 +221,60 @@ async def test_creating_many_sessions_for_one_user_and_looping(): |
211 | 221 | assert is_updated is False |
212 | 222 | is_updated = await update_session_data("invalidHandle", {"foo": "bar"}) |
213 | 223 | assert is_updated is False |
| 224 | + |
| 225 | + |
| 226 | +@fixture(scope="function") |
| 227 | +async def driver_config_client(): |
| 228 | + app = FastAPI() |
| 229 | + app.add_middleware(get_middleware()) |
| 230 | + |
| 231 | + @app.get("/") |
| 232 | + async def home(_request: Request): # type: ignore |
| 233 | + return {"hello": "world"} |
| 234 | + |
| 235 | + return TestClient(app) |
| 236 | + |
| 237 | + |
| 238 | +@mark.asyncio |
| 239 | +async def test_signout_api_works_even_if_session_is_deleted_after_creation( |
| 240 | + driver_config_client: TestClient, |
| 241 | +): |
| 242 | + init( |
| 243 | + supertokens_config=SupertokensConfig("http://localhost:3567"), |
| 244 | + app_info=InputAppInfo( |
| 245 | + app_name="SuperTokens Demo", |
| 246 | + api_domain="https://api.supertokens.io", |
| 247 | + website_domain="supertokens.io", |
| 248 | + ), |
| 249 | + framework="fastapi", |
| 250 | + recipe_list=[session.init(anti_csrf="VIA_TOKEN")], |
| 251 | + ) |
| 252 | + start_st() |
| 253 | + |
| 254 | + s = SessionRecipe.get_instance() |
| 255 | + if not isinstance(s.recipe_implementation, RecipeImplementation): |
| 256 | + raise Exception("Should never come here") |
| 257 | + user_id = "user_id" |
| 258 | + |
| 259 | + response = await create_new_session(s.recipe_implementation, user_id, {}, {}) |
| 260 | + |
| 261 | + session_handle = response["session"]["handle"] |
| 262 | + |
| 263 | + revoked = await asyncio_revoke_session(session_handle) |
| 264 | + assert revoked |
| 265 | + |
| 266 | + signout_response = driver_config_client.post( |
| 267 | + url="/auth/signout", |
| 268 | + cookies={ |
| 269 | + "sAccessToken": response["accessToken"]["token"], |
| 270 | + "sIdRefreshToken": response["idRefreshToken"]["token"], |
| 271 | + }, |
| 272 | + headers={"anti-csrf": response.get("antiCsrfToken", "")}, |
| 273 | + ) |
| 274 | + |
| 275 | + assert signout_response.json() == {"status": "OK"} |
| 276 | + |
| 277 | + assert ( |
| 278 | + signout_response.headers["set-cookie"] |
| 279 | + == """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""" |
| 280 | + ) |
0 commit comments