Skip to content

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

New events related to address changes (#9860)

SzymJ0c5b67573786
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dd33fccad1..2a697753a3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ 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
# 3.4.0
diff --git a/saleor/graphql/account/mutations/account.py b/saleor/graphql/account/mutations/account.py
index 655b8bc17b..dcb393f3fe 100644
--- a/saleor/graphql/account/mutations/account.py
+++ b/saleor/graphql/account/mutations/account.py
@@ -3,6 +3,7 @@ import jwt
from django.conf import settings
from django.contrib.auth import password_validation
from django.core.exceptions import ValidationError
+from django.db import transaction
from ....account import events as account_events
from ....account import models, notifications, search, utils
@@ -328,9 +329,18 @@ class AccountAddressCreate(ModelMutation, I18nMixin):
user = info.context.user
remove_the_oldest_user_address_if_address_limit_is_reached(user)
instance.user_addresses.add(user)
- info.context.plugins.customer_updated(user)
user.search_document = search.prepare_user_search_document_value(user)
user.save(update_fields=["search_document", "updated_at"])
+ transaction.on_commit(
+ lambda: cls.trigger_post_account_address_create_webhooks(
+ info, instance, user
+ )
+ )
+
+ @classmethod
+ def trigger_post_account_address_create_webhooks(cls, info, address, user):
+ info.context.plugins.customer_updated(user)
+ info.context.plugins.address_created(address)
class AccountAddressUpdate(BaseAddressUpdate):
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.py
index d4a390f29a..263c41b8fc 100644
--- a/saleor/graphql/account/mutations/base.py
+++ b/saleor/graphql/account/mutations/base.py
@@ -331,6 +331,7 @@ class BaseAddressUpdate(ModelMutation, I18nMixin):
info.context.plugins.customer_updated(user)
address = info.context.plugins.change_user_address(address, None, user)
+ info.context.plugins.address_updated(address)
success_response = cls.success_response(address)
success_response.user = user
@@ -391,6 +392,7 @@ class BaseAddressDelete(ModelDeleteMutation):
response.user = user
info.context.plugins.customer_updated(user)
+ info.context.plugins.address_deleted(instance)
return response
diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.py
index c95eed2a61..64d3906c85 100644
--- a/saleor/graphql/account/mutations/staff.py
+++ b/saleor/graphql/account/mutations/staff.py
@@ -3,6 +3,7 @@ from copy import copy
import graphene
from django.core.exceptions import ValidationError
+from django.db import transaction
from ....account import events as account_events
from ....account import models, utils
@@ -475,6 +476,10 @@ class AddressCreate(ModelMutation):
user.save(update_fields=["search_document", "updated_at"])
return response
+ @classmethod
+ def post_save_action(cls, info, instance, cleaned_input):
+ transaction.on_commit(lambda: info.context.plugins.address_created(instance))
+
class AddressUpdate(BaseAddressUpdate):
class Meta:
diff --git a/saleor/graphql/account/tests/test_account.py b/saleor/graphql/account/tests/test_account.py
index 473491ab69..5338350d08 100644
--- a/saleor/graphql/account/tests/test_account.py
+++ b/saleor/graphql/account/tests/test_account.py
@@ -14,6 +14,7 @@ from django.core.exceptions import ValidationError
from django.core.files import File
from django.test import override_settings
from django.utils import timezone
+from django.utils.functional import SimpleLazyObject
from freezegun import freeze_time
from ....account import events as account_events
@@ -34,6 +35,8 @@ from ....core.utils.url import prepare_url
from ....order import OrderStatus
from ....order.models import FulfillmentStatus, Order
from ....product.tests.utils import create_image
+from ....webhook.event_types import WebhookEventAsyncType
+from ....webhook.payloads import generate_meta, generate_requestor
from ...core.utils import str_to_enum, to_global_id_or_none
from ...tests.utils import (
assert_graphql_error_with_message,
@@ -47,6 +50,24 @@ from ..mutations.staff import CustomerDelete, StaffDelete, StaffUpdate, UserDele
from ..tests.utils import convert_dict_keys_to_camel_case
+def generate_address_webhook_call_args(address, event, requestor, webhook):
+ return [
+ {
+ "id": graphene.Node.to_global_id("Address", address.id),
+ "city": address.city,
+ "country": address.country,
+ "company_name": address.company_name,
+ "meta": generate_meta(
+ requestor_data=generate_requestor(SimpleLazyObject(lambda: requestor))
+ ),
+ },
+ event,
+ [webhook],
+ address,
+ SimpleLazyObject(lambda: requestor),
+ ]
+
+
@pytest.fixture
def query_customer_with_filter():
query = """
@@ -3568,6 +3589,46 @@ def test_create_address_mutation(
assert variables[field].lower() in customer_user.search_document
+@freeze_time("2022-05-12 12:00:00")
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_create_address_mutation_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ customer_user,
+ permission_manage_users,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ user_id = graphene.Node.to_global_id("User", customer_user.id)
+ variables = {"user": user_id, "city": "Dummy", "country": "PL"}
+
+ # when
+ response = staff_api_client.post_graphql(
+ ADDRESS_CREATE_MUTATION, variables, permissions=[permission_manage_users]
+ )
+ content = get_graphql_content(response)
+ address = Address.objects.last()
+
+ # then
+ assert not content["data"]["addressCreate"]["errors"]
+ assert content["data"]["addressCreate"]
+
+ mocked_webhook_trigger.assert_called_once_with(
+ *generate_address_webhook_call_args(
+ address,
+ WebhookEventAsyncType.ADDRESS_CREATED,
+ staff_api_client.user,
+ any_webhook,
+ )
+ )
+
+
@override_settings(MAX_USER_ADDRESSES=2)
def test_create_address_mutation_the_oldest_address_is_deleted(
staff_api_client, customer_user, address, permission_manage_users
@@ -3638,6 +3699,49 @@ def test_address_update_mutation(
)
+@freeze_time("2022-05-12 12:00:00")
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_address_update_mutation_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ customer_user,
+ permission_manage_users,
+ graphql_address_data,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ address = customer_user.addresses.first()
+ assert staff_api_client.user not in address.user_addresses.all()
+ variables = {
+ "addressId": graphene.Node.to_global_id("Address", address.id),
+ "address": graphql_address_data,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ ADDRESS_UPDATE_MUTATION, variables, permissions=[permission_manage_users]
+ )
+ content = get_graphql_content(response)
+ address.refresh_from_db()
+
+ # then
+ assert content["data"]["addressUpdate"]
+ mocked_webhook_trigger.assert_called_with(
+ *generate_address_webhook_call_args(
+ address,
+ WebhookEventAsyncType.ADDRESS_UPDATED,
+ staff_api_client.user,
+ any_webhook,
+ )
+ )
+
+
ACCOUNT_ADDRESS_UPDATE_MUTATION = """
mutation updateAccountAddress($addressId: ID!, $address: AddressInput!) {
accountAddressUpdate(id: $addressId, input: $address) {
@@ -3676,6 +3780,49 @@ def test_customer_update_own_address(
assert generate_address_search_document_value(address_obj) in user.search_document
+@freeze_time("2022-05-12 12:00:00")
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_customer_address_update_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ user_api_client,
+ customer_user,
+ graphql_address_data,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ address = customer_user.addresses.first()
+ address_data = graphql_address_data
+ address_data["city"] = "Poznań"
+ assert address_data["city"] != address.city
+
+ variables = {
+ "addressId": graphene.Node.to_global_id("Address", address.id),
+ "address": graphql_address_data,
+ }
+
+ # when
+ response = user_api_client.post_graphql(ACCOUNT_ADDRESS_UPDATE_MUTATION, variables)
+ content = get_graphql_content(response)
+ address.refresh_from_db()
+
+ # then
+ assert content["data"]["accountAddressUpdate"]
+ mocked_webhook_trigger.assert_called_with(
+ *generate_address_webhook_call_args(
+ address,
+ WebhookEventAsyncType.ADDRESS_UPDATED,
+ user_api_client.user,
+ any_webhook,
+ )
+ )
+
+
def test_update_address_as_anonymous_user(
api_client, customer_user, graphql_address_data
):
@@ -3765,6 +3912,43 @@ def test_address_delete_mutation(
)
+@freeze_time("2022-05-12 12:00:00")
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_address_delete_mutation_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ customer_user,
+ permission_manage_users,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ address = customer_user.addresses.first()
+ variables = {"id": graphene.Node.to_global_id("Address", address.id)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ ADDRESS_DELETE_MUTATION, variables, permissions=[permission_manage_users]
+ )
+ content = get_graphql_content(response)
+
+ # then
+ assert content["data"]["addressDelete"]
+ mocked_webhook_trigger.assert_called_with(
+ *generate_address_webhook_call_args(
+ address,
+ WebhookEventAsyncType.ADDRESS_DELETED,
+ staff_api_client.user,
+ any_webhook,
+ )
+ )
+
+
def test_address_delete_mutation_as_app(
app_api_client, customer_user, permission_manage_users
):
@@ -3813,6 +3997,40 @@ def test_customer_delete_own_address(user_api_client, customer_user):
)
+@freeze_time("2022-05-12 12:00:00")
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_customer_delete_address_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ user_api_client,
+ customer_user,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ address = customer_user.addresses.first()
+ variables = {"id": graphene.Node.to_global_id("Address", address.id)}
+
+ # when
+ response = user_api_client.post_graphql(ACCOUNT_ADDRESS_DELETE_MUTATION, variables)
+ content = get_graphql_content(response)
+
+ # then
+ assert content["data"]["accountAddressDelete"]
+ mocked_webhook_trigger.assert_called_with(
+ *generate_address_webhook_call_args(
+ address,
+ WebhookEventAsyncType.ADDRESS_DELETED,
+ user_api_client.user,
+ any_webhook,
+ )
+ )
+
+
@pytest.mark.parametrize(
"query", [ADDRESS_DELETE_MUTATION, ACCOUNT_ADDRESS_DELETE_MUTATION]
)
@@ -4359,6 +4577,40 @@ def test_customer_create_address(user_api_client, graphql_address_data):
)
+@freeze_time("2022-05-12 12:00:00")
+@patch("saleor.plugins.webhook.plugin.get_webhooks_for_event")
+@patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_customer_create_address_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ user_api_client,
+ graphql_address_data,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ variables = {"addressInput": graphql_address_data}
+
+ # when
+ response = user_api_client.post_graphql(ACCOUNT_ADDRESS_CREATE_MUTATION, variables)
+ content = get_graphql_content(response)
+ address = Address.objects.last()
+
+ # then
+ assert content["data"]["accountAddressCreate"]
+ mocked_webhook_trigger.assert_called_with(
+ *generate_address_webhook_call_args(
+ address,
+ WebhookEventAsyncType.ADDRESS_CREATED,
+ user_api_client.user,
+ any_webhook,
+ )
+ )
+
+
def test_account_address_create_return_user(user_api_client, graphql_address_data):
user = user_api_client.user
variables = {"addressInput": graphql_address_data}
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index bf1d429232..a0c14f8c0d 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1327,6 +1327,15 @@ enum WebhookEventTypeEnum {
"""All the events."""
ANY_EVENTS
+ """A new address created."""
+ ADDRESS_CREATED
+
+ """An address updated."""
+ ADDRESS_UPDATED
+
+ """An address deleted."""
+ ADDRESS_DELETED
+
"""A new app installed."""
APP_INSTALLED
@@ -1587,6 +1596,15 @@ enum WebhookEventTypeAsyncEnum {
"""All the events."""
ANY_EVENTS
+ """A new address created."""
+ ADDRESS_CREATED
+
+ """An address updated."""
+ ADDRESS_UPDATED
+
+ """An address deleted."""
+ ADDRESS_DELETED
+
"""A new app installed."""
APP_INSTALLED
@@ -2263,6 +2281,9 @@ scalar JSONString
"""An enumeration."""
enum WebhookSampleEventTypeEnum {
+ ADDRESS_CREATED
+ ADDRESS_UPDATED
+ ADDRESS_DELETED
APP_INSTALLED
APP_UPDATED
APP_DELETED
@@ -20662,6 +20683,75 @@ enum VolumeUnitsEnum {
ACRE_FT
}
+type AddressCreated 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 address the event relates to.
+
+ Added in Saleor 3.5.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ address: Address
+}
+
+type AddressUpdated 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 address the event relates to.
+
+ Added in Saleor 3.5.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ address: Address
+}
+
+type AddressDeleted 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 address the event relates to.
+
+ Added in Saleor 3.5.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ address: Address
+}
+
type AppInstalled implements Event {
"""Time of the event."""
issuedAt: DateTime
diff --git a/saleor/graphql/webhook/enums.py b/saleor/graphql/webhook/enums.py
index 82256badbb..a5cdd70070 100644
--- a/saleor/graphql/webhook/enums.py
+++ b/saleor/graphql/webhook/enums.py
@@ -23,6 +23,9 @@ order_updated_event_enum_description = (
WEBHOOK_EVENT_DESCRIPTION = {
+ WebhookEventAsyncType.ADDRESS_CREATED: "A new address created.",
+ WebhookEventAsyncType.ADDRESS_UPDATED: "An address updated.",
+ WebhookEventAsyncType.ADDRESS_DELETED: "An address deleted.",
WebhookEventAsyncType.APP_INSTALLED: "A new app installed.",
WebhookEventAsyncType.APP_UPDATED: "An app updated.",
WebhookEventAsyncType.APP_DELETED: "An app deleted.",
diff --git a/saleor/graphql/webhook/subscription_types.py b/saleor/graphql/webhook/subscription_types.py
index fa851c60e1..adf195ee93 100644
--- a/saleor/graphql/webhook/subscription_types.py
+++ b/saleor/graphql/webhook/subscription_types.py
@@ -70,6 +70,9 @@ class Event(graphene.Interface):
@classmethod
def get_type(cls, object_type: str):
types = {
+ WebhookEventAsyncType.ADDRESS_CREATED: AddressCreated,
+ WebhookEventAsyncType.ADDRESS_UPDATED: AddressUpdated,
+ WebhookEventAsyncType.ADDRESS_DELETED: AddressDeleted,
WebhookEventAsyncType.APP_INSTALLED: AppInstalled,
WebhookEventAsyncType.APP_UPDATED: AppUpdated,
WebhookEventAsyncType.APP_DELETED: AppDeleted,
@@ -175,6 +178,33 @@ class Event(graphene.Interface):
return info.context.requestor
+class AddressBase(AbstractType):
+ address = graphene.Field(
+ "saleor.graphql.account.types.Address",
+ description="The address the event relates to." + ADDED_IN_35 + PREVIEW_FEATURE,
+ )
+
+ @staticmethod
+ def resolve_address(root, _info):
+ _, address = root
+ return address
+
+
+class AddressCreated(ObjectType, AddressBase):
+ class Meta:
+ interfaces = (Event,)
+
+
+class AddressUpdated(ObjectType, AddressBase):
+ class Meta:
+ interfaces = (Event,)
+
+
+class AddressDeleted(ObjectType, AddressBase):
+ class Meta:
+ interfaces = (Event,)
+
+
class AppBase(AbstractType):
app = graphene.Field(
"saleor.graphql.app.types.App",
@@ -995,6 +1025,9 @@ class Subscription(ObjectType):
# so all subscription events types need to be added manually to `Schema`.
SUBSCRIPTION_EVENTS_TYPES = [
+ AddressCreated,
+ AddressUpdated,
+ AddressDeleted,
AppInstalled,
AppUpdated,
AppDeleted,
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index 65dbe70cb0..241018e7d9 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -134,6 +134,24 @@ class BasePlugin:
def __str__(self):
return self.PLUGIN_NAME
+ # Trigger when address is created.
+ #
+ # Overwrite this method if you need to trigger specific logic after an address is
+ # created.
+ address_created: Callable[["Address", None], None]
+
+ # Trigger when address is deleted.
+ #
+ # Overwrite this method if you need to trigger specific logic after an address is
+ # deleted.
+ address_deleted: Callable[["Address", None], None]
+
+ # Trigger when address is updated.
+ #
+ # Overwrite this method if you need to trigger specific logic after an address is
+ # updated.
+ address_updated: Callable[["Address", None], None]
+
# Trigger when app is installed.
#
# Overwrite this method if you need to trigger specific logic after an app is
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index 44130c7af6..95b51f9aaf 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -826,6 +826,18 @@ class PluginsManager(PaymentInterface):
channel_slug=channel_slug,
)
+ def address_created(self, address: "Address"):
+ default_value = None
+ return self.__run_method_on_plugins("address_created", default_value, address)
+
+ def address_updated(self, address: "Address"):
+ default_value = None
+ return self.__run_method_on_plugins("address_updated", default_value, address)
+
+ def address_deleted(self, address: "Address"):
+ default_value = None
+ return self.__run_method_on_plugins("address_deleted", default_value, address)
+
def app_installed(self, app: "App"):
default_value = None
return self.__run_method_on_plugins("app_installed", default_value, app)
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index fc24573f87..f07d88397f 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -50,7 +50,7 @@ from .utils import (
)
if TYPE_CHECKING:
- from ...account.models import User
+ from ...account.models import Address, User
from ...channel.models import Channel
from ...checkout.models import Checkout
from ...discount.models import Sale, Voucher
@@ -96,6 +96,34 @@ class WebhookPlugin(BasePlugin):
def _generate_meta(self):
return generate_meta(requestor_data=generate_requestor(self.requestor))
+ def _trigger_address_event(self, event_type, address):
+ if webhooks := get_webhooks_for_event(event_type):
+ payload = {
+ "id": graphene.Node.to_global_id("Address", address.id),
+ "city": address.city,
+ "country": address.country,
+ "company_name": address.company_name,
+ "meta": self._generate_meta(),
+ }
+ trigger_webhooks_async(
+ payload, event_type, webhooks, address, self.requestor
+ )
+
+ def address_created(self, address: "Address", previous_value: None) -> None:
+ if not self.active:
+ return previous_value
+ self._trigger_address_event(WebhookEventAsyncType.ADDRESS_CREATED, address)
+
+ def address_updated(self, address: "Address", previous_value: None) -> None:
+ if not self.active:
+ return previous_value
+ self._trigger_address_event(WebhookEventAsyncType.ADDRESS_UPDATED, address)
+
+ def address_deleted(self, address: "Address", previous_value: None) -> None:
+ if not self.active:
+ return previous_value
+ self._trigger_address_event(WebhookEventAsyncType.ADDRESS_DELETED, address)
+
def _trigger_app_event(self, event_type, app):
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 1647f28021..58992687b7 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
@@ -20,6 +20,27 @@ def subscription_webhook(webhook_app):
return fun
+def subscription_address_created_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.ADDRESS_CREATED, WebhookEventAsyncType.ADDRESS_CREATED
+ )
+
+
+def subscription_address_updated_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.ADDRESS_UPDATED, WebhookEventAsyncType.ADDRESS_UPDATED
+ )
+
+
+def subscription_address_deleted_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.ADDRESS_DELETED, WebhookEventAsyncType.ADDRESS_DELETED
+ )
+
+
@pytest.fixture
def subscription_app_installed_webhook(subscription_webhook):
return subscription_webhook(
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
index 432378ce2f..b4eb1ab06d 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
@@ -1,5 +1,53 @@
from .....graphql.tests.queries import fragments
+ADDRESS_CREATED = (
+ fragments.ADDRESS_DETAILS
+ + """
+ subscription{
+ event{
+ ...on AddressCreated{
+ address{
+ ...AddressDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+
+ADDRESS_UPDATED = (
+ fragments.ADDRESS_DETAILS
+ + """
+ subscription{
+ event{
+ ...on AddressUpdated{
+ address{
+ ...AddressDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+
+ADDRESS_DELETED = (
+ fragments.ADDRESS_DETAILS
+ + """
+ subscription{
+ event{
+ ...on AddressDeleted{
+ address{
+ ...AddressDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+
APP_INSTALLED = (
fragments.APP_DETAILS
+ """
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 d28b38ec92..d0b84a3d5c 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
@@ -16,6 +16,7 @@ from .....webhook.event_types import WebhookEventAsyncType
from ...tasks import create_deliveries_for_subscriptions, logger
from . import subscription_queries
from .payloads import (
+ generate_address_payload,
generate_app_payload,
generate_category_payload,
generate_collection_payload,
@@ -71,6 +72,57 @@ def test_subscription_query_with_meta(
assert deliveries[0].webhook == webhooks[0]
+def test_address_created(address, subscription_address_created_webhook):
+ # given
+ webhooks = [subscription_address_created_webhook]
+ event_type = WebhookEventAsyncType.ADDRESS_CREATED
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, address, webhooks)
+
+ # then
+ expected_payload = json.dumps({"address": generate_address_payload(address)})
+
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
+def test_address_updated(address, subscription_address_updated_webhook):
+ # given
+ webhooks = [subscription_address_updated_webhook]
+ event_type = WebhookEventAsyncType.ADDRESS_UPDATED
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, address, webhooks)
+
+ # then
+ expected_payload = json.dumps({"address": generate_address_payload(address)})
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
+def test_address_deleted(address, subscription_address_deleted_webhook):
+ # given
+ webhooks = [subscription_address_deleted_webhook]
+
+ id = address.id
+ address.delete()
+ address.id = id
+
+ event_type = WebhookEventAsyncType.ADDRESS_DELETED
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, address, webhooks)
+
+ # then
+ expected_payload = json.dumps({"address": generate_address_payload(address)})
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
def test_app_installed(app, subscription_app_installed_webhook):
# given
webhooks = [subscription_app_installed_webhook]
@@ -91,13 +143,13 @@ def test_app_updated(app, subscription_app_updated_webhook):
# given
webhooks = [subscription_app_updated_webhook]
event_type = WebhookEventAsyncType.APP_UPDATED
- gift_card_id = graphene.Node.to_global_id("App", app.id)
+ app_id = graphene.Node.to_global_id("App", app.id)
# when
deliveries = create_deliveries_for_subscriptions(event_type, app, webhooks)
# then
- expected_payload = generate_app_payload(app, gift_card_id)
+ expected_payload = generate_app_payload(app, app_id)
assert deliveries[0].payload.payload == expected_payload
assert len(deliveries) == len(webhooks)
assert deliveries[0].webhook == webhooks[0]
diff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.py
index f59369b28a..27fa586263 100644
--- a/saleor/webhook/event_types.py
+++ b/saleor/webhook/event_types.py
@@ -19,6 +19,10 @@ from ..core.permissions import (
class WebhookEventAsyncType:
ANY = "any_events"
+ ADDRESS_CREATED = "address_created"
+ ADDRESS_UPDATED = "address_updated"
+ ADDRESS_DELETED = "address_deleted"
+
APP_INSTALLED = "app_installed"
APP_UPDATED = "app_updated"
APP_DELETED = "app_deleted"
@@ -123,6 +127,9 @@ class WebhookEventAsyncType:
DISPLAY_LABELS = {
ANY: "Any events",
+ ADDRESS_CREATED: "Address created",
+ ADDRESS_UPDATED: "Address updated",
+ ADDRESS_DELETED: "Address deleted",
APP_INSTALLED: "App created",
APP_UPDATED: "App updated",
APP_DELETED: "App deleted",
@@ -203,6 +210,9 @@ class WebhookEventAsyncType:
CHOICES = [
(ANY, DISPLAY_LABELS[ANY]),
+ (ADDRESS_CREATED, DISPLAY_LABELS[ADDRESS_CREATED]),
+ (ADDRESS_UPDATED, DISPLAY_LABELS[ADDRESS_UPDATED]),
+ (ADDRESS_DELETED, DISPLAY_LABELS[ADDRESS_DELETED]),
(APP_INSTALLED, DISPLAY_LABELS[APP_INSTALLED]),
(APP_UPDATED, DISPLAY_LABELS[APP_UPDATED]),
(APP_DELETED, DISPLAY_LABELS[APP_DELETED]),
@@ -284,6 +294,9 @@ class WebhookEventAsyncType:
ALL = [event[0] for event in CHOICES]
PERMISSIONS = {
+ ADDRESS_CREATED: AccountPermissions.MANAGE_USERS,
+ ADDRESS_UPDATED: AccountPermissions.MANAGE_USERS,
+ ADDRESS_DELETED: AccountPermissions.MANAGE_USERS,
APP_INSTALLED: AppPermission.MANAGE_APPS,
APP_UPDATED: AppPermission.MANAGE_APPS,
APP_DELETED: AppPermission.MANAGE_APPS,
@@ -435,6 +448,9 @@ class WebhookEventSyncType:
SUBSCRIBABLE_EVENTS = [
+ WebhookEventAsyncType.ADDRESS_CREATED,
+ WebhookEventAsyncType.ADDRESS_UPDATED,
+ WebhookEventAsyncType.ADDRESS_DELETED,
WebhookEventAsyncType.APP_INSTALLED,
WebhookEventAsyncType.APP_UPDATED,
WebhookEventAsyncType.APP_DELETED,