|
| 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 | +from pytest import mark |
| 15 | +from supertokens_python.recipe import session, multitenancy, thirdparty |
| 16 | +from supertokens_python import init |
| 17 | +from supertokens_python.recipe.multitenancy.asyncio import ( |
| 18 | + create_or_update_tenant, |
| 19 | + create_or_update_third_party_config, |
| 20 | +) |
| 21 | +from supertokens_python.recipe.thirdparty.asyncio import ( |
| 22 | + manually_create_or_update_user, |
| 23 | + get_user_by_id, |
| 24 | + get_users_by_email, |
| 25 | + get_user_by_third_party_info, |
| 26 | + get_provider, |
| 27 | +) |
| 28 | +from supertokens_python.recipe.multitenancy.interfaces import TenantConfig |
| 29 | + |
| 30 | +from tests.sessions.claims.utils import get_st_init_args |
| 31 | +from tests.utils import setup_function, teardown_function, setup_multitenancy_feature |
| 32 | + |
| 33 | + |
| 34 | +_ = setup_function |
| 35 | +_ = teardown_function |
| 36 | + |
| 37 | +pytestmark = mark.asyncio |
| 38 | + |
| 39 | + |
| 40 | +async def test_multitenancy_functions(): |
| 41 | + # test that different roles can be assigned for the same user for each tenant |
| 42 | + args = get_st_init_args([session.init(), thirdparty.init(), multitenancy.init()]) |
| 43 | + init(**args) |
| 44 | + setup_multitenancy_feature() |
| 45 | + |
| 46 | + await create_or_update_tenant("t1", TenantConfig(third_party_enabled=True)) |
| 47 | + await create_or_update_tenant("t2", TenantConfig(third_party_enabled=True)) |
| 48 | + await create_or_update_tenant("t3", TenantConfig(third_party_enabled=True)) |
| 49 | + |
| 50 | + # sign up: |
| 51 | + user1a = await manually_create_or_update_user( |
| 52 | + "google", "googleid1", "[email protected]", "t1" |
| 53 | + ) |
| 54 | + user1b = await manually_create_or_update_user( |
| 55 | + "facebook", "fbid1", "[email protected]", "t1" |
| 56 | + ) |
| 57 | + user2a = await manually_create_or_update_user( |
| 58 | + "google", "googleid1", "[email protected]", "t2" |
| 59 | + ) |
| 60 | + user2b = await manually_create_or_update_user( |
| 61 | + "facebook", "fbid1", "[email protected]", "t2" |
| 62 | + ) |
| 63 | + user3a = await manually_create_or_update_user( |
| 64 | + "google", "googleid1", "[email protected]", "t3" |
| 65 | + ) |
| 66 | + user3b = await manually_create_or_update_user( |
| 67 | + "facebook", "fbid1", "[email protected]", "t3" |
| 68 | + ) |
| 69 | + |
| 70 | + assert user1a.user.tenant_ids == ["t1"] |
| 71 | + assert user1b.user.tenant_ids == ["t1"] |
| 72 | + assert user2a.user.tenant_ids == ["t2"] |
| 73 | + assert user2b.user.tenant_ids == ["t2"] |
| 74 | + assert user3a.user.tenant_ids == ["t3"] |
| 75 | + assert user3b.user.tenant_ids == ["t3"] |
| 76 | + |
| 77 | + # get user by id: |
| 78 | + g_user1a = await get_user_by_id(user1a.user.user_id) |
| 79 | + g_user1b = await get_user_by_id(user1b.user.user_id) |
| 80 | + g_user2a = await get_user_by_id(user2a.user.user_id) |
| 81 | + g_user2b = await get_user_by_id(user2b.user.user_id) |
| 82 | + g_user3a = await get_user_by_id(user3a.user.user_id) |
| 83 | + g_user3b = await get_user_by_id(user3b.user.user_id) |
| 84 | + |
| 85 | + assert g_user1a == user1a.user |
| 86 | + assert g_user1b == user1b.user |
| 87 | + assert g_user2a == user2a.user |
| 88 | + assert g_user2b == user2b.user |
| 89 | + assert g_user3a == user3a.user |
| 90 | + assert g_user3b == user3b.user |
| 91 | + |
| 92 | + # get user by email: |
| 93 | + by_email_user1 = await get_users_by_email( "[email protected]", "t1") |
| 94 | + by_email_user2 = await get_users_by_email( "[email protected]", "t2") |
| 95 | + by_email_user3 = await get_users_by_email( "[email protected]", "t3") |
| 96 | + |
| 97 | + assert by_email_user1 == [user1a.user, user1b.user] |
| 98 | + assert by_email_user2 == [user2a.user, user2b.user] |
| 99 | + assert by_email_user3 == [user3a.user, user3b.user] |
| 100 | + |
| 101 | + # get user by thirdparty id: |
| 102 | + g_user_by_tpid1a = await get_user_by_third_party_info("google", "googleid1", "t1") |
| 103 | + g_user_by_tpid1b = await get_user_by_third_party_info("facebook", "fbid1", "t1") |
| 104 | + g_user_by_tpid2a = await get_user_by_third_party_info("google", "googleid1", "t2") |
| 105 | + g_user_by_tpid2b = await get_user_by_third_party_info("facebook", "fbid1", "t2") |
| 106 | + g_user_by_tpid3a = await get_user_by_third_party_info("google", "googleid1", "t3") |
| 107 | + g_user_by_tpid3b = await get_user_by_third_party_info("facebook", "fbid1", "t3") |
| 108 | + |
| 109 | + assert g_user_by_tpid1a == user1a.user |
| 110 | + assert g_user_by_tpid1b == user1b.user |
| 111 | + assert g_user_by_tpid2a == user2a.user |
| 112 | + assert g_user_by_tpid2b == user2b.user |
| 113 | + assert g_user_by_tpid3a == user3a.user |
| 114 | + assert g_user_by_tpid3b == user3b.user |
| 115 | + |
| 116 | + |
| 117 | +async def test_get_provider(): |
| 118 | + args = get_st_init_args([session.init(), thirdparty.init(), multitenancy.init()]) |
| 119 | + init(**args) |
| 120 | + setup_multitenancy_feature() |
| 121 | + |
| 122 | + await create_or_update_tenant("t1", TenantConfig(third_party_enabled=True)) |
| 123 | + await create_or_update_tenant("t1", TenantConfig(third_party_enabled=True)) |
| 124 | + await create_or_update_tenant("t1", TenantConfig(third_party_enabled=True)) |
| 125 | + |
| 126 | + await create_or_update_third_party_config( |
| 127 | + "t1", |
| 128 | + thirdparty.ProviderConfig( |
| 129 | + "google", clients=[thirdparty.ProviderClientConfig("a")] |
| 130 | + ), |
| 131 | + ) |
| 132 | + await create_or_update_third_party_config( |
| 133 | + "t1", |
| 134 | + thirdparty.ProviderConfig( |
| 135 | + "facebook", clients=[thirdparty.ProviderClientConfig("a")] |
| 136 | + ), |
| 137 | + ) |
| 138 | + |
| 139 | + await create_or_update_third_party_config( |
| 140 | + "t2", |
| 141 | + thirdparty.ProviderConfig( |
| 142 | + "facebook", clients=[thirdparty.ProviderClientConfig("a")] |
| 143 | + ), |
| 144 | + ) |
| 145 | + await create_or_update_third_party_config( |
| 146 | + "t2", |
| 147 | + thirdparty.ProviderConfig( |
| 148 | + "discord", clients=[thirdparty.ProviderClientConfig("a")] |
| 149 | + ), |
| 150 | + ) |
| 151 | + |
| 152 | + await create_or_update_third_party_config( |
| 153 | + "t3", |
| 154 | + thirdparty.ProviderConfig( |
| 155 | + "discord", clients=[thirdparty.ProviderClientConfig("a")] |
| 156 | + ), |
| 157 | + ) |
| 158 | + await create_or_update_third_party_config( |
| 159 | + "t3", |
| 160 | + thirdparty.ProviderConfig( |
| 161 | + "linkedin", clients=[thirdparty.ProviderClientConfig("a")] |
| 162 | + ), |
| 163 | + ) |
| 164 | + |
| 165 | + provider1 = await get_provider("google", None, "t1") |
| 166 | + assert provider1.provider.config.thirdparty_id == "google" |
| 167 | + |
| 168 | + provider2 = await get_provider("facebook", None, "t1") |
| 169 | + assert provider2.provider.config.thirdparty_id == "facebook" |
| 170 | + |
| 171 | + provider3 = await get_provider("facebook", None, "t2") |
| 172 | + assert provider3.provider.config.thirdparty_id == "facebook" |
| 173 | + |
| 174 | + provider4 = await get_provider("discord", None, "t2") |
| 175 | + assert provider4.provider.config.thirdparty_id == "discord" |
| 176 | + |
| 177 | + provider5 = await get_provider("discord", None, "t3") |
| 178 | + assert provider5.provider.config.thirdparty_id == "discord" |
| 179 | + |
| 180 | + provider6 = await get_provider("linkedin", None, "t3") |
| 181 | + assert provider6.provider.config.thirdparty_id == "linkedin" |
0 commit comments