Skip to content

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

Webhook - NOTIFY (#6773)

Maciej Korycinskid52b7a46a1e5
diff --git a/saleor/account/notifications.py b/saleor/account/notifications.py
index 53162a3661..c371ee4c3a 100644
--- a/saleor/account/notifications.py
+++ b/saleor/account/notifications.py
@@ -46,7 +46,7 @@ def send_account_confirmation(user, redirect_url, manager):
payload = {
"user": get_default_user_payload(user),
"recipient_email": user.email,
- "token": default_token_generator.make_token(user),
+ "token": token,
"confirm_url": confirm_url,
**get_site_context(),
}
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 451dd4a366..c4d8066065 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -5814,6 +5814,7 @@ enum WebhookEventTypeEnum {
CHECKOUT_CREATED
CHECKOUT_UPDATED
FULFILLMENT_CREATED
+ NOTIFY_USER
}
enum WebhookSampleEventTypeEnum {
@@ -5833,6 +5834,7 @@ enum WebhookSampleEventTypeEnum {
CHECKOUT_CREATED
CHECKOUT_UPDATED
FULFILLMENT_CREATED
+ NOTIFY_USER
}
type WebhookUpdate {
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index 7dbb758cff..290910f736 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -15,6 +15,7 @@ from .tasks import trigger_webhooks_for_event
if TYPE_CHECKING:
from ...account.models import User
from ...checkout.models import Checkout
+ from ...core.notify_events import NotifyEventType
from ...invoice.models import Invoice
from ...order.models import Fulfillment, Order
from ...product.models import Product
@@ -145,3 +146,9 @@ class WebhookPlugin(BasePlugin):
trigger_webhooks_for_event.delay(
WebhookEventType.CHECKOUT_UPADTED, checkout_data
)
+
+ def notify(self, event: "NotifyEventType", payload: dict, previous_value) -> Any:
+ if not self.active:
+ return previous_value
+ data = {"notify_event": event, "payload": payload}
+ trigger_webhooks_for_event.delay(WebhookEventType.NOTIFY_USER, data)
diff --git a/saleor/plugins/webhook/tests/test_webhook.py b/saleor/plugins/webhook/tests/test_webhook.py
index fe936dd5d8..2e57c71938 100644
--- a/saleor/plugins/webhook/tests/test_webhook.py
+++ b/saleor/plugins/webhook/tests/test_webhook.py
@@ -1,8 +1,18 @@
from unittest import mock
+from urllib.parse import urlencode
import pytest
+from django.contrib.auth.tokens import default_token_generator
+from freezegun import freeze_time
+from ....account.notifications import (
+ get_default_user_payload,
+ send_account_confirmation,
+)
from ....app.models import App
+from ....core.notifications import get_site_context
+from ....core.notify_events import NotifyEventType
+from ....core.utils.url import prepare_url
from ....webhook.event_types import WebhookEventType
from ....webhook.payloads import (
generate_checkout_payload,
@@ -243,3 +253,33 @@ def test_invoice_sent(mocked_webhook_trigger, settings, fulfilled_order):
mocked_webhook_trigger.assert_called_once_with(
WebhookEventType.INVOICE_SENT, expected_data
)
+
+
+@freeze_time("2020-03-18 12:00:00")
[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")
+def test_notify_user(mocked_webhook_trigger, settings, customer_user):
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ manager = get_plugins_manager()
+
+ redirect_url = "http://redirect.com/"
+ send_account_confirmation(customer_user, redirect_url, manager)
+
+ token = default_token_generator.make_token(customer_user)
+ params = urlencode({"email": customer_user.email, "token": token})
+ confirm_url = prepare_url(params, redirect_url)
+
+ payload = {
+ "user": get_default_user_payload(customer_user),
+ "recipient_email": customer_user.email,
+ "token": token,
+ "confirm_url": confirm_url,
+ **get_site_context(),
+ }
+
+ expected_data = {
+ "notify_event": NotifyEventType.ACCOUNT_CONFIRMATION,
+ "payload": payload,
+ }
+ mocked_webhook_trigger.assert_called_once_with(
+ WebhookEventType.NOTIFY_USER, expected_data
+ )
diff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.py
index 8137b86ee1..149a94814d 100644
--- a/saleor/webhook/event_types.py
+++ b/saleor/webhook/event_types.py
@@ -32,6 +32,8 @@ class WebhookEventType:
CHECKOUT_CREATED = "checkout_created"
CHECKOUT_UPADTED = "checkout_updated"
+ NOTIFY_USER = "notify_user"
+
DISPLAY_LABELS = {
ANY: "Any events",
ORDER_CREATED: "Order created",
@@ -51,6 +53,7 @@ class WebhookEventType:
CHECKOUT_CREATED: "Checkout created",
CHECKOUT_UPADTED: "Checkout updated",
FULFILLMENT_CREATED: "Fulfillment_created",
+ NOTIFY_USER: "Notify user",
}
CHOICES = [
@@ -72,6 +75,7 @@ class WebhookEventType:
(CHECKOUT_CREATED, DISPLAY_LABELS[CHECKOUT_CREATED]),
(CHECKOUT_UPADTED, DISPLAY_LABELS[CHECKOUT_UPADTED]),
(FULFILLMENT_CREATED, DISPLAY_LABELS[FULFILLMENT_CREATED]),
+ (NOTIFY_USER, DISPLAY_LABELS[NOTIFY_USER]),
]
PERMISSIONS = {
@@ -91,4 +95,5 @@ class WebhookEventType:
CHECKOUT_CREATED: CheckoutPermissions.MANAGE_CHECKOUTS,
CHECKOUT_UPADTED: CheckoutPermissions.MANAGE_CHECKOUTS,
FULFILLMENT_CREATED: OrderPermissions.MANAGE_ORDERS,
+ NOTIFY_USER: AccountPermissions.MANAGE_USERS,
}