Skip to content

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

Change eventType for sample webhook payload

Maciej Korycinski9fc9c9cef6bb
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index fc88435e7f..9ccdededea 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3671,7 +3671,7 @@ type Query {
webhook(id: ID!): Webhook
webhooks(sortBy: WebhookSortingInput, filter: WebhookFilterInput, before: String, after: String, first: Int, last: Int): WebhookCountableConnection
webhookEvents: [WebhookEvent]
- webhookSamplePayload(eventType: WebhookEventTypeEnum!): JSONString
+ webhookSamplePayload(eventType: WebhookSampleEventTypeEnum!): JSONString
warehouse(id: ID!): Warehouse
warehouses(filter: WarehouseFilterInput, sortBy: WarehouseSortingInput, before: String, after: String, first: Int, last: Int): WarehouseCountableConnection
translations(kind: TranslatableKinds!, before: String, after: String, first: Int, last: Int): TranslatableItemConnection
@@ -4988,6 +4988,17 @@ input WebhookFilterInput {
isActive: Boolean
}
+enum WebhookSampleEventTypeEnum {
+ ORDER_CREATED
+ ORDER_FULLY_PAID
+ ORDER_UPDATED
+ ORDER_CANCELLED
+ ORDER_FULFILLED
+ CUSTOMER_CREATED
+ PRODUCT_CREATED
+ CHECKOUT_QUANTITY_CHANGED
+}
+
enum WebhookSortField {
NAME
SERVICE_ACCOUNT
diff --git a/saleor/graphql/webhook/enums.py b/saleor/graphql/webhook/enums.py
index e0c2b99d27..3dab9b609f 100644
--- a/saleor/graphql/webhook/enums.py
+++ b/saleor/graphql/webhook/enums.py
@@ -7,3 +7,11 @@ WebhookEventTypeEnum = graphene.Enum(
"WebhookEventTypeEnum",
[(str_to_enum(e_type[0]), e_type[0]) for e_type in WebhookEventType.CHOICES],
)
+WebhookSampleEventTypeEnum = graphene.Enum(
+ "WebhookSampleEventTypeEnum",
+ [
+ (str_to_enum(e_type[0]), e_type[0])
+ for e_type in WebhookEventType.CHOICES
+ if e_type[0] != WebhookEventType.ANY
+ ],
+)
diff --git a/saleor/graphql/webhook/resolvers.py b/saleor/graphql/webhook/resolvers.py
index 1f6b27f8b0..1a32b88261 100644
--- a/saleor/graphql/webhook/resolvers.py
+++ b/saleor/graphql/webhook/resolvers.py
@@ -42,8 +42,9 @@ def resolve_webhook_events():
def resolve_sample_payload(info, event_name):
service_account = info.context.service_account
required_permission = WebhookEventType.PERMISSIONS.get(event_name)
- if service_account and service_account.has_perm(required_permission):
- return payloads.generate_sample_payload(event_name)
- if info.context.user.has_perm(required_permission):
- return payloads.generate_sample_payload(event_name)
+ if required_permission:
+ if service_account and service_account.has_perm(required_permission):
+ return payloads.generate_sample_payload(event_name)
+ if info.context.user.has_perm(required_permission):
+ return payloads.generate_sample_payload(event_name)
raise PermissionDenied()
diff --git a/saleor/graphql/webhook/schema.py b/saleor/graphql/webhook/schema.py
index 16978471c1..8df25e8030 100644
--- a/saleor/graphql/webhook/schema.py
+++ b/saleor/graphql/webhook/schema.py
@@ -3,7 +3,7 @@ import graphene
from ...core.permissions import WebhookPermissions
from ..core.fields import FilterInputConnectionField
from ..decorators import permission_required
-from .enums import WebhookEventTypeEnum
+from .enums import WebhookSampleEventTypeEnum
from .filters import WebhookFilterInput
from .mutations import WebhookCreate, WebhookDelete, WebhookUpdate
from .resolvers import (
@@ -37,7 +37,7 @@ class WebhookQueries(graphene.ObjectType):
webhook_sample_payload = graphene.Field(
graphene.JSONString,
event_type=graphene.Argument(
- WebhookEventTypeEnum,
+ WebhookSampleEventTypeEnum,
required=True,
description="Name of the requested event type.",
),
diff --git a/tests/api/test_webhook.py b/tests/api/test_webhook.py
index f9b0038da1..a23d192e9d 100644
--- a/tests/api/test_webhook.py
+++ b/tests/api/test_webhook.py
@@ -4,7 +4,10 @@ import graphene
import pytest
from saleor.account.models import ServiceAccount
-from saleor.graphql.webhook.enums import WebhookEventTypeEnum
+from saleor.graphql.webhook.enums import (
+ WebhookEventTypeEnum,
+ WebhookSampleEventTypeEnum,
+)
from saleor.webhook.event_types import WebhookEventType
from saleor.webhook.models import Webhook
@@ -609,7 +612,7 @@ def test_query_webhook_events_without_permissions(staff_api_client):
SAMPLE_PAYLOAD_QUERY = """
- query webhookSamplePayload($event_type: WebhookEventTypeEnum!){
+ query webhookSamplePayload($event_type: WebhookSampleEventTypeEnum!){
webhookSamplePayload(eventType: $event_type)
}
"""
@@ -619,15 +622,15 @@ SAMPLE_PAYLOAD_QUERY = """
@pytest.mark.parametrize(
"event_type, has_access",
[
- (WebhookEventTypeEnum.ORDER_CREATED, True),
- (WebhookEventTypeEnum.ORDER_CREATED, True),
- (WebhookEventTypeEnum.ORDER_FULLY_PAID, True),
- (WebhookEventTypeEnum.ORDER_UPDATED, True),
- (WebhookEventTypeEnum.ORDER_CANCELLED, True),
- (WebhookEventTypeEnum.ORDER_FULFILLED, True),
- (WebhookEventTypeEnum.CUSTOMER_CREATED, False),
- (WebhookEventTypeEnum.PRODUCT_CREATED, False),
- (WebhookEventTypeEnum.CHECKOUT_QUANTITY_CHANGED, False),
+ (WebhookSampleEventTypeEnum.ORDER_CREATED, True),
+ (WebhookSampleEventTypeEnum.ORDER_CREATED, True),
+ (WebhookSampleEventTypeEnum.ORDER_FULLY_PAID, True),
+ (WebhookSampleEventTypeEnum.ORDER_UPDATED, True),
+ (WebhookSampleEventTypeEnum.ORDER_CANCELLED, True),
+ (WebhookSampleEventTypeEnum.ORDER_FULFILLED, True),
+ (WebhookSampleEventTypeEnum.CUSTOMER_CREATED, False),
+ (WebhookSampleEventTypeEnum.PRODUCT_CREATED, False),
+ (WebhookSampleEventTypeEnum.CHECKOUT_QUANTITY_CHANGED, False),
],
)
def test_sample_payload_query_by_service_account(
@@ -656,15 +659,15 @@ def test_sample_payload_query_by_service_account(
@pytest.mark.parametrize(
"event_type, has_access",
[
- (WebhookEventTypeEnum.ORDER_CREATED, False),
- (WebhookEventTypeEnum.ORDER_CREATED, False),
- (WebhookEventTypeEnum.ORDER_FULLY_PAID, False),
- (WebhookEventTypeEnum.ORDER_UPDATED, False),
- (WebhookEventTypeEnum.ORDER_CANCELLED, False),
- (WebhookEventTypeEnum.ORDER_FULFILLED, False),
- (WebhookEventTypeEnum.CUSTOMER_CREATED, True),
- (WebhookEventTypeEnum.PRODUCT_CREATED, True),
- (WebhookEventTypeEnum.CHECKOUT_QUANTITY_CHANGED, True),
+ (WebhookSampleEventTypeEnum.ORDER_CREATED, False),
+ (WebhookSampleEventTypeEnum.ORDER_CREATED, False),
+ (WebhookSampleEventTypeEnum.ORDER_FULLY_PAID, False),
+ (WebhookSampleEventTypeEnum.ORDER_UPDATED, False),
+ (WebhookSampleEventTypeEnum.ORDER_CANCELLED, False),
+ (WebhookSampleEventTypeEnum.ORDER_FULFILLED, False),
+ (WebhookSampleEventTypeEnum.CUSTOMER_CREATED, True),
+ (WebhookSampleEventTypeEnum.PRODUCT_CREATED, True),
+ (WebhookSampleEventTypeEnum.CHECKOUT_QUANTITY_CHANGED, True),
],
)
def test_sample_payload_query_by_staff(