Skip to content

Commit 248678b

Browse files
committed
fix: handle tenant not found error
1 parent 87b74e7 commit 248678b

File tree

18 files changed

+73
-34
lines changed

18 files changed

+73
-34
lines changed

supertokens_python/recipe/multitenancy/api/implementation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ async def login_methods_get(
4444
tenant_id, user_context
4545
)
4646

47+
if tenant_config_res is None:
48+
raise Exception("Tenant not found")
49+
4750
provider_inputs_from_static = api_options.static_third_party_providers
4851
provider_configs_from_core = tenant_config_res.third_party.providers
4952

@@ -61,6 +64,11 @@ async def login_methods_get(
6164
client_type,
6265
user_context,
6366
)
67+
68+
if provider_instance is None:
69+
# because creating instance from the merged provider list itself
70+
raise Exception("Should never come here")
71+
6472
final_provider_list.append(
6573
ThirdPartyProvider(
6674
provider_instance.id, provider_instance.config.name

supertokens_python/recipe/multitenancy/asyncio/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async def delete_tenant(
6161

6262
async def get_tenant(
6363
tenant_id: str, user_context: Optional[Dict[str, Any]] = None
64-
) -> GetTenantOkResult:
64+
) -> Optional[GetTenantOkResult]:
6565
if user_context is None:
6666
user_context = {}
6767
recipe = MultitenancyRecipe.get_instance()

supertokens_python/recipe/multitenancy/interfaces.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ async def delete_tenant(
217217
@abstractmethod
218218
async def get_tenant(
219219
self, tenant_id: str, user_context: Dict[str, Any]
220-
) -> GetTenantOkResult:
220+
) -> Optional[GetTenantOkResult]:
221221
pass
222222

223223
@abstractmethod

supertokens_python/recipe/multitenancy/recipe.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ async def login_methods_get(
216216
tenant_id, user_context
217217
)
218218

219+
if tenant_config is None:
220+
raise Exception("Tenant not found")
221+
219222
provider_inputs_from_static = api_options.static_third_party_providers
220223
provider_configs_from_core = tenant_config.third_party.providers
221224

@@ -233,6 +236,10 @@ async def login_methods_get(
233236
client_type,
234237
user_context,
235238
)
239+
240+
if provider_instance is None:
241+
raise Exception("Should never come here")
242+
236243
except ClientTypeNotFoundError:
237244
continue
238245
final_provider_list.append(

supertokens_python/recipe/multitenancy/recipe_implementation.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,16 @@ async def delete_tenant(
160160

161161
async def get_tenant(
162162
self, tenant_id: Optional[str], user_context: Dict[str, Any]
163-
) -> GetTenantOkResult:
163+
) -> Optional[GetTenantOkResult]:
164164
res = await self.querier.send_get_request(
165165
NormalisedURLPath(
166166
f"{tenant_id or DEFAULT_TENANT_ID}/recipe/multitenancy/tenant"
167167
),
168168
)
169169

170+
if res["status"] == "TENANT_NOT_FOUND_ERROR":
171+
return None
172+
170173
tenant_config = parse_tenant_config(res)
171174

172175
return GetTenantOkResult(

supertokens_python/recipe/thirdparty/api/authorisation_url.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
if TYPE_CHECKING:
2020
from supertokens_python.recipe.thirdparty.interfaces import APIOptions, APIInterface
2121

22-
from supertokens_python.exceptions import raise_bad_input_exception
22+
from supertokens_python.exceptions import raise_bad_input_exception, BadInputError
2323
from supertokens_python.utils import send_200_response
2424

2525

@@ -53,7 +53,12 @@ async def handle_authorisation_url_api(
5353
user_context=user_context,
5454
)
5555

56-
provider = provider_response.provider
56+
if provider_response is None:
57+
raise BadInputError(
58+
f"the provider {third_party_id} could not be found in the configuration"
59+
)
60+
61+
provider = provider_response
5762
result = await api_implementation.authorisation_url_get(
5863
provider=provider,
5964
redirect_uri_on_provider_dashboard=redirect_uri_on_provider_dashboard,

supertokens_python/recipe/thirdparty/api/signinup.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
if TYPE_CHECKING:
2020
from supertokens_python.recipe.thirdparty.interfaces import APIOptions, APIInterface
2121

22-
from supertokens_python.exceptions import raise_bad_input_exception
22+
from supertokens_python.exceptions import raise_bad_input_exception, BadInputError
2323
from supertokens_python.utils import send_200_response
2424

2525

@@ -64,7 +64,12 @@ async def handle_sign_in_up_api(
6464
user_context=user_context,
6565
)
6666

67-
provider = provider_response.provider
67+
if provider_response is None:
68+
raise BadInputError(
69+
f"the provider {third_party_id} could not be found in the configuration"
70+
)
71+
72+
provider = provider_response
6873

6974
result = await api_implementation.sign_in_up_post(
7075
provider=provider,

supertokens_python/recipe/thirdparty/interfaces.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ async def get_provider(
114114
client_type: Optional[str],
115115
tenant_id: str,
116116
user_context: Dict[str, Any],
117-
) -> GetProviderOkResult:
117+
) -> Optional[Provider]:
118118
pass
119119

120120

supertokens_python/recipe/thirdparty/providers/config_utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,11 @@ async def find_and_create_provider_instance(
262262
third_party_id: str,
263263
client_type: Optional[str],
264264
user_context: Dict[str, Any],
265-
) -> Provider:
265+
) -> Optional[Provider]:
266266
for provider_input in providers:
267267
if provider_input.config.third_party_id == third_party_id:
268268
provider_instance = create_provider(provider_input)
269269
await fetch_and_set_config(provider_instance, client_type, user_context)
270270
return provider_instance
271271

272-
raise Exception(
273-
f"the provider {third_party_id} could not be found in the configuration"
274-
)
272+
return None

supertokens_python/recipe/thirdparty/recipe_implementation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from supertokens_python.querier import Querier
2828

2929
from .interfaces import (
30-
GetProviderOkResult,
3130
ManuallyCreateOrUpdateUserOkResult,
3231
RecipeInterface,
3332
SignInUpOkResult,
@@ -189,6 +188,9 @@ async def get_provider(
189188
user_context=user_context,
190189
)
191190

191+
if tenant_config is None:
192+
raise Exception("Tenant not found")
193+
192194
merged_providers = merge_providers_from_core_and_static(
193195
provider_configs_from_core=tenant_config.third_party.providers,
194196
provider_inputs_from_static=self.providers,
@@ -198,6 +200,4 @@ async def get_provider(
198200
merged_providers, third_party_id, client_type, user_context
199201
)
200202

201-
return GetProviderOkResult(
202-
provider,
203-
)
203+
return provider

supertokens_python/recipe/thirdpartyemailpassword/interfaces.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ async def thirdparty_get_provider(
149149
client_type: Optional[str],
150150
tenant_id: str,
151151
user_context: Dict[str, Any],
152-
) -> ThirdPartyInterfaces.GetProviderOkResult:
152+
) -> Optional[ThirdPartyInterfaces.Provider]:
153153
pass
154154

155155
@abstractmethod

supertokens_python/recipe/thirdpartyemailpassword/recipeimplementation/implementation.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
from typing import TYPE_CHECKING, Any, Dict, List, Union, Callable, Optional
1717

1818
import supertokens_python.recipe.emailpassword.interfaces as EPInterfaces
19-
from supertokens_python.recipe.thirdparty.interfaces import GetProviderOkResult
20-
from supertokens_python.recipe.thirdparty.provider import ProviderInput
19+
from supertokens_python.recipe.thirdparty.provider import ProviderInput, Provider
2120
from supertokens_python.recipe.thirdparty.types import RawUserInfoFromProvider
2221
from supertokens_python.recipe.emailpassword.utils import EmailPasswordConfig
2322

@@ -276,7 +275,7 @@ async def thirdparty_get_provider(
276275
client_type: Optional[str],
277276
tenant_id: str,
278277
user_context: Dict[str, Any],
279-
) -> GetProviderOkResult:
278+
) -> Optional[Provider]:
280279
if self.tp_get_provider is None:
281280
raise Exception("No thirdparty provider configured")
282281

supertokens_python/recipe/thirdpartyemailpassword/recipeimplementation/third_party_recipe_implementation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
from typing import Any, Dict, List, Optional, Union
1717

1818
from supertokens_python.recipe.thirdparty.interfaces import (
19-
GetProviderOkResult,
2019
ManuallyCreateOrUpdateUserOkResult,
2120
RecipeInterface,
2221
SignInUpOkResult,
2322
)
23+
from supertokens_python.recipe.thirdparty.provider import Provider
2424
from supertokens_python.recipe.thirdparty.types import RawUserInfoFromProvider, User
2525
from supertokens_python.recipe.thirdpartyemailpassword.interfaces import (
2626
RecipeInterface as ThirdPartyEmailPasswordRecipeInterface,
@@ -164,7 +164,7 @@ async def get_provider(
164164
client_type: Optional[str],
165165
tenant_id: str,
166166
user_context: Dict[str, Any],
167-
) -> GetProviderOkResult:
167+
) -> Optional[Provider]:
168168
return await self.recipe_implementation.thirdparty_get_provider(
169169
third_party_id,
170170
client_type,

supertokens_python/recipe/thirdpartypasswordless/interfaces.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ async def thirdparty_get_provider(
136136
client_type: Optional[str],
137137
tenant_id: str,
138138
user_context: Dict[str, Any],
139-
) -> ThirdPartyInterfaces.GetProviderOkResult:
139+
) -> Optional[ThirdPartyInterfaces.Provider]:
140140
pass
141141

142142
@abstractmethod

supertokens_python/recipe/thirdpartypasswordless/recipeimplementation/implementation.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
1717

18-
from supertokens_python.recipe.thirdparty.provider import ProviderInput
18+
from supertokens_python.recipe.thirdparty.provider import ProviderInput, Provider
1919
from supertokens_python.recipe.thirdparty.types import RawUserInfoFromProvider
2020

2121
from ...passwordless.interfaces import (
@@ -42,7 +42,6 @@
4242
UpdateUserUnknownUserIdError,
4343
)
4444
from ...thirdparty.interfaces import (
45-
GetProviderOkResult,
4645
ManuallyCreateOrUpdateUserOkResult,
4746
SignInUpOkResult,
4847
)
@@ -296,7 +295,7 @@ async def thirdparty_get_provider(
296295
client_type: Optional[str],
297296
tenant_id: str,
298297
user_context: Dict[str, Any],
299-
) -> GetProviderOkResult:
298+
) -> Optional[Provider]:
300299
if self.tp_get_provider is None:
301300
raise Exception("No thirdparty provider configured")
302301
return await self.tp_get_provider(

supertokens_python/recipe/thirdpartypasswordless/recipeimplementation/third_party_recipe_implementation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
from typing import Any, Dict, List, Optional, Union
1717

1818
from supertokens_python.recipe.thirdparty.interfaces import (
19-
GetProviderOkResult,
2019
ManuallyCreateOrUpdateUserOkResult,
2120
RecipeInterface,
2221
SignInUpOkResult,
2322
)
2423
from supertokens_python.recipe.thirdparty.types import RawUserInfoFromProvider, User
2524

2625
from ..interfaces import RecipeInterface as ThirdPartyPasswordlessRecipeInterface
26+
from ...thirdparty.provider import Provider
2727

2828

2929
class RecipeImplementation(RecipeInterface):
@@ -138,7 +138,7 @@ async def get_provider(
138138
client_type: Optional[str],
139139
tenant_id: str,
140140
user_context: Dict[str, Any],
141-
) -> GetProviderOkResult:
141+
) -> Optional[Provider]:
142142
return await self.recipe_implementation.thirdparty_get_provider(
143143
third_party_id, client_type, tenant_id, user_context
144144
)

tests/multitenancy/test_tenants_crud.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,21 @@ async def test_tenant_crud():
7575
assert len(tenants.tenants) == 4
7676

7777
t1_config = await get_tenant("t1")
78+
assert t1_config is not None
7879
assert t1_config.emailpassword.enabled is True
7980
assert t1_config.passwordless.enabled is False
8081
assert t1_config.third_party.enabled is False
8182
assert t1_config.core_config == {}
8283

8384
t2_config = await get_tenant("t2")
85+
assert t2_config is not None
8486
assert t2_config.emailpassword.enabled is False
8587
assert t2_config.passwordless.enabled is True
8688
assert t2_config.third_party.enabled is False
8789
assert t2_config.core_config == {}
8890

8991
t3_config = await get_tenant("t3")
92+
assert t3_config is not None
9093
assert t3_config.emailpassword.enabled is False
9194
assert t3_config.passwordless.enabled is False
9295
assert t3_config.third_party.enabled is True
@@ -95,6 +98,7 @@ async def test_tenant_crud():
9598
# update tenant1 to add passwordless:
9699
await create_or_update_tenant("t1", TenantConfig(passwordless_enabled=True))
97100
t1_config = await get_tenant("t1")
101+
assert t1_config is not None
98102
assert t1_config.emailpassword.enabled is True
99103
assert t1_config.passwordless.enabled is True
100104
assert t1_config.third_party.enabled is False
@@ -103,6 +107,8 @@ async def test_tenant_crud():
103107
# update tenant1 to add thirdparty:
104108
await create_or_update_tenant("t1", TenantConfig(third_party_enabled=True))
105109
t1_config = await get_tenant("t1")
110+
assert t1_config is not None
111+
assert t1_config is not None
106112
assert t1_config.emailpassword.enabled is True
107113
assert t1_config.passwordless.enabled is True
108114
assert t1_config.third_party.enabled is True
@@ -131,6 +137,7 @@ async def test_tenant_thirdparty_config():
131137
)
132138

133139
tenant_config = await get_tenant("t1")
140+
assert tenant_config is not None
134141

135142
assert len(tenant_config.third_party.providers) == 1
136143
provider = tenant_config.third_party.providers[0]
@@ -189,6 +196,7 @@ async def generate_fake_email(_: str, __: str, ___: Dict[str, Any]):
189196
)
190197

191198
tenant_config = await get_tenant("t1")
199+
assert tenant_config is not None
192200
assert len(tenant_config.third_party.providers) == 1
193201
provider = tenant_config.third_party.providers[0]
194202
assert provider.third_party_id == "google"
@@ -232,6 +240,7 @@ async def generate_fake_email(_: str, __: str, ___: Dict[str, Any]):
232240
await delete_third_party_config("t1", "google")
233241

234242
tenant_config = await get_tenant("t1")
243+
assert tenant_config is not None
235244
assert len(tenant_config.third_party.providers) == 0
236245

237246

tests/thirdparty/test_multitenancy.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,25 @@ async def test_get_provider():
201201
)
202202

203203
provider1 = await get_provider("t1", "google", None)
204-
assert provider1.provider.config.third_party_id == "google"
204+
assert provider1 is not None
205+
assert provider1.config.third_party_id == "google"
205206

206207
provider2 = await get_provider("t1", "facebook", None)
207-
assert provider2.provider.config.third_party_id == "facebook"
208+
assert provider2 is not None
209+
assert provider2.config.third_party_id == "facebook"
208210

209211
provider3 = await get_provider("t2", "facebook", None)
210-
assert provider3.provider.config.third_party_id == "facebook"
212+
assert provider3 is not None
213+
assert provider3.config.third_party_id == "facebook"
211214

212215
provider4 = await get_provider("t2", "discord", None)
213-
assert provider4.provider.config.third_party_id == "discord"
216+
assert provider4 is not None
217+
assert provider4.config.third_party_id == "discord"
214218

215219
provider5 = await get_provider("t3", "discord", None)
216-
assert provider5.provider.config.third_party_id == "discord"
220+
assert provider5 is not None
221+
assert provider5.config.third_party_id == "discord"
217222

218223
provider6 = await get_provider("t3", "linkedin", None)
219-
assert provider6.provider.config.third_party_id == "linkedin"
224+
assert provider6 is not None
225+
assert provider6.config.third_party_id == "linkedin"

0 commit comments

Comments
 (0)