Skip to content

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

New events related to staff changes (#9949)

SzymJf00bdf0f555f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 20e8faf0c3..6fdb57388e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,7 +13,8 @@ All notable, unreleased changes to this project will be documented in this file.
#### Saleor Apps
- Add webhooks `PAGE_TYPE_CREATED`, `PAGE_TYPE_UPDATED` and `PAGE_TYPE_DELETED` - #9859 by @SzymJ
- Add webhooks `ADDRESS_CREATED`, `ADDRESS_UPDATED` and `ADDRESS_DELETED` - #9860 by @SzymJ
-- Returning errors in subscription webhooks payloads fix - #9905 by @SzymJ
+- Add webhooks `STAFF_CREATED`, `STAFF_UPDATED` and `STAFF_DELETED` - #9949 by @SzymJ
+- Fix returning errors in subscription webhooks payloads - #9905 by @SzymJ
# 3.4.0
diff --git a/saleor/graphql/account/bulk_mutations.py b/saleor/graphql/account/bulk_mutations.py
index 70bed8c3d8..5d333a8a73 100644
--- a/saleor/graphql/account/bulk_mutations.py
+++ b/saleor/graphql/account/bulk_mutations.py
@@ -71,6 +71,13 @@ class StaffBulkDelete(StaffDeleteMixin, UserBulkDelete):
)
return ValidationError(errors) if errors else {}
+ @classmethod
+ def bulk_action(cls, info, queryset):
+ instances = list(queryset)
+ queryset.delete()
+ for instance in instances:
+ info.context.plugins.staff_deleted(instance)
+
class UserBulkSetActive(BaseBulkMutation):
class Arguments:
diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.py
index 64d3906c85..0ed5a9284a 100644
--- a/saleor/graphql/account/mutations/staff.py
+++ b/saleor/graphql/account/mutations/staff.py
@@ -273,6 +273,10 @@ class StaffCreate(ModelMutation):
if groups:
instance.groups.add(*groups)
+ @classmethod
+ def post_save_action(cls, info, instance, cleaned_input):
+ info.context.plugins.staff_created(instance)
+
class StaffUpdate(StaffCreate):
class Arguments:
@@ -411,6 +415,10 @@ class StaffUpdate(StaffCreate):
match_orders_with_new_user(user)
return response
+ @classmethod
+ def post_save_action(cls, info, instance, cleaned_input):
+ info.context.plugins.staff_updated(instance)
+
class StaffDelete(StaffDeleteMixin, UserDelete):
class Meta:
@@ -435,7 +443,11 @@ class StaffDelete(StaffDeleteMixin, UserDelete):
# After the instance is deleted, set its ID to the original database's
# ID so that the success response contains ID of the deleted object.
instance.id = db_id
- return cls.success_response(instance)
+
+ response = cls.success_response(instance)
+ info.context.plugins.staff_deleted(instance)
+
+ return response
class AddressCreate(ModelMutation):
diff --git a/saleor/graphql/account/tests/test_account.py b/saleor/graphql/account/tests/test_account.py
index 5338350d08..222d60e9d8 100644
--- a/saleor/graphql/account/tests/test_account.py
+++ b/saleor/graphql/account/tests/test_account.py
@@ -3,7 +3,7 @@ import os
import re
from collections import defaultdict
from datetime import timedelta
-from unittest.mock import ANY, MagicMock, Mock, patch
+from unittest.mock import ANY, MagicMock, Mock, call, patch
from urllib.parse import urlencode
import graphene
@@ -2387,6 +2387,66 @@ def test_staff_create(
)
+@freeze_time("2018-05-31 12:00:01")
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_staff_create_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ staff_user,
+ permission_group_manage_users,
+ permission_manage_staff,
+ permission_manage_users,
+ channel_PLN,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ staff_user.user_permissions.add(permission_manage_users)
+ email = "[email protected]"
+ redirect_url = "https://www.example.com"
+ variables = {
+ "email": email,
+ "redirect_url": redirect_url,
+ "add_groups": [
+ graphene.Node.to_global_id("Group", permission_group_manage_users.pk)
+ ],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ STAFF_CREATE_MUTATION, variables, permissions=[permission_manage_staff]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["staffCreate"]
+ new_staff_user = User.objects.get(email=email)
+
+ # then
+ assert not data["errors"]
+ assert data["user"]
+ expected_call = call(
+ {
+ "id": graphene.Node.to_global_id("User", new_staff_user.id),
+ "email": email,
+ "meta": generate_meta(
+ requestor_data=generate_requestor(
+ SimpleLazyObject(lambda: staff_api_client.user)
+ )
+ ),
+ },
+ WebhookEventAsyncType.STAFF_CREATED,
+ [any_webhook],
+ new_staff_user,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+ assert expected_call in mocked_webhook_trigger.call_args_list
+
+
def test_staff_create_app_no_permission(
app_api_client,
staff_user,
@@ -2660,6 +2720,54 @@ def test_staff_update(staff_api_client, permission_manage_staff, media_root):
assert not staff_user.search_document
+@freeze_time("2018-05-31 12:00:01")
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_staff_update_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ permission_manage_staff,
+ media_root,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ staff_user = User.objects.create(email="[email protected]", is_staff=True)
+ assert not staff_user.search_document
+ id = graphene.Node.to_global_id("User", staff_user.id)
+ variables = {"id": id, "input": {"isActive": False}}
+
+ # when
+ response = staff_api_client.post_graphql(
+ STAFF_UPDATE_MUTATIONS, variables, permissions=[permission_manage_staff]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["staffUpdate"]
+
+ # then
+ assert not data["errors"]
+ assert data["user"]
+ mocked_webhook_trigger.assert_called_once_with(
+ {
+ "id": graphene.Node.to_global_id("User", staff_user.id),
+ "email": staff_user.email,
+ "meta": generate_meta(
+ requestor_data=generate_requestor(
+ SimpleLazyObject(lambda: staff_api_client.user)
+ )
+ ),
+ },
+ WebhookEventAsyncType.STAFF_UPDATED,
+ [any_webhook],
+ staff_user,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+
def test_staff_update_email(staff_api_client, permission_manage_staff, media_root):
query = STAFF_UPDATE_MUTATIONS
staff_user = User.objects.create(email="[email protected]", is_staff=True)
@@ -3144,6 +3252,51 @@ def test_staff_delete(staff_api_client, permission_manage_staff):
assert not User.objects.filter(pk=staff_user.id).exists()
+@freeze_time("2018-05-31 12:00:01")
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_staff_delete_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ permission_manage_staff,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ staff_user = User.objects.create(email="[email protected]", is_staff=True)
+ user_id = graphene.Node.to_global_id("User", staff_user.id)
+ variables = {"id": user_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ STAFF_DELETE_MUTATION, variables, permissions=[permission_manage_staff]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["staffDelete"]
+
+ # then
+ assert not data["errors"]
+ assert not User.objects.filter(pk=staff_user.id).exists()
+ mocked_webhook_trigger.assert_called_once_with(
+ {
+ "id": graphene.Node.to_global_id("User", staff_user.id),
+ "email": staff_user.email,
+ "meta": generate_meta(
+ requestor_data=generate_requestor(
+ SimpleLazyObject(lambda: staff_api_client.user)
+ )
+ ),
+ },
+ WebhookEventAsyncType.STAFF_DELETED,
+ [any_webhook],
+ staff_user,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+
@patch("saleor.account.signals.delete_versatile_image")
def test_staff_delete_with_avatar(
delete_versatile_image_mock,
diff --git a/saleor/graphql/account/tests/test_bulk_delete.py b/saleor/graphql/account/tests/test_bulk_delete.py
index 25c5cc30ef..f2fc6e725e 100644
--- a/saleor/graphql/account/tests/test_bulk_delete.py
+++ b/saleor/graphql/account/tests/test_bulk_delete.py
@@ -142,6 +142,40 @@ def test_delete_staff_members(
assert User.objects.filter(id__in=[user.id for user in users]).count() == len(users)
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_delete_staff_members_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ user_list,
+ permission_manage_staff,
+ superuser,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ *users, staff_1, staff_2 = user_list
+ users.append(superuser)
+
+ variables = {
+ "ids": [
+ graphene.Node.to_global_id("User", user.id) for user in [staff_1, staff_2]
+ ]
+ }
+ response = staff_api_client.post_graphql(
+ STAFF_BULK_DELETE_MUTATION, variables, permissions=[permission_manage_staff]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["staffBulkDelete"]
+ assert data["count"] == 2
+ assert not data["errors"]
+ assert mocked_webhook_trigger.call_count == 2
+
+
def test_delete_staff_members_app_no_permission(
app_api_client, user_list, permission_manage_staff, superuser
):
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 5e3845bf1b..2eef6722f3 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1523,6 +1523,11 @@ enum WebhookEventTypeEnum {
"""A shipping zone is deleted."""
SHIPPING_ZONE_DELETED
+
+ """A staff user is deleted"""
+ STAFF_CREATED
+ STAFF_UPDATED
+ STAFF_DELETED
TRANSACTION_ACTION_REQUEST
TRANSLATION_CREATED
TRANSLATION_UPDATED
@@ -1792,6 +1797,11 @@ enum WebhookEventTypeAsyncEnum {
"""A shipping zone is deleted."""
SHIPPING_ZONE_DELETED
+
+ """A staff user is deleted"""
+ STAFF_CREATED
+ STAFF_UPDATED
+ STAFF_DELETED
TRANSACTION_ACTION_REQUEST
TRANSLATION_CREATED
TRANSLATION_UPDATED
@@ -2350,6 +2360,9 @@ enum WebhookSampleEventTypeEnum {
SHIPPING_ZONE_CREATED
SHIPPING_ZONE_UPDATED
SHIPPING_ZONE_DELETED
+ STAFF_CREATED
+ STAFF_UPDATED
+ STAFF_DELETED
TRANSACTION_ACTION_REQUEST
TRANSLATION_CREATED
TRANSLATION_UPDATED
@@ -22452,6 +22465,75 @@ type ShippingZoneDeleted implements Event {
): ShippingZone
}
+type StaffCreated implements Event {
+ """Time of the event."""
+ issuedAt: DateTime
+
+ """Saleor version that triggered the event."""
+ version: String
+
+ """The user or application that triggered the event."""
+ issuingPrincipal: IssuingPrincipal
+
+ """The application receiving the webhook."""
+ recipient: App
+
+ """
+ The user the event relates to.
+
+ Added in Saleor 3.2.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ user: User
+}
+
+type StaffUpdated implements Event {
+ """Time of the event."""
+ issuedAt: DateTime
+
+ """Saleor version that triggered the event."""
+ version: String
+
+ """The user or application that triggered the event."""
+ issuingPrincipal: IssuingPrincipal
+
+ """The application receiving the webhook."""
+ recipient: App
+
+ """
+ The user the event relates to.
+
+ Added in Saleor 3.2.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ user: User
+}
+
+type StaffDeleted implements Event {
+ """Time of the event."""
+ issuedAt: DateTime
+
+ """Saleor version that triggered the event."""
+ version: String
+
+ """The user or application that triggered the event."""
+ issuingPrincipal: IssuingPrincipal
+
+ """The application receiving the webhook."""
+ recipient: App
+
+ """
+ The user the event relates to.
+
+ Added in Saleor 3.2.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ user: User
+}
+
type TransactionActionRequest implements Event {
"""Time of the event."""
issuedAt: DateTime
diff --git a/saleor/graphql/tests/queries/fragments.py b/saleor/graphql/tests/queries/fragments.py
index daa60209d4..fb7c2da497 100644
--- a/saleor/graphql/tests/queries/fragments.py
+++ b/saleor/graphql/tests/queries/fragments.py
@@ -215,10 +215,10 @@ fragment AddressDetails on Address {
"""
-USER_DETAILS = (
+CUSTOMER_DETAILS = (
ADDRESS_DETAILS
+ """
-fragment UserDetails on User {
+fragment CustomerDetails on User {
email
firstName
lastName
@@ -238,6 +238,16 @@ fragment UserDetails on User {
"""
)
+STAFF_DETAILS = """
+fragment StaffDetails on User {
+ email
+ firstName
+ lastName
+ isStaff
+ isActive
+}
+"""
+
BASIC_PRODUCT_FIELDS = """
fragment BasicProductFields on Product {
diff --git a/saleor/graphql/webhook/enums.py b/saleor/graphql/webhook/enums.py
index a5cdd70070..c299bfd2dc 100644
--- a/saleor/graphql/webhook/enums.py
+++ b/saleor/graphql/webhook/enums.py
@@ -84,6 +84,9 @@ WEBHOOK_EVENT_DESCRIPTION = {
WebhookEventAsyncType.SHIPPING_ZONE_CREATED: "A new shipping zone is created.",
WebhookEventAsyncType.SHIPPING_ZONE_UPDATED: "A shipping zone is updated.",
WebhookEventAsyncType.SHIPPING_ZONE_DELETED: "A shipping zone is deleted.",
+ WebhookEventAsyncType.STAFF_CREATED: "A new staff user is created",
+ WebhookEventAsyncType.STAFF_CREATED: "A staff user is updated",
+ WebhookEventAsyncType.STAFF_CREATED: "A staff user is deleted",
WebhookEventAsyncType.WAREHOUSE_CREATED: "A new warehouse created.",
WebhookEventAsyncType.WAREHOUSE_UPDATED: "A warehouse is updated.",
WebhookEventAsyncType.WAREHOUSE_DELETED: "A warehouse is deleted.",
diff --git a/saleor/graphql/webhook/subscription_types.py b/saleor/graphql/webhook/subscription_types.py
index adf195ee93..fb3284590a 100644
--- a/saleor/graphql/webhook/subscription_types.py
+++ b/saleor/graphql/webhook/subscription_types.py
@@ -142,6 +142,9 @@ class Event(graphene.Interface):
WebhookEventAsyncType.SHIPPING_ZONE_CREATED: ShippingZoneCreated,
WebhookEventAsyncType.SHIPPING_ZONE_UPDATED: ShippingZoneUpdated,
WebhookEventAsyncType.SHIPPING_ZONE_DELETED: ShippingZoneDeleted,
+ WebhookEventAsyncType.STAFF_CREATED: StaffCreated,
+ WebhookEventAsyncType.STAFF_UPDATED: StaffUpdated,
+ WebhookEventAsyncType.STAFF_DELETED: StaffDeleted,
WebhookEventAsyncType.TRANSACTION_ACTION_REQUEST: TransactionActionRequest,
WebhookEventAsyncType.TRANSLATION_CREATED: TranslationCreated,
WebhookEventAsyncType.TRANSLATION_UPDATED: TranslationUpdated,
@@ -870,6 +873,21 @@ class ShippingZoneDeleted(ObjectType, ShippingZoneBase):
interfaces = (Event,)
+class StaffCreated(ObjectType, UserBase):
+ class Meta:
+ interfaces = (Event,)
+
+
+class StaffUpdated(ObjectType, UserBase):
+ class Meta:
+ interfaces = (Event,)
+
+
+class StaffDeleted(ObjectType, UserBase):
+ class Meta:
+ interfaces = (Event,)
+
+
class TransactionAction(ObjectType, AbstractType):
action_type = graphene.Field(
TransactionActionEnum,
@@ -1093,6 +1111,9 @@ SUBSCRIPTION_EVENTS_TYPES = [
ShippingZoneCreated,
ShippingZoneUpdated,
ShippingZoneDeleted,
+ StaffCreated,
+ StaffUpdated,
+ StaffDeleted,
TransactionActionRequest,
TranslationCreated,
TranslationUpdated,
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index 2e5f136b33..746e90b0ae 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -759,6 +759,24 @@ class BasePlugin:
# storefront should append info to the price about "including/excluding X% VAT".
show_taxes_on_storefront: Callable[[bool], bool]
+ # Trigger when staff user is created.
+ #
+ # Overwrite this method if you need to trigger specific logic after a staff user is
+ # created.
+ staff_created: Callable[["User", Any], Any]
+
+ # Trigger when staff user is updated.
+ #
+ # Overwrite this method if you need to trigger specific logic after a staff user is
+ # updated.
+ staff_updated: Callable[["User", Any], Any]
+
+ # Trigger when staff user is deleted.
+ #
+ # Overwrite this method if you need to trigger specific logic after a staff user is
+ # deleted.
+ staff_deleted: Callable[["User", Any], Any]
+
# Trigger when tracking number is updated.
tracking_number_updated: Callable[["Fulfillment", Any], Any]
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index fb68ceb649..e68b8d2b05 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -983,6 +983,18 @@ class PluginsManager(PaymentInterface):
"shipping_zone_deleted", default_value, shipping_zone
)
+ def staff_created(self, staff_user: "User"):
+ default_value = None
+ return self.__run_method_on_plugins("staff_created", default_value, staff_user)
+
+ def staff_updated(self, staff_user: "User"):
+ default_value = None
+ return self.__run_method_on_plugins("staff_updated", default_value, staff_user)
+
+ def staff_deleted(self, staff_user: "User"):
+ default_value = None
+ return self.__run_method_on_plugins("staff_deleted", default_value, staff_user)
+
def warehouse_created(self, warehouse: "Warehouse"):
default_value = None
return self.__run_method_on_plugins(
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index f07d88397f..f5781d3a3d 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -849,6 +849,32 @@ class WebhookPlugin(BasePlugin):
WebhookEventAsyncType.SHIPPING_ZONE_DELETED, shipping_zone
)
+ def _trigger_staff_event(self, event_type, staff_user):
+ if webhooks := get_webhooks_for_event(event_type):
+ payload = {
+ "id": graphene.Node.to_global_id("User", staff_user.id),
+ "email": staff_user.email,
+ "meta": self._generate_meta(),
+ }
+ trigger_webhooks_async(
+ payload, event_type, webhooks, staff_user, self.requestor
+ )
+
+ def staff_created(self, staff_user: "User", previous_value: None) -> None:
+ if not self.active:
+ return previous_value
+ self._trigger_staff_event(WebhookEventAsyncType.STAFF_CREATED, staff_user)
+
+ def staff_updated(self, staff_user: "User", previous_value: None) -> None:
+ if not self.active:
+ return previous_value
+ self._trigger_staff_event(WebhookEventAsyncType.STAFF_UPDATED, staff_user)
+
+ def staff_deleted(self, staff_user: "User", previous_value: None) -> None:
+ if not self.active:
+ return previous_value
+ self._trigger_staff_event(WebhookEventAsyncType.STAFF_DELETED, staff_user)
+
def translation_created(self, translation: "Translation", previous_value: Any):
if not self.active:
return previous_value
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
index 58992687b7..d596ba0e3b 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
@@ -528,6 +528,29 @@ def subscription_product_created_multiple_events_webhook(subscription_webhook):
)
+def subscription_staff_created_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.STAFF_CREATED, WebhookEventAsyncType.STAFF_CREATED
+ )
+
+
+def subscription_staff_updated_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.STAFF_UPDATED,
+ WebhookEventAsyncType.STAFF_UPDATED,
+ )
+
+
+def subscription_staff_deleted_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.STAFF_DELETED,
+ WebhookEventAsyncType.STAFF_DELETED,
+ )
+
+
@pytest.fixture
def subscription_translation_created_webhook(subscription_webhook):
return subscription_webhook(
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py b/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
index 7c7adc0f5b..707687af68 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
@@ -94,7 +94,7 @@ def generate_customer_payload(customer):
"email": customer.email,
"firstName": customer.first_name,
"lastName": customer.last_name,
- "isStaff": customer.is_staff,
+ "isStaff": False,
"isActive": customer.is_active,
"addresses": [
{"id": graphene.Node.to_global_id("Address", address.pk)}
@@ -111,6 +111,18 @@ def generate_customer_payload(customer):
}
+def generate_staff_payload(staff_user):
+ return {
+ "user": {
+ "email": staff_user.email,
+ "firstName": staff_user.first_name,
+ "lastName": staff_user.last_name,
+ "isStaff": True,
+ "isActive": staff_user.is_active,
+ }
+ }
+
+
def generate_collection_payload(collection):
collection_id = graphene.Node.to_global_id("Collection", collection.pk)
products = sorted(collection.products.all(), key=lambda product: product.name)
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
index 4bfc7f3501..1e2c186797 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
@@ -450,6 +450,53 @@ SHIPPING_ZONE_DELETED = """
}
"""
+STAFF_CREATED = (
+ fragments.STAFF_DETAILS
+ + """
+ subscription{
+ event{
+ ...on StaffCreated{
+ user{
+ ...StaffDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+STAFF_UPDATED = (
+ fragments.STAFF_DETAILS
+ + """
+ subscription{
+ event{
+ ...on StaffUpdated{
+ user{
+ ...StaffDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+
+STAFF_DELETED = (
+ fragments.STAFF_DETAILS
+ + """
+ subscription{
+ event{
+ ...on StaffDeleted{
+ user{
+ ...StaffDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+
PRODUCT_UPDATED = """
subscription{
event{
@@ -782,13 +829,13 @@ FULFILLMENT_CANCELED = (
)
CUSTOMER_CREATED = (
- fragments.USER_DETAILS
+ fragments.CUSTOMER_DETAILS
+ """
subscription{
event{
...on CustomerCreated{
user{
- ...UserDetails
+ ...CustomerDetails
}
}
}
@@ -797,13 +844,13 @@ CUSTOMER_CREATED = (
)
CUSTOMER_UPDATED = (
- fragments.USER_DETAILS
+ fragments.CUSTOMER_DETAILS
+ """
subscription{
event{
...on CustomerUpdated{
user{
- ...UserDetails
+ ...CustomerDetails
}
}
}
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.py
index 21c1be5bb1..b5348a3963 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
@@ -30,6 +30,7 @@ from .payloads import (
generate_page_type_payload,
generate_sale_payload,
generate_shipping_method_payload,
+ generate_staff_payload,
generate_voucher_created_payload_with_meta,
generate_voucher_payload,
generate_warehouse_payload,
@@ -693,6 +694,56 @@ def test_shipping_zone_deleted(
assert deliveries[0].webhook == webhooks[0]
+def test_staff_created(staff_user, subscription_staff_created_webhook):
+ # given
+ webhooks = [subscription_staff_created_webhook]
+ event_type = WebhookEventAsyncType.STAFF_CREATED
+ expected_payload = json.dumps(generate_staff_payload(staff_user))
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, staff_user, webhooks)
+
+ # then
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
+def test_staff_updated(staff_user, subscription_staff_updated_webhook):
+ # given
+ webhooks = [subscription_staff_updated_webhook]
+ event_type = WebhookEventAsyncType.STAFF_UPDATED
+ expected_payload = json.dumps(generate_staff_payload(staff_user))
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, staff_user, webhooks)
+
+ # then
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
+def test_staff_deleted(staff_user, subscription_staff_deleted_webhook):
+ # given
+ webhooks = [subscription_staff_deleted_webhook]
+ id = staff_user.id
+ staff_user.delete()
+ staff_user.id = id
+
+ event_type = WebhookEventAsyncType.STAFF_DELETED
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, staff_user, webhooks)
+ expected_payload = json.dumps(generate_staff_payload(staff_user))
+
+ # then
+
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
def test_product_created(product, subscription_product_created_webhook):
webhooks = [subscription_product_created_webhook]
event_type = WebhookEventAsyncType.PRODUCT_CREATED
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index d103f12634..488612e8f0 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -5215,6 +5215,7 @@ def webhook_app(
permission_manage_discounts,
permission_manage_menus,
permission_manage_products,
+ permission_manage_staff,
):
app = App.objects.create(name="Webhook app", is_active=True)
app.permissions.add(permission_manage_shipping)
@@ -5222,6 +5223,7 @@ def webhook_app(
app.permissions.add(permission_manage_discounts)
app.permissions.add(permission_manage_menus)
app.permissions.add(permission_manage_products)
+ app.permissions.add(permission_manage_staff)
return app
diff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.py
index 27fa586263..c27e1b2403 100644
--- a/saleor/webhook/event_types.py
+++ b/saleor/webhook/event_types.py
@@ -110,6 +110,10 @@ class WebhookEventAsyncType:
SHIPPING_ZONE_UPDATED = "shipping_zone_updated"
SHIPPING_ZONE_DELETED = "shipping_zone_deleted"
+ STAFF_CREATED = "staff_created"
+ STAFF_UPDATED = "staff_updated"
+ STAFF_DELETED = "staff_deleted"
+
TRANSACTION_ACTION_REQUEST = "transaction_action_request"
TRANSLATION_CREATED = "translation_created"
@@ -196,6 +200,9 @@ class WebhookEventAsyncType:
SHIPPING_ZONE_CREATED: "Shipping zone created",
SHIPPING_ZONE_UPDATED: "Shipping zone updated",
SHIPPING_ZONE_DELETED: "Shipping zone deleted",
+ STAFF_CREATED: "Staff created",
+ STAFF_UPDATED: "Staff updated",
+ STAFF_DELETED: "Staff deleted",
TRANSACTION_ACTION_REQUEST: "Payment action request",
TRANSLATION_CREATED: "Create translation",
TRANSLATION_UPDATED: "Update translation",
@@ -279,6 +286,9 @@ class WebhookEventAsyncType:
(SHIPPING_ZONE_CREATED, DISPLAY_LABELS[SHIPPING_ZONE_CREATED]),
(SHIPPING_ZONE_UPDATED, DISPLAY_LABELS[SHIPPING_ZONE_UPDATED]),
(SHIPPING_ZONE_DELETED, DISPLAY_LABELS[SHIPPING_ZONE_DELETED]),
+ (STAFF_CREATED, DISPLAY_LABELS[STAFF_CREATED]),
+ (STAFF_UPDATED, DISPLAY_LABELS[STAFF_UPDATED]),
+ (STAFF_DELETED, DISPLAY_LABELS[STAFF_DELETED]),
(TRANSACTION_ACTION_REQUEST, DISPLAY_LABELS[TRANSACTION_ACTION_REQUEST]),
(TRANSLATION_CREATED, DISPLAY_LABELS[TRANSLATION_CREATED]),
(TRANSLATION_UPDATED, DISPLAY_LABELS[TRANSLATION_UPDATED]),
@@ -363,6 +373,9 @@ class WebhookEventAsyncType:
SHIPPING_ZONE_CREATED: ShippingPermissions.MANAGE_SHIPPING,
SHIPPING_ZONE_UPDATED: ShippingPermissions.MANAGE_SHIPPING,
SHIPPING_ZONE_DELETED: ShippingPermissions.MANAGE_SHIPPING,
+ STAFF_CREATED: AccountPermissions.MANAGE_STAFF,
+ STAFF_UPDATED: AccountPermissions.MANAGE_STAFF,
+ STAFF_DELETED: AccountPermissions.MANAGE_STAFF,
TRANSACTION_ACTION_REQUEST: PaymentPermissions.HANDLE_PAYMENTS,
TRANSLATION_CREATED: SitePermissions.MANAGE_TRANSLATIONS,
TRANSLATION_UPDATED: SitePermissions.MANAGE_TRANSLATIONS,
@@ -516,6 +529,9 @@ SUBSCRIBABLE_EVENTS = [
WebhookEventAsyncType.SHIPPING_ZONE_CREATED,
WebhookEventAsyncType.SHIPPING_ZONE_UPDATED,
WebhookEventAsyncType.SHIPPING_ZONE_DELETED,
+ WebhookEventAsyncType.STAFF_CREATED,
+ WebhookEventAsyncType.STAFF_UPDATED,
+ WebhookEventAsyncType.STAFF_DELETED,
WebhookEventAsyncType.TRANSACTION_ACTION_REQUEST,
WebhookEventAsyncType.TRANSLATION_CREATED,
WebhookEventAsyncType.TRANSLATION_UPDATED,