|
21 | 21 | from fastapi.requests import Request |
22 | 22 | from fastapi.testclient import TestClient |
23 | 23 | from pytest import fixture, mark |
| 24 | + |
24 | 25 | from supertokens_python import InputAppInfo, SupertokensConfig, init |
25 | 26 | from supertokens_python.framework.fastapi import get_middleware |
26 | 27 | from supertokens_python.ingredients.emaildelivery import EmailDeliveryInterface |
27 | 28 | from supertokens_python.ingredients.emaildelivery.types import ( |
28 | 29 | EmailContent, |
29 | 30 | EmailDeliveryConfig, |
30 | | - SMTPSettingsFrom, |
31 | 31 | SMTPServiceInterface, |
32 | 32 | SMTPSettings, |
| 33 | + SMTPSettingsFrom, |
33 | 34 | ) |
34 | | -from supertokens_python.recipe import emailpassword, session, emailverification |
35 | | -from supertokens_python.recipe.emailpassword import ( |
36 | | - InputResetPasswordUsingTokenFeature, |
37 | | -) |
| 35 | +from supertokens_python.recipe import emailpassword, emailverification, session |
| 36 | +from supertokens_python.recipe.emailpassword import InputResetPasswordUsingTokenFeature |
38 | 37 | from supertokens_python.recipe.emailpassword.emaildelivery.services import SMTPService |
39 | | -from supertokens_python.recipe.emailverification.emaildelivery.services import ( |
40 | | - SMTPService as EVSMTPService, |
41 | | -) |
42 | 38 | from supertokens_python.recipe.emailpassword.types import ( |
43 | 39 | EmailTemplateVars, |
44 | 40 | PasswordResetEmailTemplateVars, |
45 | 41 | ) |
46 | 42 | from supertokens_python.recipe.emailpassword.types import User as EPUser |
| 43 | +from supertokens_python.recipe.emailverification.emaildelivery.services import ( |
| 44 | + SMTPService as EVSMTPService, |
| 45 | +) |
| 46 | +from supertokens_python.recipe.emailverification.types import User as EVUser |
47 | 47 | from supertokens_python.recipe.emailverification.types import ( |
48 | 48 | VerificationEmailTemplateVars, |
49 | 49 | ) |
50 | | -from supertokens_python.recipe.emailverification.types import User as EVUser |
51 | 50 | from supertokens_python.recipe.session import SessionRecipe |
52 | 51 | from supertokens_python.recipe.session.recipe_implementation import ( |
53 | 52 | RecipeImplementation as SessionRecipeImplementation, |
@@ -299,6 +298,70 @@ def api_side_effect(request: httpx.Request): |
299 | 298 | assert password_reset_url != "" |
300 | 299 |
|
301 | 300 |
|
| 301 | +@mark.asyncio |
| 302 | +async def test_reset_password_custom_override_with_send_email_override( |
| 303 | + driver_config_client: TestClient, |
| 304 | +): |
| 305 | + "Reset password: test custom override with send email override" |
| 306 | + email = "" |
| 307 | + password_reset_url = "" |
| 308 | + |
| 309 | + def email_delivery_override(oi: EmailDeliveryInterface[EmailTemplateVars]): |
| 310 | + oi_send_email = oi.send_email |
| 311 | + |
| 312 | + async def send_email( |
| 313 | + template_vars: EmailTemplateVars, user_context: Dict[str, Any] |
| 314 | + ): |
| 315 | + template_vars. user. email = "[email protected]" |
| 316 | + assert isinstance(template_vars, PasswordResetEmailTemplateVars) |
| 317 | + await oi_send_email(template_vars, user_context) |
| 318 | + |
| 319 | + oi.send_email = send_email |
| 320 | + return oi |
| 321 | + |
| 322 | + async def custom_create_and_send_custom_email( |
| 323 | + user: EPUser, password_reset_link: str, _: Dict[str, Any] |
| 324 | + ): |
| 325 | + nonlocal email, password_reset_url |
| 326 | + email = user.email |
| 327 | + password_reset_url = password_reset_link |
| 328 | + |
| 329 | + init( |
| 330 | + supertokens_config=SupertokensConfig("http://localhost:3567"), |
| 331 | + app_info=InputAppInfo( |
| 332 | + app_name="ST", |
| 333 | + api_domain="http://api.supertokens.io", |
| 334 | + website_domain="http://supertokens.io", |
| 335 | + api_base_path="/auth", |
| 336 | + ), |
| 337 | + framework="fastapi", |
| 338 | + recipe_list=[ |
| 339 | + emailpassword.init( |
| 340 | + email_delivery=EmailDeliveryConfig( |
| 341 | + service=None, |
| 342 | + override=email_delivery_override, |
| 343 | + ), |
| 344 | + reset_password_using_token_feature=emailpassword.InputResetPasswordUsingTokenFeature( |
| 345 | + create_and_send_custom_email=custom_create_and_send_custom_email |
| 346 | + ), |
| 347 | + ), |
| 348 | + session.init(), |
| 349 | + ], |
| 350 | + ) |
| 351 | + start_st() |
| 352 | + |
| 353 | + sign_up_request( driver_config_client, "[email protected]", "1234abcd") |
| 354 | + |
| 355 | + resp = reset_password_request( |
| 356 | + driver_config_client, "[email protected]", use_server=True |
| 357 | + ) |
| 358 | + |
| 359 | + assert resp.status_code == 200 |
| 360 | + |
| 361 | + assert email == "[email protected]" |
| 362 | + assert password_reset_url != "" |
| 363 | + |
| 364 | + |
302 | 365 | @mark.asyncio |
303 | 366 | async def test_reset_password_smtp_service(driver_config_client: TestClient): |
304 | 367 | "Reset password: test smtp service" |
|
0 commit comments