Skip to content

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

Update tests to work with current plugins configuration

Mira Szewczykbc9525c5db7c
diff --git a/saleor/dashboard/forms.py b/saleor/dashboard/forms.py
index 6fc3c2675f..f05148b4a4 100644
--- a/saleor/dashboard/forms.py
+++ b/saleor/dashboard/forms.py
@@ -200,22 +200,12 @@ class PermissionMultipleChoiceField(forms.ModelMultipleChoiceField):
return obj.name
-class ConfigField(forms.Field):
- def __init__(self, *args, **kwargs):
- structure = kwargs.pop("structure")
- super().__init__(*args, **kwargs)
- self.label = structure["name"]
- self.name = structure["name"]
- self.help_text = structure["help_text"]
- self.initial = structure["value"]
-
-
-class ConfigBooleanField(forms.BooleanField, ConfigField):
+class ConfigBooleanField(forms.BooleanField):
def to_python(self, value):
return {"name": self.label, "value": super().to_python(value)}
-class ConfigCharField(forms.CharField, ConfigField):
+class ConfigCharField(forms.CharField):
def clean(self, value):
parsed_value = super().clean(value)
return {"name": self.label, "value": parsed_value}
diff --git a/saleor/dashboard/payment/forms.py b/saleor/dashboard/payment/forms.py
index 5a706eecd3..14485d3393 100644
--- a/saleor/dashboard/payment/forms.py
+++ b/saleor/dashboard/payment/forms.py
@@ -13,9 +13,16 @@ TYPE_TO_FIELD = {
}
-def create_custom_form_field(structure: Dict[str, str]) -> forms.Field:
+def create_custom_form_field(
+ config: Dict[str, str], structure: Dict[str, str]
+) -> forms.Field:
elem_type = structure["type"]
- return TYPE_TO_FIELD[elem_type](structure=structure)
+ elem_name = config["name"]
+ elem_value = config["value"]
+ elem_help_text = structure["help_text"]
+ return TYPE_TO_FIELD[elem_type](
+ initial=elem_value, help_text=elem_help_text, label=elem_name
+ )
class GatewayConfigurationForm(forms.ModelForm):
@@ -38,12 +45,15 @@ class GatewayConfigurationForm(forms.ModelForm):
self, current_configuration: PluginConfiguration
) -> Dict[str, forms.Field]:
parsed_fields = {}
- structure = current_configuration.configuration
- if structure is None:
+ configuration = current_configuration.configuration
+ if configuration is None:
return {}
- for elem in structure:
- slug = slugify(elem["name"])
- self.fields[slug] = create_custom_form_field(elem)
+ for elem in configuration:
+ name = elem["name"]
+ slug = slugify(name)
+ self.fields[slug] = create_custom_form_field(
+ elem, self.plugin.CONFIG_STRUCTURE[name]
+ )
return parsed_fields
def parse_values(self):
diff --git a/tests/dashboard/test_payment.py b/tests/dashboard/test_payment.py
index 7a205c84c2..b2e51b4c1b 100644
--- a/tests/dashboard/test_payment.py
+++ b/tests/dashboard/test_payment.py
@@ -35,11 +35,6 @@ def plugin_configuration(db):
return plugin_configuration
-def gateway_config_form():
- return GatewayConfigurationForm(BraintreeGatewayPlugin)
-
-
@pytest.fixture(autouse=True)
def enable_plugin(settings):
settings.PLUGINS = [
@@ -49,13 +44,7 @@ def enable_plugin(settings):
@pytest.fixture
def config_char_structure():
- return {
- "name": "Template path",
- "value": "order/payment/braintree.html",
- "type": "String",
- "help_text": "Location of django payment template for gateway.",
- "label": "Template path",
- }
+ return {"name": "Template path", "value": "order/payment/braintree.html"}
@pytest.fixture(autouse=True)
@@ -79,28 +68,30 @@ def form_data():
}
-def test_configuration_form_get_current_configuration(
- plugin_configuration, gateway_config_form
-):
+def test_configuration_form_get_current_configuration(plugin_configuration):
+ gateway_config_form = GatewayConfigurationForm(BraintreeGatewayPlugin)
assert gateway_config_form._get_current_configuration() == plugin_configuration
def test_configuration_form__create_field(config_char_structure):
- template_field = create_custom_form_field(config_char_structure)
+ field_name = config_char_structure["name"]
+ plugin_config_structure = BraintreeGatewayPlugin.CONFIG_STRUCTURE[field_name]
+ template_field = create_custom_form_field(
+ config_char_structure, plugin_config_structure
+ )
assert isinstance(template_field, ConfigCharField)
assert template_field.label == config_char_structure["name"]
assert template_field.initial == config_char_structure["value"]
+ assert template_field.help_text == plugin_config_structure["help_text"]
def test_config_boolean_field_returned_value():
- config_boolean_structure = {
- "name": "Require 3D secure",
- "value": True,
- "type": "Boolean",
- "help_text": "Determines if Saleor should enforce 3D secure during payment.",
- "label": "Require 3D secure",
- }
- boolean_field = ConfigBooleanField(structure=config_boolean_structure)
+ config_boolean_structure = {"name": "Require 3D secure", "value": True}
+ field_name = config_boolean_structure["name"]
+ boolean_field = ConfigBooleanField(
+ initial=True, label=field_name, help_text="help_text"
+ )
+ assert boolean_field.label == field_name
assert boolean_field.clean(True) == {
"name": config_boolean_structure["name"],
"value": True,
@@ -112,7 +103,11 @@ def test_config_boolean_field_returned_value():
def test_config_char_field_returned_value(config_char_structure):
- char_field = ConfigCharField(structure=config_char_structure)
+ field_name = config_char_structure["name"]
+ field_value = config_char_structure["value"]
+ char_field = ConfigCharField(
+ initial=field_value, label=field_name, help_text="Test text"
+ )
value = "1234466"
assert char_field.clean(value) == {
"name": config_char_structure["name"],