Skip to content

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

New events related to permission groups changes (#10214)

SzymJf96e490ecf7d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fb3e85385b..258e468eaa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ All notable, unreleased changes to this project will be documented in this file.
### Saleor Apps
- Add support for the CUSTOMER_* app mount points (#10163) by @krzysztofwolski
+- Add permission group webhooks: `PERMISSION_GROUP_CREATED`, `PERMISSION_GROUP_UPDATED`, `PERMISSION_GROUP_DELETED` - #10214 by @SzymJ
### Breaking changes
- Drop django-versatileimagefield package; add a proxy view to generate thumbnails on-demand - #9988 by @IKarbowiak
diff --git a/saleor/graphql/account/mutations/permission_group.py b/saleor/graphql/account/mutations/permission_group.py
index 15072d6674..4b448cf0cc 100644
--- a/saleor/graphql/account/mutations/permission_group.py
+++ b/saleor/graphql/account/mutations/permission_group.py
@@ -72,6 +72,10 @@ class PermissionGroupCreate(ModelMutation):
if users:
instance.user_set.add(*users)
+ @classmethod
+ def post_save_action(cls, info, instance, cleaned_input):
+ info.context.plugins.permission_group_created(instance)
+
@classmethod
def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)
@@ -217,6 +221,10 @@ class PermissionGroupUpdate(PermissionGroupCreate):
if remove_permissions:
instance.permissions.remove(*remove_permissions)
+ @classmethod
+ def post_save_action(cls, info, instance, cleaned_input):
+ info.context.plugins.permission_group_updated(instance)
+
@classmethod
def clean_input(
cls,
@@ -430,6 +438,10 @@ class PermissionGroupDelete(ModelDeleteMutation):
error_type_class = PermissionGroupError
error_type_field = "permission_group_errors"
+ @classmethod
+ def post_save_action(cls, info, instance, cleaned_input):
+ info.context.plugins.permission_group_deleted(instance)
+
@classmethod
def clean_instance(cls, info, instance):
requestor = info.context.user
diff --git a/saleor/graphql/account/tests/test_account_permission_group.py b/saleor/graphql/account/tests/test_account_permission_group.py
index cd4082f53f..3fd7ad4a40 100644
--- a/saleor/graphql/account/tests/test_account_permission_group.py
+++ b/saleor/graphql/account/tests/test_account_permission_group.py
@@ -1,10 +1,16 @@
+from unittest.mock import patch
+
import graphene
import pytest
from django.contrib.auth.models import Group
+from django.utils.functional import SimpleLazyObject
+from freezegun import freeze_time
from ....account.error_codes import PermissionGroupErrorCode
from ....account.models import User
from ....core.permissions import AccountPermissions, AppPermission, OrderPermissions
+from ....webhook.event_types import WebhookEventAsyncType
+from ....webhook.payloads import generate_meta, generate_requestor
from ...tests.utils import (
assert_no_permission,
get_graphql_content,
@@ -93,6 +99,67 @@ def test_permission_group_create_mutation(
assert data["errors"] == []
+@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_permission_group_create_mutation_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_users,
+ permission_manage_staff,
+ staff_api_client,
+ permission_manage_users,
+ permission_manage_apps,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ staff_user = staff_users[0]
+ staff_user.user_permissions.add(permission_manage_users, permission_manage_apps)
+ query = PERMISSION_GROUP_CREATE_MUTATION
+
+ variables = {
+ "input": {
+ "name": "New permission group",
+ "addPermissions": [
+ AccountPermissions.MANAGE_USERS.name,
+ AppPermission.MANAGE_APPS.name,
+ ],
+ "addUsers": [
+ graphene.Node.to_global_id("User", user.id) for user in staff_users
+ ],
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=(permission_manage_staff,)
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["permissionGroupCreate"]
+ group = Group.objects.last()
+
+ # then
+ assert not data["errors"]
+ mocked_webhook_trigger.assert_called_once_with(
+ {
+ "id": graphene.Node.to_global_id("Group", group.id),
+ "meta": generate_meta(
+ requestor_data=generate_requestor(
+ SimpleLazyObject(lambda: staff_api_client.user)
+ )
+ ),
+ },
+ WebhookEventAsyncType.PERMISSION_GROUP_CREATED,
+ [any_webhook],
+ group,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+
def test_permission_group_create_app_no_permission(
staff_users,
permission_manage_staff,
@@ -537,6 +604,73 @@ def test_permission_group_update_mutation(
assert data["errors"] == []
+@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_permission_group_update_mutation_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_users,
+ permission_manage_staff,
+ staff_api_client,
+ permission_manage_apps,
+ permission_manage_users,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ staff_user = staff_users[0]
+ staff_user.user_permissions.add(permission_manage_apps, permission_manage_users)
+ query = PERMISSION_GROUP_UPDATE_MUTATION
+
+ group1, group2 = Group.objects.bulk_create(
+ [Group(name="manage users"), Group(name="manage staff and users")]
+ )
+ group1.permissions.add(permission_manage_users)
+ group2.permissions.add(permission_manage_users, permission_manage_staff)
+
+ group1_user = staff_users[1]
+ group1.user_set.add(group1_user)
+ group2.user_set.add(staff_user)
+
+ variables = {
+ "id": graphene.Node.to_global_id("Group", group1.id),
+ "input": {
+ "name": "New permission group",
+ "addPermissions": [AppPermission.MANAGE_APPS.name],
+ "removePermissions": [AccountPermissions.MANAGE_USERS.name],
+ "addUsers": [graphene.Node.to_global_id("User", staff_user.pk)],
+ "removeUsers": [graphene.Node.to_global_id("User", group1_user.pk)],
+ },
+ }
+
+ # when
+ response = staff_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["permissionGroupUpdate"]
+ group1.refresh_from_db()
+
+ # then
+ assert not data["errors"]
+ mocked_webhook_trigger.assert_called_once_with(
+ {
+ "id": graphene.Node.to_global_id("Group", group1.id),
+ "meta": generate_meta(
+ requestor_data=generate_requestor(
+ SimpleLazyObject(lambda: staff_api_client.user)
+ )
+ ),
+ },
+ WebhookEventAsyncType.PERMISSION_GROUP_UPDATED,
+ [any_webhook],
+ group1,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+
def test_permission_group_update_mutation_removing_perm_left_not_manageable_perms(
permission_group_manage_users,
staff_user,
@@ -1878,6 +2012,67 @@ def test_group_delete_mutation(
assert permission_group_data["permissions"] == []
+@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_group_delete_mutation_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_users,
+ permission_manage_staff,
+ permission_manage_orders,
+ permission_manage_products,
+ staff_api_client,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ staff_user, staff_user1, staff_user2 = staff_users
+ staff_user.user_permissions.add(
+ permission_manage_orders, permission_manage_products
+ )
+ groups = Group.objects.bulk_create(
+ [Group(name="manage orders"), Group(name="manage orders and products")]
+ )
+ group1, group2 = groups
+ group1.permissions.add(permission_manage_orders, permission_manage_staff)
+ group2.permissions.add(
+ permission_manage_orders, permission_manage_products, permission_manage_staff
+ )
+
+ staff_user2.groups.add(group1, group2)
+ variables = {"id": graphene.Node.to_global_id("Group", group1.id)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ PERMISSION_GROUP_DELETE_MUTATION,
+ variables,
+ permissions=(permission_manage_staff,),
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["permissionGroupDelete"]
+
+ # then
+ assert not data["errors"]
+ mocked_webhook_trigger.assert_called_once_with(
+ {
+ "id": graphene.Node.to_global_id("Group", group1.id),
+ "meta": generate_meta(
+ requestor_data=generate_requestor(
+ SimpleLazyObject(lambda: staff_api_client.user)
+ )
+ ),
+ },
+ WebhookEventAsyncType.PERMISSION_GROUP_DELETED,
+ [any_webhook],
+ group1,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+
def test_group_delete_mutation_app_no_permission(
staff_users,
permission_manage_staff,
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 816ed11424..8169c6c21a 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1569,6 +1569,15 @@ enum WebhookEventTypeEnum {
"""A page type is deleted."""
PAGE_TYPE_DELETED
+ """A new permission group is created."""
+ PERMISSION_GROUP_CREATED
+
+ """A permission group is updated."""
+ PERMISSION_GROUP_UPDATED
+
+ """A permission group is deleted."""
+ PERMISSION_GROUP_DELETED
+
"""A new shipping price is created."""
SHIPPING_PRICE_CREATED
@@ -1968,6 +1977,15 @@ enum WebhookEventTypeAsyncEnum {
"""A page type is deleted."""
PAGE_TYPE_DELETED
+ """A new permission group is created."""
+ PERMISSION_GROUP_CREATED
+
+ """A permission group is updated."""
+ PERMISSION_GROUP_UPDATED
+
+ """A permission group is deleted."""
+ PERMISSION_GROUP_DELETED
+
"""A new shipping price is created."""
SHIPPING_PRICE_CREATED
@@ -2571,6 +2589,9 @@ enum WebhookSampleEventTypeEnum {
PAGE_TYPE_CREATED
PAGE_TYPE_UPDATED
PAGE_TYPE_DELETED
+ PERMISSION_GROUP_CREATED
+ PERMISSION_GROUP_UPDATED
+ PERMISSION_GROUP_DELETED
SHIPPING_PRICE_CREATED
SHIPPING_PRICE_UPDATED
SHIPPING_PRICE_DELETED
@@ -23028,6 +23049,75 @@ type PageTypeDeleted implements Event {
pageType: PageType
}
+type PermissionGroupCreated 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 permission group the event relates to.
+
+ Added in Saleor 3.6.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ permissionGroup: Group
+}
+
+type PermissionGroupUpdated 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 permission group the event relates to.
+
+ Added in Saleor 3.6.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ permissionGroup: Group
+}
+
+type PermissionGroupDeleted 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 permission group the event relates to.
+
+ Added in Saleor 3.6.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ permissionGroup: Group
+}
+
type ShippingPriceCreated 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 e4ea41356b..9856f6ce29 100644
--- a/saleor/graphql/tests/queries/fragments.py
+++ b/saleor/graphql/tests/queries/fragments.py
@@ -321,6 +321,19 @@ fragment PageTypeDetails on PageType{
"""
+PERMISSION_GROUP_DETAILS = """
+fragment PermissionGroupDetails on Group{
+ name
+ permissions {
+ name
+ }
+ users {
+ email
+ }
+}
+"""
+
+
SALE_DETAILS = """
fragment SaleDetails on Sale {
id
diff --git a/saleor/graphql/webhook/enums.py b/saleor/graphql/webhook/enums.py
index 3185b666d3..0b2c1e8e04 100644
--- a/saleor/graphql/webhook/enums.py
+++ b/saleor/graphql/webhook/enums.py
@@ -87,6 +87,11 @@ WEBHOOK_EVENT_DESCRIPTION = {
WebhookEventAsyncType.PAGE_TYPE_CREATED: "A new page type is created.",
WebhookEventAsyncType.PAGE_TYPE_UPDATED: "A page type is updated.",
WebhookEventAsyncType.PAGE_TYPE_DELETED: "A page type is deleted.",
+ WebhookEventAsyncType.PERMISSION_GROUP_CREATED: (
+ "A new permission group is created."
+ ),
+ WebhookEventAsyncType.PERMISSION_GROUP_UPDATED: "A permission group is updated.",
+ WebhookEventAsyncType.PERMISSION_GROUP_DELETED: "A permission group is deleted.",
WebhookEventAsyncType.PRODUCT_CREATED: "A new product is created.",
WebhookEventAsyncType.PRODUCT_UPDATED: "A product is updated.",
WebhookEventAsyncType.PRODUCT_DELETED: "A product is deleted.",
diff --git a/saleor/graphql/webhook/subscription_types.py b/saleor/graphql/webhook/subscription_types.py
index c66c8b85d3..170a0dfdab 100644
--- a/saleor/graphql/webhook/subscription_types.py
+++ b/saleor/graphql/webhook/subscription_types.py
@@ -23,7 +23,13 @@ from ...webhook.event_types import WebhookEventAsyncType
from ..account.types import User as UserType
from ..app.types import App as AppType
from ..channel import ChannelContext
-from ..core.descriptions import ADDED_IN_32, ADDED_IN_34, ADDED_IN_35, PREVIEW_FEATURE
+from ..core.descriptions import (
+ ADDED_IN_32,
+ ADDED_IN_34,
+ ADDED_IN_35,
+ ADDED_IN_36,
+ PREVIEW_FEATURE,
+)
from ..core.scalars import PositiveDecimal
from ..payment.enums import TransactionActionEnum
from ..payment.types import TransactionItem
@@ -143,6 +149,9 @@ class Event(graphene.Interface):
WebhookEventAsyncType.PAGE_TYPE_CREATED: PageTypeCreated,
WebhookEventAsyncType.PAGE_TYPE_UPDATED: PageTypeUpdated,
WebhookEventAsyncType.PAGE_TYPE_DELETED: PageTypeDeleted,
+ WebhookEventAsyncType.PERMISSION_GROUP_CREATED: PermissionGroupCreated,
+ WebhookEventAsyncType.PERMISSION_GROUP_UPDATED: PermissionGroupUpdated,
+ WebhookEventAsyncType.PERMISSION_GROUP_DELETED: PermissionGroupDeleted,
WebhookEventAsyncType.SHIPPING_PRICE_CREATED: ShippingPriceCreated,
WebhookEventAsyncType.SHIPPING_PRICE_UPDATED: ShippingPriceUpdated,
WebhookEventAsyncType.SHIPPING_PRICE_DELETED: ShippingPriceDeleted,
@@ -878,6 +887,35 @@ class PageTypeDeleted(ObjectType, PageTypeBase):
interfaces = (Event,)
+class PermissionGroupBase(AbstractType):
+ permission_group = graphene.Field(
+ "saleor.graphql.account.types.Group",
+ description="The permission group the event relates to."
+ + ADDED_IN_36
+ + PREVIEW_FEATURE,
+ )
+
+ @staticmethod
+ def resolve_permission_group(root, _info):
+ _, permission_group = root
+ return permission_group
+
+
+class PermissionGroupCreated(ObjectType, PermissionGroupBase):
+ class Meta:
+ interfaces = (Event,)
+
+
+class PermissionGroupUpdated(ObjectType, PermissionGroupBase):
+ class Meta:
+ interfaces = (Event,)
+
+
+class PermissionGroupDeleted(ObjectType, PermissionGroupBase):
+ class Meta:
+ interfaces = (Event,)
+
+
class ShippingPriceBase(AbstractType):
shipping_method = graphene.Field(
"saleor.graphql.shipping.types.ShippingMethodType",
@@ -1195,6 +1233,9 @@ SUBSCRIPTION_EVENTS_TYPES = [
PageTypeCreated,
PageTypeUpdated,
PageTypeDeleted,
+ PermissionGroupCreated,
+ PermissionGroupUpdated,
+ PermissionGroupDeleted,
ShippingPriceCreated,
ShippingPriceUpdated,
ShippingPriceDeleted,
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index 50cbcb5b84..82eca299a6 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -37,7 +37,7 @@ from .models import PluginConfiguration
if TYPE_CHECKING:
# flake8: noqa
- from ..account.models import Address, User
+ from ..account.models import Address, Group, User
from ..app.models import App
from ..attribute.models import Attribute, AttributeValue
from ..channel.models import Channel
@@ -705,6 +705,24 @@ class BasePlugin:
# updated.
page_type_updated: Callable[["PageType", Any], Any]
+ # Trigger when permission group is created.
+ #
+ # Overwrite this method if you need to trigger specific logic when a permission
+ # group is created.
+ permission_group_created: Callable[["Group", Any], Any]
+
+ # Trigger when permission group type is deleted.
+ #
+ # Overwrite this method if you need to trigger specific logic when a permission
+ # group is deleted.
+ permission_group_deleted: Callable[["Group", Any], Any]
+
+ # Trigger when permission group is updated.
+ #
+ # Overwrite this method if you need to trigger specific logic when a permission
+ # group is updated.
+ permission_group_updated: Callable[["Group", Any], Any]
+
# Trigger directly before order creation.
#
# Overwrite this method if you need to trigger specific logic before an order is
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index 074b1ea46a..604e344824 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -37,7 +37,7 @@ from .models import PluginConfiguration
if TYPE_CHECKING:
# flake8: noqa
- from ..account.models import Address, User
+ from ..account.models import Address, Group, User
from ..app.models import App
from ..attribute.models import Attribute, AttributeValue
from ..checkout.fetch import CheckoutInfo, CheckoutLineInfo
@@ -830,6 +830,24 @@ class PluginsManager(PaymentInterface):
"page_type_deleted", default_value, page_type
)
+ def permission_group_created(self, group: "Group"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "permission_group_created", default_value, group
+ )
+
+ def permission_group_updated(self, group: "Group"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "permission_group_updated", default_value, group
+ )
+
+ def permission_group_deleted(self, group: "Group"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "permission_group_deleted", default_value, group
+ )
+
def transaction_action_request(
self, payment_data: "TransactionActionData", channel_slug: str
):
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index 12da522990..ec62e6a17f 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -61,7 +61,7 @@ from .utils import (
)
if TYPE_CHECKING:
- from ...account.models import Address, User
+ from ...account.models import Address, Group, User
from ...attribute.models import Attribute, AttributeValue
from ...channel.models import Channel
from ...checkout.models import Checkout
@@ -895,6 +895,35 @@ class WebhookPlugin(BasePlugin):
WebhookEventAsyncType.PAGE_TYPE_DELETED, page_type
)
+ def _trigger_permission_group_event(self, event_type, group):
+ if webhooks := get_webhooks_for_event(event_type):
+ payload = {
+ "id": graphene.Node.to_global_id("Group", group.id),
+ "meta": self._generate_meta(),
+ }
+ trigger_webhooks_async(payload, event_type, webhooks, group, self.requestor)
+
+ def permission_group_created(self, group: "Group", previous_value: Any) -> Any:
+ if not self.active:
+ return previous_value
+ self._trigger_permission_group_event(
+ WebhookEventAsyncType.PERMISSION_GROUP_CREATED, group
+ )
+
+ def permission_group_updated(self, group: "Group", previous_value: Any) -> Any:
+ if not self.active:
+ return previous_value
+ self._trigger_permission_group_event(
+ WebhookEventAsyncType.PERMISSION_GROUP_UPDATED, group
+ )
+
+ def permission_group_deleted(self, group: "Group", previous_value: Any) -> Any:
+ if not self.active:
+ return previous_value
+ self._trigger_permission_group_event(
+ WebhookEventAsyncType.PERMISSION_GROUP_DELETED, group
+ )
+
def _trigger_shipping_price_event(self, event_type, shipping_method):
if webhooks := get_webhooks_for_event(event_type):
payload = {
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
index f0835f4cfa..6b4091afb3 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
@@ -579,6 +579,30 @@ def subscription_page_type_deleted_webhook(subscription_webhook):
)
+def subscription_permission_group_created_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.PERMISSION_GROUP_CREATED,
+ WebhookEventAsyncType.PERMISSION_GROUP_CREATED,
+ )
+
+
+def subscription_permission_group_updated_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.PERMISSION_GROUP_UPDATED,
+ WebhookEventAsyncType.PERMISSION_GROUP_UPDATED,
+ )
+
+
+def subscription_permission_group_deleted_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.PERMISSION_GROUP_DELETED,
+ WebhookEventAsyncType.PERMISSION_GROUP_DELETED,
+ )
+
+
@pytest.fixture
def subscription_product_created_multiple_events_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 0c4fe370e1..1757495774 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
@@ -220,6 +220,18 @@ def generate_page_type_payload(page_type):
}
+def generate_permission_group_payload(group):
+ return {
+ "permissionGroup": {
+ "name": group.name,
+ "permissions": [
+ {"name": permission.name} for permission in group.permissions.all()
+ ],
+ "users": [{"email": user.email} for user in group.user_set.all()],
+ }
+ }
+
+
def generate_invoice_payload(invoice):
return {
"invoice": {
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
index 36a1aaade8..e3da4798e2 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
@@ -1152,6 +1152,51 @@ PAGE_TYPE_DELETED = (
"""
)
+PERMISSION_GROUP_CREATED = (
+ fragments.PERMISSION_GROUP_DETAILS
+ + """
+ subscription{
+ event{
+ ...on PermissionGroupCreated{
+ permissionGroup{
+ ...PermissionGroupDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+PERMISSION_GROUP_UPDATED = (
+ fragments.PERMISSION_GROUP_DETAILS
+ + """
+ subscription{
+ event{
+ ...on PermissionGroupUpdated{
+ permissionGroup{
+ ...PermissionGroupDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+PERMISSION_GROUP_DELETED = (
+ fragments.PERMISSION_GROUP_DETAILS
+ + """
+ subscription{
+ event{
+ ...on PermissionGroupDeleted{
+ permissionGroup{
+ ...PermissionGroupDetails
+ }
+ }
+ }
+ }
+"""
+)
+
MULTIPLE_EVENTS = """
subscription{
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 3ee56f873f..e643b9d710 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_menu_payload,
generate_page_payload,
generate_page_type_payload,
+ generate_permission_group_payload,
generate_sale_payload,
generate_shipping_method_payload,
generate_staff_payload,
@@ -1438,6 +1439,64 @@ def test_page_type_deleted(page_type, subscription_page_type_deleted_webhook):
assert deliveries[0].webhook == webhooks[0]
+def test_permission_group_created(
+ permission_group_manage_users, subscription_permission_group_created_webhook
+):
+ # given
+ group = permission_group_manage_users
+ webhooks = [subscription_permission_group_created_webhook]
+ event_type = WebhookEventAsyncType.PERMISSION_GROUP_CREATED
+ expected_payload = json.dumps(generate_permission_group_payload(group))
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, group, webhooks)
+
+ # then
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
+def test_permission_group_updated(
+ permission_group_manage_users, subscription_permission_group_updated_webhook
+):
+ # given
+ group = permission_group_manage_users
+ webhooks = [subscription_permission_group_updated_webhook]
+ event_type = WebhookEventAsyncType.PERMISSION_GROUP_UPDATED
+ expected_payload = json.dumps(generate_permission_group_payload(group))
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, group, webhooks)
+
+ # then
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
+def test_permission_group_deleted(
+ permission_group_manage_users, subscription_permission_group_deleted_webhook
+):
+ # given
+ group = permission_group_manage_users
+ group_id = group.id
+ group.delete()
+ group.id = group_id
+
+ webhooks = [subscription_permission_group_deleted_webhook]
+ event_type = WebhookEventAsyncType.PERMISSION_GROUP_DELETED
+ expected_payload = json.dumps(generate_permission_group_payload(group))
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, group, webhooks)
+
+ # then
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
def test_product_created_multiple_events_in_subscription(
product, subscription_product_created_multiple_events_webhook
):
diff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.py
index 1645b28da2..a489b02696 100644
--- a/saleor/webhook/event_types.py
+++ b/saleor/webhook/event_types.py
@@ -112,6 +112,10 @@ class WebhookEventAsyncType:
PAGE_TYPE_UPDATED = "page_type_updated"
PAGE_TYPE_DELETED = "page_type_deleted"
+ PERMISSION_GROUP_CREATED = "permission_group_created"
+ PERMISSION_GROUP_UPDATED = "permission_group_updated"
+ PERMISSION_GROUP_DELETED = "permission_group_deleted"
+
SHIPPING_PRICE_CREATED = "shipping_price_created"
SHIPPING_PRICE_UPDATED = "shipping_price_updated"
SHIPPING_PRICE_DELETED = "shipping_price_deleted"
@@ -212,6 +216,9 @@ class WebhookEventAsyncType:
PAGE_TYPE_CREATED: "Page type created",
PAGE_TYPE_UPDATED: "Page type updated",
PAGE_TYPE_DELETED: "Page type deleted",
+ PERMISSION_GROUP_CREATED: "Permission group created",
+ PERMISSION_GROUP_UPDATED: "Permission group updated",
+ PERMISSION_GROUP_DELETED: "Permission group deleted",
SHIPPING_PRICE_CREATED: "Shipping price created",
SHIPPING_PRICE_UPDATED: "Shipping price updated",
SHIPPING_PRICE_DELETED: "Shipping price deleted",
@@ -306,6 +313,9 @@ class WebhookEventAsyncType:
(PAGE_TYPE_CREATED, DISPLAY_LABELS[PAGE_TYPE_CREATED]),
(PAGE_TYPE_UPDATED, DISPLAY_LABELS[PAGE_TYPE_UPDATED]),
(PAGE_TYPE_DELETED, DISPLAY_LABELS[PAGE_TYPE_DELETED]),
+ (PERMISSION_GROUP_CREATED, DISPLAY_LABELS[PERMISSION_GROUP_CREATED]),
+ (PERMISSION_GROUP_UPDATED, DISPLAY_LABELS[PERMISSION_GROUP_UPDATED]),
+ (PERMISSION_GROUP_DELETED, DISPLAY_LABELS[PERMISSION_GROUP_DELETED]),
(SHIPPING_PRICE_CREATED, DISPLAY_LABELS[SHIPPING_PRICE_CREATED]),
(SHIPPING_PRICE_UPDATED, DISPLAY_LABELS[SHIPPING_PRICE_UPDATED]),
(SHIPPING_PRICE_DELETED, DISPLAY_LABELS[SHIPPING_PRICE_DELETED]),
@@ -401,6 +411,9 @@ class WebhookEventAsyncType:
PAGE_TYPE_CREATED: PageTypePermissions.MANAGE_PAGE_TYPES_AND_ATTRIBUTES,
PAGE_TYPE_UPDATED: PageTypePermissions.MANAGE_PAGE_TYPES_AND_ATTRIBUTES,
PAGE_TYPE_DELETED: PageTypePermissions.MANAGE_PAGE_TYPES_AND_ATTRIBUTES,
+ PERMISSION_GROUP_CREATED: AccountPermissions.MANAGE_STAFF,
+ PERMISSION_GROUP_UPDATED: AccountPermissions.MANAGE_STAFF,
+ PERMISSION_GROUP_DELETED: AccountPermissions.MANAGE_STAFF,
SHIPPING_PRICE_CREATED: ShippingPermissions.MANAGE_SHIPPING,
SHIPPING_PRICE_UPDATED: ShippingPermissions.MANAGE_SHIPPING,
SHIPPING_PRICE_DELETED: ShippingPermissions.MANAGE_SHIPPING,
@@ -574,6 +587,9 @@ SUBSCRIBABLE_EVENTS = [
WebhookEventAsyncType.PAGE_TYPE_CREATED,
WebhookEventAsyncType.PAGE_TYPE_UPDATED,
WebhookEventAsyncType.PAGE_TYPE_DELETED,
+ WebhookEventAsyncType.PERMISSION_GROUP_CREATED,
+ WebhookEventAsyncType.PERMISSION_GROUP_UPDATED,
+ WebhookEventAsyncType.PERMISSION_GROUP_DELETED,
WebhookEventAsyncType.SHIPPING_PRICE_CREATED,
WebhookEventAsyncType.SHIPPING_PRICE_UPDATED,
WebhookEventAsyncType.SHIPPING_PRICE_DELETED,