Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Emit webhooks when setPassword confirms an account (#19833)

Lukasz Ostrowski9ea6bdeae2e7
diff --git a/saleor/graphql/account/mutations/account/confirm_account.py b/saleor/graphql/account/mutations/account/confirm_account.py
index cbcf14f045..dfb3e8e223 100644
--- a/saleor/graphql/account/mutations/account/confirm_account.py
+++ b/saleor/graphql/account/mutations/account/confirm_account.py
@@ -65,6 +65,14 @@ class ConfirmAccount(BaseMutation):
type=WebhookEventAsyncType.ACCOUNT_CONFIRMED,
description="Account was confirmed.",
),
+ WebhookEventInfo(
+ type=WebhookEventAsyncType.CUSTOMER_UPDATED,
+ description="Called if a customer account was confirmed.",
+ ),
+ WebhookEventInfo(
+ type=WebhookEventAsyncType.STAFF_UPDATED,
+ description="Called if a staff account was confirmed.",
+ ),
]
@staticmethod
@@ -191,3 +199,7 @@ class ConfirmAccount(BaseMutation):
def post_save_action(cls, info: ResolveInfo, instance):
manager = get_plugin_manager_promise(info.context).get()
cls.call_event(manager.account_confirmed, instance)
+ cls.call_event(
+ manager.staff_updated if instance.is_staff else manager.customer_updated,
+ instance,
+ )
diff --git a/saleor/graphql/account/mutations/authentication/set_password.py b/saleor/graphql/account/mutations/authentication/set_password.py
index d07e496d68..228258d51f 100644
--- a/saleor/graphql/account/mutations/authentication/set_password.py
+++ b/saleor/graphql/account/mutations/authentication/set_password.py
@@ -12,11 +12,14 @@ from .....core.tokens import (
try_generators,
)
from .....order.utils import match_orders_with_new_user
+from .....webhook.event_types import WebhookEventAsyncType
from ....core import ResolveInfo
from ....core.context import disallow_replica_in_context
from ....core.doc_category import DOC_CATEGORY_USERS
from ....core.mutations import validation_error_to_error_type
from ....core.types import AccountError
+from ....core.utils import WebhookEventInfo
+from ....plugins.dataloaders import get_plugin_manager_promise
from ....site.dataloaders import get_site_promise
from ..base import INVALID_TOKEN
from . import CreateToken
@@ -39,6 +42,20 @@ class SetPassword(CreateToken):
doc_category = DOC_CATEGORY_USERS
error_type_class = AccountError
error_type_field = "account_errors"
+ webhook_events_info = [
+ WebhookEventInfo(
+ type=WebhookEventAsyncType.ACCOUNT_CONFIRMED,
+ description="Called if the account was not previously confirmed.",
+ ),
+ WebhookEventInfo(
+ type=WebhookEventAsyncType.CUSTOMER_UPDATED,
+ description="Called if a customer account was confirmed.",
+ ),
+ WebhookEventInfo(
+ type=WebhookEventAsyncType.STAFF_UPDATED,
+ description="Called if a staff account was confirmed.",
+ ),
+ ]
@classmethod
@allow_writer()
@@ -50,10 +67,17 @@ class SetPassword(CreateToken):
site_settings = get_site_promise(info.context).get().settings
try:
check_password_login_not_disabled(site_settings)
- cls._set_password_for_user(email, password, token)
+ user, confirmed = cls._set_password_for_user(email, password, token)
except ValidationError as e:
errors = validation_error_to_error_type(e, AccountError)
return cls.handle_typed_errors(errors)
+ if confirmed:
+ manager = get_plugin_manager_promise(info.context).get()
+ cls.call_event(manager.account_confirmed, user)
+ cls.call_event(
+ manager.staff_updated if user.is_staff else manager.customer_updated,
+ user,
+ )
return super().mutate(root, info, email=email, password=password)
@classmethod
@@ -90,9 +114,11 @@ class SetPassword(CreateToken):
# To reset the password user need to process the token sent separately by email,
# so we can be sure that the user has access to email account and can be
# confirmed.
- if not user.is_confirmed:
+ confirmed = not user.is_confirmed
+ if confirmed:
user.is_confirmed = True
match_orders_with_new_user(user)
fields_to_save.append("is_confirmed")
user.save(update_fields=fields_to_save)
account_events.customer_password_reset_event(user=user)
+ return user, confirmed
diff --git a/saleor/graphql/account/tests/mutations/account/test_confirm_account.py b/saleor/graphql/account/tests/mutations/account/test_confirm_account.py
index b48bc39379..f9e7352415 100644
--- a/saleor/graphql/account/tests/mutations/account/test_confirm_account.py
+++ b/saleor/graphql/account/tests/mutations/account/test_confirm_account.py
@@ -47,6 +47,7 @@ def account_merging_enabled(site_settings):
@freeze_time("2018-05-31 12:00:01")
+@patch("saleor.plugins.manager.PluginsManager.customer_updated")
@patch("saleor.plugins.manager.PluginsManager.account_confirmed")
@pytest.mark.parametrize(
("merge_mode", "should_merge"),
@@ -57,6 +58,7 @@ def account_merging_enabled(site_settings):
)
def test_account_confirmation(
mocked_account_confirmed,
+ mocked_customer_updated,
merge_mode: str,
should_merge: bool,
api_client,
@@ -93,6 +95,7 @@ def test_account_confirmation(
assert customer_user.is_confirmed is True, "should have confirmed the the user"
mocked_account_confirmed.assert_called_once_with(customer_user)
+ mocked_customer_updated.assert_called_once_with(customer_user)
gift_card.refresh_from_db(fields=("created_by", "created_by_email"))
order.refresh_from_db(fields=("user", "user_email"))
@@ -401,9 +404,11 @@ def test_account_confirmation_invalid_token(
@patch(
"saleor.graphql.account.mutations.account.confirm_account.match_orders_with_new_user"
)
+@patch("saleor.plugins.manager.PluginsManager.customer_updated")
@patch("saleor.plugins.manager.PluginsManager.account_confirmed")
def test_account_confirmation_rejects_token_for_wrong_scope(
mocked_account_confirmed,
+ mocked_customer_updated,
match_orders_with_new_user_mock,
assign_gift_cards_mock,
user_api_client,
@@ -443,6 +448,7 @@ def test_account_confirmation_rejects_token_for_wrong_scope(
match_orders_with_new_user_mock.assert_not_called()
assign_gift_cards_mock.assert_not_called()
mocked_account_confirmed.assert_not_called()
+ mocked_customer_updated.assert_not_called()
# Sanity check: a valid token should work properly
valid_token = account_confirm_token_generator.make_token(customer_user)
diff --git a/saleor/graphql/account/tests/mutations/authentication/test_set_password.py b/saleor/graphql/account/tests/mutations/authentication/test_set_password.py
index 481c4f7212..72faf99180 100644
--- a/saleor/graphql/account/tests/mutations/authentication/test_set_password.py
+++ b/saleor/graphql/account/tests/mutations/authentication/test_set_password.py
@@ -1,5 +1,6 @@
from unittest.mock import patch
+import pytest
from freezegun import freeze_time
from ......account import events as account_events
@@ -109,6 +110,60 @@ def test_set_password_confirm_user_and_match_orders(
match_orders_with_new_user_mock.assert_called_once_with(customer_user)
+ ("_case", "is_staff", "is_confirmed", "expected_updated_event"),
+ [
+ ("unconfirmed customer", False, False, "customer_updated"),
+ ("unconfirmed staff", True, False, "staff_updated"),
+ ("already confirmed customer", False, True, None),
+ ],
+)
+@patch("saleor.plugins.manager.PluginsManager.staff_updated")
+@patch("saleor.plugins.manager.PluginsManager.customer_updated")
+@patch("saleor.plugins.manager.PluginsManager.account_confirmed")
+@patch("saleor.account.throttling.cache")
+def test_set_password_emits_confirmation_webhooks(
+ mocked_cache,
+ mocked_account_confirmed,
+ mocked_customer_updated,
+ mocked_staff_updated,
+ _case,
+ is_staff,
+ is_confirmed,
+ expected_updated_event,
+ user_api_client,
+ setup_mock_for_cache,
+):
+ # given
+ setup_mock_for_cache({}, mocked_cache)
+ user = dangerously_create_test_user(
+ password="old-password",
+ is_staff=is_staff,
+ is_confirmed=is_confirmed,
+ )
+ token = password_reset_token_generator.make_token(user)
+ variables = {"email": user.email, "password": "new-password", "token": token}
+
+ # when
+ response = user_api_client.post_graphql(SET_PASSWORD_MUTATION, variables)
+
+ # then
+ content = get_graphql_content(response)
+ assert content["data"]["setPassword"]["errors"] == []
+ updated_mocks = {
+ "customer_updated": mocked_customer_updated,
+ "staff_updated": mocked_staff_updated,
+ }
+ if expected_updated_event:
+ mocked_account_confirmed.assert_called_once_with(user)
+ updated_mocks.pop(expected_updated_event).assert_called_once_with(user)
+ else:
+ mocked_account_confirmed.assert_not_called()
+ for not_expected_mock in updated_mocks.values():
+ not_expected_mock.assert_not_called()
+
+
@patch("saleor.account.throttling.cache")
def test_set_password_invalid_token(
mocked_cache, user_api_client, customer_user, setup_mock_for_cache
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 709452da2f..3e91d1995b 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -21986,6 +21986,8 @@ type Mutation {
Triggers the following webhook events:
- ACCOUNT_CONFIRMED (async): Account was confirmed.
+ - CUSTOMER_UPDATED (async): Called if a customer account was confirmed.
+ - STAFF_UPDATED (async): Called if a staff account was confirmed.
"""
confirmAccount(
"""E-mail of the user performing account confirmation."""
@@ -21998,10 +22000,15 @@ type Mutation {
"""A one-time token required to confirm the account."""
token: String!
- ): ConfirmAccount @doc(category: "Users") @webhookEventsInfo(asyncEvents: [ACCOUNT_CONFIRMED], syncEvents: [])
+ ): ConfirmAccount @doc(category: "Users") @webhookEventsInfo(asyncEvents: [ACCOUNT_CONFIRMED, CUSTOMER_UPDATED, STAFF_UPDATED], syncEvents: [])
"""
Sets the user's password from the token sent by email using the RequestPasswordReset mutation.
+
+ Triggers the following webhook events:
+ - ACCOUNT_CONFIRMED (async): Called if the account was not previously confirmed.
+ - CUSTOMER_UPDATED (async): Called if a customer account was confirmed.
+ - STAFF_UPDATED (async): Called if a staff account was confirmed.
"""
setPassword(
"""Email of a user."""
@@ -22012,7 +22019,7 @@ type Mutation {
"""A one-time token required to set the password."""
token: String!
- ): SetPassword @doc(category: "Users")
+ ): SetPassword @doc(category: "Users") @webhookEventsInfo(asyncEvents: [ACCOUNT_CONFIRMED, CUSTOMER_UPDATED, STAFF_UPDATED], syncEvents: [])
"""
Change the password of the logged in user.
@@ -33097,8 +33104,10 @@ Confirm user account with token sent by email during registration.
Triggers the following webhook events:
- ACCOUNT_CONFIRMED (async): Account was confirmed.
+- CUSTOMER_UPDATED (async): Called if a customer account was confirmed.
+- STAFF_UPDATED (async): Called if a staff account was confirmed.
"""
-type ConfirmAccount @doc(category: "Users") @webhookEventsInfo(asyncEvents: [ACCOUNT_CONFIRMED], syncEvents: []) {
+type ConfirmAccount @doc(category: "Users") @webhookEventsInfo(asyncEvents: [ACCOUNT_CONFIRMED, CUSTOMER_UPDATED, STAFF_UPDATED], syncEvents: []) {
"""An activated user account."""
user: User
accountErrors: [AccountError!]! @deprecated(reason: "Use `errors` field instead.")
@@ -33107,8 +33116,13 @@ type ConfirmAccount @doc(category: "Users") @webhookEventsInfo(asyncEvents: [ACC
"""
Sets the user's password from the token sent by email using the RequestPasswordReset mutation.
+
+Triggers the following webhook events:
+- ACCOUNT_CONFIRMED (async): Called if the account was not previously confirmed.
+- CUSTOMER_UPDATED (async): Called if a customer account was confirmed.
+- STAFF_UPDATED (async): Called if a staff account was confirmed.
"""
-type SetPassword @doc(category: "Users") {
+type SetPassword @doc(category: "Users") @webhookEventsInfo(asyncEvents: [ACCOUNT_CONFIRMED, CUSTOMER_UPDATED, STAFF_UPDATED], syncEvents: []) {
"""JWT token, required to authenticate."""
token: String