Skip to content

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

Do not call sync webhooks before async webhook when webhook's subscription query is missing (#16535)

Maciej Korycinski63a25a0c46d5
diff --git a/saleor/core/utils/events.py b/saleor/core/utils/events.py
index 9fe55bb1df..ff49002c29 100644
--- a/saleor/core/utils/events.py
+++ b/saleor/core/utils/events.py
@@ -8,17 +8,36 @@ from ...webhook.event_types import WebhookEventAsyncType
from ...webhook.models import Webhook
-def webhook_async_event_requires_sync_webhooks_to_trigger(
- event_name: str,
- webhook_event_map: dict[str, set["Webhook"]],
- possible_sync_events: list[str],
+def any_webhook_has_subscription(
+ events: list[str], webhook_event_map: dict[str, set["Webhook"]]
) -> bool:
- """Check if calling the event requires additional actions.
+ event_has_subscription = False
+ for event in events:
+ event_has_subscription = any(
+ [
+ bool(webhook.subscription_query)
+ for webhook in webhook_event_map.get(event, [])
+ ]
+ )
+ if event_has_subscription:
+ break
+ return event_has_subscription
- In case of having active webhook with the `event_name`, the function will return
- True, when any sync from `possible_sync_events`'s webhooks are active. `True`
- means that sync webhook should be triggered first, before calling async webhook.
- """
+
+def any_webhook_is_active_for_events(
+ events: list[str], webhook_event_map: dict[str, set["Webhook"]]
+) -> bool:
+ """Check if any webhook is active for given events."""
+
+ active_webhook_events = set(
+ [event for event, webhooks in webhook_event_map.items() if webhooks]
+ )
+ if not active_webhook_events.intersection(events):
+ return False
+ return True
+
+
+def _validate_event_name(event_name, webhook_event_map):
is_async_event = event_name in WebhookEventAsyncType.ALL
if not is_async_event:
raise ValueError(f"Event {event_name} is not an async event.")
@@ -26,6 +45,8 @@ def webhook_async_event_requires_sync_webhooks_to_trigger(
if event_name not in webhook_event_map:
raise ValueError(f"Event {event_name} not found in webhook_event_map.")
+
+def _validate_webhook_event_map(webhook_event_map, possible_sync_events):
missing_possible_sync_events_in_map = set(possible_sync_events).difference(
webhook_event_map.keys()
)
@@ -34,15 +55,40 @@ def webhook_async_event_requires_sync_webhooks_to_trigger(
f"Event {missing_possible_sync_events_in_map} not found in webhook_event_map."
)
+
+def webhook_async_event_requires_sync_webhooks_to_trigger(
+ event_name: str,
+ webhook_event_map: dict[str, set["Webhook"]],
+ possible_sync_events: list[str],
+) -> bool:
+ """Check if calling the event requires additional actions.
+
+ In case of having active webhook with the `event_name`, the function will return
+ True, when any sync from `possible_sync_events`'s webhooks are active. `True`
+ means that sync webhook should be triggered first, before calling async webhook.
+ """
+ _validate_event_name(event_name, webhook_event_map)
+
+ _validate_webhook_event_map(webhook_event_map, possible_sync_events)
+
if not webhook_event_map[event_name] and not webhook_event_map.get(
WebhookEventAsyncType.ANY
):
return False
- active_webhook_events = set(
- [event for event, webhooks in webhook_event_map.items() if webhooks]
+ if not any_webhook_is_active_for_events(possible_sync_events, webhook_event_map):
+ return False
+
+ async_webhooks_have_subscriptions = any_webhook_has_subscription(
+ [event_name], webhook_event_map
+ )
+ if not async_webhooks_have_subscriptions:
+ return False
+
+ sync_events_have_subscriptions = any_webhook_has_subscription(
+ possible_sync_events, webhook_event_map
)
- if not active_webhook_events.intersection(possible_sync_events):
+ if not sync_events_have_subscriptions:
return False
return True
diff --git a/saleor/core/utils/tests/test_events.py b/saleor/core/utils/tests/test_events.py
index e154214df6..cc43b317c2 100644
--- a/saleor/core/utils/tests/test_events.py
+++ b/saleor/core/utils/tests/test_events.py
@@ -43,7 +43,7 @@ def test_webhook_async_event_requires_sync_webhooks_to_trigger_not_async_event()
)
-def test_webhook_async_event_requires_sync_webhooks_to_trigger():
+def test_webhook_async_event_requires_sync_webhooks_to_trigger_when_no_webhooks():
# given
event_name = WebhookEventAsyncType.ORDER_CREATED
webhook_event_map = get_webhooks_for_multiple_events(
@@ -60,6 +60,79 @@ def test_webhook_async_event_requires_sync_webhooks_to_trigger():
assert not should_trigger
+def test_webhook_async_event_requires_sync_webhooks_to_trigger_when_async_webhook(
+ webhook, permission_manage_orders
+):
+ # given
+ webhook.events.create(event_type=WebhookEventAsyncType.ORDER_CREATED)
+ webhook.app.permissions.set([permission_manage_orders])
+
+ event_name = WebhookEventAsyncType.ORDER_CREATED
+ webhook_event_map = get_webhooks_for_multiple_events(
+ [WebhookEventAsyncType.ORDER_CREATED, *WebhookEventSyncType.ORDER_EVENTS]
+ )
+
+ # when
+
+ should_trigger = webhook_async_event_requires_sync_webhooks_to_trigger(
+ event_name, webhook_event_map, WebhookEventSyncType.ORDER_EVENTS
+ )
+
+ # then
+ assert not should_trigger
+
+
+def test_webhook_async_event_requires_sync_webhooks_to_trigger_when_sync_webhook_active(
+ webhook, permission_manage_orders, setup_order_webhooks
+):
+ # given
+ different_event = WebhookEventAsyncType.ORDER_EXPIRED
+ (
+ tax_webhook,
+ shipping_filter_webhook,
+ order_created_webhook,
+ ) = setup_order_webhooks(different_event)
+
+ event_name = WebhookEventAsyncType.ORDER_CREATED
+ webhook_event_map = get_webhooks_for_multiple_events(
+ [WebhookEventAsyncType.ORDER_CREATED, *WebhookEventSyncType.ORDER_EVENTS]
+ )
+
+ # when
+
+ should_trigger = webhook_async_event_requires_sync_webhooks_to_trigger(
+ event_name, webhook_event_map, WebhookEventSyncType.ORDER_EVENTS
+ )
+
+ # then
+ assert not should_trigger
+
+
+def test_webhook_async_event_requires_sync_webhooks_to_trigger_webhooks_active(
+ setup_order_webhooks,
+):
+ # given
+ event_name = WebhookEventAsyncType.ORDER_CREATED
+ (
+ tax_webhook,
+ shipping_filter_webhook,
+ order_created_webhook,
+ ) = setup_order_webhooks(event_name)
+
+ webhook_event_map = get_webhooks_for_multiple_events(
+ [event_name, *WebhookEventSyncType.ORDER_EVENTS]
+ )
+
+ # when
+
+ should_trigger = webhook_async_event_requires_sync_webhooks_to_trigger(
+ event_name, webhook_event_map, WebhookEventSyncType.ORDER_EVENTS
+ )
+
+ # then
+ assert should_trigger
+
+
def test_webhook_async_event_requires_sync_webhooks_to_trigger_missing_event_in_map():
# given
event_name = WebhookEventAsyncType.ORDER_CREATED
@@ -80,3 +153,126 @@ def test_webhook_async_event_requires_sync_webhooks_to_trigger_missing_event_in_
webhook_async_event_requires_sync_webhooks_to_trigger(
event_name, webhook_event_map, WebhookEventSyncType.ORDER_EVENTS
)
+
+
[email protected]("subscription_query", ["", None])
+def test_webhook_async_event_requires_sync_webhooks_to_trigger_no_subscription_for_event(
+ setup_order_webhooks, subscription_query, webhook, permission_manage_orders
+):
+ # given
+ (
+ tax_webhook,
+ shipping_filter_webhook,
+ order_created_webhook,
+ ) = setup_order_webhooks(WebhookEventAsyncType.ORDER_CREATED)
+
+ order_created_webhook.subscription_query = subscription_query
+ order_created_webhook.save(update_fields=["subscription_query"])
+
+ webhook.events.create(event_type=WebhookEventAsyncType.ORDER_CREATED)
+ webhook.app.permissions.set([permission_manage_orders])
+ webhook.subscription_query = subscription_query
+ webhook.save(update_fields=["subscription_query"])
+
+ event_name = WebhookEventAsyncType.ORDER_CREATED
+ webhook_event_map = get_webhooks_for_multiple_events(
+ [WebhookEventAsyncType.ORDER_CREATED, *WebhookEventSyncType.ORDER_EVENTS]
+ )
+
+ # when
+
+ should_trigger = webhook_async_event_requires_sync_webhooks_to_trigger(
+ event_name, webhook_event_map, WebhookEventSyncType.ORDER_EVENTS
+ )
+
+ # then
+ assert not should_trigger
+
+
[email protected]("subscription_query", ["", None])
+def test_webhook_async_event_requires_sync_webhooks_to_trigger_no_subscription_single_webhook(
+ setup_order_webhooks, subscription_query, webhook, permission_manage_orders
+):
+ # given
+ webhook.events.create(event_type=WebhookEventAsyncType.ORDER_CREATED)
+ webhook.app.permissions.set([permission_manage_orders])
+ webhook.subscription_query = subscription_query
+ webhook.save(update_fields=["subscription_query"])
+
+ (
+ tax_webhook,
+ shipping_filter_webhook,
+ order_created_webhook,
+ ) = setup_order_webhooks(WebhookEventAsyncType.ORDER_CREATED)
+
+ event_name = WebhookEventAsyncType.ORDER_CREATED
+ webhook_event_map = get_webhooks_for_multiple_events(
+ [WebhookEventAsyncType.ORDER_CREATED, *WebhookEventSyncType.ORDER_EVENTS]
+ )
+
+ # when
+
+ should_trigger = webhook_async_event_requires_sync_webhooks_to_trigger(
+ event_name, webhook_event_map, WebhookEventSyncType.ORDER_EVENTS
+ )
+
+ # then
+ assert should_trigger
+
+
[email protected]("subscription_query", ["", None])
+def test_webhook_async_event_requires_sync_webhooks_to_trigger_no_subscription_for_async_events(
+ setup_order_webhooks, subscription_query
+):
+ # given
+ (
+ tax_webhook,
+ shipping_filter_webhook,
+ order_created_webhook,
+ ) = setup_order_webhooks(WebhookEventAsyncType.ORDER_CREATED)
+ tax_webhook.subscription_query = subscription_query
+ tax_webhook.save(update_fields=["subscription_query"])
+ shipping_filter_webhook.subscription_query = subscription_query
+ shipping_filter_webhook.save(update_fields=["subscription_query"])
+
+ event_name = WebhookEventAsyncType.ORDER_CREATED
+ webhook_event_map = get_webhooks_for_multiple_events(
+ [WebhookEventAsyncType.ORDER_CREATED, *WebhookEventSyncType.ORDER_EVENTS]
+ )
+
+ # when
+
+ should_trigger = webhook_async_event_requires_sync_webhooks_to_trigger(
+ event_name, webhook_event_map, WebhookEventSyncType.ORDER_EVENTS
+ )
+
+ # then
+ assert not should_trigger
+
+
[email protected]("subscription_query", ["", None])
+def test_webhook_async_event_requires_sync_webhooks_to_trigger_no_subscription_for_single_async_event(
+ setup_order_webhooks, subscription_query
+):
+ # given
+ (
+ tax_webhook,
+ shipping_filter_webhook,
+ order_created_webhook,
+ ) = setup_order_webhooks(WebhookEventAsyncType.ORDER_CREATED)
+ shipping_filter_webhook.subscription_query = subscription_query
+ shipping_filter_webhook.save(update_fields=["subscription_query"])
+
+ event_name = WebhookEventAsyncType.ORDER_CREATED
+ webhook_event_map = get_webhooks_for_multiple_events(
+ [WebhookEventAsyncType.ORDER_CREATED, *WebhookEventSyncType.ORDER_EVENTS]
+ )
+
+ # when
+
+ should_trigger = webhook_async_event_requires_sync_webhooks_to_trigger(
+ event_name, webhook_event_map, WebhookEventSyncType.ORDER_EVENTS
+ )
+
+ # then
+ assert should_trigger