Skip to content

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

Move ASYNC webhooks to Promise approach (#18794)

Maciej Korycinskib4cc3b3f211f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6878045d95..b8feedb1c1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Scalars `Minute`, `Hour` and `Day` now inherit from `NonNegativeInt`, which mean GraphQL disallows negative values for time units.
### Webhooks
+For order webhook events, sync webhooks (such as `ORDER_CALCULATE_TAXES` and `ORDER_FILTER_SHIPPING_METHODS`) are no longer pre-fired before sending async webhook events. Sync webhooks are now only triggered when their data is actually requested, improving performance and decoupling async event delivery from sync webhook execution.
### Other changes
- Improved page search with search vectors. Pages can now be searched by slug, title, content, attribute values, and page type information.
diff --git a/saleor/core/utils/events.py b/saleor/core/utils/events.py
index b72637a38e..6d07f07d4a 100644
--- a/saleor/core/utils/events.py
+++ b/saleor/core/utils/events.py
@@ -2,8 +2,6 @@ from django.db import transaction
from ...checkout.fetch import CheckoutInfo
from ...checkout.models import Checkout
-from ...order.fetch import OrderInfo
-from ...order.models import Order
from ...webhook.event_types import WebhookEventAsyncType
from ...webhook.models import Webhook
@@ -127,13 +125,12 @@ def call_event(func_obj, *func_args, **func_kwargs):
Ensures that in atomic transaction event is called on_commit.
"""
is_protected_instance = any(
- isinstance(arg, Checkout | CheckoutInfo | Order | OrderInfo)
- for arg in func_args
+ isinstance(arg, Checkout | CheckoutInfo) for arg in func_args
)
func_obj_self = getattr(func_obj, "__self__", None)
is_plugin_manager_method = "PluginsManager" in str(
getattr(func_obj_self, "__class__", "")
)
if is_protected_instance and is_plugin_manager_method:
- raise NotImplementedError("`call_event` doesn't support checkout/order events.")
+ raise NotImplementedError("`call_event` doesn't support checkout events.")
call_event_including_protected_events(func_obj, *func_args, **func_kwargs)
diff --git a/saleor/graphql/meta/tests/mutations/test_order.py b/saleor/graphql/meta/tests/mutations/test_order.py
index 587ffb7d70..bde9e58b69 100644
--- a/saleor/graphql/meta/tests/mutations/test_order.py
+++ b/saleor/graphql/meta/tests/mutations/test_order.py
@@ -5,7 +5,6 @@ from django.test import override_settings
from .....core.models import EventDelivery
from .....order import OrderStatus
-from .....order.actions import call_order_event
from .....payment.models import TransactionItem
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from . import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
@@ -661,10 +660,6 @@ def test_add_private_metadata_for_fulfillment(
)
-@patch(
- "saleor.graphql.meta.extra_methods.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -673,7 +668,6 @@ def test_add_private_metadata_for_fulfillment(
def test_change_in_public_metadata_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
staff_api_client,
order_with_lines,
@@ -721,8 +715,15 @@ def test_change_in_public_metadata_triggers_webhooks(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -735,13 +736,7 @@ def test_change_in_public_metadata_triggers_webhooks(
== WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
- assert wrapped_call_order_event.called
-
-@patch(
- "saleor.graphql.meta.extra_methods.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -750,7 +745,6 @@ def test_change_in_public_metadata_triggers_webhooks(
def test_change_in_private_metadata_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
staff_api_client,
permission_manage_orders,
@@ -797,8 +791,15 @@ def test_change_in_private_metadata_triggers_webhooks(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -810,5 +811,3 @@ def test_change_in_private_metadata_triggers_webhooks(
filter_shipping_delivery.event_type
== WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
-
- assert wrapped_call_order_event.called
diff --git a/saleor/graphql/order/tests/mutations/test_draft_order_complete.py b/saleor/graphql/order/tests/mutations/test_draft_order_complete.py
index ee17d8e340..e31bcab3fd 100644
--- a/saleor/graphql/order/tests/mutations/test_draft_order_complete.py
+++ b/saleor/graphql/order/tests/mutations/test_draft_order_complete.py
@@ -22,7 +22,7 @@ from .....discount.utils.voucher import (
)
from .....order import OrderOrigin, OrderStatus
from .....order import events as order_events
-from .....order.actions import call_order_event, order_created
+from .....order.actions import order_created
from .....order.calculations import fetch_order_prices_if_expired
from .....order.error_codes import OrderErrorCode
from .....order.interface import OrderTaxedPricesData
@@ -1727,10 +1727,6 @@ def test_draft_order_complete_with_invalid_address_save_addresses_on(
assert customer_user.addresses.first().id != order.billing_address.id
-@patch(
- "saleor.order.actions.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -1739,7 +1735,6 @@ def test_draft_order_complete_with_invalid_address_save_addresses_on(
def test_draft_order_complete_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
staff_api_client,
permission_group_manage_orders,
@@ -1810,8 +1805,15 @@ def test_draft_order_complete_triggers_webhooks(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -1824,8 +1826,6 @@ def test_draft_order_complete_triggers_webhooks(
== WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
- assert wrapped_call_order_event.called
-
@patch(
"saleor.graphql.order.mutations.draft_order_complete.order_created",
diff --git a/saleor/graphql/order/tests/mutations/test_draft_order_create.py b/saleor/graphql/order/tests/mutations/test_draft_order_create.py
index 1bc1e6c2c8..9649ef6f18 100644
--- a/saleor/graphql/order/tests/mutations/test_draft_order_create.py
+++ b/saleor/graphql/order/tests/mutations/test_draft_order_create.py
@@ -19,7 +19,6 @@ from .....discount import DiscountType, DiscountValueType, RewardType, RewardVal
from .....discount.models import VoucherChannelListing, VoucherCode, VoucherCustomer
from .....order import OrderStatus
from .....order import events as order_events
-from .....order.actions import call_order_event
from .....order.calculations import process_order_prices
from .....order.error_codes import OrderErrorCode
from .....order.models import Order, OrderEvent, OrderLine
@@ -3696,10 +3695,7 @@ def test_draft_order_create_voucher_with_usage_limit(
assert data["voucherCode"] == code_1.code
-@patch(
- "saleor.graphql.order.mutations.draft_order_create.call_order_event",
- wraps=call_order_event,
-)
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -3708,7 +3704,7 @@ def test_draft_order_create_voucher_with_usage_limit(
def test_draft_order_create_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
+ mocked_cache,
setup_order_webhooks,
app_api_client,
permission_manage_orders,
@@ -3718,8 +3714,11 @@ def test_draft_order_create_triggers_webhooks(
channel_PLN,
graphql_address_data,
settings,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
+
mocked_send_webhook_request_sync.return_value = []
(
tax_webhook,
@@ -3764,7 +3763,7 @@ def test_draft_order_create_triggers_webhooks(
content = get_graphql_content(response)
assert not content["data"]["draftOrderCreate"]["errors"]
- # confirm that event delivery was generated for each async webhook.
+ # # confirm that event delivery was generated for each async webhook.
draft_order_created_delivery = EventDelivery.objects.get(
webhook_id=draft_order_created_webhook.id
)
@@ -3779,12 +3778,20 @@ def test_draft_order_create_triggers_webhooks(
# confirm each sync webhook was called without saving event delivery
assert mocked_send_webhook_request_sync.call_count == 2
+
assert not EventDelivery.objects.exclude(
webhook_id=draft_order_created_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -3797,8 +3804,6 @@ def test_draft_order_create_triggers_webhooks(
== WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
- assert wrapped_call_order_event.called
-
def test_draft_order_create_with_metadata(
staff_api_client,
diff --git a/saleor/graphql/order/tests/mutations/test_draft_order_delete.py b/saleor/graphql/order/tests/mutations/test_draft_order_delete.py
index d1ff1ca1e1..531597b2ec 100644
--- a/saleor/graphql/order/tests/mutations/test_draft_order_delete.py
+++ b/saleor/graphql/order/tests/mutations/test_draft_order_delete.py
@@ -7,7 +7,6 @@ from django.test import override_settings
from .....core.models import EventDelivery
from .....discount.models import VoucherCode
from .....order import OrderStatus
-from .....order.actions import call_order_event
from .....order.error_codes import OrderErrorCode
from .....webhook.event_types import WebhookEventAsyncType
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -302,10 +301,6 @@ def test_draft_order_delete_release_voucher_codes_single_use(
assert voucher_code.is_active is True
-@patch(
- "saleor.graphql.order.mutations.draft_order_delete.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -314,7 +309,6 @@ def test_draft_order_delete_release_voucher_codes_single_use(
def test_draft_order_delete_do_not_trigger_sync_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
settings,
staff_api_client,
@@ -356,7 +350,6 @@ def test_draft_order_delete_do_not_trigger_sync_webhooks(
MessageGroupId="example.com:saleorappadditional",
)
assert not mocked_send_webhook_request_sync.called
- assert wrapped_call_order_event.called
def test_draft_order_delete_with_voucher_and_include_draft_order_in_voucher_usage_false(
diff --git a/saleor/graphql/order/tests/mutations/test_draft_order_update.py b/saleor/graphql/order/tests/mutations/test_draft_order_update.py
index a9962aab9b..ab2c42c3d9 100644
--- a/saleor/graphql/order/tests/mutations/test_draft_order_update.py
+++ b/saleor/graphql/order/tests/mutations/test_draft_order_update.py
@@ -2487,10 +2487,6 @@ def test_draft_order_update_replace_entire_order_voucher_with_shipping_voucher(
assert order_discount.voucher_code == shipping_code
-@patch(
- "saleor.graphql.order.mutations.draft_order_update.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -2499,7 +2495,6 @@ def test_draft_order_update_replace_entire_order_voucher_with_shipping_voucher(
def test_draft_order_update_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
voucher,
app_api_client,
@@ -2566,8 +2561,15 @@ def test_draft_order_update_triggers_webhooks(
webhook_id=draft_order_updated_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -2580,13 +2582,7 @@ def test_draft_order_update_triggers_webhooks(
== WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
- assert wrapped_call_order_event.called
-
-@patch(
- "saleor.graphql.order.mutations.draft_order_update.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -2595,7 +2591,6 @@ def test_draft_order_update_triggers_webhooks(
def test_draft_order_update_triggers_webhooks_when_tax_webhook_not_needed(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
voucher,
app_api_client,
@@ -2664,8 +2659,6 @@ def test_draft_order_update_triggers_webhooks_when_tax_webhook_not_needed(
)
assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
- assert wrapped_call_order_event.called
-
def test_draft_order_update_address_reset_save_address_flag_to_default_value(
staff_api_client,
@@ -3269,13 +3262,8 @@ def test_draft_order_update_with_voucher_apply_once_per_order_and_manual_line_di
@patch(
"saleor.graphql.order.mutations.draft_order_update.update_order_search_vector",
)
-@patch(
- "saleor.graphql.order.mutations.draft_order_update.call_order_event",
- wraps=call_order_event,
-)
@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
def test_draft_order_update_nothing_changed(
- wrapped_call_order_event,
mocked_update_order_search_vector,
setup_order_webhooks,
staff_api_client,
@@ -3315,9 +3303,6 @@ def test_draft_order_update_nothing_changed(
# ensure the update fields were empty
mocked_update_order_search_vector.assert_not_called()
- # confirm that order events were not triggered
- assert not wrapped_call_order_event.called
-
# confirm that event delivery was generated for each async webhook.
assert not EventDelivery.objects.filter(webhook_id=draft_order_updated_webhook.id)
@@ -3352,16 +3337,11 @@ def test_draft_order_update_with_language_code(
assert order.language_code == "pl"
-@patch(
- "saleor.graphql.order.mutations.draft_order_update.call_order_event",
- wraps=call_order_event,
-)
@patch(
"saleor.graphql.order.mutations.draft_order_update.DraftOrderUpdate._save_order_instance"
)
def test_draft_order_update_no_changes(
save_order_mock,
- call_event_mock,
staff_api_client,
permission_group_manage_orders,
order_with_lines,
@@ -3448,7 +3428,6 @@ def test_draft_order_update_no_changes(
assert not content["data"]["draftOrderUpdate"]["errors"]
order.refresh_from_db()
save_order_mock.assert_not_called()
- call_event_mock.assert_not_called()
@patch(
@@ -3558,12 +3537,7 @@ def test_draft_order_update_emit_events(
call_event_mock.reset_mock()
-@patch(
- "saleor.graphql.order.mutations.draft_order_update.call_order_event",
- wraps=call_order_event,
-)
def test_draft_order_update_address_not_changed_save_flag_changed(
- call_event_mock,
staff_api_client,
permission_group_manage_orders,
order_with_lines,
@@ -3618,15 +3592,9 @@ def test_draft_order_update_address_not_changed_save_flag_changed(
order.refresh_from_db()
assert order.draft_save_billing_address is True
assert order.draft_save_shipping_address is True
- call_event_mock.assert_called()
-@patch(
- "saleor.graphql.order.mutations.draft_order_update.call_order_event",
- wraps=call_order_event,
-)
def test_draft_order_update_address_not_set(
- call_event_mock,
staff_api_client,
permission_group_manage_orders,
order_with_lines,
@@ -3660,15 +3628,9 @@ def test_draft_order_update_address_not_set(
order.refresh_from_db()
assert order.shipping_address
assert order.billing_address
- call_event_mock.assert_called()
-@patch(
- "saleor.graphql.order.mutations.draft_order_update.call_order_event",
- wraps=call_order_event,
-)
def test_draft_order_update_same_shipping_method_no_shipping_price_set(
- call_event_mock,
staff_api_client,
permission_group_manage_orders,
order_with_lines,
@@ -3706,7 +3668,6 @@ def test_draft_order_update_same_shipping_method_no_shipping_price_set(
assert not content["data"]["draftOrderUpdate"]["errors"]
order.refresh_from_db()
assert order.undiscounted_base_shipping_price_amount != 0
- call_event_mock.assert_called()
def test_draft_order_update_metadata_key_updated_in_meantime(
diff --git a/saleor/graphql/order/tests/mutations/test_order_cancel.py b/saleor/graphql/order/tests/mutations/test_order_cancel.py
index 991faec4fb..5a24f82543 100644
--- a/saleor/graphql/order/tests/mutations/test_order_cancel.py
+++ b/saleor/graphql/order/tests/mutations/test_order_cancel.py
@@ -7,7 +7,7 @@ from .....core.models import EventDelivery
from .....giftcard import GiftCardEvents
from .....giftcard.events import gift_cards_bought_event
from .....order import OrderStatus
-from .....order.actions import call_order_events, cancel_order
+from .....order.actions import cancel_order
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -132,10 +132,6 @@ def test_order_cancel_no_channel_access(
assert_no_permission(response)
-@patch(
- "saleor.order.actions.call_order_events",
- wraps=call_order_events,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -146,7 +142,6 @@ def test_order_cancel_skip_trigger_webhooks(
mock_clean_order_cancel,
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_events,
setup_order_webhooks,
staff_api_client,
permission_group_manage_orders,
@@ -214,4 +209,3 @@ def test_order_cancel_skip_trigger_webhooks(
any_order=True,
)
assert not mocked_send_webhook_request_sync.called
- assert wrapped_call_order_events.called
diff --git a/saleor/graphql/order/tests/mutations/test_order_capture.py b/saleor/graphql/order/tests/mutations/test_order_capture.py
index d57d153b31..82656cee81 100644
--- a/saleor/graphql/order/tests/mutations/test_order_capture.py
+++ b/saleor/graphql/order/tests/mutations/test_order_capture.py
@@ -8,7 +8,7 @@ from .....core.notify import NotifyEventType
from .....core.tests.utils import get_site_context_payload
from .....order import OrderOrigin, OrderStatus
from .....order import events as order_events
-from .....order.actions import call_order_event, order_charged
+from .....order.actions import order_charged
from .....order.notifications import get_default_order_payload
from .....payment import ChargeStatus
from .....payment.models import Payment
@@ -172,10 +172,7 @@ def test_order_capture_by_app(
assert data["totalCaptured"]["amount"] == float(amount)
-@patch(
- "saleor.order.actions.call_order_event",
- wraps=call_order_event,
-)
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -189,16 +186,21 @@ def test_order_capture_by_app(
def test_order_capture_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
+ mocked_cache,
setup_order_webhooks,
staff_api_client,
permission_group_manage_orders,
payment_txn_preauth,
+ order_with_lines,
staff_user,
settings,
+ tax_data_response_factory,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
mocked_send_webhook_request_sync.return_value = []
+
(
tax_webhook,
shipping_filter_webhook,
@@ -210,6 +212,9 @@ def test_order_capture_triggers_webhooks(
WebhookEventAsyncType.ORDER_FULLY_PAID,
]
)
+ order = payment_txn_preauth.order
+ order.channel.automatically_confirm_all_new_orders = False
+ order.channel.save(update_fields=["automatically_confirm_all_new_orders"])
permission_group_manage_orders.user_set.add(staff_api_client.user)
order = payment_txn_preauth.order
@@ -265,8 +270,15 @@ def test_order_capture_triggers_webhooks(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -279,13 +291,7 @@ def test_order_capture_triggers_webhooks(
== WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
- assert wrapped_call_order_event.called
-
-@patch(
- "saleor.order.actions.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -299,7 +305,6 @@ def test_order_capture_triggers_webhooks(
def test_draft_order_capture_dont_triggers_fully_paid_webhook(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
staff_api_client,
permission_group_manage_orders,
@@ -350,4 +355,3 @@ def test_draft_order_capture_dont_triggers_fully_paid_webhook(
).exists()
assert not mocked_send_webhook_request_async.called
- assert not wrapped_call_order_event.called
diff --git a/saleor/graphql/order/tests/mutations/test_order_confirm.py b/saleor/graphql/order/tests/mutations/test_order_confirm.py
index 371d969e87..ef05a95a59 100644
--- a/saleor/graphql/order/tests/mutations/test_order_confirm.py
+++ b/saleor/graphql/order/tests/mutations/test_order_confirm.py
@@ -13,7 +13,6 @@ from .....order import events as order_events
from .....order.actions import (
WEBHOOK_EVENTS_FOR_ORDER_CHARGED,
WEBHOOK_EVENTS_FOR_ORDER_CONFIRMED,
- call_order_event,
handle_fully_paid_order,
order_charged,
order_confirmed,
@@ -604,10 +603,6 @@ def test_order_confirm_skip_address_validation(
assert order.status == OrderStatus.UNFULFILLED
-@patch(
- "saleor.order.actions.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -618,7 +613,6 @@ def test_order_confirm_triggers_webhooks(
capture_mock,
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
staff_api_client,
order_unconfirmed,
@@ -698,4 +692,3 @@ def test_order_confirm_triggers_webhooks(
any_order=True,
)
assert not mocked_send_webhook_request_sync.called
- assert wrapped_call_order_event.called
diff --git a/saleor/graphql/order/tests/mutations/test_order_fulfill.py b/saleor/graphql/order/tests/mutations/test_order_fulfill.py
index 719b2de3e9..eda17acd66 100644
--- a/saleor/graphql/order/tests/mutations/test_order_fulfill.py
+++ b/saleor/graphql/order/tests/mutations/test_order_fulfill.py
@@ -9,7 +9,7 @@ from .....core.models import EventDelivery
from .....giftcard import GiftCardEvents
from .....giftcard.models import GiftCard, GiftCardEvent
from .....order import FulfillmentStatus, OrderEvents, OrderStatus
-from .....order.actions import call_order_events, order_fulfilled
+from .....order.actions import order_fulfilled
from .....order.error_codes import OrderErrorCode
from .....order.models import Fulfillment, FulfillmentLine
from .....product.models import ProductVariant
@@ -1560,10 +1560,6 @@ def test_order_fulfill_tracking_number_updated_event_triggered(
assert mocked_fulfillment_created[0][1] == WebhookEventAsyncType.FULFILLMENT_CREATED
-@patch(
- "saleor.order.actions.call_order_events",
- wraps=call_order_events,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -1572,7 +1568,6 @@ def test_order_fulfill_tracking_number_updated_event_triggered(
def test_order_fulfill_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_events,
setup_order_webhooks,
staff_api_client,
order_with_lines,
@@ -1658,7 +1653,6 @@ def test_order_fulfill_triggers_webhooks(
],
any_order=True,
)
- assert wrapped_call_order_events.called
def test_order_fulfill_fulfilled_order_race_condition(
diff --git a/saleor/graphql/order/tests/mutations/test_order_line_delete.py b/saleor/graphql/order/tests/mutations/test_order_line_delete.py
index c68318226b..1aa25d4f72 100644
--- a/saleor/graphql/order/tests/mutations/test_order_line_delete.py
+++ b/saleor/graphql/order/tests/mutations/test_order_line_delete.py
@@ -8,7 +8,6 @@ from django.test import override_settings
from .....core.models import EventDelivery
from .....order import OrderStatus
from .....order import events as order_events
-from .....order.actions import call_order_event
from .....order.error_codes import OrderErrorCode
from .....order.models import OrderEvent
from .....warehouse.models import Stock
@@ -322,10 +321,6 @@ def test_order_line_delete_non_removable_gift(
(OrderStatus.UNCONFIRMED, WebhookEventAsyncType.ORDER_UPDATED),
],
)
-@patch(
- "saleor.graphql.order.mutations.utils.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -334,7 +329,6 @@ def test_order_line_delete_non_removable_gift(
def test_order_line_delete_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
draft_order,
permission_group_manage_orders,
@@ -382,8 +376,15 @@ def test_order_line_delete_triggers_webhooks(
assert mocked_send_webhook_request_sync.call_count == 2
assert not EventDelivery.objects.exclude(webhook_id=order_webhook.id).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -395,5 +396,3 @@ def test_order_line_delete_triggers_webhooks(
filter_shipping_delivery.event_type
== WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
-
- assert wrapped_call_order_event.called
diff --git a/saleor/graphql/order/tests/mutations/test_order_line_update.py b/saleor/graphql/order/tests/mutations/test_order_line_update.py
index d02ce13d02..45ef1a9c1b 100644
--- a/saleor/graphql/order/tests/mutations/test_order_line_update.py
+++ b/saleor/graphql/order/tests/mutations/test_order_line_update.py
@@ -15,7 +15,6 @@ from .....discount.utils.voucher import (
)
from .....order import OrderStatus
from .....order import events as order_events
-from .....order.actions import call_order_event
from .....order.calculations import fetch_order_prices_if_expired
from .....order.error_codes import OrderErrorCode
from .....order.models import OrderEvent
@@ -631,10 +630,6 @@ def test_order_line_update_gift_promotion(
(OrderStatus.UNCONFIRMED, WebhookEventAsyncType.ORDER_UPDATED),
],
)
-@patch(
- "saleor.graphql.order.mutations.utils.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -643,7 +638,6 @@ def test_order_line_update_gift_promotion(
def test_order_line_update_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
order_with_lines,
permission_group_manage_orders,
@@ -693,8 +687,15 @@ def test_order_line_update_triggers_webhooks(
assert mocked_send_webhook_request_sync.call_count == 2
assert not EventDelivery.objects.exclude(webhook_id=order_webhook.id).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -707,8 +708,6 @@ def test_order_line_update_triggers_webhooks(
== WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
- assert wrapped_call_order_event.called
-
def test_order_line_update_catalogue_discount(
order_with_lines_and_catalogue_promotion,
diff --git a/saleor/graphql/order/tests/mutations/test_order_lines_create.py b/saleor/graphql/order/tests/mutations/test_order_lines_create.py
index fedc11bf1b..99f6156cf8 100644
--- a/saleor/graphql/order/tests/mutations/test_order_lines_create.py
+++ b/saleor/graphql/order/tests/mutations/test_order_lines_create.py
@@ -20,7 +20,6 @@ from .....discount.utils.voucher import (
)
from .....order import OrderStatus
from .....order import events as order_events
-from .....order.actions import call_order_event
from .....order.calculations import fetch_order_prices_if_expired
from .....order.error_codes import OrderErrorCode
from .....order.models import OrderEvent, OrderLine
@@ -1293,10 +1292,6 @@ def test_order_lines_create_no_shipping_address(
(OrderStatus.UNCONFIRMED, WebhookEventAsyncType.ORDER_UPDATED),
],
)
-@patch(
- "saleor.graphql.order.mutations.utils.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -1305,7 +1300,6 @@ def test_order_lines_create_no_shipping_address(
def test_order_lines_create_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
order_with_lines,
permission_group_manage_orders,
@@ -1354,8 +1348,15 @@ def test_order_lines_create_triggers_webhooks(
assert mocked_send_webhook_request_sync.call_count == 2
assert not EventDelivery.objects.exclude(webhook_id=order_webhook.id).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -1368,8 +1369,6 @@ def test_order_lines_create_triggers_webhooks(
== WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
- assert wrapped_call_order_event.called
-
def test_order_lines_create_with_catalogue_discount_existing_variant(
order_with_lines_and_catalogue_promotion,
diff --git a/saleor/graphql/order/tests/mutations/test_order_note_add.py b/saleor/graphql/order/tests/mutations/test_order_note_add.py
index 062db2eaa0..ec0c3a33f6 100644
--- a/saleor/graphql/order/tests/mutations/test_order_note_add.py
+++ b/saleor/graphql/order/tests/mutations/test_order_note_add.py
@@ -8,7 +8,6 @@ from .....account.models import CustomerEvent
from .....core.models import EventDelivery
from .....order import OrderStatus
from .....order import events as order_events
-from .....order.actions import call_order_event
from .....order.error_codes import OrderNoteAddErrorCode
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -184,10 +183,6 @@ def test_order_note_add_fail_on_missing_permission(staff_api_client, order):
(OrderStatus.UNCONFIRMED, WebhookEventAsyncType.ORDER_UPDATED),
],
)
-@patch(
- "saleor.graphql.order.mutations.utils.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -196,7 +191,6 @@ def test_order_note_add_fail_on_missing_permission(staff_api_client, order):
def test_order_note_add_user_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
staff_api_client,
permission_group_manage_orders,
@@ -242,10 +236,16 @@ def test_order_note_add_user_triggers_webhooks(
assert mocked_send_webhook_request_sync.call_count == 2
assert not EventDelivery.objects.exclude(webhook_id=order_webhook.id).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
-
tax_delivery = tax_delivery_call.args[0]
assert tax_delivery.webhook_id == tax_webhook.id
@@ -255,5 +255,3 @@ def test_order_note_add_user_triggers_webhooks(
filter_shipping_delivery.event_type
== WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
-
- assert wrapped_call_order_event.called
diff --git a/saleor/graphql/order/tests/mutations/test_order_note_update.py b/saleor/graphql/order/tests/mutations/test_order_note_update.py
index 7c99805efb..53fcf798ec 100644
--- a/saleor/graphql/order/tests/mutations/test_order_note_update.py
+++ b/saleor/graphql/order/tests/mutations/test_order_note_update.py
@@ -7,7 +7,6 @@ from django.test import override_settings
from .....account.models import CustomerEvent
from .....core.models import EventDelivery
from .....order import OrderEvents, OrderStatus
-from .....order.actions import call_order_event
from .....order.error_codes import OrderNoteUpdateErrorCode
from .....order.models import OrderEvent
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
@@ -216,10 +215,6 @@ def test_order_note_remove_fail_on_missing_permission(staff_api_client, order):
(OrderStatus.UNCONFIRMED, WebhookEventAsyncType.ORDER_UPDATED),
],
)
-@patch(
- "saleor.graphql.order.mutations.utils.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -228,7 +223,6 @@ def test_order_note_remove_fail_on_missing_permission(staff_api_client, order):
def test_order_note_update_user_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
staff_api_client,
permission_manage_orders,
@@ -286,8 +280,15 @@ def test_order_note_update_user_triggers_webhooks(
assert mocked_send_webhook_request_sync.call_count == 2
assert not EventDelivery.objects.exclude(webhook_id=order_webhook.id).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -299,5 +300,3 @@ def test_order_note_update_user_triggers_webhooks(
filter_shipping_delivery.event_type
== WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
-
- assert wrapped_call_order_event.called
diff --git a/saleor/graphql/order/tests/mutations/test_order_update.py b/saleor/graphql/order/tests/mutations/test_order_update.py
index 9a5f280134..5dcf032d98 100644
--- a/saleor/graphql/order/tests/mutations/test_order_update.py
+++ b/saleor/graphql/order/tests/mutations/test_order_update.py
@@ -542,10 +542,6 @@ def test_order_update_invalid_address_skip_validation(
assert order.billing_address.validation_skipped is True
-@patch(
- "saleor.graphql.order.mutations.order_update.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -554,7 +550,6 @@ def test_order_update_invalid_address_skip_validation(
def test_order_update_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
staff_api_client,
permission_group_manage_orders,
@@ -603,8 +598,15 @@ def test_order_update_triggers_webhooks(
assert mocked_send_webhook_request_sync.call_count == 2
assert not EventDelivery.objects.exclude(webhook_id=order_webhook.id).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -617,8 +619,6 @@ def test_order_update_triggers_webhooks(
== WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
- assert wrapped_call_order_event.called
-
@patch("saleor.plugins.manager.PluginsManager.order_updated")
def test_order_update_only_metadata_legacy_webhook_emission_on(
diff --git a/saleor/graphql/order/tests/mutations/test_order_update_shipping.py b/saleor/graphql/order/tests/mutations/test_order_update_shipping.py
index 80e6ed6dbd..374df2b979 100644
--- a/saleor/graphql/order/tests/mutations/test_order_update_shipping.py
+++ b/saleor/graphql/order/tests/mutations/test_order_update_shipping.py
@@ -12,7 +12,6 @@ from .....discount.utils.voucher import (
create_or_update_voucher_discount_objects_for_order,
)
from .....order import OrderStatus
-from .....order.actions import call_order_event
from .....order.calculations import fetch_order_prices_if_expired
from .....order.error_codes import OrderErrorCode
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
@@ -670,10 +669,6 @@ def test_order_shipping_update_mutation_properly_recalculate_total(
assert data["order"]["shippingMethod"] is None
-@patch(
- "saleor.graphql.order.mutations.order_update_shipping.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -682,7 +677,6 @@ def test_order_shipping_update_mutation_properly_recalculate_total(
def test_order_update_shipping_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
staff_api_client,
permission_group_manage_orders,
@@ -751,13 +745,7 @@ def test_order_update_shipping_triggers_webhooks(
)
assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
- assert wrapped_call_order_event.called
-
-@patch(
- "saleor.graphql.order.mutations.order_update_shipping.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -766,7 +754,6 @@ def test_order_update_shipping_triggers_webhooks(
def test_draft_order_update_shipping_triggers_proper_updated_webhook(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
staff_api_client,
permission_group_manage_orders,
@@ -806,13 +793,7 @@ def test_draft_order_update_shipping_triggers_proper_updated_webhook(
MessageGroupId="example.com:saleorappadditional",
)
- assert wrapped_call_order_event.called
-
-@patch(
- "saleor.graphql.order.mutations.order_update_shipping.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -821,7 +802,6 @@ def test_draft_order_update_shipping_triggers_proper_updated_webhook(
def test_draft_order_update_shipping_triggers_proper_updated_webhook_for_null_shipping_method(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
staff_api_client,
permission_group_manage_orders,
@@ -859,13 +839,7 @@ def test_draft_order_update_shipping_triggers_proper_updated_webhook_for_null_sh
MessageGroupId="example.com:saleorappadditional",
)
- assert wrapped_call_order_event.called
-
-@patch(
- "saleor.graphql.order.mutations.order_update_shipping.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -874,7 +848,6 @@ def test_draft_order_update_shipping_triggers_proper_updated_webhook_for_null_sh
def test_editable_order_update_shipping_triggers_proper_updated_webhook_for_null_shipping_method(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
staff_api_client,
permission_group_manage_orders,
@@ -913,5 +886,3 @@ def test_editable_order_update_shipping_triggers_proper_updated_webhook_for_null
queue=settings.ORDER_WEBHOOK_EVENTS_CELERY_QUEUE_NAME,
MessageGroupId="example.com:saleorappadditional",
)
-
- assert wrapped_call_order_event.called
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index 5e7146bcfd..aed48d6fe2 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -6,7 +6,6 @@ from typing import TYPE_CHECKING, Optional, TypedDict
from uuid import UUID
import graphene
-from django.conf import settings
from django.contrib.sites.models import Site
from django.db import transaction
from django.db.models import F
@@ -18,8 +17,6 @@ from ..core.tracing import traced_atomic_transaction
from ..core.transactions import transaction_with_commit_on_errors
from ..core.utils.events import (
call_event,
- call_event_including_protected_events,
- webhook_async_event_requires_sync_webhooks_to_trigger,
)
from ..giftcard import GiftCardLineData
from ..order.lock_objects import order_lines_qs_select_for_update
@@ -33,17 +30,15 @@ from ..payment import (
from ..payment.interface import RefundData
from ..payment.models import Payment, Transaction, TransactionItem
from ..plugins.manager import PluginsManager
-from ..shipping.models import ShippingMethodChannelListing
from ..warehouse.management import (
deallocate_stock,
deallocate_stock_for_orders,
decrease_stock,
)
from ..warehouse.models import Stock
-from ..webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
+from ..webhook.event_types import WebhookEventAsyncType
from ..webhook.utils import get_webhooks_for_multiple_events
from . import (
- ORDER_EDITABLE_STATUS,
FulfillmentLineData,
FulfillmentStatus,
OrderChargeStatus,
@@ -52,8 +47,6 @@ from . import (
events,
utils,
)
-from .calculations import fetch_order_prices_if_expired
-from .delivery_context import get_valid_shipping_methods_for_order
from .events import (
draft_order_created_from_replace_event,
fulfillment_refunded_event,
@@ -103,28 +96,23 @@ class OrderFulfillmentLineInfo(TypedDict):
WEBHOOK_EVENTS_FOR_ORDER_FULFILLED = {
WebhookEventAsyncType.ORDER_FULFILLED,
WebhookEventAsyncType.ORDER_UPDATED,
- *WebhookEventSyncType.ORDER_EVENTS,
}
WEBHOOK_EVENTS_FOR_ORDER_REFUNDED = {
WebhookEventAsyncType.ORDER_FULLY_REFUNDED,
WebhookEventAsyncType.ORDER_REFUNDED,
WebhookEventAsyncType.ORDER_UPDATED,
- *WebhookEventSyncType.ORDER_EVENTS,
}
WEBHOOK_EVENTS_FOR_ORDER_CANCELED = {
WebhookEventAsyncType.ORDER_CANCELLED,
WebhookEventAsyncType.ORDER_UPDATED,
- *WebhookEventSyncType.ORDER_EVENTS,
}
WEBHOOK_EVENTS_FOR_FULLY_PAID = {
WebhookEventAsyncType.ORDER_UPDATED,
WebhookEventAsyncType.ORDER_FULLY_PAID,
- *WebhookEventSyncType.ORDER_EVENTS,
}
WEBHOOK_EVENTS_FOR_ORDER_AUTHORIZED = {
WebhookEventAsyncType.ORDER_UPDATED,
- *WebhookEventSyncType.ORDER_EVENTS,
}
WEBHOOK_EVENTS_FOR_ORDER_CHARGED = {
@@ -133,7 +121,6 @@ WEBHOOK_EVENTS_FOR_ORDER_CHARGED = {
WEBHOOK_EVENTS_FOR_ORDER_CONFIRMED = {
WebhookEventAsyncType.ORDER_CONFIRMED,
- *WebhookEventSyncType.ORDER_EVENTS,
}
@@ -159,38 +146,6 @@ ORDER_WEBHOOK_EVENT_MAP = {
}
-def _trigger_order_sync_webhooks(
- manager: "PluginsManager",
- order: "Order",
- webhook_event_map: dict[str, set["Webhook"]],
- database_connection_name: str = settings.DATABASE_CONNECTION_DEFAULT_NAME,
-):
- # FIXME: We can drop this after migrating the ASYNC webhooks to promise
- # approach
- if (
- webhook_event_map.get(WebhookEventSyncType.ORDER_CALCULATE_TAXES)
- and order.should_refresh_prices
- ):
- fetch_order_prices_if_expired(
- order,
- manager,
- # This is temporary, as the whole handler will be dropped when moving
- # ASYNC webhooks to Promises.
- requestor=None,
- database_connection_name=database_connection_name,
- ).get()
- if webhook_event_map.get(WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS):
- shipping_listings = ShippingMethodChannelListing.objects.filter(
- channel_id=order.channel_id
- )
- get_valid_shipping_methods_for_order(
- order,
- shipping_listings,
- manager.requestor_getter,
- database_connection_name=database_connection_name,
- ).get()
-
-
def _get_extra_for_order_logger(order: "Order") -> dict:
return {
"order_id": order.id,
@@ -292,7 +247,6 @@ def call_order_events(
manager: "PluginsManager",
event_names: list[str],
order: "Order",
- database_connection_name: str = settings.DATABASE_CONNECTION_DEFAULT_NAME,
webhook_event_map: dict[str, set["Webhook"]] | None = None,
):
missing_events = set(event_names).difference(ORDER_WEBHOOK_EVENT_MAP.keys())
@@ -302,44 +256,19 @@ def call_order_events(
)
if webhook_event_map is None:
- webhook_event_map = get_webhooks_for_multiple_events(
- [*event_names, *WebhookEventSyncType.ORDER_EVENTS]
- )
-
- any_event_requires_sync_webhooks = any(
- webhook_async_event_requires_sync_webhooks_to_trigger(
- event_name,
- webhook_event_map,
- possible_sync_events=WebhookEventSyncType.ORDER_EVENTS,
- )
- for event_name in event_names
- )
-
- should_trigger_sync = (
- any_event_requires_sync_webhooks
- and order.status in ORDER_EDITABLE_STATUS
- and WebhookEventAsyncType.DRAFT_ORDER_DELETED not in event_names
- )
- if should_trigger_sync:
- _trigger_order_sync_webhooks(
- manager,
- order,
- database_connection_name=database_connection_name,
- webhook_event_map=webhook_event_map,
- )
+ webhook_event_map = get_webhooks_for_multiple_events(event_names)
for event_name in event_names:
plugin_manager_method_name = ORDER_WEBHOOK_EVENT_MAP[event_name]
webhooks = webhook_event_map.get(event_name, set())
event_func = getattr(manager, plugin_manager_method_name)
- call_event_including_protected_events(event_func, order, webhooks=webhooks)
+ call_event(event_func, order, webhooks=webhooks)
def call_order_event(
manager: "PluginsManager",
event_name: str,
order: "Order",
- database_connection_name: str = settings.DATABASE_CONNECTION_DEFAULT_NAME,
webhook_event_map: dict[str, set["Webhook"]] | None = None,
):
if event_name not in ORDER_WEBHOOK_EVENT_MAP:
@@ -349,34 +278,11 @@ def call_order_event(
event_func = getattr(manager, plugin_manager_method_name)
if webhook_event_map is None:
- webhook_event_map = get_webhooks_for_multiple_events(
- [event_name, *WebhookEventSyncType.ORDER_EVENTS]
- )
+ webhook_event_map = get_webhooks_for_multiple_events([event_name])
webhooks = webhook_event_map.get(event_name, set())
- if order.status not in ORDER_EDITABLE_STATUS:
- call_event_including_protected_events(event_func, order, webhooks=webhooks)
- return
- if event_name == WebhookEventAsyncType.DRAFT_ORDER_DELETED:
- call_event_including_protected_events(event_func, order, webhooks=webhooks)
- return
-
- if not webhook_async_event_requires_sync_webhooks_to_trigger(
- event_name, webhook_event_map, WebhookEventSyncType.ORDER_EVENTS
- ):
- call_event_including_protected_events(event_func, order, webhooks=webhooks)
- return
-
- _trigger_order_sync_webhooks(
- manager,
- order,
- database_connection_name=database_connection_name,
- webhook_event_map=webhook_event_map,
- )
-
- call_event_including_protected_events(event_func, order, webhooks=webhooks)
- return
+ call_event(event_func, order, webhooks=webhooks)
def order_created(
@@ -814,7 +720,6 @@ def order_transaction_updated(
if transaction_item.authorized_value != previous_authorized_value:
webhook_events = {
WebhookEventAsyncType.ORDER_UPDATED,
- *WebhookEventSyncType.ORDER_EVENTS,
}
order_updated = True
@@ -827,7 +732,6 @@ def order_transaction_updated(
elif transaction_item.charged_value != previous_charged_value:
order_updated = True
webhook_events.add(WebhookEventAsyncType.ORDER_UPDATED)
- webhook_events = webhook_events.union(WebhookEventSyncType.ORDER_EVENTS)
if transaction_item.refunded_value > previous_refunded_value:
# order_updated False as order_refunded triggers order_updated
diff --git a/saleor/order/tests/test_order_actions.py b/saleor/order/tests/test_order_actions.py
index 0f91277699..bcfbed2236 100644
--- a/saleor/order/tests/test_order_actions.py
+++ b/saleor/order/tests/test_order_actions.py
@@ -34,11 +34,6 @@ from .. import (
)
from ..actions import (
WEBHOOK_EVENTS_FOR_FULLY_PAID,
- WEBHOOK_EVENTS_FOR_ORDER_CANCELED,
- WEBHOOK_EVENTS_FOR_ORDER_CHARGED,
- WEBHOOK_EVENTS_FOR_ORDER_CREATED,
- WEBHOOK_EVENTS_FOR_ORDER_FULFILLED,
- WEBHOOK_EVENTS_FOR_ORDER_REFUNDED,
automatically_fulfill_digital_lines,
call_order_event,
call_order_events,
@@ -358,10 +353,7 @@ def test_handle_fully_paid_order_for_draft_order(
mock_send_payment_confirmation.assert_called_once_with(order_info, manager)
-@patch(
- "saleor.order.actions.call_order_events",
- wraps=call_order_events,
-)
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -370,14 +362,17 @@ def test_handle_fully_paid_order_for_draft_order(
def test_handle_fully_paid_order_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_events,
+ mocked_cache,
setup_order_webhooks,
order_with_lines,
customer_user,
settings,
django_capture_on_commit_callbacks,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
+
plugins_manager = get_plugins_manager(False)
mocked_send_webhook_request_sync.return_value = []
(
@@ -390,6 +385,7 @@ def test_handle_fully_paid_order_triggers_webhooks(
WebhookEventAsyncType.ORDER_FULLY_PAID,
]
)
+
order = order_with_lines
order.status = OrderStatus.UNCONFIRMED
order.should_refresh_prices = True
@@ -440,12 +436,20 @@ def test_handle_fully_paid_order_triggers_webhooks(
# confirm each sync webhook was called without saving event delivery
assert mocked_send_webhook_request_sync.call_count == 2
+
assert not EventDelivery.objects.exclude(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -459,13 +463,6 @@ def test_handle_fully_paid_order_triggers_webhooks(
)
assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
- wrapped_call_order_events.assert_called_once_with(
- plugins_manager,
- [WebhookEventAsyncType.ORDER_FULLY_PAID, WebhookEventAsyncType.ORDER_UPDATED],
- order,
- webhook_event_map=webhook_event_map,
- )
-
def test_mark_as_paid_with_payment(admin_user, draft_order):
manager = get_plugins_manager(allow_replica=False)
@@ -657,10 +654,6 @@ def test_cancel_order(
)
-@patch(
- "saleor.order.actions.call_order_events",
- wraps=call_order_events,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -669,7 +662,6 @@ def test_cancel_order(
def test_cancel_order_dont_trigger_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_events,
setup_order_webhooks,
order_with_lines,
settings,
@@ -689,10 +681,6 @@ def test_cancel_order_dont_trigger_webhooks(
]
)
- webhook_event_map = get_webhooks_for_multiple_events(
- WEBHOOK_EVENTS_FOR_ORDER_CANCELED
- )
-
order = order_with_lines
order.status = OrderStatus.UNCONFIRMED
order.should_refresh_prices = True
@@ -739,16 +727,6 @@ def test_cancel_order_dont_trigger_webhooks(
)
assert not mocked_send_webhook_request_sync.called
- wrapped_call_order_events.assert_called_once_with(
- plugins_manager,
- [
- WebhookEventAsyncType.ORDER_CANCELLED,
- WebhookEventAsyncType.ORDER_UPDATED,
- ],
- order,
- webhook_event_map=webhook_event_map,
- )
-
@patch("saleor.plugins.manager.PluginsManager.order_refunded")
@patch("saleor.plugins.manager.PluginsManager.order_fully_refunded")
@@ -818,10 +796,7 @@ def test_order_refunded_by_app(
order_refunded_mock.assert_called_once_with(order, webhooks=set())
-@patch(
- "saleor.order.actions.call_order_events",
- wraps=call_order_events,
-)
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -830,14 +805,17 @@ def test_order_refunded_by_app(
def test_order_refunded_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_events,
+ mocked_cache,
setup_order_webhooks,
order_with_lines,
settings,
django_capture_on_commit_callbacks,
app,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
+
mocked_send_webhook_request_sync.return_value = []
(
tax_webhook,
@@ -871,10 +849,6 @@ def test_order_refunded_triggers_webhooks(
amount = order.total.gross.amount
plugins_manager = get_plugins_manager(allow_replica=False)
- webhook_event_map = get_webhooks_for_multiple_events(
- WEBHOOK_EVENTS_FOR_ORDER_REFUNDED
- )
-
# when
with django_capture_on_commit_callbacks(execute=True):
order_refunded(order, None, app, amount, payment, plugins_manager)
@@ -918,8 +892,15 @@ def test_order_refunded_triggers_webhooks(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -933,22 +914,8 @@ def test_order_refunded_triggers_webhooks(
)
assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
- wrapped_call_order_events.assert_called_once_with(
- plugins_manager,
- [
- WebhookEventAsyncType.ORDER_REFUNDED,
- WebhookEventAsyncType.ORDER_UPDATED,
- WebhookEventAsyncType.ORDER_FULLY_REFUNDED,
- ],
- order,
- webhook_event_map=webhook_event_map,
- )
-
-@patch(
- "saleor.order.actions.call_order_event",
- wraps=call_order_event,
-)
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -957,14 +924,17 @@ def test_order_refunded_triggers_webhooks(
def test_order_voided_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
+ mocked_cache,
setup_order_webhooks,
order_with_lines,
settings,
django_capture_on_commit_callbacks,
app,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
+
mocked_send_webhook_request_sync.return_value = []
(
tax_webhook,
@@ -1013,8 +983,15 @@ def test_order_voided_triggers_webhooks(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -1028,26 +1005,15 @@ def test_order_voided_triggers_webhooks(
)
assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
- wrapped_call_order_event.assert_called_once_with(
- plugins_manager,
- WebhookEventAsyncType.ORDER_UPDATED,
- order,
- )
-
-@patch(
- "saleor.order.actions.call_order_events",
- wraps=call_order_events,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
)
@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_order_fulfilled_dont_trigger_webhooks(
+def test_order_fulfilled_does_not_trigger_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_events,
setup_order_webhooks,
fulfilled_order,
settings,
@@ -1068,10 +1034,6 @@ def test_order_fulfilled_dont_trigger_webhooks(
)
plugins_manager = get_plugins_manager(allow_replica=False)
- webhook_event_map = get_webhooks_for_multiple_events(
- WEBHOOK_EVENTS_FOR_ORDER_FULFILLED
- )
-
# when
with django_capture_on_commit_callbacks(execute=True):
order_fulfilled(
@@ -1120,21 +1082,8 @@ def test_order_fulfilled_dont_trigger_webhooks(
)
assert not mocked_send_webhook_request_sync.called
- wrapped_call_order_events.assert_called_once_with(
- plugins_manager,
- [
- WebhookEventAsyncType.ORDER_UPDATED,
- WebhookEventAsyncType.ORDER_FULFILLED,
- ],
- fulfilled_order,
- webhook_event_map=webhook_event_map,
- )
-
-@patch(
- "saleor.order.actions.call_order_event",
- wraps=call_order_event,
-)
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -1143,15 +1092,18 @@ def test_order_fulfilled_dont_trigger_webhooks(
def test_order_awaits_fulfillment_approval_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
+ mocked_cache,
setup_order_webhooks,
fulfilled_order,
settings,
site_settings,
django_capture_on_commit_callbacks,
app,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
+
fulfillment = fulfilled_order.fulfillments.first()
fulfillment.status = FulfillmentStatus.WAITING_FOR_APPROVAL
fulfillment.save()
@@ -1206,8 +1158,15 @@ def test_order_awaits_fulfillment_approval_triggers_webhooks(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -1221,17 +1180,8 @@ def test_order_awaits_fulfillment_approval_triggers_webhooks(
)
assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
- wrapped_call_order_event.assert_called_once_with(
- plugins_manager,
- WebhookEventAsyncType.ORDER_UPDATED,
- fulfilled_order,
- )
-
-@patch(
- "saleor.order.actions.call_order_event",
- wraps=call_order_event,
-)
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -1240,14 +1190,16 @@ def test_order_awaits_fulfillment_approval_triggers_webhooks(
def test_order_authorized_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
+ mocked_cache,
setup_order_webhooks,
order_with_lines,
settings,
django_capture_on_commit_callbacks,
app,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
order = order_with_lines
order.status = OrderStatus.UNCONFIRMED
@@ -1300,8 +1252,15 @@ def test_order_authorized_triggers_webhooks(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -1315,22 +1274,8 @@ def test_order_authorized_triggers_webhooks(
)
assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
- wrapped_call_order_event.assert_called_once_with(
- plugins_manager,
- WebhookEventAsyncType.ORDER_UPDATED,
- order,
- webhook_event_map=None,
- )
-
-@patch(
- "saleor.order.actions.call_order_event",
- wraps=call_order_event,
-)
-@patch(
- "saleor.order.actions.call_order_events",
- wraps=call_order_events,
-)
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -1339,15 +1284,22 @@ def test_order_authorized_triggers_webhooks(
def test_order_charged_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_events,
- wrapped_call_order_event,
+ mocked_cache,
setup_order_webhooks,
order_with_lines,
settings,
django_capture_on_commit_callbacks,
app,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
+
+ order = order_with_lines
+
+ # Turn off auto confirm as we want to check the webhooks
+ order.channel.automatically_confirm_all_new_orders = False
+ order.channel.save(update_fields=["automatically_confirm_all_new_orders"])
order = order_with_lines
order.status = OrderStatus.UNCONFIRMED
@@ -1368,10 +1320,6 @@ def test_order_charged_triggers_webhooks(
]
)
- webhook_event_map = get_webhooks_for_multiple_events(
- WEBHOOK_EVENTS_FOR_ORDER_CHARGED
- )
-
plugins_manager = get_plugins_manager(allow_replica=False)
order_info = fetch_order_info(order)
@@ -1420,8 +1368,15 @@ def test_order_charged_triggers_webhooks(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -1435,19 +1390,6 @@ def test_order_charged_triggers_webhooks(
)
assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
- wrapped_call_order_events.assert_called_once_with(
- plugins_manager,
- [WebhookEventAsyncType.ORDER_FULLY_PAID, WebhookEventAsyncType.ORDER_UPDATED],
- order,
- webhook_event_map=webhook_event_map,
- )
- wrapped_call_order_event.assert_called_once_with(
- plugins_manager,
- WebhookEventAsyncType.ORDER_PAID,
- order,
- webhook_event_map=webhook_event_map,
- )
-
def test_fulfill_order_lines(order_with_lines):
order = order_with_lines
@@ -1704,14 +1646,7 @@ def test_order_transaction_updated_order_fully_paid(
order_updated.assert_called_once_with(order_with_lines, webhooks=set())
-@patch(
- "saleor.order.actions.call_order_event",
- wraps=call_order_event,
-)
-@patch(
- "saleor.order.actions.call_order_events",
- wraps=call_order_events,
-)
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -1720,16 +1655,18 @@ def test_order_transaction_updated_order_fully_paid(
def test_order_transaction_updated_for_charged_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_events,
- wrapped_call_order_event,
+ mocked_cache,
setup_order_webhooks,
order_with_lines,
transaction_item_generator,
django_capture_on_commit_callbacks,
app,
settings,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
+
plugins_manager = get_plugins_manager(allow_replica=False)
mocked_send_webhook_request_sync.return_value = []
(
@@ -1743,15 +1680,16 @@ def test_order_transaction_updated_for_charged_triggers_webhooks(
WebhookEventAsyncType.ORDER_UPDATED,
]
)
+
order = order_with_lines
+
+ order.channel.automatically_confirm_all_new_orders = False
+ order.channel.save(update_fields=["automatically_confirm_all_new_orders"])
+
order.status = OrderStatus.UNCONFIRMED
order.should_refresh_prices = True
order.save(update_fields=["status", "should_refresh_prices"])
- webhook_event_map = get_webhooks_for_multiple_events(
- WEBHOOK_EVENTS_FOR_ORDER_CHARGED
- )
-
order_info = fetch_order_info(order_with_lines)
transaction_item = transaction_item_generator(
order_id=order_with_lines.pk,
@@ -1814,8 +1752,15 @@ def test_order_transaction_updated_for_charged_triggers_webhooks(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -1829,24 +1774,8 @@ def test_order_transaction_updated_for_charged_triggers_webhooks(
)
assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
- wrapped_call_order_events.assert_called_once_with(
- plugins_manager,
- [WebhookEventAsyncType.ORDER_FULLY_PAID, WebhookEventAsyncType.ORDER_UPDATED],
- order,
- webhook_event_map=webhook_event_map,
- )
- wrapped_call_order_event.assert_called_once_with(
- plugins_manager,
- WebhookEventAsyncType.ORDER_PAID,
- order,
- webhook_event_map=webhook_event_map,
- )
-
-@patch(
- "saleor.order.actions.call_order_event",
- wraps=call_order_event,
-)
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -1855,15 +1784,18 @@ def test_order_transaction_updated_for_charged_triggers_webhooks(
def test_order_transaction_updated_for_authorized_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
+ mocked_cache,
setup_order_webhooks,
order_with_lines,
transaction_item_generator,
django_capture_on_commit_callbacks,
app,
settings,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
+
plugins_manager = get_plugins_manager(allow_replica=False)
mocked_send_webhook_request_sync.return_value = []
(
@@ -1878,10 +1810,6 @@ def test_order_transaction_updated_for_authorized_triggers_webhooks(
order.should_refresh_prices = True
order.save(update_fields=["status", "should_refresh_prices"])
- webhook_event_map = get_webhooks_for_multiple_events(
- [WebhookEventAsyncType.ORDER_UPDATED, *WebhookEventSyncType.ORDER_EVENTS]
- )
-
order_info = fetch_order_info(order_with_lines)
transaction_item = transaction_item_generator(
order_id=order_with_lines.pk,
@@ -1926,8 +1854,15 @@ def test_order_transaction_updated_for_authorized_triggers_webhooks(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -1941,18 +1876,8 @@ def test_order_transaction_updated_for_authorized_triggers_webhooks(
)
assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
- wrapped_call_order_event.assert_called_once_with(
- plugins_manager,
- WebhookEventAsyncType.ORDER_UPDATED,
- order,
- webhook_event_map=webhook_event_map,
- )
-
-@patch(
- "saleor.order.actions.call_order_events",
- wraps=call_order_events,
-)
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -1961,15 +1886,18 @@ def test_order_transaction_updated_for_authorized_triggers_webhooks(
def test_order_transaction_updated_for_refunded_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_events,
+ mocked_cache,
setup_order_webhooks,
order_with_lines,
transaction_item_generator,
django_capture_on_commit_callbacks,
app,
settings,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
+
plugins_manager = get_plugins_manager(allow_replica=False)
mocked_send_webhook_request_sync.return_value = []
(
@@ -1983,15 +1911,12 @@ def test_order_transaction_updated_for_refunded_triggers_webhooks(
WebhookEventAsyncType.ORDER_UPDATED,
]
)
+
order = order_with_lines
order.status = OrderStatus.UNCONFIRMED
order.should_refresh_prices = True
order.save(update_fields=["status", "should_refresh_prices"])
- webhook_event_map = get_webhooks_for_multiple_events(
- WEBHOOK_EVENTS_FOR_ORDER_REFUNDED
- )
-
order_info = fetch_order_info(order_with_lines)
transaction_item = transaction_item_generator(
order_id=order_with_lines.pk,
@@ -2055,8 +1980,15 @@ def test_order_transaction_updated_for_refunded_triggers_webhooks(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -2070,17 +2002,6 @@ def test_order_transaction_updated_for_refunded_triggers_webhooks(
)
assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
- wrapped_call_order_events.assert_called_once_with(
- plugins_manager,
- [
- WebhookEventAsyncType.ORDER_REFUNDED,
- WebhookEventAsyncType.ORDER_UPDATED,
- WebhookEventAsyncType.ORDER_FULLY_REFUNDED,
- ],
- order,
- webhook_event_map=webhook_event_map,
- )
-
@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.plugins.manager.PluginsManager.order_fully_paid")
@@ -2480,88 +2401,113 @@ def test_order_transaction_updated_order_fully_refunded_with_transaction_and_pay
order_fully_refunded.assert_called_once_with(order_with_lines, webhooks=set())
- "webhook_event",
- [
- WebhookEventAsyncType.ORDER_CREATED,
- WebhookEventAsyncType.ORDER_CONFIRMED,
- WebhookEventAsyncType.ORDER_PAID,
- WebhookEventAsyncType.ORDER_FULLY_PAID,
- WebhookEventAsyncType.ORDER_REFUNDED,
- WebhookEventAsyncType.ORDER_FULLY_REFUNDED,
- WebhookEventAsyncType.ORDER_UPDATED,
- WebhookEventAsyncType.ORDER_CANCELLED,
- WebhookEventAsyncType.ORDER_EXPIRED,
- WebhookEventAsyncType.ORDER_FULFILLED,
- WebhookEventAsyncType.ORDER_METADATA_UPDATED,
- ],
+@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
+@patch(
+ "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
)
+@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
+def test_call_order_event_incorrect_webhook_event(
+ mocked_send_webhook_request_async,
+ mocked_send_webhook_request_sync,
+ setup_order_webhooks,
+ order_with_lines,
+ django_capture_on_commit_callbacks,
+):
+ # given
+ plugins_manager = get_plugins_manager(False)
+ order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ order.should_refresh_prices = True
+ order.save(update_fields=["status", "should_refresh_prices"])
+
+ mocked_send_webhook_request_sync.return_value = []
+ setup_order_webhooks(WebhookEventAsyncType.ORDER_CREATED)
+
+ incorrect_event = WebhookEventAsyncType.CHECKOUT_UPDATED
+
+ # when
+ with django_capture_on_commit_callbacks(execute=True):
+ with pytest.raises(
+ ValueError,
+ match=f"Event {incorrect_event} not found in ORDER_WEBHOOK_EVENT_MAP.",
+ ):
+ call_order_event(
+ plugins_manager,
+ incorrect_event,
+ order,
+ )
+
+ # then
+ assert not mocked_send_webhook_request_async.called
+ assert not mocked_send_webhook_request_sync.called
+
+
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
)
@patch(
- "saleor.order.actions.call_event_including_protected_events",
+ "saleor.core.utils.events.call_event_including_protected_events",
wraps=call_event_including_protected_events,
)
@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_event_triggers_sync_webhook(
+def test_call_order_for_draft_order_deleted(
mocked_call_event_including_protected_events,
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
+ mocked_cache,
setup_order_webhooks,
order_with_lines,
settings,
django_capture_on_commit_callbacks,
- webhook_event,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
+
plugins_manager = get_plugins_manager(False)
order = order_with_lines
- order.status = OrderStatus.UNCONFIRMED
- order.should_refresh_prices = True
- order.save(update_fields=["status", "should_refresh_prices"])
+ order.status = OrderStatus.DRAFT
+ order.save(update_fields=["status"])
mocked_send_webhook_request_sync.return_value = []
(
tax_webhook,
shipping_filter_webhook,
order_webhook,
- ) = setup_order_webhooks(webhook_event)
+ ) = setup_order_webhooks(WebhookEventAsyncType.DRAFT_ORDER_DELETED)
# when
with django_capture_on_commit_callbacks(execute=True):
call_order_event(
plugins_manager,
- webhook_event,
+ WebhookEventAsyncType.DRAFT_ORDER_DELETED,
order,
)
# then
order_delivery = EventDelivery.objects.get(webhook_id=order_webhook.id)
+
mocked_send_webhook_request_async.assert_called_once_with(
kwargs={"event_delivery_id": order_delivery.id, "telemetry_context": ANY},
queue=settings.ORDER_WEBHOOK_EVENTS_CELERY_QUEUE_NAME,
MessageGroupId="example.com:saleorappadditional",
)
- # confirm each sync webhook was called without saving event delivery
- assert mocked_send_webhook_request_sync.call_count == 2
- assert not EventDelivery.objects.exclude(webhook_id=order_webhook.id).exists()
-
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
)
- tax_delivery = tax_delivery_call.args[0]
- assert tax_delivery.webhook_id == tax_webhook.id
-
filter_shipping_delivery = filter_shipping_call.args[0]
assert filter_shipping_delivery.webhook_id == shipping_filter_webhook.id
assert (
filter_shipping_delivery.event_type
== WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
+ assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
mocked_call_event_including_protected_events.assert_called_once_with(
ANY, order_with_lines, webhooks={order_webhook}
@@ -2573,7 +2519,54 @@ def test_call_order_event_triggers_sync_webhook(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
)
@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_event_incorrect_webhook_event(
+def test_call_order_event_skips_when_async_webhooks_missing(
+ mocked_send_webhook_request_async,
+ mocked_send_webhook_request_sync,
+ setup_order_webhooks,
+ order_with_lines,
+ settings,
+ django_capture_on_commit_callbacks,
+):
+ # given
+ plugins_manager = get_plugins_manager(False)
+ order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ order.should_refresh_prices = True
+ order.save(update_fields=["status", "should_refresh_prices"])
+
+ mocked_send_webhook_request_sync.return_value = []
+ (
+ tax_webhook,
+ shipping_filter_webhook,
+ checkout_webhook,
+ ) = setup_order_webhooks(WebhookEventAsyncType.CHECKOUT_CREATED)
+
+ # when
+ with django_capture_on_commit_callbacks(execute=True):
+ call_order_event(
+ plugins_manager,
+ WebhookEventAsyncType.ORDER_CREATED,
+ order,
+ )
+
+ # then
+ tax_delivery = EventDelivery.objects.filter(webhook_id=tax_webhook.id).first()
+ filter_shipping_delivery = EventDelivery.objects.filter(
+ webhook_id=shipping_filter_webhook.id,
+ event_type=WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS,
+ ).first()
+ assert not filter_shipping_delivery
+ assert not tax_delivery
+ assert not mocked_send_webhook_request_async.called
+ assert not mocked_send_webhook_request_sync.called
+
+
+@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
+@patch(
+ "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
+)
+@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
+def test_call_order_events_incorrect_webhook_event(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
setup_order_webhooks,
@@ -2596,11 +2589,14 @@ def test_call_order_event_incorrect_webhook_event(
with django_capture_on_commit_callbacks(execute=True):
with pytest.raises(
ValueError,
- match=f"Event {incorrect_event} not found in ORDER_WEBHOOK_EVENT_MAP.",
+ match=f"Events { {incorrect_event} } not found in ORDER_WEBHOOK_EVENT_MAP.",
):
- call_order_event(
+ call_order_events(
plugins_manager,
- incorrect_event,
+ [
+ incorrect_event,
+ WebhookEventAsyncType.ORDER_METADATA_UPDATED,
+ ],
order,
)
@@ -2609,895 +2605,77 @@ def test_call_order_event_incorrect_webhook_event(
assert not mocked_send_webhook_request_sync.called
- "webhook_event",
- [
- WebhookEventAsyncType.ORDER_CREATED,
- WebhookEventAsyncType.ORDER_CONFIRMED,
- WebhookEventAsyncType.ORDER_PAID,
- WebhookEventAsyncType.ORDER_FULLY_PAID,
- WebhookEventAsyncType.ORDER_REFUNDED,
- WebhookEventAsyncType.ORDER_FULLY_REFUNDED,
- WebhookEventAsyncType.ORDER_UPDATED,
- WebhookEventAsyncType.ORDER_CANCELLED,
- WebhookEventAsyncType.ORDER_EXPIRED,
- WebhookEventAsyncType.ORDER_FULFILLED,
- WebhookEventAsyncType.ORDER_METADATA_UPDATED,
- ],
-)
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
)
@patch(
- "saleor.order.actions.call_event_including_protected_events",
+ "saleor.core.utils.events.call_event_including_protected_events",
wraps=call_event_including_protected_events,
)
@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_event_missing_filter_shipping_method_webhook(
+def test_call_order_events_for_draft_order_deleted(
mocked_call_event_including_protected_events,
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
+ mocked_cache,
setup_order_webhooks,
order_with_lines,
settings,
django_capture_on_commit_callbacks,
- webhook_event,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
+
plugins_manager = get_plugins_manager(False)
order = order_with_lines
- order.status = OrderStatus.UNCONFIRMED
- order.should_refresh_prices = True
- order.save(update_fields=["status", "should_refresh_prices"])
+ order.status = OrderStatus.DRAFT
+ order.save(update_fields=["status"])
mocked_send_webhook_request_sync.return_value = []
(
tax_webhook,
shipping_filter_webhook,
order_webhook,
- ) = setup_order_webhooks(webhook_event)
-
- shipping_filter_webhook.is_active = False
- shipping_filter_webhook.save(update_fields=["is_active"])
+ ) = setup_order_webhooks(WebhookEventAsyncType.DRAFT_ORDER_DELETED)
# when
with django_capture_on_commit_callbacks(execute=True):
- call_order_event(
+ call_order_events(
plugins_manager,
- webhook_event,
+ [
+ WebhookEventAsyncType.DRAFT_ORDER_DELETED,
+ WebhookEventAsyncType.ORDER_METADATA_UPDATED,
+ ],
order,
)
# then
order_delivery = EventDelivery.objects.get(webhook_id=order_webhook.id)
+
mocked_send_webhook_request_async.assert_called_once_with(
kwargs={"event_delivery_id": order_delivery.id, "telemetry_context": ANY},
queue=settings.ORDER_WEBHOOK_EVENTS_CELERY_QUEUE_NAME,
MessageGroupId="example.com:saleorappadditional",
)
+ assert mocked_send_webhook_request_sync.call_count == 1
- mocked_send_webhook_request_sync.assert_called_once()
- assert not EventDelivery.objects.exclude(webhook_id=order_webhook.id).exists()
-
- tax_delivery = mocked_send_webhook_request_sync.mock_calls[0].args[0]
- assert tax_delivery.webhook_id == tax_webhook.id
-
- mocked_call_event_including_protected_events.assert_called_once_with(
- ANY, order_with_lines, webhooks={order_webhook}
- )
-
-
- "webhook_event",
- [
- WebhookEventAsyncType.ORDER_CREATED,
- WebhookEventAsyncType.ORDER_CONFIRMED,
- WebhookEventAsyncType.ORDER_PAID,
- WebhookEventAsyncType.ORDER_FULLY_PAID,
- WebhookEventAsyncType.ORDER_REFUNDED,
- WebhookEventAsyncType.ORDER_FULLY_REFUNDED,
- WebhookEventAsyncType.ORDER_UPDATED,
- WebhookEventAsyncType.ORDER_CANCELLED,
- WebhookEventAsyncType.ORDER_EXPIRED,
- WebhookEventAsyncType.ORDER_FULFILLED,
- WebhookEventAsyncType.ORDER_METADATA_UPDATED,
- WebhookEventAsyncType.DRAFT_ORDER_CREATED,
- WebhookEventAsyncType.DRAFT_ORDER_UPDATED,
- ],
-)
-@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-@patch(
- "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
-)
-@patch(
- "saleor.order.actions.call_event_including_protected_events",
- wraps=call_event_including_protected_events,
-)
-@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_event_skips_tax_webhook_when_prices_are_valid(
- mocked_call_event_including_protected_events,
- mocked_send_webhook_request_async,
- mocked_send_webhook_request_sync,
- setup_order_webhooks,
- order_with_lines,
- settings,
- django_capture_on_commit_callbacks,
- webhook_event,
-):
- # given
- plugins_manager = get_plugins_manager(False)
- order = order_with_lines
- order.status = OrderStatus.UNCONFIRMED
- order.save(
- update_fields=[
- "status",
- ]
- )
-
- mocked_send_webhook_request_sync.return_value = []
- (
- tax_webhook,
- shipping_filter_webhook,
- order_webhook,
- ) = setup_order_webhooks(webhook_event)
-
- # when
- with django_capture_on_commit_callbacks(execute=True):
- call_order_event(
- plugins_manager,
- webhook_event,
- order,
- )
-
- # then
- order_delivery = EventDelivery.objects.get(webhook_id=order_webhook.id)
- mocked_send_webhook_request_async.assert_called_once_with(
- kwargs={"event_delivery_id": order_delivery.id, "telemetry_context": ANY},
- queue=settings.ORDER_WEBHOOK_EVENTS_CELERY_QUEUE_NAME,
- MessageGroupId="example.com:saleorappadditional",
- )
-
- # confirm each sync webhook was called without saving event delivery
- mocked_send_webhook_request_sync.assert_called_once()
- assert not EventDelivery.objects.exclude(webhook_id=order_webhook.id).exists()
-
- filter_shipping_call = mocked_send_webhook_request_sync.mock_calls[0]
- filter_shipping_delivery = filter_shipping_call.args[0]
- assert filter_shipping_delivery.webhook_id == shipping_filter_webhook.id
- assert (
- filter_shipping_delivery.event_type
- == WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
- )
- assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
-
- mocked_call_event_including_protected_events.assert_called_once_with(
- ANY, order_with_lines, webhooks={order_webhook}
- )
-
-
- "webhook_event",
- [
- WebhookEventAsyncType.ORDER_CREATED,
- WebhookEventAsyncType.ORDER_CONFIRMED,
- WebhookEventAsyncType.ORDER_PAID,
- WebhookEventAsyncType.ORDER_FULLY_PAID,
- WebhookEventAsyncType.ORDER_REFUNDED,
- WebhookEventAsyncType.ORDER_FULLY_REFUNDED,
- WebhookEventAsyncType.ORDER_UPDATED,
- WebhookEventAsyncType.ORDER_CANCELLED,
- WebhookEventAsyncType.ORDER_EXPIRED,
- WebhookEventAsyncType.ORDER_FULFILLED,
- WebhookEventAsyncType.ORDER_METADATA_UPDATED,
- WebhookEventAsyncType.DRAFT_ORDER_CREATED,
- WebhookEventAsyncType.DRAFT_ORDER_UPDATED,
- ],
-)
-@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-@patch(
- "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
-)
-@patch(
- "saleor.order.actions.call_event_including_protected_events",
- wraps=call_event_including_protected_events,
-)
-@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_event_skips_sync_webhooks_when_order_not_editable(
- mocked_call_event_including_protected_events,
- mocked_send_webhook_request_async,
- mocked_send_webhook_request_sync,
- setup_order_webhooks,
- order_with_lines,
- settings,
- django_capture_on_commit_callbacks,
- webhook_event,
-):
- # given
- plugins_manager = get_plugins_manager(False)
- order = order_with_lines
- order.status = OrderStatus.UNFULFILLED
- order.save(
- update_fields=[
- "status",
- ]
- )
-
- mocked_send_webhook_request_sync.return_value = []
- (
- tax_webhook,
- shipping_filter_webhook,
- order_webhook,
- ) = setup_order_webhooks(webhook_event)
-
- # when
- with django_capture_on_commit_callbacks(execute=True):
- call_order_event(
- plugins_manager,
- webhook_event,
- order,
- )
-
- # then
- order_delivery = EventDelivery.objects.get(webhook_id=order_webhook.id)
- mocked_send_webhook_request_async.assert_called_once_with(
- kwargs={"event_delivery_id": order_delivery.id, "telemetry_context": ANY},
- queue=settings.ORDER_WEBHOOK_EVENTS_CELERY_QUEUE_NAME,
- MessageGroupId="example.com:saleorappadditional",
- )
- assert not mocked_send_webhook_request_sync.called
- assert not EventDelivery.objects.exclude(webhook_id=order_webhook.id).exists()
- mocked_call_event_including_protected_events.assert_called_once_with(
- ANY, order_with_lines, webhooks={order_webhook}
- )
-
-
-@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-@patch(
- "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
-)
-@patch(
- "saleor.order.actions.call_event_including_protected_events",
- wraps=call_event_including_protected_events,
-)
-@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_event_skips_sync_webhooks_when_draft_order_deleted(
- mocked_call_event_including_protected_events,
- mocked_send_webhook_request_async,
- mocked_send_webhook_request_sync,
- setup_order_webhooks,
- order_with_lines,
- settings,
- django_capture_on_commit_callbacks,
-):
- # given
- plugins_manager = get_plugins_manager(False)
- order = order_with_lines
- order.status = OrderStatus.DRAFT
- order.save(update_fields=["status"])
-
- mocked_send_webhook_request_sync.return_value = []
- (
- tax_webhook,
- shipping_filter_webhook,
- order_webhook,
- ) = setup_order_webhooks(WebhookEventAsyncType.DRAFT_ORDER_DELETED)
-
- # when
- with django_capture_on_commit_callbacks(execute=True):
- call_order_event(
- plugins_manager,
- WebhookEventAsyncType.DRAFT_ORDER_DELETED,
- order,
- )
-
- # then
- order_delivery = EventDelivery.objects.get(webhook_id=order_webhook.id)
- tax_delivery = EventDelivery.objects.filter(webhook_id=tax_webhook.id).first()
- filter_shipping_delivery = EventDelivery.objects.filter(
- webhook_id=shipping_filter_webhook.id,
- event_type=WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS,
- ).first()
- assert not filter_shipping_delivery
- assert not tax_delivery
- mocked_send_webhook_request_async.assert_called_once_with(
- kwargs={"event_delivery_id": order_delivery.id, "telemetry_context": ANY},
- queue=settings.ORDER_WEBHOOK_EVENTS_CELERY_QUEUE_NAME,
- MessageGroupId="example.com:saleorappadditional",
- )
- assert not mocked_send_webhook_request_sync.called
- mocked_call_event_including_protected_events.assert_called_once_with(
- ANY, order_with_lines, webhooks={order_webhook}
- )
-
-
-@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-@patch(
- "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
-)
-@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_event_skips_when_async_webhooks_missing(
- mocked_send_webhook_request_async,
- mocked_send_webhook_request_sync,
- setup_order_webhooks,
- order_with_lines,
- settings,
- django_capture_on_commit_callbacks,
-):
- # given
- plugins_manager = get_plugins_manager(False)
- order = order_with_lines
- order.status = OrderStatus.UNCONFIRMED
- order.should_refresh_prices = True
- order.save(update_fields=["status", "should_refresh_prices"])
-
- mocked_send_webhook_request_sync.return_value = []
- (
- tax_webhook,
- shipping_filter_webhook,
- checkout_webhook,
- ) = setup_order_webhooks(WebhookEventAsyncType.CHECKOUT_CREATED)
-
- # when
- with django_capture_on_commit_callbacks(execute=True):
- call_order_event(
- plugins_manager,
- WebhookEventAsyncType.ORDER_CREATED,
- order,
- )
-
- # then
- tax_delivery = EventDelivery.objects.filter(webhook_id=tax_webhook.id).first()
- filter_shipping_delivery = EventDelivery.objects.filter(
- webhook_id=shipping_filter_webhook.id,
- event_type=WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS,
- ).first()
- assert not filter_shipping_delivery
- assert not tax_delivery
- assert not mocked_send_webhook_request_async.called
- assert not mocked_send_webhook_request_sync.called
-
-
-@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-@patch(
- "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
-)
-@patch(
- "saleor.order.actions.call_event_including_protected_events",
- wraps=call_event_including_protected_events,
-)
-@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_event_skips_when_sync_webhooks_missing(
- mocked_call_event_including_protected_events,
- mocked_send_webhook_request_async,
- mocked_send_webhook_request_sync,
- order_with_lines,
- settings,
- webhook,
- permission_manage_orders,
- django_capture_on_commit_callbacks,
-):
- # given
- plugins_manager = get_plugins_manager(False)
- order = order_with_lines
- order.status = OrderStatus.UNCONFIRMED
- order.should_refresh_prices = True
- order.save(update_fields=["status", "should_refresh_prices"])
-
- webhook.app.permissions.add(permission_manage_orders)
-
- mocked_send_webhook_request_sync.return_value = []
-
- # when
- with django_capture_on_commit_callbacks(execute=True):
- call_order_event(
- plugins_manager,
- WebhookEventAsyncType.ORDER_CREATED,
- order,
- )
-
- # then
- order_delivery = EventDelivery.objects.get(webhook_id=webhook.id)
- mocked_send_webhook_request_async.assert_called_once_with(
- kwargs={"event_delivery_id": order_delivery.id, "telemetry_context": ANY},
- queue=settings.ORDER_WEBHOOK_EVENTS_CELERY_QUEUE_NAME,
- MessageGroupId="example.com:saleorapptest",
- )
- assert not mocked_send_webhook_request_sync.called
- mocked_call_event_including_protected_events.assert_called_once_with(
- plugins_manager.order_created, order_with_lines, webhooks={webhook}
- )
-
-
- "webhook_event",
- [
- WebhookEventAsyncType.ORDER_CREATED,
- WebhookEventAsyncType.ORDER_CONFIRMED,
- WebhookEventAsyncType.ORDER_PAID,
- WebhookEventAsyncType.ORDER_FULLY_PAID,
- WebhookEventAsyncType.ORDER_REFUNDED,
- WebhookEventAsyncType.ORDER_FULLY_REFUNDED,
- WebhookEventAsyncType.ORDER_UPDATED,
- WebhookEventAsyncType.ORDER_CANCELLED,
- WebhookEventAsyncType.ORDER_EXPIRED,
- WebhookEventAsyncType.ORDER_FULFILLED,
- ],
-)
-@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-@patch(
- "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
-)
-@patch(
- "saleor.order.actions.call_event_including_protected_events",
- wraps=call_event_including_protected_events,
-)
-@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_events_triggers_sync_webhook(
- mocked_call_event_including_protected_events,
- mocked_send_webhook_request_async,
- mocked_send_webhook_request_sync,
- setup_order_webhooks,
- order_with_lines,
- settings,
- django_capture_on_commit_callbacks,
- webhook_event,
-):
- # given
- plugins_manager = get_plugins_manager(False)
- order = order_with_lines
- order.status = OrderStatus.UNCONFIRMED
- order.should_refresh_prices = True
- order.save(update_fields=["status", "should_refresh_prices"])
-
- mocked_send_webhook_request_sync.return_value = []
- (
- tax_webhook,
- shipping_filter_webhook,
- order_webhook,
- ) = setup_order_webhooks(webhook_event)
-
- # when
- with django_capture_on_commit_callbacks(execute=True):
- call_order_events(
- plugins_manager,
- [
- webhook_event,
- WebhookEventAsyncType.ORDER_METADATA_UPDATED,
- ],
- order,
- )
-
- # then
- order_delivery = EventDelivery.objects.get(webhook_id=order_webhook.id)
- mocked_send_webhook_request_async.assert_called_once_with(
- kwargs={"event_delivery_id": order_delivery.id, "telemetry_context": ANY},
- queue=settings.ORDER_WEBHOOK_EVENTS_CELERY_QUEUE_NAME,
- MessageGroupId="example.com:saleorappadditional",
- )
- # confirm each sync webhook was called without saving event delivery
- assert mocked_send_webhook_request_sync.call_count == 2
- assert not EventDelivery.objects.exclude(webhook_id=order_webhook.id).exists()
-
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
- )
-
- tax_delivery = tax_delivery_call.args[0]
- assert tax_delivery.webhook_id == tax_webhook.id
-
- filter_shipping_delivery = filter_shipping_call.args[0]
- assert filter_shipping_delivery.webhook_id == shipping_filter_webhook.id
- assert (
- filter_shipping_delivery.event_type
- == WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
- )
- assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
-
- mocked_call_event_including_protected_events.assert_has_calls(
- [
- call(
- ANY,
- order_with_lines,
- webhooks={order_webhook},
- ),
- call(
- plugins_manager.order_metadata_updated, order_with_lines, webhooks=set()
- ),
- ]
- )
-
-
-@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-@patch(
- "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
-)
-@patch(
- "saleor.order.actions.call_event_including_protected_events",
- wraps=call_event_including_protected_events,
-)
-@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_events_incorrect_webhook_event(
- mocked_call_event_including_protected_events,
- mocked_send_webhook_request_async,
- mocked_send_webhook_request_sync,
- setup_order_webhooks,
- order_with_lines,
- django_capture_on_commit_callbacks,
-):
- # given
- plugins_manager = get_plugins_manager(False)
- order = order_with_lines
- order.status = OrderStatus.UNCONFIRMED
- order.should_refresh_prices = True
- order.save(update_fields=["status", "should_refresh_prices"])
-
- mocked_send_webhook_request_sync.return_value = []
- setup_order_webhooks(WebhookEventAsyncType.ORDER_CREATED)
-
- incorrect_event = WebhookEventAsyncType.CHECKOUT_UPDATED
-
- # when
- with django_capture_on_commit_callbacks(execute=True):
- with pytest.raises(
- ValueError,
- match=f"Events { {incorrect_event} } not found in ORDER_WEBHOOK_EVENT_MAP.",
- ):
- call_order_events(
- plugins_manager,
- [
- incorrect_event,
- WebhookEventAsyncType.ORDER_METADATA_UPDATED,
- ],
- order,
- )
-
- # then
- assert not mocked_send_webhook_request_async.called
- assert not mocked_send_webhook_request_sync.called
- assert not mocked_call_event_including_protected_events.called
-
-
- "webhook_event",
- [
- WebhookEventAsyncType.ORDER_CREATED,
- WebhookEventAsyncType.ORDER_CONFIRMED,
- WebhookEventAsyncType.ORDER_PAID,
- WebhookEventAsyncType.ORDER_FULLY_PAID,
- WebhookEventAsyncType.ORDER_REFUNDED,
- WebhookEventAsyncType.ORDER_FULLY_REFUNDED,
- WebhookEventAsyncType.ORDER_UPDATED,
- WebhookEventAsyncType.ORDER_CANCELLED,
- WebhookEventAsyncType.ORDER_EXPIRED,
- WebhookEventAsyncType.ORDER_FULFILLED,
- ],
-)
-@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-@patch(
- "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
-)
-@patch(
- "saleor.order.actions.call_event_including_protected_events",
- wraps=call_event_including_protected_events,
-)
-@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_events_missing_filter_shipping_method_webhook(
- mocked_call_event_including_protected_events,
- mocked_send_webhook_request_async,
- mocked_send_webhook_request_sync,
- setup_order_webhooks,
- order_with_lines,
- settings,
- django_capture_on_commit_callbacks,
- webhook_event,
-):
- # given
- plugins_manager = get_plugins_manager(False)
- order = order_with_lines
- order.status = OrderStatus.UNCONFIRMED
- order.should_refresh_prices = True
- order.save(update_fields=["status", "should_refresh_prices"])
-
- mocked_send_webhook_request_sync.return_value = []
- (
- tax_webhook,
- shipping_filter_webhook,
- order_webhook,
- ) = setup_order_webhooks(webhook_event)
-
- shipping_filter_webhook.is_active = False
- shipping_filter_webhook.save(update_fields=["is_active"])
-
- # when
- with django_capture_on_commit_callbacks(execute=True):
- call_order_events(
- plugins_manager,
- [
- webhook_event,
- WebhookEventAsyncType.ORDER_METADATA_UPDATED,
- ],
- order,
- )
-
- # then
- order_delivery = EventDelivery.objects.get(webhook_id=order_webhook.id)
- mocked_send_webhook_request_async.assert_called_once_with(
- kwargs={"event_delivery_id": order_delivery.id, "telemetry_context": ANY},
- queue=settings.ORDER_WEBHOOK_EVENTS_CELERY_QUEUE_NAME,
- MessageGroupId="example.com:saleorappadditional",
- )
-
- # confirm each sync webhook was called without saving event delivery
- mocked_send_webhook_request_sync.assert_called_once()
- assert not EventDelivery.objects.exclude(webhook_id=order_webhook.id).exists()
-
- tax_delivery = mocked_send_webhook_request_sync.mock_calls[0].args[0]
- assert tax_delivery.webhook_id == tax_webhook.id
-
- mocked_call_event_including_protected_events.assert_has_calls(
- [
- call(
- ANY,
- order_with_lines,
- webhooks={order_webhook},
- ),
- call(
- plugins_manager.order_metadata_updated, order_with_lines, webhooks=set()
- ),
- ]
- )
-
-
- "webhook_event",
- [
- WebhookEventAsyncType.ORDER_CREATED,
- WebhookEventAsyncType.ORDER_CONFIRMED,
- WebhookEventAsyncType.ORDER_PAID,
- WebhookEventAsyncType.ORDER_FULLY_PAID,
- WebhookEventAsyncType.ORDER_REFUNDED,
- WebhookEventAsyncType.ORDER_FULLY_REFUNDED,
- WebhookEventAsyncType.ORDER_UPDATED,
- WebhookEventAsyncType.ORDER_CANCELLED,
- WebhookEventAsyncType.ORDER_EXPIRED,
- WebhookEventAsyncType.ORDER_FULFILLED,
- WebhookEventAsyncType.DRAFT_ORDER_CREATED,
- WebhookEventAsyncType.DRAFT_ORDER_UPDATED,
- ],
-)
-@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-@patch(
- "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
-)
-@patch(
- "saleor.order.actions.call_event_including_protected_events",
- wraps=call_event_including_protected_events,
-)
-@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_events_skips_tax_webhook_when_prices_are_valid(
- mocked_call_event_including_protected_events,
- mocked_send_webhook_request_async,
- mocked_send_webhook_request_sync,
- setup_order_webhooks,
- order_with_lines,
- settings,
- django_capture_on_commit_callbacks,
- webhook_event,
-):
- # given
- plugins_manager = get_plugins_manager(False)
- order = order_with_lines
- order.status = OrderStatus.UNCONFIRMED
- order.save(
- update_fields=[
- "status",
- ]
- )
-
- mocked_send_webhook_request_sync.return_value = []
- (
- tax_webhook,
- shipping_filter_webhook,
- order_webhook,
- ) = setup_order_webhooks(webhook_event)
-
- # when
- with django_capture_on_commit_callbacks(execute=True):
- call_order_events(
- plugins_manager,
- [
- webhook_event,
- WebhookEventAsyncType.ORDER_METADATA_UPDATED,
- ],
- order,
- )
-
- # then
- order_delivery = EventDelivery.objects.get(webhook_id=order_webhook.id)
- mocked_send_webhook_request_async.assert_called_once_with(
- kwargs={"event_delivery_id": order_delivery.id, "telemetry_context": ANY},
- queue=settings.ORDER_WEBHOOK_EVENTS_CELERY_QUEUE_NAME,
- MessageGroupId="example.com:saleorappadditional",
- )
-
- # confirm each sync webhook was called without saving event delivery
- mocked_send_webhook_request_sync.assert_called_once()
- assert not EventDelivery.objects.exclude(webhook_id=order_webhook.id).exists()
-
- filter_shipping_call = mocked_send_webhook_request_sync.mock_calls[0]
- filter_shipping_delivery = filter_shipping_call.args[0]
- assert filter_shipping_delivery.webhook_id == shipping_filter_webhook.id
- assert (
- filter_shipping_delivery.event_type
- == WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
- )
- assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
-
- mocked_call_event_including_protected_events.assert_has_calls(
- [
- call(
- ANY,
- order_with_lines,
- webhooks={order_webhook},
- ),
- call(
- plugins_manager.order_metadata_updated, order_with_lines, webhooks=set()
- ),
- ]
- )
-
-
- "webhook_event",
- [
- WebhookEventAsyncType.ORDER_CREATED,
- WebhookEventAsyncType.ORDER_CONFIRMED,
- WebhookEventAsyncType.ORDER_PAID,
- WebhookEventAsyncType.ORDER_FULLY_PAID,
- WebhookEventAsyncType.ORDER_REFUNDED,
- WebhookEventAsyncType.ORDER_FULLY_REFUNDED,
- WebhookEventAsyncType.ORDER_UPDATED,
- WebhookEventAsyncType.ORDER_CANCELLED,
- WebhookEventAsyncType.ORDER_EXPIRED,
- WebhookEventAsyncType.ORDER_FULFILLED,
- WebhookEventAsyncType.DRAFT_ORDER_CREATED,
- WebhookEventAsyncType.DRAFT_ORDER_UPDATED,
- ],
-)
-@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-@patch(
- "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
-)
-@patch(
- "saleor.order.actions.call_event_including_protected_events",
- wraps=call_event_including_protected_events,
-)
-@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_events_skips_sync_webhooks_when_order_not_editable(
- mocked_call_event_including_protected_events,
- mocked_send_webhook_request_async,
- mocked_send_webhook_request_sync,
- setup_order_webhooks,
- order_with_lines,
- settings,
- django_capture_on_commit_callbacks,
- webhook_event,
-):
- # given
- plugins_manager = get_plugins_manager(False)
- order = order_with_lines
- order.status = OrderStatus.UNFULFILLED
- order.save(
- update_fields=[
- "status",
- ]
- )
-
- mocked_send_webhook_request_sync.return_value = []
- (
- tax_webhook,
- shipping_filter_webhook,
- order_webhook,
- ) = setup_order_webhooks(webhook_event)
-
- # when
- with django_capture_on_commit_callbacks(execute=True):
- call_order_events(
- plugins_manager,
- [
- webhook_event,
- WebhookEventAsyncType.ORDER_METADATA_UPDATED,
- ],
- order,
- )
-
- # then
- order_delivery = EventDelivery.objects.get(webhook_id=order_webhook.id)
- tax_delivery = EventDelivery.objects.filter(webhook_id=tax_webhook.id).first()
- filter_shipping_delivery = EventDelivery.objects.filter(
- webhook_id=shipping_filter_webhook.id,
- event_type=WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS,
- ).first()
- assert not filter_shipping_delivery
- assert not tax_delivery
- mocked_send_webhook_request_async.assert_called_once_with(
- kwargs={"event_delivery_id": order_delivery.id, "telemetry_context": ANY},
- queue=settings.ORDER_WEBHOOK_EVENTS_CELERY_QUEUE_NAME,
- MessageGroupId="example.com:saleorappadditional",
- )
- assert not mocked_send_webhook_request_sync.called
- mocked_call_event_including_protected_events.assert_has_calls(
- [
- call(
- ANY,
- order_with_lines,
- webhooks={order_webhook},
- ),
- call(
- plugins_manager.order_metadata_updated, order_with_lines, webhooks=set()
- ),
- ]
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
)
-
-@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-@patch(
- "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
-)
-@patch(
- "saleor.order.actions.call_event_including_protected_events",
- wraps=call_event_including_protected_events,
-)
-@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_events_skips_sync_webhooks_when_draft_order_deleted(
- mocked_call_event_including_protected_events,
- mocked_send_webhook_request_async,
- mocked_send_webhook_request_sync,
- setup_order_webhooks,
- order_with_lines,
- settings,
- django_capture_on_commit_callbacks,
-):
- # given
- plugins_manager = get_plugins_manager(False)
- order = order_with_lines
- order.status = OrderStatus.DRAFT
- order.save(update_fields=["status"])
-
- mocked_send_webhook_request_sync.return_value = []
- (
- tax_webhook,
- shipping_filter_webhook,
- order_webhook,
- ) = setup_order_webhooks(WebhookEventAsyncType.DRAFT_ORDER_DELETED)
-
- # when
- with django_capture_on_commit_callbacks(execute=True):
- call_order_events(
- plugins_manager,
- [
- WebhookEventAsyncType.DRAFT_ORDER_DELETED,
- WebhookEventAsyncType.ORDER_METADATA_UPDATED,
- ],
- order,
- )
-
- # then
- order_delivery = EventDelivery.objects.get(webhook_id=order_webhook.id)
- tax_delivery = EventDelivery.objects.filter(webhook_id=tax_webhook.id).first()
- filter_shipping_delivery = EventDelivery.objects.filter(
- webhook_id=shipping_filter_webhook.id,
- event_type=WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS,
- ).first()
- assert not filter_shipping_delivery
- assert not tax_delivery
- mocked_send_webhook_request_async.assert_called_once_with(
- kwargs={"event_delivery_id": order_delivery.id, "telemetry_context": ANY},
- queue=settings.ORDER_WEBHOOK_EVENTS_CELERY_QUEUE_NAME,
- MessageGroupId="example.com:saleorappadditional",
+ filter_shipping_delivery = filter_shipping_call.args[0]
+ assert filter_shipping_delivery.webhook_id == shipping_filter_webhook.id
+ assert (
+ filter_shipping_delivery.event_type
+ == WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
- assert not mocked_send_webhook_request_sync.called
+ assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
+
mocked_call_event_including_protected_events.assert_has_calls(
[
call(
@@ -3517,7 +2695,7 @@ def test_call_order_events_skips_sync_webhooks_when_draft_order_deleted(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
)
@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_events_skips_when_async_webhooks_missing(
+def test_call_order_events_when_async_webhooks_missing(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
setup_order_webhooks,
@@ -3533,11 +2711,8 @@ def test_call_order_events_skips_when_async_webhooks_missing(
order.save(update_fields=["status", "should_refresh_prices"])
mocked_send_webhook_request_sync.return_value = []
- (
- tax_webhook,
- shipping_filter_webhook,
- checkout_webhook,
- ) = setup_order_webhooks(WebhookEventAsyncType.CHECKOUT_CREATED)
+
+ setup_order_webhooks(WebhookEventAsyncType.CHECKOUT_CREATED)
# when
with django_capture_on_commit_callbacks(execute=True):
@@ -3551,105 +2726,156 @@ def test_call_order_events_skips_when_async_webhooks_missing(
)
# then
- tax_delivery = EventDelivery.objects.filter(webhook_id=tax_webhook.id).first()
- filter_shipping_delivery = EventDelivery.objects.filter(
- webhook_id=shipping_filter_webhook.id,
- event_type=WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS,
- ).first()
- assert not filter_shipping_delivery
- assert not tax_delivery
assert not mocked_send_webhook_request_async.called
assert not mocked_send_webhook_request_sync.called
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
)
-@patch(
- "saleor.order.actions.call_event_including_protected_events",
- wraps=call_event_including_protected_events,
-)
@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_call_order_events_skips_when_sync_webhooks_missing(
- mocked_call_event_including_protected_events,
+def test_unconfirmed_order_created_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
+ mocked_cache,
+ setup_order_webhooks,
order_with_lines,
+ customer_user,
settings,
- webhook,
- permission_manage_orders,
django_capture_on_commit_callbacks,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
+
plugins_manager = get_plugins_manager(False)
+ mocked_send_webhook_request_sync.return_value = []
+ (
+ tax_webhook,
+ shipping_filter_webhook,
+ additional_order_webhook,
+ ) = setup_order_webhooks(
+ [
+ WebhookEventAsyncType.ORDER_CREATED,
+ WebhookEventAsyncType.ORDER_PAID,
+ WebhookEventAsyncType.ORDER_UPDATED,
+ WebhookEventAsyncType.ORDER_FULLY_PAID,
+ WebhookEventAsyncType.ORDER_CONFIRMED,
+ ]
+ )
+
order = order_with_lines
+
+ order.channel.automatically_confirm_all_new_orders = False
+ order.channel.save(update_fields=["automatically_confirm_all_new_orders"])
+
order.status = OrderStatus.UNCONFIRMED
order.should_refresh_prices = True
- order.save(update_fields=["status", "should_refresh_prices"])
+ order.charge_status = OrderChargeStatus.FULL
+ order.user = customer_user
+ order.save(
+ update_fields=["status", "should_refresh_prices", "charge_status", "user"]
+ )
- webhook.app.permissions.add(permission_manage_orders)
+ user_number_of_orders = customer_user.number_of_orders
- mocked_send_webhook_request_sync.return_value = []
+ order_info = OrderInfo(
+ order=order,
+ customer_email=order.get_customer_email(),
+ channel=order.channel,
+ payment=order.get_last_payment(),
+ lines_data=[],
+ )
# when
with django_capture_on_commit_callbacks(execute=True):
- call_order_events(
- plugins_manager,
- [
- WebhookEventAsyncType.ORDER_CREATED,
- WebhookEventAsyncType.ORDER_METADATA_UPDATED,
- ],
- order,
- )
+ order_created(order_info, user=customer_user, app=None, manager=plugins_manager)
# then
- order_delivery = EventDelivery.objects.get(webhook_id=webhook.id)
- mocked_send_webhook_request_async.assert_called_once_with(
- kwargs={"event_delivery_id": order_delivery.id, "telemetry_context": ANY},
- queue=settings.ORDER_WEBHOOK_EVENTS_CELERY_QUEUE_NAME,
- MessageGroupId="example.com:saleorapptest",
+ # confirm that event delivery was generated for each async webhook.
+ order_updated_delivery = EventDelivery.objects.get(
+ webhook_id=additional_order_webhook.id,
+ event_type=WebhookEventAsyncType.ORDER_CREATED,
)
- assert not mocked_send_webhook_request_sync.called
- mocked_call_event_including_protected_events.assert_has_calls(
+ order_paid_delivery = EventDelivery.objects.get(
+ webhook_id=additional_order_webhook.id,
+ event_type=WebhookEventAsyncType.ORDER_PAID,
+ )
+ order_fully_paid_delivery = EventDelivery.objects.get(
+ webhook_id=additional_order_webhook.id,
+ event_type=WebhookEventAsyncType.ORDER_FULLY_PAID,
+ )
+
+ order_deliveries = [
+ order_updated_delivery,
+ order_paid_delivery,
+ order_fully_paid_delivery,
+ ]
+
+ mocked_send_webhook_request_async.assert_has_calls(
[
call(
- plugins_manager.order_created,
- order_with_lines,
- webhooks={webhook},
- ),
- call(
- plugins_manager.order_metadata_updated, order_with_lines, webhooks=set()
- ),
- ]
+ kwargs={"event_delivery_id": delivery.id, "telemetry_context": ANY},
+ queue=settings.ORDER_WEBHOOK_EVENTS_CELERY_QUEUE_NAME,
+ MessageGroupId="example.com:saleorappadditional",
+ )
+ for delivery in order_deliveries
+ ],
+ any_order=True,
)
+ # confirm each sync webhook was called without saving event delivery
+ assert mocked_send_webhook_request_sync.call_count == 2
+ assert not EventDelivery.objects.exclude(
+ webhook_id=additional_order_webhook.id
+ ).exists()
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
+ )
-@patch(
- "saleor.order.actions.call_order_event",
- wraps=call_order_event,
-)
-@patch(
- "saleor.order.actions.call_order_events",
- wraps=call_order_events,
-)
+ tax_delivery = tax_delivery_call.args[0]
+ assert tax_delivery.webhook_id == tax_webhook.id
+
+ filter_shipping_delivery = filter_shipping_call.args[0]
+ assert filter_shipping_delivery.webhook_id == shipping_filter_webhook.id
+ assert (
+ filter_shipping_delivery.event_type
+ == WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
+ )
+ assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
+ customer_user.refresh_from_db()
+ assert customer_user.number_of_orders == user_number_of_orders + 1
+
+
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
)
@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
-def test_order_created_triggers_webhooks(
+def test_confirmed_order_created_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_events,
- wrapped_call_order_event,
+ mocked_cache,
setup_order_webhooks,
order_with_lines,
customer_user,
settings,
django_capture_on_commit_callbacks,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
+
plugins_manager = get_plugins_manager(False)
mocked_send_webhook_request_sync.return_value = []
(
@@ -3666,6 +2892,10 @@ def test_order_created_triggers_webhooks(
]
)
order = order_with_lines
+
+ order.channel.automatically_confirm_all_new_orders = True
+ order.channel.save(update_fields=["automatically_confirm_all_new_orders"])
+
order.status = OrderStatus.UNCONFIRMED
order.should_refresh_prices = True
order.charge_status = OrderChargeStatus.FULL
@@ -3676,8 +2906,6 @@ def test_order_created_triggers_webhooks(
user_number_of_orders = customer_user.number_of_orders
- order.channel.automatically_confirm_all_new_orders = True
- order.channel.save(update_fields=["automatically_confirm_all_new_orders"])
order_info = OrderInfo(
order=order,
customer_email=order.get_customer_email(),
@@ -3686,10 +2914,6 @@ def test_order_created_triggers_webhooks(
lines_data=[],
)
- webhook_event_map = get_webhooks_for_multiple_events(
- WEBHOOK_EVENTS_FOR_ORDER_CREATED
- )
-
# when
with django_capture_on_commit_callbacks(execute=True):
order_created(order_info, user=customer_user, app=None, manager=plugins_manager)
@@ -3731,65 +2955,14 @@ def test_order_created_triggers_webhooks(
],
any_order=True,
)
- # confirm each sync webhook was called without saving event delivery
- assert mocked_send_webhook_request_sync.call_count == 2
- assert not EventDelivery.objects.exclude(
- webhook_id=additional_order_webhook.id
- ).exists()
-
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
- )
-
- tax_delivery = tax_delivery_call.args[0]
- assert tax_delivery.webhook_id == tax_webhook.id
-
- filter_shipping_delivery = filter_shipping_call.args[0]
- assert filter_shipping_delivery.webhook_id == shipping_filter_webhook.id
- assert (
- filter_shipping_delivery.event_type
- == WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
- )
- assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
- wrapped_call_order_events.assert_called_once_with(
- plugins_manager,
- [WebhookEventAsyncType.ORDER_FULLY_PAID, WebhookEventAsyncType.ORDER_UPDATED],
- order,
- webhook_event_map=webhook_event_map,
- )
- wrapped_call_order_event.assert_has_calls(
- [
- call(
- plugins_manager,
- WebhookEventAsyncType.ORDER_CREATED,
- order,
- webhook_event_map=webhook_event_map,
- ),
- call(
- plugins_manager,
- WebhookEventAsyncType.ORDER_CONFIRMED,
- order,
- webhook_event_map=webhook_event_map,
- ),
- call(
- plugins_manager,
- WebhookEventAsyncType.ORDER_PAID,
- order,
- webhook_event_map=webhook_event_map,
- ),
- ],
- any_order=True,
- )
+ assert not mocked_send_webhook_request_sync.called
customer_user.refresh_from_db()
assert customer_user.number_of_orders == user_number_of_orders + 1
-@patch(
- "saleor.order.actions.call_order_event",
- wraps=call_order_event,
-)
+@patch("saleor.webhook.transport.synchronous.transport.cache")
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -3798,14 +2971,17 @@ def test_order_created_triggers_webhooks(
def test_order_confirmed_triggers_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
+ mocked_cache,
setup_order_webhooks,
order_with_lines,
customer_user,
settings,
django_capture_on_commit_callbacks,
+ setup_mock_for_cache,
):
# given
+ setup_mock_for_cache({}, mocked_cache)
+
plugins_manager = get_plugins_manager(False)
mocked_send_webhook_request_sync.return_value = []
(
@@ -3848,8 +3024,15 @@ def test_order_confirmed_triggers_webhooks(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -3863,8 +3046,6 @@ def test_order_confirmed_triggers_webhooks(
)
assert filter_shipping_call.kwargs["timeout"] == settings.WEBHOOK_SYNC_TIMEOUT
- assert wrapped_call_order_event.called
-
def test_order_confirmed_charges_funds_authorized_from_gift_card(
order_with_lines,
diff --git a/saleor/order/tests/test_tasks.py b/saleor/order/tests/test_tasks.py
index 394f521212..6edb8fd49e 100644
--- a/saleor/order/tests/test_tasks.py
+++ b/saleor/order/tests/test_tasks.py
@@ -13,7 +13,6 @@ from ...discount.models import VoucherCustomer
from ...warehouse.models import Allocation
from ...webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from .. import OrderEvents, OrderStatus
-from ..actions import call_order_event, call_order_events
from ..models import Order, OrderEvent, get_order_number
from ..tasks import (
_bulk_release_voucher_usage,
@@ -381,10 +380,6 @@ def test_expire_orders_task_after(order_list, allocations, channel_USD):
).exists()
-@patch(
- "saleor.order.tasks.call_order_events",
- wraps=call_order_events,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -393,7 +388,6 @@ def test_expire_orders_task_after(order_list, allocations, channel_USD):
def test_expire_orders_task_do_not_call_sync_webhooks(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_events,
setup_order_webhooks,
order_list,
channel_USD,
@@ -459,7 +453,6 @@ def test_expire_orders_task_do_not_call_sync_webhooks(
)
assert not mocked_send_webhook_request_sync.called
- assert wrapped_call_order_events.called
@freeze_time("2020-03-18 12:00:00")
@@ -813,10 +806,6 @@ def test_bulk_release_voucher_usage_voucher_usage_mismatch(
assert code.used == 0
-@patch(
- "saleor.order.tasks.call_order_event",
- wraps=call_order_event,
-)
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
@patch(
"saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
@@ -825,7 +814,6 @@ def test_bulk_release_voucher_usage_voucher_usage_mismatch(
def test_send_order_updated(
mocked_send_webhook_request_async,
mocked_send_webhook_request_sync,
- wrapped_call_order_event,
setup_order_webhooks,
order_with_lines,
settings,
@@ -878,8 +866,15 @@ def test_send_order_updated(
webhook_id=additional_order_webhook.id
).exists()
- tax_delivery_call, filter_shipping_call = (
- mocked_send_webhook_request_sync.mock_calls
+ filter_shipping_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == shipping_filter_webhook.id
+ )
+ tax_delivery_call = next(
+ call
+ for call in mocked_send_webhook_request_sync.mock_calls
+ if call.args[0].webhook_id == tax_webhook.id
)
tax_delivery = tax_delivery_call.args[0]
@@ -891,5 +886,3 @@ def test_send_order_updated(
filter_shipping_delivery.event_type
== WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS
)
-
- assert wrapped_call_order_event.called
diff --git a/saleor/order/tests/webhooks/test_exclude_shipping.py b/saleor/order/tests/webhooks/test_exclude_shipping.py
index bdb6fcbe56..d13f416d24 100644
--- a/saleor/order/tests/webhooks/test_exclude_shipping.py
+++ b/saleor/order/tests/webhooks/test_exclude_shipping.py
@@ -10,6 +10,7 @@ from measurement.measures import Weight
from prices import Money
from promise import Promise
+from ....core.prices import quantize_price
from ....shipping.interface import ShippingMethodData
from ....webhook.const import CACHE_EXCLUDED_SHIPPING_TIME
from ....webhook.event_types import WebhookEventSyncType
@@ -139,6 +140,28 @@ def test_excluded_shipping_methods_for_order(
payload_dict = {"order": {"id": 1, "some_field": "12"}}
payload = json.dumps(payload_dict)
mocked_payload.return_value = payload
+ expected_cache_key_data = {
+ "order": {
+ "id": 1,
+ "some_field": "12",
+ "base_shipping_price_amount": str(
+ quantize_price(
+ order_with_lines.base_shipping_price.amount,
+ order_with_lines.currency,
+ )
+ ),
+ "lines_pricing": [
+ {
+ "base_unit_price_amount": str(
+ quantize_price(
+ line.base_unit_price.amount, order_with_lines.currency
+ )
+ ),
+ }
+ for line in order_with_lines.lines.all()
+ ],
+ }
+ }
# when
excluded_methods = excluded_shipping_methods_for_order(
@@ -165,7 +188,7 @@ def test_excluded_shipping_methods_for_order(
requestor=None,
)
expected_cache_key = generate_cache_key_for_webhook(
- payload_dict,
+ expected_cache_key_data,
shipping_webhook.target_url,
WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS,
shipping_app.id,
@@ -245,6 +268,28 @@ def test_multiple_app_with_excluded_shipping_methods_for_order(
payload_dict = {"order": {"id": 1, "some_field": "12"}}
payload = json.dumps(payload_dict)
mocked_payload.return_value = payload
+ expected_cache_key_data = {
+ "order": {
+ "id": 1,
+ "some_field": "12",
+ "base_shipping_price_amount": str(
+ quantize_price(
+ order_with_lines.base_shipping_price.amount,
+ order_with_lines.currency,
+ )
+ ),
+ "lines_pricing": [
+ {
+ "base_unit_price_amount": str(
+ quantize_price(
+ line.base_unit_price.amount, order_with_lines.currency
+ )
+ ),
+ }
+ for line in order_with_lines.lines.all()
+ ],
+ }
+ }
# when
excluded_methods = excluded_shipping_methods_for_order(
@@ -285,13 +330,13 @@ def test_multiple_app_with_excluded_shipping_methods_for_order(
)
assert mocked_webhook.call_count == 2
expected_cache_for_first_webhook_key = generate_cache_key_for_webhook(
- payload_dict,
+ expected_cache_key_data,
shipping_webhook.target_url,
WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS,
shipping_app.id,
)
expected_cache_for_second_webhook_key = generate_cache_key_for_webhook(
- payload_dict,
+ expected_cache_key_data,
second_shipping_webhook.target_url,
WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS,
second_shipping_app.id,
@@ -379,6 +424,28 @@ def test_multiple_webhooks_on_the_same_app_with_excluded_shipping_methods_for_or
payload_dict = {"order": {"id": 1, "some_field": "12"}}
payload = json.dumps(payload_dict)
mocked_payload.return_value = payload
+ expected_cache_key_data = {
+ "order": {
+ "id": 1,
+ "some_field": "12",
+ "base_shipping_price_amount": str(
+ quantize_price(
+ order_with_lines.base_shipping_price.amount,
+ order_with_lines.currency,
+ )
+ ),
+ "lines_pricing": [
+ {
+ "base_unit_price_amount": str(
+ quantize_price(
+ line.base_unit_price.amount, order_with_lines.currency
+ )
+ ),
+ }
+ for line in order_with_lines.lines.all()
+ ],
+ }
+ }
# when
excluded_methods = excluded_shipping_methods_for_order(
@@ -417,13 +484,13 @@ def test_multiple_webhooks_on_the_same_app_with_excluded_shipping_methods_for_or
assert mocked_webhook.call_count == 2
expected_cache_for_first_webhook_key = generate_cache_key_for_webhook(
- payload_dict,
+ expected_cache_key_data,
first_webhook.target_url,
WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS,
shipping_app.id,
)
expected_cache_for_second_webhook_key = generate_cache_key_for_webhook(
- payload_dict,
+ expected_cache_key_data,
second_webhook.target_url,
WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS,
shipping_app.id,
diff --git a/saleor/order/tests/webhooks/test_exclude_shipping_cache.py b/saleor/order/tests/webhooks/test_exclude_shipping_cache.py
index 30d473f1ae..e5045970c5 100644
--- a/saleor/order/tests/webhooks/test_exclude_shipping_cache.py
+++ b/saleor/order/tests/webhooks/test_exclude_shipping_cache.py
@@ -8,6 +8,7 @@ import pytest
from measurement.measures import Weight
from prices import Money
+from ....core.prices import quantize_price
from ....shipping.interface import ShippingMethodData
from ....webhook.event_types import WebhookEventSyncType
from ....webhook.transport.utils import generate_cache_key_for_webhook
@@ -121,6 +122,28 @@ def test_excluded_shipping_methods_for_order_stores_in_cache_when_empty(
payload_dict = {"order": {"id": 1, "some_field": "12"}}
payload = json.dumps(payload_dict)
mocked_payload.return_value = payload
+ expected_cache_key_data = {
+ "order": {
+ "id": 1,
+ "some_field": "12",
+ "base_shipping_price_amount": str(
+ quantize_price(
+ order_with_lines.base_shipping_price.amount,
+ order_with_lines.currency,
+ )
+ ),
+ "lines_pricing": [
+ {
+ "base_unit_price_amount": str(
+ quantize_price(
+ line.base_unit_price.amount, order_with_lines.currency
+ )
+ ),
+ }
+ for line in order_with_lines.lines.all()
+ ],
+ }
+ }
mocked_cache_get.return_value = None
@@ -136,7 +159,7 @@ def test_excluded_shipping_methods_for_order_stores_in_cache_when_empty(
assert mocked_webhook.called
expected_cache_key = generate_cache_key_for_webhook(
- payload_dict,
+ expected_cache_key_data,
shipping_webhook.target_url,
WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS,
shipping_app.id,
@@ -181,6 +204,28 @@ def test_excluded_shipping_methods_for_order_stores_in_cache_when_payload_is_dif
payload_dict = {"order": {"id": 1, "some_field": "12"}}
payload = json.dumps(payload_dict)
mocked_payload.return_value = payload
+ expected_cache_key_data = {
+ "order": {
+ "id": 1,
+ "some_field": "12",
+ "base_shipping_price_amount": str(
+ quantize_price(
+ order_with_lines.base_shipping_price.amount,
+ order_with_lines.currency,
+ )
+ ),
+ "lines_pricing": [
+ {
+ "base_unit_price_amount": str(
+ quantize_price(
+ line.base_unit_price.amount, order_with_lines.currency
+ )
+ ),
+ }
+ for line in order_with_lines.lines.all()
+ ],
+ }
+ }
mocked_cache_get.return_value = None
@@ -195,7 +240,7 @@ def test_excluded_shipping_methods_for_order_stores_in_cache_when_payload_is_dif
assert mocked_webhook.called
expected_cache_key = generate_cache_key_for_webhook(
- payload_dict,
+ expected_cache_key_data,
shipping_webhook.target_url,
WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS,
shipping_app.id,
diff --git a/saleor/order/webhooks/exclude_shipping.py b/saleor/order/webhooks/exclude_shipping.py
index 0e4478427a..0eeb13b53e 100644
--- a/saleor/order/webhooks/exclude_shipping.py
+++ b/saleor/order/webhooks/exclude_shipping.py
@@ -10,6 +10,7 @@ from promise import Promise
from pydantic import ValidationError
from ...core.db.connection import allow_writer
+from ...core.prices import quantize_price
from ...core.utils.json_serializer import CustomJsonEncoder
from ...shipping.interface import ShippingMethodData
from ...webhook import traced_payload_generator
@@ -31,7 +32,6 @@ from ...webhook.utils import get_webhooks_for_event
if TYPE_CHECKING:
from ...account.models import User
from ...app.models import App
- from ...checkout.models import Checkout
from ...order.models import Order
CACHE_EXCLUDED_SHIPPING_TIME = 60 * 3
@@ -83,9 +83,51 @@ def excluded_shipping_methods_for_order(
)
-def _get_cache_data_for_exclude_shipping_methods(payload: str) -> dict:
+def _get_cache_data_for_exclude_shipping_methods(order: "Order", payload: str) -> dict:
payload_dict = json.loads(payload)
- source_object = payload_dict.get("checkout", payload_dict.get("order", {}))
+ source_object = payload_dict.get("order", {})
+
+ # Drop fields that can be set by tax-app
+ order_fields_to_drop = [
+ "shipping_price_gross_amount",
+ "shipping_price_net_amount",
+ "total_net_amount",
+ "total_gross_amount",
+ "shipping_tax_rate",
+ "undiscounted_total_net_amount",
+ "undiscounted_total_gross_amount",
+ ]
+ line_fields_to_drop = [
+ "undiscounted_unit_price_gross_amount",
+ "undiscounted_unit_price_net_amount",
+ "undiscounted_total_price_net_amount",
+ "undiscounted_total_price_gross_amount",
+ "unit_price_net_amount",
+ "unit_price_gross_amount",
+ "tax_rate",
+ "total_price_net_amount",
+ "total_price_gross_amount",
+ ]
+
+ for field in order_fields_to_drop:
+ source_object.pop(field, None)
+
+ source_object["base_shipping_price_amount"] = str(
+ quantize_price(order.base_shipping_price_amount, order.currency)
+ )
+ source_object["lines_pricing"] = [
+ {
+ "base_unit_price_amount": str(
+ quantize_price(order_line.base_unit_price_amount, order.currency)
+ ),
+ }
+ for order_line in order.lines.all()
+ ]
+
+ lines_list = source_object.get("lines", [])
+ for line in lines_list:
+ for field in line_fields_to_drop:
+ line.pop(field, None)
# drop fields that change between requests but are not relevant for cache key
source_object.pop("last_change", None)
@@ -98,9 +140,7 @@ def _get_excluded_shipping_methods_or_fetch(
webhooks: QuerySet,
event_type: str,
static_payload: str,
- subscribable_object: (
- tuple[Union["Order", "Checkout"], list["ShippingMethodData"]] | None
- ),
+ subscribable_object: tuple["Order", list["ShippingMethodData"]],
allow_replica: bool,
requestor: Union["App", "User", None],
) -> Promise[dict[str, list[ExcludedShippingMethod]]]:
@@ -109,10 +149,10 @@ def _get_excluded_shipping_methods_or_fetch(
The data will be fetched from the cache. If missing it will fetch it from all
defined webhooks by calling a request to each of them one by one.
"""
- cache_data = _get_cache_data_for_exclude_shipping_methods(static_payload)
+ order, _ = subscribable_object
+ cache_data = _get_cache_data_for_exclude_shipping_methods(order, static_payload)
# Gather responses from webhooks
promised_responses = []
-
for webhook in webhooks:
promised_responses.append(
trigger_webhook_sync_promise_if_not_cached(
@@ -145,9 +185,7 @@ def _get_excluded_shipping_methods_or_fetch(
def _get_excluded_shipping_data(
event_type: str,
static_payload: str,
- subscribable_object: (
- tuple[Union["Order", "Checkout"], list["ShippingMethodData"]] | None
- ),
+ subscribable_object: tuple["Order", list["ShippingMethodData"]],
allow_replica: bool,
requestor: Union["App", "User", None],
) -> Promise[list[ExcludedShippingMethod]]:
diff --git a/saleor/plugins/tests/sample_plugins.py b/saleor/plugins/tests/sample_plugins.py
index fa5c28314c..c45ae5047c 100644
--- a/saleor/plugins/tests/sample_plugins.py
+++ b/saleor/plugins/tests/sample_plugins.py
@@ -299,11 +299,6 @@ class PluginSample(BasePlugin):
) -> Optional["TaxData"]:
return sample_tax_data(checkout_info.checkout)
- def get_taxes_for_order(
- self, order: "Order", app_identifier, previous_value
- ) -> Optional["TaxData"]:
- return sample_tax_data(order)
-
def sample_not_implemented(self, previous_value):
return NotImplemented
diff --git a/saleor/plugins/webhook/tests/test_webhook.py b/saleor/plugins/webhook/tests/test_webhook.py
index 50262679e3..376c9236b8 100644
--- a/saleor/plugins/webhook/tests/test_webhook.py
+++ b/saleor/plugins/webhook/tests/test_webhook.py
@@ -2510,7 +2510,7 @@ def test_trigger_webhook_sync_with_subscription_within_mutation_use_default_db(
)
@mock.patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
@mock.patch(
- "saleor.webhook.transport.asynchronous.transport.generate_payload_from_subscription"
+ "saleor.webhook.transport.asynchronous.transport.generate_payload_promise_from_subscription"
)
def test_trigger_webhook_async_with_subscription_use_main_db(
mocked_generate_payload,
diff --git a/saleor/webhook/transport/asynchronous/tests/test_create_deliveries_for_subscriptions.py b/saleor/webhook/transport/asynchronous/tests/test_create_deliveries_for_subscriptions.py
index bf2b8aafaf..8bcdf7abe2 100644
--- a/saleor/webhook/transport/asynchronous/tests/test_create_deliveries_for_subscriptions.py
+++ b/saleor/webhook/transport/asynchronous/tests/test_create_deliveries_for_subscriptions.py
@@ -4,7 +4,9 @@ from unittest import mock
import graphene
from django.test import override_settings
-from .....graphql.webhook.subscription_payload import generate_payload_from_subscription
+from .....graphql.webhook.subscription_payload import (
+ generate_payload_promise_from_subscription,
+)
from .....webhook.event_types import WebhookEventAsyncType
from .....webhook.models import Webhook
from ..transport import (
@@ -115,8 +117,8 @@ def test_create_deliveries_no_payload_changes_limiting_disabled(webhook_app, var
@override_settings(ENABLE_LIMITING_WEBHOOKS_FOR_IDENTICAL_PAYLOADS=True)
@mock.patch(
- "saleor.webhook.transport.asynchronous.transport.generate_payload_from_subscription",
- wraps=generate_payload_from_subscription,
+ "saleor.webhook.transport.asynchronous.transport.generate_payload_promise_from_subscription",
+ wraps=generate_payload_promise_from_subscription,
)
def test_create_deliveries_reuse_request_for_webhooks(
mock_generate_payload_from_subscription, webhook_app, variant
@@ -171,7 +173,7 @@ def test_create_deliveries_for_multiple_subscription_objects(
# when
deliveries = create_deliveries_for_multiple_subscription_objects(
event_type, product_list, webhooks
- )
+ ).get()
# then
assert len(deliveries) == len(product_list)
@@ -198,7 +200,7 @@ def test_create_deliveries_for_multiple_subscription_objects_when_more_objs_than
# when
deliveries = create_deliveries_for_multiple_subscription_objects(
event_type, product_list, webhooks
- )
+ ).get()
# then
assert len(deliveries) == len(product_list)
diff --git a/saleor/webhook/transport/asynchronous/transport.py b/saleor/webhook/transport/asynchronous/transport.py
index 17267194d7..9d716b84d7 100644
--- a/saleor/webhook/transport/asynchronous/transport.py
+++ b/saleor/webhook/transport/asynchronous/transport.py
@@ -14,6 +14,7 @@ from django.apps import apps
from django.conf import settings
from django.db import transaction
from opentelemetry.trace import StatusCode
+from promise import Promise
from ....celeryconf import app
from ....core import EventDeliveryStatus
@@ -28,7 +29,6 @@ from ....core.tracing import webhooks_otel_trace
from ....core.utils import get_domain
from ....core.utils.url import sanitize_url_for_logging
from ....graphql.webhook.subscription_payload import (
- generate_payload_from_subscription,
generate_payload_promise_from_subscription,
get_pre_save_payload_key,
initialize_request,
@@ -90,7 +90,7 @@ def create_deliveries_for_multiple_subscription_objects(
allow_replica=False,
pre_save_payloads: dict | None = None,
request_time: datetime.datetime | None = None,
-) -> list[EventDelivery]:
+) -> Promise[list[EventDelivery]]:
"""Create event deliveries with payloads based on multiple subscription objects.
Trigger webhooks for each object in `subscribable_objects`. EventDeliveries and
@@ -110,17 +110,14 @@ def create_deliveries_for_multiple_subscription_objects(
logger.info(
"Skipping subscription webhook. Event %s is not subscribable.", event_type
)
- return []
-
- event_payloads = []
- event_payloads_data = []
- event_deliveries = []
- event_deliveries_for_bulk_update = []
+ return Promise.resolve([])
is_sync_event = event_type in WebhookEventSyncType.ALL
dataloaders: dict[str, type[DataLoader]] = {}
request_map: dict[int, SaleorContext] = {}
+ promises = []
+ subscribable_object_with_webhook = []
for subscribable_object in subscribable_objects:
# Dataloaders are shared between calls to generate_payload_from_subscription to
# reuse their cache. This avoids unnecessary DB queries when different webhooks
@@ -139,13 +136,24 @@ def create_deliveries_for_multiple_subscription_objects(
)
request_map[webhook.app_id] = request
- data = generate_payload_from_subscription(
+ promise = generate_payload_promise_from_subscription(
event_type=event_type,
subscribable_object=subscribable_object,
subscription_query=webhook.subscription_query,
request=request,
)
-
+ subscribable_object_with_webhook.append((subscribable_object, webhook))
+ promises.append(promise)
+
+ def process_webhook_payloads(webhook_payloads):
+ event_payloads = []
+ event_payloads_data = []
+ event_deliveries = []
+ event_deliveries_for_bulk_update = []
+
+ for (subscribable_object, webhook), data in zip(
+ subscribable_object_with_webhook, webhook_payloads, strict=False
+ ):
if not data:
logger.info(
"No payload was generated with subscription for event: %s",
@@ -196,16 +204,18 @@ def create_deliveries_for_multiple_subscription_objects(
event_payloads_data = []
event_deliveries_for_bulk_update = []
- with allow_writer():
- # Use transaction to ensure EventPayload and EventDelivery are created together, preventing inconsistent DB state.
- with transaction.atomic():
- EventPayload.objects.bulk_create_with_payload_files(
- event_payloads, event_payloads_data
- )
- event_deliveries.extend(
- EventDelivery.objects.bulk_create(event_deliveries_for_bulk_update)
- )
- return event_deliveries
+ with allow_writer():
+ # Use transaction to ensure EventPayload and EventDelivery are created together, preventing inconsistent DB state.
+ with transaction.atomic():
+ EventPayload.objects.bulk_create_with_payload_files(
+ event_payloads, event_payloads_data
+ )
+ event_deliveries.extend(
+ EventDelivery.objects.bulk_create(event_deliveries_for_bulk_update)
+ )
+ return event_deliveries
+
+ return Promise.all(promises).then(process_webhook_payloads)
def create_deliveries_for_subscriptions(
@@ -237,7 +247,7 @@ def create_deliveries_for_subscriptions(
allow_replica,
pre_save_payloads,
request_time,
- )
+ ).get()
def create_deliveries_for_deferred_payload_subscriptions(
@@ -353,6 +363,7 @@ def trigger_webhooks_async_for_multiple_objects(
# List of deliveries with payloads.
deliveries: list[EventDelivery] = []
+ delivery_promise: Promise[list[EventDelivery]] | None = None
# List of deliveries and data to generate deferred payloads for each subscribable
# object. Note: we assume that all subscribable objects are of the same type.
@@ -400,16 +411,14 @@ def trigger_webhooks_async_for_multiple_objects(
)
)
else:
- deliveries.extend(
- create_deliveries_for_multiple_subscription_objects(
- event_type=event_type,
- subscribable_objects=subscribable_objects,
- webhooks=subscription_webhooks,
- requestor=requestor,
- allow_replica=allow_replica,
- pre_save_payloads=pre_save_payloads,
- request_time=request_time,
- )
+ delivery_promise = create_deliveries_for_multiple_subscription_objects(
+ event_type=event_type,
+ subscribable_objects=subscribable_objects,
+ webhooks=subscription_webhooks,
+ requestor=requestor,
+ allow_replica=allow_replica,
+ pre_save_payloads=pre_save_payloads,
+ request_time=request_time,
)
domain = get_domain()
@@ -439,22 +448,27 @@ def trigger_webhooks_async_for_multiple_objects(
MessageGroupId=message_group_id,
)
- for delivery in deliveries:
- message_group_id = get_sqs_message_group_id(domain, delivery.webhook.app)
- # TODO: switch to new `send_webhooks_async_for_app` task when we have
- # deduplication mechanism in place.
+ def process_deliveries(deliveries_list):
+ for delivery in deliveries_list:
+ message_group_id = get_sqs_message_group_id(domain, delivery.webhook.app)
+ # TODO: switch to new `send_webhooks_async_for_app` task when we have
+ # deduplication mechanism in place.
- send_webhook_request_async.apply_async(
- kwargs={
- "event_delivery_id": delivery.pk,
- "telemetry_context": get_task_context().to_dict(),
- },
- queue=get_queue_name_for_webhook(
- delivery.webhook,
- default_queue=queue or settings.WEBHOOK_CELERY_QUEUE_NAME,
- ),
- MessageGroupId=message_group_id, # for AWS SQS fair queues
- )
+ send_webhook_request_async.apply_async(
+ kwargs={
+ "event_delivery_id": delivery.pk,
+ "telemetry_context": get_task_context().to_dict(),
+ },
+ queue=get_queue_name_for_webhook(
+ delivery.webhook,
+ default_queue=queue or settings.WEBHOOK_CELERY_QUEUE_NAME,
+ ),
+ MessageGroupId=message_group_id, # for AWS SQS fair queues
+ )
+
+ process_deliveries(deliveries)
+ if delivery_promise:
+ delivery_promise.then(process_deliveries).get()
def trigger_webhooks_async(
diff --git a/saleor/webhook/transport/synchronous/transport.py b/saleor/webhook/transport/synchronous/transport.py
index 757c988dbb..1e59516cef 100644
--- a/saleor/webhook/transport/synchronous/transport.py
+++ b/saleor/webhook/transport/synchronous/transport.py
@@ -222,7 +222,6 @@ def trigger_webhook_sync_promise_if_not_cached(
- Send a synchronous webhook request if cache is expired.
- Fetch response from cache if it is still valid.
"""
-
cache_key = generate_cache_key_for_webhook(
cache_data, webhook.target_url, event_type, webhook.app_id
)