Skip to content

Commit fc2ea20

Browse files
Merge pull request #263 from supertokens/sendEmail-bug-emailpassword
fix: send_email override issue
2 parents 984b424 + 6cf55b0 commit fc2ea20

File tree

5 files changed

+84
-14
lines changed

5 files changed

+84
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## unreleased
99

10+
## [0.11.9] - 2022-12-06
11+
12+
- Fixes issue where if send_email is overridden with a different email, it will reset that email.
13+
1014
## [0.11.8] - 2022-11-28
1115

1216
### Added:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
setup(
7272
name="supertokens_python",
73-
version="0.11.8",
73+
version="0.11.9",
7474
author="SuperTokens",
7575
license="Apache 2.0",
7676
author_email="[email protected]",

supertokens_python/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414
SUPPORTED_CDI_VERSIONS = ["2.9", "2.10", "2.11", "2.12", "2.13", "2.14", "2.15"]
15-
VERSION = "0.11.8"
15+
VERSION = "0.11.9"
1616
TELEMETRY = "/telemetry"
1717
USER_COUNT = "/users/count"
1818
USER_DELETE = "/user/remove"

supertokens_python/recipe/emailpassword/emaildelivery/services/backward_compatibility/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict, Union
1818

1919
from httpx import AsyncClient
20+
2021
from supertokens_python.ingredients.emaildelivery.types import EmailDeliveryInterface
2122
from supertokens_python.logger import log_debug_message
2223
from supertokens_python.recipe.emailpassword.interfaces import (
2324
EmailTemplateVars,
2425
RecipeInterface,
2526
)
26-
from supertokens_python.recipe.emailpassword.types import (
27-
User,
28-
)
27+
from supertokens_python.recipe.emailpassword.types import User
2928
from supertokens_python.supertokens import AppInfo
3029
from supertokens_python.utils import handle_httpx_client_exceptions
3130

@@ -96,6 +95,10 @@ async def send_email(
9695
if user is None:
9796
raise Exception("Should never come here")
9897

98+
# we add this here cause the user may have overridden the sendEmail function
99+
# to change the input email and if we don't do this, the input email
100+
# will get reset by the getUserById call above.
101+
user.email = template_vars.user.email
99102
try:
100103
await self.reset_password_feature_send_email_func(
101104
user, template_vars.password_reset_link, user_context

tests/emailpassword/test_emaildelivery.py

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,32 @@
2121
from fastapi.requests import Request
2222
from fastapi.testclient import TestClient
2323
from pytest import fixture, mark
24+
2425
from supertokens_python import InputAppInfo, SupertokensConfig, init
2526
from supertokens_python.framework.fastapi import get_middleware
2627
from supertokens_python.ingredients.emaildelivery import EmailDeliveryInterface
2728
from supertokens_python.ingredients.emaildelivery.types import (
2829
EmailContent,
2930
EmailDeliveryConfig,
30-
SMTPSettingsFrom,
3131
SMTPServiceInterface,
3232
SMTPSettings,
33+
SMTPSettingsFrom,
3334
)
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
3837
from supertokens_python.recipe.emailpassword.emaildelivery.services import SMTPService
39-
from supertokens_python.recipe.emailverification.emaildelivery.services import (
40-
SMTPService as EVSMTPService,
41-
)
4238
from supertokens_python.recipe.emailpassword.types import (
4339
EmailTemplateVars,
4440
PasswordResetEmailTemplateVars,
4541
)
4642
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
4747
from supertokens_python.recipe.emailverification.types import (
4848
VerificationEmailTemplateVars,
4949
)
50-
from supertokens_python.recipe.emailverification.types import User as EVUser
5150
from supertokens_python.recipe.session import SessionRecipe
5251
from supertokens_python.recipe.session.recipe_implementation import (
5352
RecipeImplementation as SessionRecipeImplementation,
@@ -299,6 +298,70 @@ def api_side_effect(request: httpx.Request):
299298
assert password_reset_url != ""
300299

301300

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+
302365
@mark.asyncio
303366
async def test_reset_password_smtp_service(driver_config_client: TestClient):
304367
"Reset password: test smtp service"

0 commit comments

Comments
 (0)