Skip to content

Commit f062633

Browse files
authored
Merge pull request #388 from supertokens/fix/ep-userroles-tenantid
fix: Set tenant_id as None by default in ep and userroles functions
2 parents f640f31 + a8a82a8 commit f062633

File tree

7 files changed

+78
-65
lines changed

7 files changed

+78
-65
lines changed

supertokens_python/recipe/emailpassword/asyncio/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async def get_user_by_id(
6666

6767
async def get_user_by_email(
6868
email: str,
69-
tenant_id: Optional[str],
69+
tenant_id: Optional[str] = None,
7070
user_context: Union[None, Dict[str, Any]] = None,
7171
) -> Union[User, None]:
7272
if user_context is None:
@@ -78,7 +78,7 @@ async def get_user_by_email(
7878

7979
async def create_reset_password_token(
8080
user_id: str,
81-
tenant_id: Optional[str],
81+
tenant_id: Optional[str] = None,
8282
user_context: Union[None, Dict[str, Any]] = None,
8383
):
8484
if user_context is None:
@@ -91,7 +91,7 @@ async def create_reset_password_token(
9191
async def reset_password_using_token(
9292
token: str,
9393
new_password: str,
94-
tenant_id: Optional[str],
94+
tenant_id: Optional[str] = None,
9595
user_context: Union[None, Dict[str, Any]] = None,
9696
):
9797
if user_context is None:
@@ -104,7 +104,7 @@ async def reset_password_using_token(
104104
async def sign_in(
105105
email: str,
106106
password: str,
107-
tenant_id: Optional[str],
107+
tenant_id: Optional[str] = None,
108108
user_context: Union[None, Dict[str, Any]] = None,
109109
):
110110
if user_context is None:
@@ -117,7 +117,7 @@ async def sign_in(
117117
async def sign_up(
118118
email: str,
119119
password: str,
120-
tenant_id: Optional[str],
120+
tenant_id: Optional[str] = None,
121121
user_context: Union[None, Dict[str, Any]] = None,
122122
):
123123
if user_context is None:

supertokens_python/recipe/emailpassword/syncio/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get_user_by_id(
5151

5252
def get_user_by_email(
5353
email: str,
54-
tenant_id: Optional[str],
54+
tenant_id: Optional[str] = None,
5555
user_context: Union[None, Dict[str, Any]] = None,
5656
) -> Union[None, User]:
5757
from supertokens_python.recipe.emailpassword.asyncio import get_user_by_email
@@ -61,7 +61,7 @@ def get_user_by_email(
6161

6262
def create_reset_password_token(
6363
user_id: str,
64-
tenant_id: Optional[str],
64+
tenant_id: Optional[str] = None,
6565
user_context: Union[None, Dict[str, Any]] = None,
6666
):
6767
from supertokens_python.recipe.emailpassword.asyncio import (
@@ -74,7 +74,7 @@ def create_reset_password_token(
7474
def reset_password_using_token(
7575
token: str,
7676
new_password: str,
77-
tenant_id: Optional[str],
77+
tenant_id: Optional[str] = None,
7878
user_context: Union[None, Dict[str, Any]] = None,
7979
):
8080
from supertokens_python.recipe.emailpassword.asyncio import (
@@ -89,7 +89,7 @@ def reset_password_using_token(
8989
def sign_in(
9090
email: str,
9191
password: str,
92-
tenant_id: Optional[str],
92+
tenant_id: Optional[str] = None,
9393
user_context: Union[None, Dict[str, Any]] = None,
9494
) -> Union[SignInOkResult, SignInWrongCredentialsError]:
9595
from supertokens_python.recipe.emailpassword.asyncio import sign_in
@@ -100,7 +100,7 @@ def sign_in(
100100
def sign_up(
101101
email: str,
102102
password: str,
103-
tenant_id: Optional[str],
103+
tenant_id: Optional[str] = None,
104104
user_context: Union[None, Dict[str, Any]] = None,
105105
):
106106
from supertokens_python.recipe.emailpassword.asyncio import sign_up

supertokens_python/recipe/userroles/asyncio/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919

2020
async def add_role_to_user(
21-
tenant_id: Optional[str],
2221
user_id: str,
2322
role: str,
23+
tenant_id: Optional[str] = None,
2424
user_context: Union[Dict[str, Any], None] = None,
2525
) -> Union[AddRoleToUserOkResult, UnknownRoleError]:
2626
if user_context is None:
@@ -31,9 +31,9 @@ async def add_role_to_user(
3131

3232

3333
async def remove_user_role(
34-
tenant_id: Optional[str],
3534
user_id: str,
3635
role: str,
36+
tenant_id: Optional[str] = None,
3737
user_context: Union[Dict[str, Any], None] = None,
3838
) -> Union[RemoveUserRoleOkResult, UnknownRoleError]:
3939
if user_context is None:
@@ -44,8 +44,8 @@ async def remove_user_role(
4444

4545

4646
async def get_roles_for_user(
47-
tenant_id: Optional[str],
4847
user_id: str,
48+
tenant_id: Optional[str] = None,
4949
user_context: Union[Dict[str, Any], None] = None,
5050
) -> GetRolesForUserOkResult:
5151
if user_context is None:
@@ -58,8 +58,8 @@ async def get_roles_for_user(
5858

5959

6060
async def get_users_that_have_role(
61-
tenant_id: Optional[str],
6261
role: str,
62+
tenant_id: Optional[str] = None,
6363
user_context: Union[Dict[str, Any], None] = None,
6464
) -> Union[GetUsersThatHaveRoleOkResult, UnknownRoleError]:
6565
if user_context is None:

supertokens_python/recipe/userroles/syncio/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,45 @@
3131

3232

3333
def add_role_to_user(
34-
tenant_id: Optional[str],
3534
user_id: str,
3635
role: str,
36+
tenant_id: Optional[str] = None,
3737
user_context: Union[Dict[str, Any], None] = None,
3838
) -> Union[AddRoleToUserOkResult, UnknownRoleError]:
3939
from supertokens_python.recipe.userroles.asyncio import add_role_to_user
4040

41-
return sync(add_role_to_user(tenant_id, user_id, role, user_context))
41+
return sync(add_role_to_user(user_id, role, tenant_id, user_context))
4242

4343

4444
def remove_user_role(
45-
tenant_id: Optional[str],
4645
user_id: str,
4746
role: str,
47+
tenant_id: Optional[str] = None,
4848
user_context: Union[Dict[str, Any], None] = None,
4949
) -> Union[RemoveUserRoleOkResult, UnknownRoleError]:
5050
from supertokens_python.recipe.userroles.asyncio import remove_user_role
5151

52-
return sync(remove_user_role(tenant_id, user_id, role, user_context))
52+
return sync(remove_user_role(user_id, role, tenant_id, user_context))
5353

5454

5555
def get_roles_for_user(
56-
tenant_id: Optional[str],
5756
user_id: str,
57+
tenant_id: Optional[str] = None,
5858
user_context: Union[Dict[str, Any], None] = None,
5959
) -> GetRolesForUserOkResult:
6060
from supertokens_python.recipe.userroles.asyncio import get_roles_for_user
6161

62-
return sync(get_roles_for_user(tenant_id, user_id, user_context))
62+
return sync(get_roles_for_user(user_id, tenant_id, user_context))
6363

6464

6565
def get_users_that_have_role(
66-
tenant_id: Optional[str],
6766
role: str,
67+
tenant_id: Optional[str] = None,
6868
user_context: Union[Dict[str, Any], None] = None,
6969
) -> Union[GetUsersThatHaveRoleOkResult, UnknownRoleError]:
7070
from supertokens_python.recipe.userroles.asyncio import get_users_that_have_role
7171

72-
return sync(get_users_that_have_role(tenant_id, role, user_context))
72+
return sync(get_users_that_have_role(role, tenant_id, user_context))
7373

7474

7575
def create_new_role_or_add_permissions(

tests/emailpassword/test_multitenancy.py

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@
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-
import setup
15-
1614
from pytest import mark
1715
from supertokens_python.recipe import session, userroles, emailpassword, multitenancy
1816
from supertokens_python import init
1917
from supertokens_python.recipe.multitenancy.asyncio import (
2018
create_or_update_tenant,
21-
associate_user_to_tenant,
2219
)
2320
from supertokens_python.recipe.emailpassword.asyncio import (
2421
sign_up,
@@ -28,14 +25,14 @@
2825
create_reset_password_token,
2926
reset_password_using_token,
3027
)
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,
28+
from supertokens_python.recipe.emailpassword.interfaces import (
29+
SignUpOkResult,
30+
SignInOkResult,
31+
CreateResetPasswordOkResult,
3632
)
33+
from supertokens_python.recipe.multitenancy.interfaces import TenantConfig
3734

38-
from tests.sessions.claims.utils import get_st_init_args
35+
from tests.utils import get_st_init_args
3936
from tests.utils import setup_function, teardown_function, setup_multitenancy_feature
4037

4138

@@ -55,16 +52,20 @@ async def test_multitenancy_in_user_roles():
5552
multitenancy.init(),
5653
]
5754
)
58-
init(**args)
55+
init(**args) # type: ignore
5956
setup_multitenancy_feature()
6057

6158
await create_or_update_tenant("t1", TenantConfig(email_password_enabled=True))
6259
await create_or_update_tenant("t2", TenantConfig(email_password_enabled=True))
6360
await create_or_update_tenant("t3", TenantConfig(email_password_enabled=True))
6461

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")
62+
user1 = await sign_up("[email protected]", "password1", "t1")
63+
user2 = await sign_up("[email protected]", "password2", "t2")
64+
user3 = await sign_up("[email protected]", "password3", "t3")
65+
66+
assert isinstance(user1, SignUpOkResult)
67+
assert isinstance(user2, SignUpOkResult)
68+
assert isinstance(user3, SignUpOkResult)
6869

6970
assert user1.user.user_id != user2.user.user_id
7071
assert user2.user.user_id != user3.user.user_id
@@ -75,9 +76,13 @@ async def test_multitenancy_in_user_roles():
7576
assert user3.user.tenant_ids == ["t3"]
7677

7778
# 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")
79+
ep_user1 = await sign_in("[email protected]", "password1", "t1")
80+
ep_user2 = await sign_in("[email protected]", "password1", "t2")
81+
ep_user3 = await sign_in("[email protected]", "password1", "t3")
82+
83+
assert isinstance(ep_user1, SignInOkResult)
84+
assert isinstance(ep_user2, SignInOkResult)
85+
assert isinstance(ep_user3, SignInOkResult)
8186

8287
assert ep_user1.user.user_id == user2.user.user_id == user3.user.user_id
8388

@@ -91,32 +96,40 @@ async def test_multitenancy_in_user_roles():
9196
assert g_user3 == user3.user
9297

9398
# 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]")
99+
by_email_user1 = await get_user_by_email("[email protected]", "t1")
100+
by_email_user2 = await get_user_by_email("[email protected]", "t2")
101+
by_email_user3 = await get_user_by_email("[email protected]", "t3")
97102

98103
assert by_email_user1 == user1.user
99104
assert by_email_user2 == user2.user
100105
assert by_email_user3 == user3.user
101106

102107
# 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)
108+
pless_reset_link1 = await create_reset_password_token(user1.user.user_id, "t1")
109+
pless_reset_link2 = await create_reset_password_token(user1.user.user_id, "t2")
110+
pless_reset_link3 = await create_reset_password_token(user1.user.user_id, "t3")
111+
112+
assert isinstance(pless_reset_link1, CreateResetPasswordOkResult)
113+
assert isinstance(pless_reset_link2, CreateResetPasswordOkResult)
114+
assert isinstance(pless_reset_link3, CreateResetPasswordOkResult)
106115

107116
assert pless_reset_link1.token is not None
108117
assert pless_reset_link2.token is not None
109118
assert pless_reset_link3.token is not None
110119

111120
# 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")
121+
await reset_password_using_token(pless_reset_link1.token, "newpassword1", "t1")
122+
await reset_password_using_token(pless_reset_link2.token, "newpassword2", "t2")
123+
await reset_password_using_token(pless_reset_link3.token, "newpassword3", "t3")
115124

116125
# 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")
126+
s_user1 = await sign_in("[email protected]", "newpassword1", "t1")
127+
s_user2 = await sign_in("[email protected]", "newpassword2", "t2")
128+
s_user3 = await sign_in("[email protected]", "newpassword3", "t3")
129+
130+
assert isinstance(s_user1, SignInOkResult)
131+
assert isinstance(s_user2, SignInOkResult)
132+
assert isinstance(s_user3, SignInOkResult)
120133

121134
assert s_user1.user == user1.user
122135
assert s_user2.user == user2.user

tests/thirdparty/test_multitenancy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
)
2828
from supertokens_python.recipe.multitenancy.interfaces import TenantConfig
2929

30-
from tests.sessions.claims.utils import get_st_init_args
30+
from tests.utils import get_st_init_args
3131
from tests.utils import setup_function, teardown_function, setup_multitenancy_feature
3232

3333

@@ -40,7 +40,7 @@
4040
async def test_multitenancy_functions():
4141
# test that different roles can be assigned for the same user for each tenant
4242
args = get_st_init_args([session.init(), thirdparty.init(), multitenancy.init()])
43-
init(**args)
43+
init(**args) # type: ignore
4444
setup_multitenancy_feature()
4545

4646
await create_or_update_tenant("t1", TenantConfig(third_party_enabled=True))

0 commit comments

Comments
 (0)