Skip to content

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

Fix update mutation for webhook events

Maciej Korycinskifa66ac947941
diff --git a/saleor/graphql/webhook/mutations.py b/saleor/graphql/webhook/mutations.py
index 1df374bca4..0cc0d5ca6e 100644
--- a/saleor/graphql/webhook/mutations.py
+++ b/saleor/graphql/webhook/mutations.py
@@ -131,6 +131,19 @@ class WebhookUpdate(ModelMutation):
error_type_class = WebhookError
error_type_field = "webhook_errors"
+ @classmethod
+ def save(cls, info, instance, cleaned_input):
+ instance.save()
+ events = set(cleaned_input.get("events", []))
+ if events:
+ instance.events.all().delete()
+ models.WebhookEvent.objects.bulk_create(
+ [
+ models.WebhookEvent(webhook=instance, event_type=event)
+ for event in events
+ ]
+ )
+
class WebhookDelete(ModelDeleteMutation):
webhook = graphene.Field(Webhook)
diff --git a/tests/api/test_webhook.py b/tests/api/test_webhook.py
index 7bb2b9f10c..3e4114170d 100644
--- a/tests/api/test_webhook.py
+++ b/tests/api/test_webhook.py
@@ -284,7 +284,7 @@ def test_webhook_update_by_staff(
webhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)
variables = {
"id": webhook_id,
- "events": [WebhookEventTypeEnum.ORDER_CREATED.name],
+ "events": [WebhookEventTypeEnum.CUSTOMER_CREATED.name],
"is_active": False,
}
staff_api_client.user.user_permissions.add(permission_manage_webhooks)
@@ -294,7 +294,7 @@ def test_webhook_update_by_staff(
assert webhook.is_active is False
events = webhook.events.all()
assert len(events) == 1
- assert events[0].event_type == WebhookEventTypeEnum.ORDER_CREATED.value
+ assert events[0].event_type == WebhookEventTypeEnum.CUSTOMER_CREATED.value
def test_webhook_update_by_staff_without_permission(
@@ -304,7 +304,10 @@ def test_webhook_update_by_staff_without_permission(
webhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)
variables = {
"id": webhook_id,
- "events": [WebhookEventTypeEnum.ORDER_CREATED.name],
+ "events": [
+ WebhookEventTypeEnum.ORDER_CREATED.name,
+ WebhookEventTypeEnum.CUSTOMER_CREATED.name,
+ ],
"is_active": False,
}
response = staff_api_client.post_graphql(query, variables=variables)