Skip to content

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

Allow service account to fetch its webhooks

Maciej Korycinskiae91fdb2ddae
diff --git a/saleor/graphql/webhook/resolvers.py b/saleor/graphql/webhook/resolvers.py
index b370122c99..46be1b97eb 100644
--- a/saleor/graphql/webhook/resolvers.py
+++ b/saleor/graphql/webhook/resolvers.py
@@ -1,5 +1,26 @@
-from ...webhook.models import Webhook
+import graphene
+from graphql_jwt.exceptions import PermissionDenied
+from ...webhook import models
+from .types import Webhook
-def resolve_webhooks():
- return Webhook.objects.all()
+
+def resolve_webhooks(info):
+ service_account = info.context.service_account
+ if service_account:
+ return models.Webhook.objects.filter(service_account=service_account)
+ user = info.context.user
+ if not user.has_perm("webhook.manage_webhooks"):
+ raise PermissionDenied()
+ return models.Webhook.objects.all()
+
+
+def resolve_webhook(info, webhook_id):
+ service_account = info.context.service_account
+ if service_account:
+ _, webhook_id = graphene.Node.from_global_id(webhook_id)
+ return service_account.webhooks.filter(id=webhook_id).first()
+ user = info.context.user
+ if user.has_perm("webhook.manage_webhooks"):
+ return graphene.Node.get_node_from_global_id(info, webhook_id, Webhook)
+ raise PermissionDenied()
diff --git a/saleor/graphql/webhook/schema.py b/saleor/graphql/webhook/schema.py
index ebc885915b..81bd6b2d6c 100644
--- a/saleor/graphql/webhook/schema.py
+++ b/saleor/graphql/webhook/schema.py
@@ -1,9 +1,8 @@
import graphene
from ..core.fields import PrefetchingConnectionField
-from ..decorators import permission_required
from .mutations import WebhookCreate, WebhookDelete, WebhookUpdate
-from .resolvers import resolve_webhooks
+from .resolvers import resolve_webhook, resolve_webhooks
from .types import Webhook
@@ -15,13 +14,11 @@ class WebhookQueries(graphene.ObjectType):
)
webhooks = PrefetchingConnectionField(Webhook, description="List of webhooks")
- @permission_required("webhook.manage_webhooks")
- def resolve_webhooks(self, _info, **_kwargs):
- return resolve_webhooks()
+ def resolve_webhooks(self, info, **_kwargs):
+ return resolve_webhooks(info)
- @permission_required("webhook.manage_webhooks")
def resolve_webhook(self, info, **data):
- return graphene.Node.get_node_from_global_id(info, data["id"], Webhook)
+ return resolve_webhook(info, data["id"])
class WebhookMutations(graphene.ObjectType):
diff --git a/tests/api/test_webhook.py b/tests/api/test_webhook.py
index 3df5d03379..0de687d350 100644
--- a/tests/api/test_webhook.py
+++ b/tests/api/test_webhook.py
@@ -342,6 +342,44 @@ def test_query_webhooks_by_staff_without_permission(staff_api_client, webhook):
assert_no_permission(response)
+def test_query_webhooks_by_service_account(service_account_api_client, webhook):
+ second_sa = ServiceAccount.objects.create(
+ name="Sample service account", is_active=True
+ )
+ second_webhook = Webhook.objects.create(
+ service_account=second_sa, target_url="http://www.example.com/test"
+ )
+ second_webhook.events.create(event_type="order_created")
+
+ query = QUERY_WEBHOOKS
+ response = service_account_api_client.post_graphql(query)
+ content = get_graphql_content(response)
+ webhooks = content["data"]["webhooks"]["edges"]
+ assert len(webhooks) == 1
+ webhook_data = webhooks[0]
+ webhook_id = webhook_data["node"]["id"]
+ _, webhook_id = graphene.Node.from_global_id(webhook_id)
+ assert webhook_id == str(webhook.id)
+
+
+def test_query_webhooks_by_service_account_without_permissions(
+ service_account_api_client
+):
+ second_sa = ServiceAccount.objects.create(
+ name="Sample service account", is_active=True
+ )
+ second_webhook = Webhook.objects.create(
+ service_account=second_sa, target_url="http://www.example.com/test"
+ )
+ second_webhook.events.create(event_type="order_created")
+
+ query = QUERY_WEBHOOKS
+ response = service_account_api_client.post_graphql(query)
+ content = get_graphql_content(response)
+ webhooks = content["data"]["webhooks"]["edges"]
+ assert len(webhooks) == 0
+
+
QUERY_WEBHOOK = """
query webhook($id: ID!){
webhook(id: $id){
@@ -378,3 +416,39 @@ def test_query_webhook_by_staff_without_permission(staff_api_client, webhook):
variables = {"id": webhook_id}
response = staff_api_client.post_graphql(query, variables=variables)
assert_no_permission(response)
+
+
+def test_query_webhook_by_service_account(service_account_api_client, webhook):
+ query = QUERY_WEBHOOK
+
+ webhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)
+ variables = {"id": webhook_id}
+ response = service_account_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
+ events = webhook.events.all()
+ assert len(events) == 1
+ assert events[0].event_type == WebhookEventTypeEnum.ORDER_CREATED.value
+
+
+def test_query_webhook_by_service_account_without_permission(
+ service_account_api_client
+):
+ second_sa = ServiceAccount.objects.create(
+ name="Sample service account", is_active=True
+ )
+ webhook = Webhook.objects.create(
+ service_account=second_sa, target_url="http://www.example.com/test"
+ )
+ webhook.events.create(event_type="order_created")
+
+ query = QUERY_WEBHOOK
+ webhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)
+ variables = {"id": webhook_id}
+ response = service_account_api_client.post_graphql(query, variables=variables)
+ content = get_graphql_content(response)
+ webhook_response = content["data"]["webhook"]
+ assert webhook_response is None