Skip to content

Commit

Permalink
Excessive webhooks payloads 3.1 (#9013)
Browse files Browse the repository at this point in the history
* Initial work on fixing webhooks performance

* Fix rest of tests

* Fix part of tests

* Fix webhook protocols

* Fix rest of tests
  • Loading branch information
tomaszszymanski129 committed Jan 31, 2022
1 parent 09e9f66 commit b9a2291
Show file tree
Hide file tree
Showing 10 changed files with 701 additions and 242 deletions.
4 changes: 4 additions & 0 deletions saleor/graphql/checkout/tests/test_checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,20 @@ def test_update_checkout_shipping_method_if_invalid(
"""


@mock.patch("saleor.plugins.webhook.plugin._get_webhooks_for_event")
@mock.patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
def test_checkout_create_triggers_webhooks(
mocked_webhook_trigger,
mocked_get_webhooks_for_event,
any_webhook,
user_api_client,
stock,
graphql_address_data,
settings,
channel_USD,
):
"""Create checkout object using GraphQL API."""
mocked_get_webhooks_for_event.return_value = [any_webhook]
settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
variant = stock.product_variant
variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
Expand Down
31 changes: 25 additions & 6 deletions saleor/graphql/page/tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,18 @@ def test_page_create_mutation(staff_api_client, permission_manage_pages, page_ty


@freeze_time("1914-06-28 10:50")
@mock.patch("saleor.plugins.webhook.plugin._get_webhooks_for_event")
@mock.patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
def test_page_create_trigger_page_webhook(
mocked_webhook_trigger,
mocked_get_webhooks_for_event,
any_webhook,
staff_api_client,
permission_manage_pages,
page_type,
settings,
):
mocked_get_webhooks_for_event.return_value = [any_webhook]
settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]

page_slug = "test-slug"
Expand Down Expand Up @@ -384,7 +388,7 @@ def test_page_create_trigger_page_webhook(
expected_data = generate_page_payload(page, staff_api_client.user)

mocked_webhook_trigger.assert_called_once_with(
expected_data, WebhookEventAsyncType.PAGE_CREATED
expected_data, WebhookEventAsyncType.PAGE_CREATED, [any_webhook]
)


Expand Down Expand Up @@ -1235,10 +1239,18 @@ def test_page_delete_mutation(staff_api_client, page, permission_manage_pages):


@freeze_time("1914-06-28 10:50")
@mock.patch("saleor.plugins.webhook.plugin._get_webhooks_for_event")
@mock.patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
def test_page_delete_trigger_webhook(
mocked_webhook_trigger, staff_api_client, page, permission_manage_pages, settings
mocked_webhook_trigger,
mocked_get_webhooks_for_event,
any_webhook,
staff_api_client,
page,
permission_manage_pages,
settings,
):
mocked_get_webhooks_for_event.return_value = [any_webhook]
settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
variables = {"id": graphene.Node.to_global_id("Page", page.id)}
response = staff_api_client.post_graphql(
Expand All @@ -1251,7 +1263,7 @@ def test_page_delete_trigger_webhook(
page.refresh_from_db()
expected_data = generate_page_payload(page, staff_api_client.user)
mocked_webhook_trigger.assert_called_once_with(
expected_data, WebhookEventAsyncType.PAGE_DELETED
expected_data, WebhookEventAsyncType.PAGE_DELETED, [any_webhook]
)


Expand Down Expand Up @@ -1393,14 +1405,22 @@ def test_update_page(staff_api_client, permission_manage_pages, page):
assert attr_data in expected_attributes


@mock.patch("saleor.plugins.webhook.plugin._get_webhooks_for_event")
@mock.patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
@freeze_time("2020-03-18 12:00:00")
def test_update_page_trigger_webhook(
mocked_webhook_trigger, staff_api_client, permission_manage_pages, page, settings
mocked_webhook_trigger,
mocked_get_webhooks_for_event,
any_webhook,
staff_api_client,
permission_manage_pages,
page,
settings,
):
query = UPDATE_PAGE_MUTATION

settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
mocked_get_webhooks_for_event.return_value = [any_webhook]

page_title = page.title
new_slug = "new-slug"
Expand Down Expand Up @@ -1431,8 +1451,7 @@ def test_update_page_trigger_webhook(
page.publication_date = date(2020, 3, 18)
expected_data = generate_page_payload(page, staff_api_client.user)
mocked_webhook_trigger.assert_called_once_with(
expected_data,
WebhookEventAsyncType.PAGE_UPDATED,
expected_data, WebhookEventAsyncType.PAGE_UPDATED, [any_webhook]
)


Expand Down
8 changes: 8 additions & 0 deletions saleor/graphql/product/tests/test_bulk_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,18 +509,22 @@ def test_delete_products_with_images(
mocked_recalculate_orders_task.assert_not_called()


@patch("saleor.plugins.webhook.plugin._get_webhooks_for_event")
@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
@patch("saleor.order.tasks.recalculate_orders_task.delay")
def test_delete_products_trigger_webhook(
mocked_recalculate_orders_task,
mocked_webhook_trigger,
mocked_get_webhooks_for_event,
any_webhook,
staff_api_client,
product_list,
permission_manage_products,
channel_USD,
settings,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]

query = DELETE_PRODUCTS_MUTATION
Expand All @@ -540,16 +544,20 @@ def test_delete_products_trigger_webhook(
mocked_recalculate_orders_task.assert_not_called()


@patch("saleor.plugins.webhook.plugin._get_webhooks_for_event")
@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
def test_delete_products_without_variants(
mocked_webhook_trigger,
mocked_get_webhooks_for_event,
any_webhook,
staff_api_client,
product_list,
permission_manage_products,
channel_USD,
settings,
):
# given
mocked_get_webhooks_for_event.return_value = [any_webhook]
settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]

for product in product_list:
Expand Down
6 changes: 5 additions & 1 deletion saleor/graphql/product/tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -7578,16 +7578,20 @@ def test_delete_product_with_image(


@freeze_time("1914-06-28 10:50")
@patch("saleor.plugins.webhook.plugin._get_webhooks_for_event")
@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
@patch("saleor.order.tasks.recalculate_orders_task.delay")
def test_delete_product_trigger_webhook(
mocked_recalculate_orders_task,
mocked_webhook_trigger,
mocked_get_webhooks_for_event,
any_webhook,
staff_api_client,
product,
permission_manage_products,
settings,
):
mocked_get_webhooks_for_event.return_value = [any_webhook]
settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]

query = DELETE_PRODUCT_MUTATION
Expand All @@ -7607,7 +7611,7 @@ def test_delete_product_trigger_webhook(
product, variants_id, staff_api_client.user
)
mocked_webhook_trigger.assert_called_once_with(
expected_data, WebhookEventAsyncType.PRODUCT_DELETED
expected_data, WebhookEventAsyncType.PRODUCT_DELETED, [any_webhook]
)
mocked_recalculate_orders_task.assert_not_called()

Expand Down

0 comments on commit b9a2291

Please sign in to comment.