Skip to content

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

Fix `NOTIFY_USER` with another event issue (#13584)

Artur5565b141c9a7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a05f607a42..aa97810780 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -143,6 +143,8 @@ Shipping methods can be removed by the user after it has been assigned to a chec
- Add missing descriptions to page module - #13536 by @devilsautumn
- Fix seo field to accept null value - #13512 by @ssuraliya
- Add missing descriptions to payment module - #13546 by @devilsautumn
+- Fix `NOTIFY_USER` allow to create webhook with only one event - #13584 by @Air-t
+
# 3.14.0
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 8cf66683aa..fe1b6f159d 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -18549,6 +18549,7 @@ enum WebhookErrorCode @doc(category: "Webhooks") {
UNABLE_TO_PARSE
MISSING_EVENT
INVALID_CUSTOM_HEADERS
+ INVALID_NOTIFY_WITH_SUBSCRIPTION
}
input WebhookCreateInput @doc(category: "Webhooks") {
diff --git a/saleor/graphql/webhook/mixins.py b/saleor/graphql/webhook/mixins.py
new file mode 100644
index 0000000000..128c0c088b
--- /dev/null
+++ b/saleor/graphql/webhook/mixins.py
@@ -0,0 +1,21 @@
+from django.core.exceptions import ValidationError
+
+from ...webhook.error_codes import WebhookErrorCode
+from ...webhook.event_types import WebhookEventAsyncType
+
+
+class NotifyUserEventValidationMixin:
+ @classmethod
+ def validate_events(cls, events):
+ # NOTIFY_USER needs to be the only one event registered per webhook.
+ # This solves issue temporarily. NOTIFY_USER will be deprecated in the future.
+ if WebhookEventAsyncType.NOTIFY_USER in events and len(events) > 1:
+ raise ValidationError(
+ {
+ "async_events": ValidationError(
+ "The NOTIFY_USER webhook cannot be combined with other events.",
+ code=WebhookErrorCode.INVALID_NOTIFY_WITH_SUBSCRIPTION.value,
+ )
+ }
+ )
+ return events
diff --git a/saleor/graphql/webhook/mutations/webhook_create.py b/saleor/graphql/webhook/mutations/webhook_create.py
index 6d96628a5a..9133ef62b0 100644
--- a/saleor/graphql/webhook/mutations/webhook_create.py
+++ b/saleor/graphql/webhook/mutations/webhook_create.py
@@ -26,6 +26,7 @@ from ...core.mutations import ModelMutation
from ...core.types import BaseInputObjectType, NonNullList, WebhookError
from ...core.utils import raise_validation_error
from .. import enums
+from ..mixins import NotifyUserEventValidationMixin
from ..subscription_query import SubscriptionQuery
from ..types import Webhook
@@ -80,7 +81,7 @@ class WebhookCreateInput(BaseInputObjectType):
doc_category = DOC_CATEGORY_WEBHOOKS
-class WebhookCreate(ModelMutation):
+class WebhookCreate(ModelMutation, NotifyUserEventValidationMixin):
class Arguments:
input = WebhookCreateInput(
description="Fields required to create a webhook.", required=True
@@ -147,8 +148,10 @@ class WebhookCreate(ModelMutation):
return cleaned_data
- @staticmethod
- def _clean_webhook_events(data, subscription_query: Optional[SubscriptionQuery]):
+ @classmethod
+ def _clean_webhook_events(
+ cls, data, subscription_query: Optional[SubscriptionQuery]
+ ):
# if `events` field is not empty, use this field. Otherwise get event types
# from `async_events` and `sync_events`. If the fields are also empty,
# parse events from `query`.
@@ -159,6 +162,7 @@ class WebhookCreate(ModelMutation):
if not events and subscription_query:
events = subscription_query.events
+ cls.validate_events(events)
data["events"] = events
return data
diff --git a/saleor/graphql/webhook/mutations/webhook_update.py b/saleor/graphql/webhook/mutations/webhook_update.py
index 577d20e24b..5db42a0a15 100644
--- a/saleor/graphql/webhook/mutations/webhook_update.py
+++ b/saleor/graphql/webhook/mutations/webhook_update.py
@@ -16,6 +16,7 @@ from ...core.doc_category import DOC_CATEGORY_WEBHOOKS
from ...core.fields import JSONString
from ...core.types import BaseInputObjectType, NonNullList, WebhookError
from .. import enums
+from ..mixins import NotifyUserEventValidationMixin
from ..types import Webhook
from . import WebhookCreate
@@ -75,7 +76,7 @@ class WebhookUpdateInput(BaseInputObjectType):
doc_category = DOC_CATEGORY_WEBHOOKS
-class WebhookUpdate(WebhookCreate):
+class WebhookUpdate(WebhookCreate, NotifyUserEventValidationMixin):
class Arguments:
id = graphene.ID(required=True, description="ID of a webhook to update.")
input = WebhookUpdateInput(
@@ -97,6 +98,7 @@ class WebhookUpdate(WebhookCreate):
def save(cls, _info: ResolveInfo, instance, cleaned_input):
instance.save()
events = set(cleaned_input.get("events", []))
+ cls.validate_events(events)
if events:
instance.events.all().delete()
models.WebhookEvent.objects.bulk_create(
diff --git a/saleor/graphql/webhook/tests/mutations/test_webhook_create.py b/saleor/graphql/webhook/tests/mutations/test_webhook_create.py
index 9e149d4b7c..47b8668062 100644
--- a/saleor/graphql/webhook/tests/mutations/test_webhook_create.py
+++ b/saleor/graphql/webhook/tests/mutations/test_webhook_create.py
@@ -322,3 +322,29 @@ def test_webhook_create_invalid_custom_headers(app_api_client):
'"X-*" or "Authorization*".'
)
assert error["code"] == WebhookErrorCode.INVALID_CUSTOM_HEADERS.name
+
+
+def test_webhook_create_notify_user_with_another_event(app_api_client):
+ # given
+ query = WEBHOOK_CREATE
+ variables = {
+ "input": {
+ "name": "NOTIFY_USER with another event fails to save",
+ "targetUrl": "https://www.example.com",
+ "asyncEvents": [
+ WebhookEventTypeAsyncEnum.ORDER_CREATED.name,
+ WebhookEventTypeAsyncEnum.NOTIFY_USER.name,
+ ],
+ }
+ }
+
+ # when
+ response = app_api_client.post_graphql(query, variables=variables)
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["webhookCreate"]
+ assert not data["webhook"]
+ error = data["errors"][0]
+ assert error["field"] == "asyncEvents"
+ assert error["code"] == WebhookErrorCode.INVALID_NOTIFY_WITH_SUBSCRIPTION.name
diff --git a/saleor/graphql/webhook/tests/mutations/test_webhook_update.py b/saleor/graphql/webhook/tests/mutations/test_webhook_update.py
index 9660da929d..8ac6c35b09 100644
--- a/saleor/graphql/webhook/tests/mutations/test_webhook_update.py
+++ b/saleor/graphql/webhook/tests/mutations/test_webhook_update.py
@@ -273,3 +273,31 @@ def test_webhook_update_invalid_custom_headers(
'"X-*" or "Authorization*".'
)
assert error["code"] == WebhookErrorCode.INVALID_CUSTOM_HEADERS.name
+
+
+def test_webhook_update_notify_user_with_another_event(app_api_client, webhook):
+ # given
+ query = WEBHOOK_UPDATE
+ webhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)
+ variables = {
+ "id": webhook_id,
+ "input": {
+ "name": "NOTIFY_USER with another event fails to save",
+ "targetUrl": "https://www.example.com",
+ "asyncEvents": [
+ WebhookEventTypeAsyncEnum.ORDER_CREATED.name,
+ WebhookEventTypeAsyncEnum.NOTIFY_USER.name,
+ ],
+ },
+ }
+
+ # when
+ response = app_api_client.post_graphql(query, variables=variables)
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["webhookUpdate"]
+ assert not data["webhook"]
+ error = data["errors"][0]
+ assert error["field"] == "asyncEvents"
+ assert error["code"] == WebhookErrorCode.INVALID_NOTIFY_WITH_SUBSCRIPTION.name
diff --git a/saleor/webhook/error_codes.py b/saleor/webhook/error_codes.py
index 441531f0ed..dfca857ca5 100644
--- a/saleor/webhook/error_codes.py
+++ b/saleor/webhook/error_codes.py
@@ -13,6 +13,7 @@ class WebhookErrorCode(Enum):
UNABLE_TO_PARSE = "unable_to_parse"
MISSING_EVENT = "missing_event"
INVALID_CUSTOM_HEADERS = "invalid_custom_headers"
+ INVALID_NOTIFY_WITH_SUBSCRIPTION = "invalid_notify_with_subscription"
class WebhookDryRunErrorCode(Enum):