Skip to content

Commit ed3a1cb

Browse files
committed
test: Add more tests for session claim features
1 parent 349596a commit ed3a1cb

File tree

8 files changed

+536
-0
lines changed

8 files changed

+536
-0
lines changed

tests/sessions/__init__.py

Whitespace-only changes.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import time
2+
from unittest.mock import MagicMock
3+
4+
from supertokens_python import init
5+
from supertokens_python.framework import BaseRequest
6+
from supertokens_python.recipe import session
7+
from supertokens_python.recipe.session.asyncio import create_new_session
8+
from tests.utils import setup_function, teardown_function, start_st, min_api_version
9+
from .utils import (
10+
st_init_args,
11+
NoneClaim,
12+
get_st_init_args,
13+
session_functions_override_with_claim,
14+
TrueClaim,
15+
)
16+
17+
_ = setup_function # type:ignore
18+
_ = teardown_function # type:ignore
19+
20+
timestamp = time.time()
21+
22+
23+
@min_api_version("2.13")
24+
async def test_create_access_token_payload_with_session_claims():
25+
init(**st_init_args) # type:ignore
26+
start_st()
27+
28+
dummy_req: BaseRequest = MagicMock()
29+
s = await create_new_session(dummy_req, "someId")
30+
31+
payload = s.get_access_token_payload()
32+
assert payload["st-true"]["t"] > 0
33+
payload["st-true"]["t"] = timestamp # to avoid patching
34+
assert payload == {"st-true": {"v": True, "t": timestamp}}
35+
36+
37+
@min_api_version("2.13")
38+
async def test_should_create_access_token_payload_with_session_claims_with_an_none_value():
39+
init(**get_st_init_args(NoneClaim)) # type:ignore
40+
start_st()
41+
42+
dummy_req: BaseRequest = MagicMock()
43+
s = await create_new_session(dummy_req, "someId")
44+
45+
payload = s.get_access_token_payload()
46+
assert payload == {}
47+
48+
49+
@min_api_version("2.13")
50+
async def test_should_merge_claims_and_passed_access_token_payload_obj():
51+
new_st_init = {
52+
**st_init_args,
53+
"recipe_list": [
54+
session.init(
55+
override=session.InputOverrideConfig(
56+
functions=session_functions_override_with_claim(
57+
TrueClaim, {"user-custom-claim": "foo"}
58+
),
59+
)
60+
),
61+
],
62+
}
63+
init(**new_st_init) # type:ignore
64+
start_st()
65+
66+
dummy_req: BaseRequest = MagicMock()
67+
s = await create_new_session(dummy_req, "someId")
68+
69+
payload = s.get_access_token_payload()
70+
assert payload["st-true"]["t"] > 0
71+
payload["st-true"]["t"] = timestamp # to avoid patching
72+
assert payload == {
73+
"st-true": {"v": True, "t": timestamp},
74+
"user-custom-claim": "foo",
75+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import time
2+
from unittest.mock import AsyncMock, patch
3+
4+
from pytest import mark
5+
6+
from supertokens_python.recipe.session.session_class import Session
7+
from tests.sessions.claims.utils import NoneClaim, TrueClaim
8+
9+
timestamp = time.time()
10+
11+
pytestmark = (
12+
mark.asyncio
13+
) # no need to apply @mark.asyncio on each test because of this!
14+
15+
16+
async def test_should_not_change_if_claim_fetch_value_returns_none():
17+
recipe_implementation_mock = AsyncMock()
18+
session = Session(
19+
recipe_implementation_mock,
20+
"test_access_token",
21+
"test_session_handle",
22+
"test_user_id",
23+
{},
24+
)
25+
with patch.object(
26+
Session,
27+
"merge_into_access_token_payload",
28+
wraps=session.merge_into_access_token_payload,
29+
) as mock:
30+
await session.fetch_and_set_claim(NoneClaim)
31+
mock.assert_called_once_with({}, None)
32+
33+
34+
async def test_should_update_if_claim_fetch_value_returns_value():
35+
recipe_implementation_mock = AsyncMock()
36+
session = Session(
37+
recipe_implementation_mock,
38+
"test_access_token",
39+
"test_session_handle",
40+
"test_user_id",
41+
{},
42+
)
43+
with patch.object(
44+
Session,
45+
"merge_into_access_token_payload",
46+
wraps=session.merge_into_access_token_payload,
47+
) as mock:
48+
await session.fetch_and_set_claim(TrueClaim)
49+
update, _ = mock.call_args_list[0].args
50+
assert update["st-true"]["t"] > 0
51+
update["st-true"]["t"] = timestamp
52+
mock.assert_called_once_with({"st-true": {"t": timestamp, "v": True}}, None)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from unittest.mock import MagicMock
2+
3+
from pytest import mark, raises
4+
5+
from supertokens_python import init
6+
from supertokens_python.framework.request import BaseRequest
7+
from supertokens_python.recipe import session
8+
from supertokens_python.recipe.session.asyncio import (
9+
create_new_session,
10+
get_claim_value,
11+
)
12+
from supertokens_python.recipe.session.interfaces import SessionContainer
13+
from tests.utils import setup_function, teardown_function, start_st
14+
from .utils import TrueClaim, st_init_args
15+
16+
_ = setup_function # type:ignore
17+
_ = teardown_function # type:ignore
18+
19+
20+
@mark.asyncio
21+
async def test_should_get_the_right_value():
22+
init(**st_init_args) # type:ignore
23+
start_st()
24+
25+
dummy_req: BaseRequest = MagicMock()
26+
s = await create_new_session(dummy_req, "someId")
27+
28+
res = await s.get_claim_value(TrueClaim)
29+
assert res is True
30+
31+
32+
@mark.asyncio
33+
async def test_should_get_the_right_value_using_session_handle():
34+
init(**st_init_args) # type:ignore
35+
start_st()
36+
37+
dummy_req: BaseRequest = MagicMock()
38+
s: SessionContainer = await create_new_session(dummy_req, "someId")
39+
40+
res = await get_claim_value(s.get_handle(), TrueClaim)
41+
assert res is True
42+
43+
44+
@mark.asyncio
45+
async def test_should_throw_for_non_existing_handle():
46+
new_st_init = {**st_init_args, "recipe_list": [session.init()]}
47+
init(**new_st_init) # type: ignore
48+
start_st()
49+
50+
with raises(Exception) as e:
51+
_ = await get_claim_value("non_existing_handle", TrueClaim)
52+
assert str(e) == "Session does not exist"
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import time as real_time
2+
from unittest.mock import MagicMock, AsyncMock, patch
3+
4+
from pytest import mark
5+
6+
from supertokens_python import init
7+
from supertokens_python.framework import BaseRequest
8+
from supertokens_python.recipe.session import SessionContainer
9+
from supertokens_python.recipe.session.asyncio import create_new_session
10+
from supertokens_python.recipe.session.asyncio import (
11+
remove_claim,
12+
get_session_information,
13+
)
14+
from supertokens_python.recipe.session.session_class import Session
15+
from tests.sessions.claims.utils import TrueClaim
16+
from tests.utils import start_st, setup_function, teardown_function
17+
from .test_get_claims_value import st_init_args
18+
from .utils import time_patch_wrapper
19+
20+
_ = setup_function # type:ignore
21+
_ = teardown_function # type:ignore
22+
23+
timestamp = real_time.time()
24+
25+
26+
@mark.asyncio
27+
async def test_should_attempt_to_set_claim_to_none():
28+
recipe_implementation_mock = AsyncMock()
29+
session = Session(
30+
recipe_implementation_mock,
31+
"test_access_token",
32+
"test_session_handle",
33+
"test_user_id",
34+
{},
35+
)
36+
with patch.object(
37+
Session,
38+
"merge_into_access_token_payload",
39+
wraps=session.merge_into_access_token_payload,
40+
) as mock:
41+
await session.remove_claim(TrueClaim)
42+
mock.assert_called_once_with({"st-true": None}, None)
43+
44+
45+
@time_patch_wrapper
46+
async def test_should_clear_previously_set_claim(time_mock: MagicMock):
47+
time_mock.time.return_value = timestamp # type: ignore
48+
init(**st_init_args) # type:ignore
49+
start_st()
50+
51+
dummy_req: BaseRequest = MagicMock()
52+
s: SessionContainer = await create_new_session(dummy_req, "someId")
53+
54+
payload = s.get_access_token_payload()
55+
56+
assert payload == {"st-true": {"v": True, "t": timestamp}}
57+
58+
59+
@time_patch_wrapper
60+
async def test_should_clear_previously_set_claim_using_handle(time_mock: MagicMock):
61+
time_mock.time.return_value = timestamp # type: ignore
62+
init(**st_init_args) # type:ignore
63+
start_st()
64+
65+
dummy_req: BaseRequest = MagicMock()
66+
s: SessionContainer = await create_new_session(dummy_req, "someId")
67+
68+
payload = s.get_access_token_payload()
69+
assert payload == {"st-true": {"v": True, "t": timestamp}}
70+
71+
res = await remove_claim(s.get_handle(), TrueClaim)
72+
assert res is True
73+
74+
session_info = await get_session_information(s.get_handle())
75+
assert session_info is not None
76+
payload_after = session_info.access_token_payload
77+
assert payload_after == {}
78+
79+
80+
@time_patch_wrapper
81+
async def test_should_work_ok_for_non_existing_handle(_time_mock: MagicMock):
82+
init(**st_init_args) # type:ignore
83+
start_st()
84+
85+
res = await remove_claim("non-existing-handle", TrueClaim)
86+
assert res is False
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import time
2+
from unittest.mock import AsyncMock, patch, MagicMock
3+
4+
from pytest import mark
5+
6+
from supertokens_python import init
7+
from supertokens_python.framework import BaseRequest
8+
from supertokens_python.recipe.session.asyncio import (
9+
create_new_session,
10+
get_session_information,
11+
set_claim_value,
12+
)
13+
from supertokens_python.recipe.session.session_class import Session
14+
from tests.sessions.claims.utils import TrueClaim, st_init_args
15+
from tests.utils import setup_function, teardown_function
16+
from tests.utils import start_st
17+
18+
_ = setup_function # type:ignore
19+
_ = teardown_function # type:ignore
20+
21+
timestamp = time.time()
22+
23+
pytestmark = (
24+
mark.asyncio
25+
) # no need to apply @mark.asyncio on each test because of this!
26+
27+
28+
async def test_should_merge_the_right_value():
29+
recipe_implementation_mock = AsyncMock()
30+
session = Session(
31+
recipe_implementation_mock,
32+
"test_access_token",
33+
"test_session_handle",
34+
"test_user_id",
35+
{},
36+
)
37+
with patch.object(
38+
Session,
39+
"merge_into_access_token_payload",
40+
wraps=session.merge_into_access_token_payload,
41+
) as mock:
42+
await session.set_claim_value(TrueClaim, "NEW_TRUE")
43+
update, _ = mock.call_args_list[0].args
44+
assert update["st-true"]["t"] > 0
45+
update["st-true"]["t"] = timestamp
46+
mock.assert_called_once_with(
47+
{"st-true": {"t": timestamp, "v": "NEW_TRUE"}}, None
48+
)
49+
50+
51+
async def test_should_overwrite_claim_value():
52+
init(**st_init_args) # type: ignore
53+
start_st()
54+
55+
dummy_req: BaseRequest = MagicMock()
56+
s = await create_new_session(dummy_req, "someId")
57+
58+
payload = s.get_access_token_payload()
59+
assert payload["st-true"]["t"] > 0
60+
payload["st-true"]["t"] = timestamp
61+
assert payload == {"st-true": {"t": timestamp, "v": True}}
62+
63+
await s.set_claim_value(TrueClaim, "NEW_TRUE")
64+
65+
# Payload should be updated now:
66+
payload = s.get_access_token_payload()
67+
assert payload["st-true"]["t"] > 0
68+
payload["st-true"]["t"] = timestamp
69+
assert payload == {"st-true": {"t": timestamp, "v": "NEW_TRUE"}}
70+
71+
72+
async def test_should_overwrite_claim_value_using_session_handle():
73+
init(**st_init_args) # type: ignore
74+
start_st()
75+
76+
dummy_req: BaseRequest = MagicMock()
77+
s = await create_new_session(dummy_req, "someId")
78+
79+
payload = s.get_access_token_payload()
80+
assert payload["st-true"]["t"] > 0
81+
payload["st-true"]["t"] = timestamp
82+
assert payload == {"st-true": {"t": timestamp, "v": True}}
83+
84+
await set_claim_value(s.get_handle(), TrueClaim, "NEW_TRUE")
85+
86+
# Payload should be updated now:
87+
# Note that the session var (s) still contains the old payload.
88+
# We need to fetch the new one.
89+
s = await get_session_information(s.get_handle())
90+
assert s is not None
91+
payload = s.access_token_payload
92+
assert payload["st-true"]["t"] > 0
93+
payload["st-true"]["t"] = timestamp
94+
assert payload == {"st-true": {"t": timestamp, "v": "NEW_TRUE"}}
95+
96+
97+
async def test_should_work_ok_for_non_existing_handles():
98+
init(**st_init_args) # type: ignore
99+
start_st()
100+
101+
res = await set_claim_value("non-existing-handle", TrueClaim, "NEW_TRUE")
102+
assert res is False

0 commit comments

Comments
 (0)