Skip to content

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

Fix tests

Maciej Korycinski16ebb6413c02
diff --git a/saleor/plugins/sendgrid/tests/test_plugin.py b/saleor/plugins/sendgrid/tests/test_plugin.py
index 7a2aeafc18..fad23f93d7 100644
--- a/saleor/plugins/sendgrid/tests/test_plugin.py
+++ b/saleor/plugins/sendgrid/tests/test_plugin.py
@@ -1,3 +1,4 @@
+from dataclasses import asdict
from unittest.mock import MagicMock, patch
import pytest
@@ -5,91 +6,87 @@ from django.core.exceptions import ValidationError
from ....core.notify_events import AdminNotifyEvent, UserNotifyEvent
from ...models import PluginConfiguration
-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,
- }
+from ..plugin import EVENT_MAP
-@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
-):
+def test_get_event_map():
+ for event in UserNotifyEvent.CHOICES:
+ assert event in EVENT_MAP
+
+
+@patch("saleor.plugins.sendgrid.plugin.EVENT_MAP")
+def test_notify_when_plugin_disabled(mocked_event_map, sendgrid_email_plugin):
+ mocked_event_task = MagicMock()
+ event_map = {
+ UserNotifyEvent.ACCOUNT_PASSWORD_RESET: (
+ mocked_event_task,
+ "account_password_reset_template_id",
+ )
+ }
+ mocked_event_map.__getitem__.side_effect = event_map.__getitem__
+ mocked_event_map.get.side_effect = event_map.get
+
plugin = sendgrid_email_plugin(active=False)
plugin.notify(UserNotifyEvent.ACCOUNT_PASSWORD_RESET, {}, None)
- assert not mocked_get_event_to_task_map.called
+ assert not mocked_event_task.delay.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
-):
+@patch("saleor.plugins.sendgrid.plugin.EVENT_MAP")
+def test_notify_not_valid_event_type(mocked_event_map, sendgrid_email_plugin):
+ mocked_event_task = MagicMock()
+ event_map = {
+ UserNotifyEvent.ACCOUNT_PASSWORD_RESET: (
+ mocked_event_task,
+ "account_password_reset_template_id",
+ )
+ }
+ mocked_event_map.__getitem__.side_effect = event_map.__getitem__
+ mocked_event_map.get.side_effect = event_map.get
+
plugin = sendgrid_email_plugin(api_key="AB12", active=True)
plugin.notify(AdminNotifyEvent.CSV_EXPORT_FAILED, {}, None)
- assert not mocked_get_event_to_task_map.called
+ assert not mocked_event_task.delay.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):
+@patch("saleor.plugins.sendgrid.plugin.EVENT_MAP")
+def test_notify_missing_handler(mocked_event_map, sendgrid_email_plugin):
sample_payload = {"key_1": "value"}
- event_map = MagicMock()
- mocked_get_event_to_task_map.return_value = event_map
+ mocked_event_task = MagicMock()
+ event_map = {
+ UserNotifyEvent.ACCOUNT_CHANGE_EMAIL_REQUEST: (
+ mocked_event_task,
+ "account_password_reset_template_id",
+ )
+ }
+ mocked_event_map.__contains__.side_effect = event_map.__contains__
plugin = sendgrid_email_plugin(api_key="AB12", 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
+ assert mocked_event_map.__contains__.called
+ assert not mocked_event_task.delay.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
-):
+@patch("saleor.plugins.sendgrid.plugin.EVENT_MAP")
+def test_notify_missing_template_id(mocked_event_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
+ mocked_event_task = MagicMock()
+ event_map = {
+ UserNotifyEvent.ACCOUNT_PASSWORD_RESET: (
+ mocked_event_task,
+ "account_password_reset_template_id",
+ )
+ }
+ mocked_event_map.__getitem__.side_effect = event_map.__getitem__
+ mocked_event_map.__contains__.return_value = True
+ mocked_event_map.get.side_effect = event_map.get
plugin = sendgrid_email_plugin(
active=True, api_key="AB12", account_password_reset_template_id=None
@@ -97,17 +94,24 @@ def test_notify_missing_template_id(
plugin.notify(UserNotifyEvent.ACCOUNT_PASSWORD_RESET, sample_payload, None)
- assert mocked_get_event_to_task_map.called
- assert not event_map.__getitem__.called
+ assert mocked_event_map.get.called
+ assert not mocked_event_task.delay.called
-@patch("saleor.plugins.sendgrid.plugin.get_event_to_task_map")
-def test_notify(mocked_get_event_to_task_map, sendgrid_email_plugin):
+@patch("saleor.plugins.sendgrid.plugin.EVENT_MAP")
+def test_notify(mocked_event_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
+ mocked_event_task = MagicMock()
+ event_map = {
+ UserNotifyEvent.ACCOUNT_PASSWORD_RESET: (
+ mocked_event_task,
+ "account_password_reset_template_id",
+ )
+ }
+ mocked_event_map.__getitem__.side_effect = event_map.__getitem__
+ mocked_event_map.__contains__.return_value = True
+ mocked_event_map.get.side_effect = event_map.get
plugin = sendgrid_email_plugin(
active=True, api_key="AB12", account_password_reset_template_id="123"
@@ -115,7 +119,9 @@ def test_notify(mocked_get_event_to_task_map, sendgrid_email_plugin):
plugin.notify(UserNotifyEvent.ACCOUNT_PASSWORD_RESET, sample_payload, None)
- event_map.__getitem__.assert_called_once_with("account_password_reset")
+ mocked_event_task.delay.assert_called_once_with(
+ sample_payload, asdict(plugin.config)
+ )
def test_save_plugin_configuration_missing_api_key(