|
| 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 sign_up |
| 24 | +from supertokens_python.recipe.multitenancy.interfaces import TenantConfig |
| 25 | +from supertokens_python.recipe.userroles.asyncio import ( |
| 26 | + create_new_role_or_add_permissions, |
| 27 | + add_role_to_user, |
| 28 | + get_roles_for_user, |
| 29 | +) |
| 30 | + |
| 31 | +from tests.sessions.claims.utils import get_st_init_args |
| 32 | +from tests.utils import setup_function, teardown_function, setup_multitenancy_feature |
| 33 | + |
| 34 | + |
| 35 | +_ = setup_function |
| 36 | +_ = teardown_function |
| 37 | + |
| 38 | +pytestmark = mark.asyncio |
| 39 | + |
| 40 | + |
| 41 | +async def test_multitenancy_in_user_roles(): |
| 42 | + # test that different roles can be assigned for the same user for each tenant |
| 43 | + args = get_st_init_args( |
| 44 | + [ |
| 45 | + session.init(), |
| 46 | + userroles.init(), |
| 47 | + emailpassword.init(), |
| 48 | + multitenancy.init(), |
| 49 | + ] |
| 50 | + ) |
| 51 | + init(**args) |
| 52 | + setup_multitenancy_feature() |
| 53 | + |
| 54 | + await create_or_update_tenant("t1", TenantConfig(email_password_enabled=True)) |
| 55 | + await create_or_update_tenant("t2", TenantConfig(email_password_enabled=True)) |
| 56 | + await create_or_update_tenant("t3", TenantConfig(email_password_enabled=True)) |
| 57 | + |
| 58 | + user = await sign_up( "[email protected]", "password1") |
| 59 | + user_id = user.user.user_id |
| 60 | + |
| 61 | + await associate_user_to_tenant("t1", user_id) |
| 62 | + await associate_user_to_tenant("t2", user_id) |
| 63 | + await associate_user_to_tenant("t3", user_id) |
| 64 | + |
| 65 | + await create_new_role_or_add_permissions("role1", []) |
| 66 | + await create_new_role_or_add_permissions("role2", []) |
| 67 | + await create_new_role_or_add_permissions("role3", []) |
| 68 | + |
| 69 | + await add_role_to_user("t1", user_id, "role1") |
| 70 | + await add_role_to_user("t1", user_id, "role2") |
| 71 | + await add_role_to_user("t2", user_id, "role2") |
| 72 | + await add_role_to_user("t2", user_id, "role3") |
| 73 | + await add_role_to_user("t3", user_id, "role3") |
| 74 | + await add_role_to_user("t3", user_id, "role1") |
| 75 | + |
| 76 | + roles = await get_roles_for_user("t1", user_id) |
| 77 | + assert roles.roles == ["role1", "role2"] |
| 78 | + |
| 79 | + roles = await get_roles_for_user("t2", user_id) |
| 80 | + assert roles.roles == ["role2", "role3"] |
| 81 | + |
| 82 | + roles = await get_roles_for_user("t3", user_id) |
| 83 | + assert roles.roles == ["role1", "role3"] |
0 commit comments