Skip to content

Commit e7f89c0

Browse files
committed
test: Fix failing tests and use url decode for token cookies
1 parent 6f1262f commit e7f89c0

File tree

3 files changed

+25
-45
lines changed

3 files changed

+25
-45
lines changed

tests/Django/test_django.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
from django.test import RequestFactory, TestCase
2222
from supertokens_python import InputAppInfo, SupertokensConfig, init
2323
from supertokens_python.framework.django import middleware
24+
from supertokens_python.framework.django.django_response import (
25+
DjangoResponse as SuperTokensDjangoWrapper,
26+
)
2427
from supertokens_python.recipe import emailpassword, session
2528
from supertokens_python.recipe.emailpassword.interfaces import APIInterface, APIOptions
2629
from supertokens_python.recipe.session import SessionContainer
@@ -391,18 +394,11 @@ async def test_optional_session(self):
391394
assert dict_response["s"] == "empty session"
392395

393396

394-
from django.http import HttpResponse
395-
from supertokens_python.framework.django.django_response import (
396-
DjangoResponse as SuperTokensDjangoWrapper,
397-
)
398-
399-
400-
class SupertokensResponseTest(TestCase):
401-
def test_remove_header_works(self):
402-
response = HttpResponse()
403-
st_response = SuperTokensDjangoWrapper(response)
397+
def test_remove_header_works():
398+
response = HttpResponse()
399+
st_response = SuperTokensDjangoWrapper(response)
404400

405-
st_response.set_header("foo", "bar")
406-
assert st_response.get_header("foo") == "bar"
407-
st_response.remove_header("foo")
408-
assert st_response.get_header("foo") is None
401+
st_response.set_header("foo", "bar")
402+
assert st_response.get_header("foo") == "bar"
403+
st_response.remove_header("foo")
404+
assert st_response.get_header("foo") is None

tests/test_session.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,12 @@ async def get_session_information(
355355

356356
from supertokens_python.recipe.session.exceptions import raise_unauthorised_exception
357357
from supertokens_python.recipe.session.interfaces import APIInterface, APIOptions
358-
from tests.utils import extract_all_cookies, get_st_init_args
358+
from tests.utils import (
359+
extract_all_cookies,
360+
get_st_init_args,
361+
extract_info,
362+
assert_info_clears_tokens,
363+
)
359364

360365

361366
async def test_revoking_session_during_refresh_with_revoke_session_with_200(
@@ -402,18 +407,8 @@ async def refresh_post(api_options: APIOptions, user_context: Dict[str, Any]):
402407
)
403408

404409
assert response.status_code == 200
405-
cookies = extract_all_cookies(response)
406-
407-
assert response.headers["anti-csrf"] != ""
408-
assert response.headers["front-token"] != ""
409-
410-
assert cookies["sAccessToken"]["value"] == ""
411-
assert cookies["sRefreshToken"]["value"] == ""
412-
assert cookies["sAccessToken"]["expires"] == "Thu, 01 Jan 1970 00:00:00 GMT"
413-
assert cookies["sRefreshToken"]["expires"] == "Thu, 01 Jan 1970 00:00:00 GMT"
414-
415-
assert cookies["sAccessToken"]["domain"] == ""
416-
assert cookies["sRefreshToken"]["domain"] == ""
410+
info = extract_info(response)
411+
assert_info_clears_tokens(info, "cookie")
417412

418413

419414
async def test_revoking_session_during_refresh_with_revoke_session_sending_401(
@@ -464,18 +459,8 @@ async def refresh_post(api_options: APIOptions, user_context: Dict[str, Any]):
464459
)
465460

466461
assert response.status_code == 401
467-
cookies = extract_all_cookies(response)
468-
469-
assert response.headers["anti-csrf"] != ""
470-
assert response.headers["front-token"] != ""
471-
472-
assert cookies["sAccessToken"]["value"] == ""
473-
assert cookies["sRefreshToken"]["value"] == ""
474-
assert cookies["sAccessToken"]["expires"] == "Thu, 01 Jan 1970 00:00:00 GMT"
475-
assert cookies["sRefreshToken"]["expires"] == "Thu, 01 Jan 1970 00:00:00 GMT"
476-
477-
assert cookies["sAccessToken"]["domain"] == ""
478-
assert cookies["sRefreshToken"]["domain"] == ""
462+
info = extract_info(response)
463+
assert_info_clears_tokens(info, "cookie")
479464

480465

481466
async def test_revoking_session_during_refresh_and_throw_unauthorized(

tests/utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
# under the License.
1414
import asyncio
1515
from datetime import datetime, timezone
16+
from urllib.parse import unquote
1617
from http.cookies import SimpleCookie
1718
from os import environ, kill, remove, scandir
1819
from shutil import rmtree
1920
from signal import SIGTERM
2021
from subprocess import DEVNULL, run
2122
from time import sleep
22-
from typing import Any, Dict, List, cast, Optional
23+
from typing import Any, Dict, List, cast
2324

2425
from requests.models import Response
2526
from yaml import FullLoader, dump, load
@@ -244,8 +245,8 @@ def extract_info(response: Response) -> Dict[str, Any]:
244245

245246
return {
246247
**cookies,
247-
"accessToken": access_token,
248-
"refreshToken": refresh_token,
248+
"accessToken": None if access_token is None else unquote(access_token),
249+
"refreshToken": None if refresh_token is None else unquote(refresh_token),
249250
"frontToken": response.headers.get("front-token"),
250251
"status_code": response.status_code,
251252
"body": response.json(),
@@ -521,9 +522,7 @@ async def __call__( # pylint: disable=invalid-overridden-method, useless-super-
521522
}
522523

523524

524-
def get_st_init_args(
525-
recipe_list: List[Any]
526-
) -> Dict[str, Any]:
525+
def get_st_init_args(recipe_list: List[Any]) -> Dict[str, Any]:
527526
return {**st_init_common_args, "recipe_list": recipe_list}
528527

529528

0 commit comments

Comments
 (0)