Skip to content

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

Add tests

Maciej Korycinski7fe5a1d09d2e
diff --git a/.isort.cfg b/.isort.cfg
index f8067be68d..52135d0f67 100644
--- a/.isort.cfg
+++ b/.isort.cfg
@@ -1,2 +1,2 @@
[settings]
-known_third_party = Adyen,PIL,authorizenet,babel,boto3,braintree,celery,dj_database_url,dj_email_url,django,django_cache_url,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,google,google_measurement_protocol,graphene,graphene_django,graphene_federation,graphql,graphql_relay,html2text,html_to_draftjs,i18naddress,jaeger_client,jwt,kombu,lxml,markdown,measurement,mptt,oauthlib,openpyxl,opentracing,petl,phonenumber_field,phonenumbers,pkg_resources,prices,promise,pybars,pytest,pythonjsonlogger,pytimeparse,pytz,razorpay,requests,sentry_sdk,storages,stripe,tqdm,urllib3,versatileimagefield,weasyprint
+known_third_party = Adyen,PIL,authorizenet,babel,boto3,braintree,celery,dj_database_url,dj_email_url,django,django_cache_url,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,google,google_measurement_protocol,graphene,graphene_django,graphene_federation,graphql,graphql_relay,html2text,html_to_draftjs,i18naddress,jaeger_client,jwt,kombu,lxml,markdown,measurement,mptt,oauthlib,openpyxl,opentracing,petl,phonenumber_field,phonenumbers,pkg_resources,prices,promise,pybars,pytest,pythonjsonlogger,pytimeparse,pytz,razorpay,requests,sendgrid,sentry_sdk,storages,stripe,tqdm,urllib3,versatileimagefield,weasyprint
diff --git a/saleor/plugins/sendgrid/plugin.py b/saleor/plugins/sendgrid/plugin.py
index 0780afb138..cbe3f3629a 100644
--- a/saleor/plugins/sendgrid/plugin.py
+++ b/saleor/plugins/sendgrid/plugin.py
@@ -229,5 +229,3 @@ class SendgridEmailPlugin(BasePlugin):
return previous_value
event_to_task_map[event].delay(payload, asdict(self.config))
-
- # FIXME validation of the provided plugin data
diff --git a/saleor/plugins/sendgrid/tasks.py b/saleor/plugins/sendgrid/tasks.py
index 9d84cf24d1..d6c5859c86 100644
--- a/saleor/plugins/sendgrid/tasks.py
+++ b/saleor/plugins/sendgrid/tasks.py
@@ -28,7 +28,6 @@ def send_email(configuration: SendgridConfiguration, template_id, payload):
retry_kwargs={"max_retries": 5},
)
def send_account_confirmation_email_task(payload: dict, configuration: dict):
- print(configuration)
configuration = SendgridConfiguration(**configuration)
send_email(
configuration=configuration,
diff --git a/saleor/plugins/sendgrid/tests/__init__.py b/saleor/plugins/sendgrid/tests/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/plugins/sendgrid/tests/conftest.py b/saleor/plugins/sendgrid/tests/conftest.py
new file mode 100644
index 0000000000..ad3e26c332
--- /dev/null
+++ b/saleor/plugins/sendgrid/tests/conftest.py
@@ -0,0 +1,102 @@
+import pytest
+
+from ...manager import get_plugins_manager
+from ..plugin import SendgridEmailPlugin
+
+
+def sendgrid_email_plugin(settings):
+ def fun(
+ active=True,
+ sender_name=None,
+ sender_address=None,
+ account_confirmation_template_id=None,
+ account_set_customer_password_template_id=None,
+ account_delete_template_id=None,
+ account_change_email_confirm_template_id=None,
+ account_change_email_request_template_id=None,
+ account_password_reset_template_id=None,
+ invoice_ready_template_id=None,
+ order_confirmation_template_id=None,
+ order_confirmed_template_id=None,
+ order_fulfillment_confirmation_template_id=None,
+ order_fulfillment_update_template_id=None,
+ order_payment_confirmation_template_id=None,
+ order_canceled_template_id=None,
+ order_refund_confirmation_template_id=None,
+ api_key=None,
+ ):
+
+ settings.PLUGINS = ["saleor.plugins.sendgrid.plugin.SendgridEmailPlugin"]
+ manager = get_plugins_manager()
+ manager.save_plugin_configuration(
+ SendgridEmailPlugin.PLUGIN_ID,
+ {
+ "active": active,
+ "configuration": [
+ {"name": "sender_name", "value": sender_name},
+ {"name": "sender_address", "value": sender_address},
+ {
+ "name": "account_confirmation_template_id",
+ "value": account_confirmation_template_id,
+ },
+ {
+ "name": "account_set_customer_password_template_id",
+ "value": account_set_customer_password_template_id,
+ },
+ {
+ "name": "account_delete_template_id",
+ "value": account_delete_template_id,
+ },
+ {
+ "name": "account_change_email_confirm_template_id",
+ "value": account_change_email_confirm_template_id,
+ },
+ {
+ "name": "account_change_email_request_template_id",
+ "value": account_change_email_request_template_id,
+ },
+ {
+ "name": "account_password_reset_template_id",
+ "value": account_password_reset_template_id,
+ },
+ {
+ "name": "invoice_ready_template_id",
+ "value": invoice_ready_template_id,
+ },
+ {
+ "name": "order_confirmation_template_id",
+ "value": order_confirmation_template_id,
+ },
+ {
+ "name": "order_confirmed_template_id",
+ "value": order_confirmed_template_id,
+ },
+ {
+ "name": "order_fulfillment_confirmation_template_id",
+ "value": order_fulfillment_confirmation_template_id,
+ },
+ {
+ "name": "order_fulfillment_update_template_id",
+ "value": order_fulfillment_update_template_id,
+ },
+ {
+ "name": "order_payment_confirmation_template_id",
+ "value": order_payment_confirmation_template_id,
+ },
+ {
+ "name": "order_canceled_template_id",
+ "value": order_canceled_template_id,
+ },
+ {
+ "name": "order_refund_confirmation_template_id",
+ "value": order_refund_confirmation_template_id,
+ },
+ {"name": "api_key", "value": api_key},
+ ],
+ },
+ )
+ manager = get_plugins_manager()
+ return manager.plugins[0]
+
+ return fun
diff --git a/saleor/plugins/sendgrid/tests/test_plugin.py b/saleor/plugins/sendgrid/tests/test_plugin.py
new file mode 100644
index 0000000000..b0819f93d0
--- /dev/null
+++ b/saleor/plugins/sendgrid/tests/test_plugin.py
@@ -0,0 +1,112 @@
+from unittest.mock import MagicMock, patch
+
+from ....core.notify_events import AdminNotifyEvent, UserNotifyEvent
+from .. import tasks
+from ..plugin import get_event_to_task_map
+
+
+def test_get_event_to_task_map():
+ assert get_event_to_task_map() == {
+ UserNotifyEvent.ACCOUNT_CONFIRMATION: (
+ tasks.send_account_confirmation_email_task
+ ),
+ UserNotifyEvent.ACCOUNT_SET_CUSTOMER_PASSWORD: (
+ tasks.send_set_user_password_email_task
+ ),
+ UserNotifyEvent.ACCOUNT_DELETE: (
+ tasks.send_account_delete_confirmation_email_task
+ ),
+ UserNotifyEvent.ACCOUNT_CHANGE_EMAIL_CONFIRM: (
+ tasks.send_user_change_email_notification_task
+ ),
+ UserNotifyEvent.ACCOUNT_CHANGE_EMAIL_REQUEST: (
+ tasks.send_request_email_change_email_task
+ ),
+ UserNotifyEvent.ACCOUNT_PASSWORD_RESET: tasks.send_password_reset_email_task,
+ UserNotifyEvent.INVOICE_READY: tasks.send_invoice_email_task,
+ UserNotifyEvent.ORDER_CONFIRMATION: tasks.send_order_confirmation_email_task,
+ UserNotifyEvent.ORDER_CONFIRMED: tasks.send_order_confirmed_email_task,
+ UserNotifyEvent.ORDER_FULFILLMENT_CONFIRMATION: (
+ tasks.send_fulfillment_confirmation_email_task
+ ),
+ UserNotifyEvent.ORDER_FULFILLMENT_UPDATE: (
+ tasks.send_fulfillment_update_email_task
+ ),
+ UserNotifyEvent.ORDER_PAYMENT_CONFIRMATION: (
+ tasks.send_payment_confirmation_email_task
+ ),
+ UserNotifyEvent.ORDER_CANCELED: tasks.send_order_canceled_email_task,
+ UserNotifyEvent.ORDER_REFUND_CONFIRMATION: tasks.send_order_refund_email_task,
+ }
+
+
+@patch("saleor.plugins.sendgrid.plugin.get_event_to_task_map")
+def test_notify_when_plugin_disabled(
+ mocked_get_event_to_task_map, sendgrid_email_plugin
+):
+ plugin = sendgrid_email_plugin(active=False)
+
+ plugin.notify(UserNotifyEvent.ACCOUNT_PASSWORD_RESET, {}, None)
+
+ assert not mocked_get_event_to_task_map.called
+
+
+@patch("saleor.plugins.sendgrid.plugin.get_event_to_task_map")
+def test_notify_not_valid_event_type(
+ mocked_get_event_to_task_map, sendgrid_email_plugin
+):
+ plugin = sendgrid_email_plugin(active=True)
+
+ plugin.notify(AdminNotifyEvent.CSV_EXPORT_FAILED, {}, None)
+
+ assert not mocked_get_event_to_task_map.called
+
+
+@patch("saleor.plugins.sendgrid.plugin.get_event_to_task_map")
+def test_notify_missing_handler(mocked_get_event_to_task_map, sendgrid_email_plugin):
+ sample_payload = {"key_1": "value"}
+
+ event_map = MagicMock()
+ mocked_get_event_to_task_map.return_value = event_map
+
+ plugin = sendgrid_email_plugin(active=True)
+
+ plugin.notify(UserNotifyEvent.ACCOUNT_PASSWORD_RESET, sample_payload, None)
+
+ assert mocked_get_event_to_task_map.called
+ assert not event_map.__getitem__.called
+
+
+@patch("saleor.plugins.sendgrid.plugin.get_event_to_task_map")
+def test_notify_missing_template_id(
+ mocked_get_event_to_task_map, sendgrid_email_plugin
+):
+ sample_payload = {"key_1": "value"}
+
+ event_map = MagicMock()
+ event_map.__contains__.return_value = True
+ mocked_get_event_to_task_map.return_value = event_map
+
+ plugin = sendgrid_email_plugin(active=True, account_password_reset_template_id=None)
+
+ plugin.notify(UserNotifyEvent.ACCOUNT_PASSWORD_RESET, sample_payload, None)
+
+ assert mocked_get_event_to_task_map.called
+ assert not event_map.__getitem__.called
+
+
+@patch("saleor.plugins.sendgrid.plugin.get_event_to_task_map")
+def test_notify(mocked_get_event_to_task_map, sendgrid_email_plugin):
+ sample_payload = {"key_1": "value"}
+
+ event_map = MagicMock()
+ event_map.__contains__.return_value = True
+ mocked_get_event_to_task_map.return_value = event_map
+
+ plugin = sendgrid_email_plugin(
+ active=True, account_password_reset_template_id="123"
+ )
+
+ plugin.notify(UserNotifyEvent.ACCOUNT_PASSWORD_RESET, sample_payload, None)
+
+ event_map.__getitem__.assert_called_once_with("account_password_reset")
diff --git a/saleor/plugins/sendgrid/tests/test_tasks.py b/saleor/plugins/sendgrid/tests/test_tasks.py
new file mode 100644
index 0000000000..97f0f7e91d
--- /dev/null
+++ b/saleor/plugins/sendgrid/tests/test_tasks.py
@@ -0,0 +1,586 @@
+from dataclasses import asdict
+from unittest.mock import Mock, patch
+
+import pytest
+
+from ....account import CustomerEvents
+from ....account.models import CustomerEvent
+from ....account.notifications import get_default_user_payload
+from ....invoice import InvoiceEvents
+from ....invoice.models import Invoice, InvoiceEvent
+from ....order import OrderEvents, OrderEventsEmails
+from ....order.models import OrderEvent
+from ....order.notifications import (
+ get_default_fulfillment_payload,
+ get_default_order_payload,
+)
+from ..tasks import (
+ send_account_confirmation_email_task,
+ send_account_delete_confirmation_email_task,
+ send_email,
+ send_fulfillment_confirmation_email_task,
+ send_fulfillment_update_email_task,
+ send_invoice_email_task,
+ send_order_canceled_email_task,
+ send_order_confirmation_email_task,
+ send_order_confirmed_email_task,
+ send_order_refund_email_task,
+ send_password_reset_email_task,
+ send_payment_confirmation_email_task,
+ send_request_email_change_email_task,
+ send_set_user_password_email_task,
+ send_user_change_email_notification_task,
+)
+
+
+def sample_payload(customer_user):
+ token = "token123"
+ return {
+ "user": get_default_user_payload(customer_user),
+ "recipient_email": "[email protected]",
+ "token": token,
+ "reset_url": f"http://localhost:8000/redirect{token}",
+ "domain": "localhost:8000",
+ "site_name": "Saleor",
+ }
+
+
+@patch("saleor.plugins.sendgrid.tasks.Mail")
+@patch("saleor.plugins.sendgrid.tasks.SendGridAPIClient.send")
+def test_send_email(
+ mocked_api_client, mocked_mail, sendgrid_email_plugin, sample_payload
+):
+ plugin = sendgrid_email_plugin(
+ sender_name="Sander Name",
+ sender_address="[email protected]",
+ api_key="123",
+ )
+ template_id = "ABC"
+ recipient_email = sample_payload["recipient_email"]
+
+ config = plugin.config
+
+ mock_message = Mock()
+ mocked_mail.return_value = mock_message
+
+ send_email(config, template_id=template_id, payload=sample_payload)
+
+ mocked_mail.assert_called_with(
+ from_email=(config.sender_address, config.sender_name),
+ to_emails=recipient_email,
+ )
+
+ mock_message.dynamic_template_data = sample_payload
+ mock_message.template_id = template_id
+
+ mocked_api_client.assert_called_with(mock_message)
+
+
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_account_confirmation_email_task(
+ mocked_send_email, customer_user, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+ recipient_email = "[email protected]"
+ token = "token123"
+ payload = {
+ "user": get_default_user_payload(customer_user),
+ "recipient_email": recipient_email,
+ "token": token,
+ "reset_url": f"http://localhost:8000/redirect{token}",
+ "domain": "localhost:8000",
+ "site_name": "Saleor",
+ }
+
+ plugin = sendgrid_email_plugin(account_confirmation_template_id=template_id)
+
+ send_account_confirmation_email_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+
+
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_password_reset_email_task(
+ mocked_send_email, customer_user, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+ recipient_email = "[email protected]"
+ token = "token123"
+ payload = {
+ "user": get_default_user_payload(customer_user),
+ "recipient_email": recipient_email,
+ "token": token,
+ "reset_url": f"http://localhost:8000/redirect{token}",
+ "domain": "localhost:8000",
+ "site_name": "Saleor",
+ }
+
+ plugin = sendgrid_email_plugin(
+ api_key="A12",
+ account_password_reset_template_id=template_id,
+ sender_name="Sander Name",
+ sender_address="[email protected]",
+ )
+
+ send_password_reset_email_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+ event = CustomerEvent.objects.get()
+ assert event.type == CustomerEvents.PASSWORD_RESET_LINK_SENT
+ assert event.user == customer_user
+
+
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_request_email_change_email_task(
+ mocked_send_email, customer_user, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+ token = "token123"
+ recipient_email = "[email protected]"
+ payload = {
+ "user": get_default_user_payload(customer_user),
+ "recipient_email": recipient_email,
+ "token": token,
+ "redirect_url": f"http://localhost:8000/redirect{token}",
+ "old_email": "[email protected]",
+ "new_email": "[email protected]",
+ "site_name": "Saleor",
+ "domain": "localhost:8000",
+ }
+
+ plugin = sendgrid_email_plugin(
+ api_key="A12",
+ account_change_email_request_template_id=template_id,
+ sender_name="Sander Name",
+ sender_address="[email protected]",
+ )
+
+ send_request_email_change_email_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+ event = CustomerEvent.objects.get()
+ assert event.type == CustomerEvents.EMAIL_CHANGE_REQUEST
+ assert event.user == customer_user
+ assert event.parameters == {
+ "old_email": payload["old_email"],
+ "new_email": payload["recipient_email"],
+ }
+
+
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_user_change_email_notification_task(
+ mocked_send_email, customer_user, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+
+ recipient_email = "[email protected]"
+ payload = {
+ "user": get_default_user_payload(customer_user),
+ "recipient_email": recipient_email,
+ "site_name": "Saleor",
+ "domain": "localhost:8000",
+ "old_email": "[email protected]",
+ "new_email": recipient_email,
+ }
+
+ plugin = sendgrid_email_plugin(
+ api_key="A12",
+ account_change_email_confirm_template_id=template_id,
+ sender_name="Sander Name",
+ sender_address="[email protected]",
+ )
+
+ send_user_change_email_notification_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+ event = CustomerEvent.objects.get()
+ assert event.type == CustomerEvents.EMAIL_CHANGED
+ assert event.user == customer_user
+ assert event.parameters == {
+ "old_email": payload["old_email"],
+ "new_email": payload["recipient_email"],
+ }
+
+
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_account_delete_confirmation_email_task(
+ mocked_send_email, customer_user, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+
+ recipient_email = "[email protected]"
+ token = "token123"
+ payload = {
+ "user": get_default_user_payload(customer_user),
+ "recipient_email": recipient_email,
+ "token": token,
+ "delete_url": f"http://localhost:8000/redirect{token}",
+ "site_name": "Saleor",
+ "domain": "localhost:8000",
+ }
+
+ plugin = sendgrid_email_plugin(
+ api_key="A12",
+ account_delete_template_id=template_id,
+ sender_name="Sander Name",
+ sender_address="[email protected]",
+ )
+
+ send_account_delete_confirmation_email_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+
+
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_set_user_password_email_task(
+ mocked_send_email, customer_user, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+
+ recipient_email = "[email protected]"
+ token = "token123"
+ payload = {
+ "user": get_default_user_payload(customer_user),
+ "recipient_email": recipient_email,
+ "token": token,
+ "password_set_url": f"http://localhost:8000/redirect{token}",
+ "site_name": "Saleor",
+ "domain": "localhost:8000",
+ }
+
+ plugin = sendgrid_email_plugin(
+ api_key="A12",
+ account_set_customer_password_template_id=template_id,
+ sender_name="Sander Name",
+ sender_address="[email protected]",
+ )
+
+ send_set_user_password_email_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+
+
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_invoice_email_task(
+ mocked_send_email, staff_user, order, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+
+ invoice = Invoice.objects.create(order=order)
+ recipient_email = "[email protected]"
+ payload = {
+ "invoice": {
+ "id": invoice.id,
+ "order_id": order.id,
+ "number": 999,
+ "download_url": "http://localhost:8000/download",
+ },
+ "recipient_email": recipient_email,
+ "site_name": "Saleor",
+ "domain": "localhost:8000",
+ "requester_user_id": staff_user.id,
+ }
+
+ plugin = sendgrid_email_plugin(
+ api_key="A12",
+ invoice_ready_template_id=template_id,
+ sender_name="Sander Name",
+ sender_address="[email protected]",
+ )
+
+ send_invoice_email_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+ invoice_event = InvoiceEvent.objects.get()
+ assert invoice_event.type == InvoiceEvents.SENT
+ assert invoice_event.user == staff_user
+
+ order_event = OrderEvent.objects.get()
+ assert order_event.type == OrderEvents.INVOICE_SENT
+ assert order_event.user == staff_user
+
+
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_order_confirmation_email_task(
+ mocked_send_email, staff_user, order, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+
+ recipient_email = "[email protected]"
+ payload = {
+ "order": get_default_order_payload(order, "http://localhost:8000/redirect"),
+ "recipient_email": recipient_email,
+ "site_name": "Saleor",
+ "domain": "localhost:8000",
+ }
+
+ plugin = sendgrid_email_plugin(
+ api_key="A12",
+ order_confirmation_template_id=template_id,
+ sender_name="Sander Name",
+ sender_address="[email protected]",
+ )
+
+ send_order_confirmation_email_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+
+ order_event = OrderEvent.objects.get()
+ assert order_event.type == OrderEvents.EMAIL_SENT
+ assert order_event.parameters == {
+ "email": recipient_email,
+ "email_type": OrderEventsEmails.ORDER_CONFIRMATION,
+ }
+
+
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_fulfillment_confirmation_email_task(
+ mocked_send_email, staff_user, fulfillment, order, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+
+ payload = get_default_fulfillment_payload(order, fulfillment)
+ payload["requester_user_id"] = staff_user.pk
+
+ plugin = sendgrid_email_plugin(
+ api_key="A12",
+ order_fulfillment_confirmation_template_id=template_id,
+ sender_name="Sander Name",
+ sender_address="[email protected]",
+ )
+
+ send_fulfillment_confirmation_email_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+
+ order_event = OrderEvent.objects.get()
+ assert order_event.type == OrderEvents.EMAIL_SENT
+ assert order_event.user == staff_user
+ assert order_event.parameters == {
+ "email": order.user_email,
+ "email_type": OrderEventsEmails.FULFILLMENT,
+ }
+
+
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_fulfillment_update_email_task(
+ mocked_send_email, staff_user, fulfillment, order, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+
+ payload = get_default_fulfillment_payload(order, fulfillment)
+
+ plugin = sendgrid_email_plugin(
+ api_key="A12",
+ order_fulfillment_update_template_id=template_id,
+ sender_name="Sander Name",
+ sender_address="[email protected]",
+ )
+
+ send_fulfillment_update_email_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+
+
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_payment_confirmation_email_task(
+ mocked_send_email, payment_dummy, staff_user, order, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+
+ recipient_email = "[email protected]"
+ payload = {
+ "order": get_default_order_payload(order, "http://localhost:8000/redirect"),
+ "recipient_email": recipient_email,
+ "payment": {
+ "created": payment_dummy.created,
+ "modified": payment_dummy.modified,
+ "charge_status": payment_dummy.charge_status,
+ "total": payment_dummy.total,
+ "captured_amount": payment_dummy.captured_amount,
+ "currency": payment_dummy.currency,
+ },
+ "site_name": "Saleor",
+ "domain": "localhost:8000",
+ }
+
+ plugin = sendgrid_email_plugin(
+ api_key="A12",
+ order_payment_confirmation_template_id=template_id,
+ sender_name="Sander Name",
+ sender_address="[email protected]",
+ )
+
+ send_payment_confirmation_email_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+
+ order_event = OrderEvent.objects.get()
+ assert order_event.type == OrderEvents.EMAIL_SENT
+ assert order_event.parameters == {
+ "email": recipient_email,
+ "email_type": OrderEventsEmails.PAYMENT,
+ }
+
+
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_order_canceled_email_task(
+ mocked_send_email, staff_user, order, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+
+ recipient_email = "[email protected]"
+ payload = {
+ "order": get_default_order_payload(order, "http://localhost:8000/redirect"),
+ "recipient_email": recipient_email,
+ "site_name": "Saleor",
+ "domain": "localhost:8000",
+ "requester_user_id": staff_user.pk,
+ }
+
+ plugin = sendgrid_email_plugin(
+ api_key="A12",
+ order_canceled_template_id=template_id,
+ sender_name="Sander Name",
+ sender_address="[email protected]",
+ )
+
+ send_order_canceled_email_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+
+ order_event = OrderEvent.objects.get()
+ assert order_event.type == OrderEvents.EMAIL_SENT
+ assert order_event.parameters == {
+ "email": recipient_email,
+ "email_type": OrderEventsEmails.ORDER_CANCEL,
+ }
+
+
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_order_refund_email_task(
+ mocked_send_email, staff_user, order, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+
+ recipient_email = "[email protected]"
+ payload = {
+ "order": get_default_order_payload(order, "http://localhost:8000/redirect"),
+ "recipient_email": recipient_email,
+ "amount": order.total_gross_amount,
+ "currency": order.currency,
+ "site_name": "Saleor",
+ "domain": "localhost:8000",
+ "requester_user_id": staff_user.pk,
+ }
+
+ plugin = sendgrid_email_plugin(
+ api_key="A12",
+ order_refund_confirmation_template_id=template_id,
+ sender_name="Sander Name",
+ sender_address="[email protected]",
+ )
+
+ send_order_refund_email_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+
+ order_event = OrderEvent.objects.get()
+ assert order_event.type == OrderEvents.EMAIL_SENT
+ assert order_event.parameters == {
+ "email": recipient_email,
+ "email_type": OrderEventsEmails.ORDER_REFUND,
+ }
+
+
+@patch("saleor.plugins.sendgrid.tasks.send_email")
+def test_send_order_confirmed_email_task(
+ mocked_send_email, staff_user, order, sendgrid_email_plugin
+):
+ template_id = "ABC1"
+
+ recipient_email = "[email protected]"
+ payload = {
+ "order": get_default_order_payload(order, "http://localhost:8000/redirect"),
+ "recipient_email": recipient_email,
+ "amount": order.total_gross_amount,
+ "currency": order.currency,
+ "site_name": "Saleor",
+ "domain": "localhost:8000",
+ "requester_user_id": staff_user.pk,
+ }
+
+ plugin = sendgrid_email_plugin(
+ api_key="A12",
+ order_confirmed_template_id=template_id,
+ sender_name="Sander Name",
+ sender_address="[email protected]",
+ )
+
+ send_order_confirmed_email_task(payload, asdict(plugin.config))
+
+ mocked_send_email.assert_called_with(
+ configuration=plugin.config,
+ template_id=template_id,
+ payload=payload,
+ )
+
+ order_event = OrderEvent.objects.get()
+ assert order_event.type == OrderEvents.EMAIL_SENT
+ assert order_event.parameters == {
+ "email": recipient_email,
+ "email_type": OrderEventsEmails.CONFIRMED,
+ }