Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Saleor 2831/3.1/pluginsmanager call action only active plugin (#8109)
L3str4ngeb9fa4b1c9ad8
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.pyindex faad050e91..038e8728ba 100644--- a/saleor/plugins/manager.py+++ b/saleor/plugins/manager.py@@ -117,9 +117,9 @@ class PluginsManager(PaymentInterface):channel_slug: Optional[str] = None,**kwargs):- """Try to run a method with the given name on each declared plugin."""+ """Try to run a method with the given name on each declared active plugin."""value = default_value- plugins = self.get_plugins(channel_slug=channel_slug)+ plugins = self.get_plugins(channel_slug=channel_slug, active_only=True)for plugin in plugins:value = self.__run_method_on_single_plugin(plugin, method_name, value, *args, **kwargsdiff --git a/saleor/plugins/tests/fixtures.py b/saleor/plugins/tests/fixtures.pyindex 86043eb181..4b7ccf870a 100644--- a/saleor/plugins/tests/fixtures.py+++ b/saleor/plugins/tests/fixtures.py@@ -5,8 +5,14 @@ from django_prices_vatlayer.models import VATfrom django_prices_vatlayer.utils import get_tax_for_ratefrom ..base_plugin import ConfigurationTypeField+from ..manager import PluginsManagerfrom ..models import PluginConfiguration-from .sample_plugins import ChannelPluginSample, PluginInactive, PluginSample+from .sample_plugins import (+ ALL_PLUGINS,+ ChannelPluginSample,+ PluginInactive,+ PluginSample,+)@pytest.fixture@@ -134,3 +140,14 @@ def vatlayer(db, tax_rates, taxes, setup_vatlayer):}VAT.objects.create(country_code="DE", data=tax_rates_2)return taxes+++def plugins_manager():+ return PluginsManager(plugins=[])+++def all_plugins_manager():+ plugins_as_module_paths = [p.__module__ + "." + p.__name__ for p in ALL_PLUGINS]+ return PluginsManager(plugins=plugins_as_module_paths)diff --git a/saleor/plugins/tests/sample_plugins.py b/saleor/plugins/tests/sample_plugins.pyindex 4ad35df9e1..14ab2ed62d 100644--- a/saleor/plugins/tests/sample_plugins.py+++ b/saleor/plugins/tests/sample_plugins.py@@ -221,6 +221,9 @@ class PluginSample(BasePlugin):def get_order_shipping_tax_rate(self, order: "Order", previous_value: Decimal):return Decimal("0.080").quantize(Decimal(".01"))+ def sample_not_implemented(self, previous_value):+ return NotImplemented+class ChannelPluginSample(PluginSample):PLUGIN_ID = "channel.plugin.sample"@@ -321,3 +324,19 @@ class InactivePaymentGateway(BasePlugin):def process_payment(self, payment_information, previous_value):pass+++ACTIVE_PLUGINS = (+ ChannelPluginSample,+ ActivePaymentGateway,+ ActivePlugin,+ ActiveDummyPaymentGateway,+)++INACTIVE_PLUGINS = (+ InactivePaymentGateway,+ PluginInactive,+ InactiveChannelPluginSample,+)++ALL_PLUGINS = ACTIVE_PLUGINS + INACTIVE_PLUGINSdiff --git a/saleor/plugins/tests/test_manager.py b/saleor/plugins/tests/test_manager.pyindex f364b77bfb..11d02bbb6f 100644--- a/saleor/plugins/tests/test_manager.py+++ b/saleor/plugins/tests/test_manager.py@@ -1,5 +1,6 @@import jsonfrom decimal import Decimal+from unittest import mockimport pytestfrom django.http import HttpResponseNotFound, JsonResponse@@ -15,6 +16,8 @@ from ..base_plugin import ExternalAccessTokensfrom ..manager import PluginsManager, get_plugins_managerfrom ..models import PluginConfigurationfrom ..tests.sample_plugins import (+ ACTIVE_PLUGINS,+ ALL_PLUGINS,ActiveDummyPaymentGateway,ActivePaymentGateway,ChannelPluginSample,@@ -875,3 +878,101 @@ def test_list_external_authentications_active_only(channel_USD):"id": PluginSample.PLUGIN_ID,"name": PluginSample.PLUGIN_NAME,} in external_auths+++def test_run_method_on_plugins_default_value(plugins_manager):+ default_value = "default"+ value = plugins_manager._PluginsManager__run_method_on_plugins(+ method_name="test_method",+ default_value=default_value,+ )++ assert value == default_value+++def test_run_method_on_plugins_default_value_when_not_existing_method_is_called(+ channel_USD, all_plugins_manager+):+ default_value = "default"+ value = all_plugins_manager._PluginsManager__run_method_on_plugins(+ method_name="test_method",+ default_value=default_value,+ )++ assert value == default_value+++def test_run_method_on_plugins_value_overridden_by_plugin_method(+ channel_USD, all_plugins_manager+):+ expected = ActiveDummyPaymentGateway.SUPPORTED_CURRENCIES # last active plugin+ value = all_plugins_manager._PluginsManager__run_method_on_plugins(+ method_name="get_supported_currencies",+ default_value="default_value",+ )++ assert value == expected+++ "saleor.plugins.manager.PluginsManager._PluginsManager__run_method_on_single_plugin"+)+def test_run_method_on_plugins_only_on_active_ones(+ mocked_method, channel_USD, all_plugins_manager+):+ all_plugins_manager._PluginsManager__run_method_on_plugins(+ method_name="test_method_name",+ default_value="default_value",+ )+ active_plugins_count = len(ACTIVE_PLUGINS)++ assert len(all_plugins_manager.all_plugins) == len(ALL_PLUGINS)+ assert (+ len([p for p in all_plugins_manager.all_plugins if p.active])+ == active_plugins_count+ )+ assert mocked_method.call_count == active_plugins_count++ called_plugins_id = [arg.args[0].PLUGIN_ID for arg in mocked_method.call_args_list]+ expected_active_plugins_id = [p.PLUGIN_ID for p in ACTIVE_PLUGINS]++ assert called_plugins_id == expected_active_plugins_id+++def test_run_method_on_single_plugin_method_does_not_exist(plugins_manager):+ default_value = "default_value"+ method_name = "method_does_not_exist"+ plugin = ActivePaymentGateway++ assert (+ plugins_manager._PluginsManager__run_method_on_single_plugin(+ plugin, method_name, default_value+ )+ == default_value+ )+++def test_run_method_on_single_plugin_method_not_implemented(plugins_manager):+ default_value = "default_value"+ plugin = ChannelPluginSample(configuration=None, active=True)+ method_name = ChannelPluginSample.sample_not_implemented.__name__++ assert (+ plugins_manager._PluginsManager__run_method_on_single_plugin(+ plugin, method_name, default_value+ )+ == default_value+ )+++def test_run_method_on_single_plugin_valid_response(plugins_manager):+ default_value = "default_value"+ plugin = ActiveDummyPaymentGateway(configuration=None, active=True)+ method_name = ActivePaymentGateway.get_supported_currencies.__name__++ assert (+ plugins_manager._PluginsManager__run_method_on_single_plugin(+ plugin, method_name, default_value+ )+ == plugin.SUPPORTED_CURRENCIES+ )