Skip to content

Commit d020f1f

Browse files
committed
test: Add test for emailpassword multitenancy
1 parent e16f262 commit d020f1f

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

supertokens_python/recipe/emailpassword/types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
14+
from __future__ import annotations
1415
from typing import Awaitable, Callable, List, TypeVar, Union
1516

1617
from supertokens_python.ingredients.emaildelivery import EmailDeliveryIngredient
@@ -29,6 +30,15 @@ def __init__(
2930
self.time_joined = time_joined
3031
self.tenant_ids = tenant_ids
3132

33+
def __eq__(self, other: 'User'):
34+
return (
35+
isinstance(other, self.__class__)
36+
and self.user_id == other.user_id
37+
and self.email == other.email
38+
and self.time_joined == other.time_joined
39+
and self.tenant_ids == other.tenant_ids
40+
)
41+
3242

3343
class UsersResponse:
3444
def __init__(self, users: List[User], next_pagination_token: Union[str, None]):
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved.
2+
#
3+
# This software is licensed under the Apache License, Version 2.0 (the
4+
# "License") as published by the Apache Software Foundation.
5+
#
6+
# You may not use this file except in compliance with the License. You may
7+
# obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
import setup
15+
16+
from pytest import mark
17+
from supertokens_python.recipe import session, userroles, emailpassword, multitenancy
18+
from supertokens_python import init
19+
from supertokens_python.recipe.multitenancy.asyncio import (
20+
create_or_update_tenant,
21+
associate_user_to_tenant,
22+
)
23+
from supertokens_python.recipe.emailpassword.asyncio import (
24+
sign_up,
25+
sign_in,
26+
get_user_by_id,
27+
get_user_by_email,
28+
create_reset_password_token,
29+
reset_password_using_token,
30+
)
31+
from supertokens_python.recipe.multitenancy.interfaces import TenantConfig
32+
from supertokens_python.recipe.userroles.asyncio import (
33+
create_new_role_or_add_permissions,
34+
add_role_to_user,
35+
get_roles_for_user,
36+
)
37+
38+
from tests.sessions.claims.utils import get_st_init_args
39+
from tests.utils import setup_function, teardown_function, setup_multitenancy_feature
40+
41+
42+
_ = setup_function
43+
_ = teardown_function
44+
45+
pytestmark = mark.asyncio
46+
47+
48+
async def test_multitenancy_in_user_roles():
49+
# test that different roles can be assigned for the same user for each tenant
50+
args = get_st_init_args(
51+
[
52+
session.init(),
53+
userroles.init(),
54+
emailpassword.init(),
55+
multitenancy.init(),
56+
]
57+
)
58+
init(**args)
59+
setup_multitenancy_feature()
60+
61+
await create_or_update_tenant("t1", TenantConfig(email_password_enabled=True))
62+
await create_or_update_tenant("t2", TenantConfig(email_password_enabled=True))
63+
await create_or_update_tenant("t3", TenantConfig(email_password_enabled=True))
64+
65+
user1 = await sign_up("t1", "[email protected]", "password1")
66+
user2 = await sign_up("t2", "[email protected]", "password2")
67+
user3 = await sign_up("t3", "[email protected]", "password3")
68+
69+
assert user1.user.user_id != user2.user.user_id
70+
assert user2.user.user_id != user3.user.user_id
71+
assert user3.user.user_id != user1.user.user_id
72+
73+
assert user1.user.tenant_ids == ["t1"]
74+
assert user2.user.tenant_ids == ["t2"]
75+
assert user3.user.tenant_ids == ["t3"]
76+
77+
# sign in
78+
ep_user1 = await sign_in("t1", "[email protected]", "password1")
79+
ep_user2 = await sign_in("t2", "[email protected]", "password1")
80+
ep_user3 = await sign_in("t3", "[email protected]", "password1")
81+
82+
assert ep_user1.user.user_id == user2.user.user_id == user3.user.user_id
83+
84+
# get user by id:
85+
g_user1 = await get_user_by_id(user1.user.user_id)
86+
g_user2 = await get_user_by_id(user2.user.user_id)
87+
g_user3 = await get_user_by_id(user3.user.user_id)
88+
89+
assert g_user1 == user1.user
90+
assert g_user2 == user2.user
91+
assert g_user3 == user3.user
92+
93+
# get user by email:
94+
by_email_user1 = await get_user_by_email("t1", "[email protected]")
95+
by_email_user2 = await get_user_by_email("t2", "[email protected]")
96+
by_email_user3 = await get_user_by_email("t3", "[email protected]")
97+
98+
assert by_email_user1 == user1.user
99+
assert by_email_user2 == user2.user
100+
assert by_email_user3 == user3.user
101+
102+
# create password reset token:
103+
pless_reset_link1 = await create_reset_password_token("t1", user1.user.user_id)
104+
pless_reset_link2 = await create_reset_password_token("t2", user1.user.user_id)
105+
pless_reset_link3 = await create_reset_password_token("t3", user1.user.user_id)
106+
107+
assert pless_reset_link1.token is not None
108+
assert pless_reset_link2.token is not None
109+
assert pless_reset_link3.token is not None
110+
111+
# reset password using token:
112+
await reset_password_using_token("t1", pless_reset_link1.token, "newpassword1")
113+
await reset_password_using_token("t2", pless_reset_link1.token, "newpassword2")
114+
await reset_password_using_token("t3", pless_reset_link1.token, "newpassword3")
115+
116+
# new password should work:
117+
s_user1 = await sign_in("t1", "[email protected]", "newpassword1")
118+
s_user2 = await sign_in("t2", "[email protected]", "newpassword2")
119+
s_user3 = await sign_in("t3", "[email protected]", "newpassword3")
120+
121+
assert s_user1.user == user1.user
122+
assert s_user2.user == user2.user
123+
assert s_user3.user == user3.user

0 commit comments

Comments
 (0)