Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add subscription_query to Webhook type and tests (#9502)
Jakub41f25ce3f895
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 356c9623ef..fe5a005da1 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -1062,9 +1062,18 @@ type Webhook implements Node {"""Return the last n elements from the list."""last: Int): EventDeliveryCountableConnection++ """Target URL for webhook."""targetUrl: String!++ """Informs if webhook is activated."""isActive: Boolean!++ """Used to create a hash signature with each payload."""secretKey: String++ """Used to define payloads for specific events."""+ subscriptionQuery: String}"""An object with an ID"""@@ -1565,11 +1574,23 @@ type EventDeliveryAttempt implements Node {"""Event delivery creation date and time."""createdAt: DateTime!++ """Task id for delivery attempt."""taskId: String++ """Delivery attempt duration."""duration: Float++ """Delivery attempt response content."""response: String++ """Response headers for delivery attempt."""responseHeaders: String++ """Delivery attempt response status code."""responseStatusCode: Int++ """Request headers for delivery attempt."""requestHeaders: String"""Event delivery status."""diff --git a/saleor/graphql/webhook/tests/test_webhook.py b/saleor/graphql/webhook/tests/test_webhook.pyindex aea8772e98..aee0fb1446 100644--- a/saleor/graphql/webhook/tests/test_webhook.py+++ b/saleor/graphql/webhook/tests/test_webhook.py@@ -168,6 +168,33 @@ def test_webhook_create_by_staff_without_permission(staff_api_client, app):assert Webhook.objects.count() == 0+def test_webhook_create_by_staff_invalid_query(+ app_api_client, permission_manage_orders+):+ query = WEBHOOK_CREATE+ variables = {+ "input": {+ "name": "New integration",+ "targetUrl": "https://www.example.com",+ "asyncEvents": [+ WebhookEventTypeAsyncEnum.ORDER_CREATED.name,+ ],+ "query": "invalid_query",+ }+ }+ response = app_api_client.post_graphql(+ query,+ variables=variables,+ permissions=[permission_manage_orders],+ check_no_permissions=False,+ )+ content = get_graphql_content(response)+ expected_errors = [{"field": "query", "message": "Subscription query is not valid"}]+ content = content["data"]["webhookCreate"]+ assert not content["webhook"]+ assert content["errors"] == expected_errors++WEBHOOK_DELETE_BY_APP = """mutation webhookDelete($id: ID!) {webhookDelete(id: $id) {@@ -322,6 +349,7 @@ QUERY_WEBHOOK = """webhook(id: $id) {idisActive+ subscriptionQueryasyncEvents {eventType}@@ -345,6 +373,27 @@ def test_query_webhook_by_staff(staff_api_client, webhook, permission_manage_appwebhook_response = content["data"]["webhook"]assert webhook_response["id"] == webhook_idassert webhook_response["isActive"] == webhook.is_active+ assert webhook_response["subscriptionQuery"] is None+ events = webhook.events.all()+ assert len(events) == 1+ assert events[0].event_type == WebhookEventTypeAsyncEnum.ORDER_CREATED.value+++def test_query_webhook_with_subscription_query_by_staff(+ staff_api_client, subscription_order_created_webhook, permission_manage_apps+):+ query = QUERY_WEBHOOK+ webhook = subscription_order_created_webhook+ webhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)+ variables = {"id": webhook_id}+ staff_api_client.user.user_permissions.add(permission_manage_apps)+ response = staff_api_client.post_graphql(query, variables=variables)++ content = get_graphql_content(response)+ webhook_response = content["data"]["webhook"]+ assert webhook_response["id"] == webhook_id+ assert webhook_response["isActive"] == webhook.is_active+ assert webhook_response["subscriptionQuery"] == webhook.subscription_queryevents = webhook.events.all()assert len(events) == 1assert events[0].event_type == WebhookEventTypeAsyncEnum.ORDER_CREATED.valuediff --git a/saleor/graphql/webhook/types.py b/saleor/graphql/webhook/types.pyindex bfc14c1a8b..9e2d55bcf2 100644--- a/saleor/graphql/webhook/types.py+++ b/saleor/graphql/webhook/types.py@@ -76,12 +76,18 @@ class EventDeliveryAttempt(ModelObjectType):created_at = graphene.DateTime(description="Event delivery creation date and time.", required=True)- task_id = graphene.String()- duration = graphene.Float()- response = graphene.String()- response_headers = graphene.String()- response_status_code = graphene.Int()- request_headers = graphene.String()+ task_id = graphene.String(description="Task id for delivery attempt.")+ duration = graphene.Float(description="Delivery attempt duration.")+ response = graphene.String(description="Delivery attempt response content.")+ response_headers = graphene.String(+ description="Response headers for delivery attempt."+ )+ response_status_code = graphene.Int(+ description="Delivery attempt response status code."+ )+ request_headers = graphene.String(+ description="Request headers for delivery attempt."+ )status = EventDeliveryStatusEnum(description="Event delivery status.", required=True)@@ -164,9 +170,16 @@ class Webhook(ModelObjectType):filter=EventDeliveryFilterInput(description="Event delivery filter options."),description="Event deliveries.",)- target_url = graphene.String(required=True)- is_active = graphene.Boolean(required=True)- secret_key = graphene.String()+ target_url = graphene.String(required=True, description="Target URL for webhook.")+ is_active = graphene.Boolean(+ required=True, description="Informs if webhook is activated."+ )+ secret_key = graphene.String(+ description="Used to create a hash signature with each payload."+ )+ subscription_query = graphene.String(+ description="Used to define payloads for specific events."+ )class Meta:description = "Webhook."diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.pyindex f9ca2d13f9..d3eefeda23 100644--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py+++ b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py@@ -483,6 +483,27 @@ def test_create_deliveries_for_subscriptions_unsubscribable_event(assert len(deliveries) == 0+@patch("saleor.graphql.webhook.subscription_payload.get_default_backend")[email protected](logger, "warning")+def test_create_deliveries_for_subscriptions_document_executed_with_error(+ mocked_task_logger,+ mocked_backend,+ product,+ subscription_product_updated_webhook,+):+ # given+ webhooks = [subscription_product_updated_webhook]+ event_type = WebhookEventAsyncType.ORDER_CREATED+ mocked_backend.document_from_string.execute.errors = "errors"+ # when+ deliveries = create_deliveries_for_subscriptions(event_type, product, webhooks)+ # then+ mocked_task_logger.assert_called_with(+ f"No payload was generated with subscription for event: {event_type}"+ )+ assert len(deliveries) == 0++TEST_VALID_SUBSCRIPTION_QUERY = """subscription{event{