diff --git a/src/openforms/variables/tests/test_views.py b/src/openforms/variables/tests/test_views.py index 305d51aa66..d3c03d55cd 100644 --- a/src/openforms/variables/tests/test_views.py +++ b/src/openforms/variables/tests/test_views.py @@ -7,9 +7,11 @@ from openforms.accounts.tests.factories import StaffUserFactory, UserFactory from openforms.prefill.constants import IdentifierRoles +from openforms.registrations.base import BasePlugin +from openforms.registrations.registry import Registry as RegistrationRegistry from openforms.variables.base import BaseStaticVariable from openforms.variables.constants import FormVariableDataTypes -from openforms.variables.registry import Registry +from openforms.variables.registry import Registry as VariableRegistry class GetStaticVariablesViewTest(APITestCase): @@ -52,7 +54,7 @@ class DemoNow(BaseStaticVariable): def get_initial_value(self, *args, **kwargs): return "2021-07-16T21:15:00+00:00" - register = Registry() + register = VariableRegistry() register("now")(DemoNow) with patch( @@ -83,3 +85,94 @@ def get_initial_value(self, *args, **kwargs): }, data[0], ) + + +class GetRegistrationPluginVariablesViewTest(APITestCase): + def test_auth_required(self): + url = reverse( + "api:variables:registration", + ) + + response = self.client.get(url) + + self.assertEqual(status.HTTP_403_FORBIDDEN, response.status_code) + + def test_staff_required(self): + # add the permissions to verify we specifically check is_staff + user = UserFactory.create( + is_staff=False, user_permissions=["view_formvariable"] + ) + url = reverse( + "api:variables:registration", + ) + + self.client.force_authenticate(user=user) + response = self.client.get(url) + + self.assertEqual(status.HTTP_403_FORBIDDEN, response.status_code) + + def test_get_registration_plugin_variable(self): + user = StaffUserFactory.create(user_permissions=["change_form"]) + url = reverse( + "api:variables:registration", + ) + + self.client.force_authenticate(user=user) + + class DemoNow(BaseStaticVariable): + name = "Now" + data_type = FormVariableDataTypes.string + + def get_initial_value(self, *args, **kwargs): + return "demo initial value" + + variable_registry = VariableRegistry() + variable_registry("now")(DemoNow) + + class DemoRegistrationPlugin(BasePlugin): + verbose_name = "Demo verbose name" + + def register_submission(self, submission, options): + pass + + def get_variables_registry(self): + return variable_registry + + plugin_registry = RegistrationRegistry() + plugin_registry("demo")(DemoRegistrationPlugin) + + with patch( + "openforms.variables.service.registration_plugins_registry", + new=plugin_registry, + ): + response = self.client.get(url) + + self.assertEqual(status.HTTP_200_OK, response.status_code) + + data = response.data + + self.assertEqual(1, len(data)) + self.assertEqual( + { + "plugin_identifier": "demo", + "plugin_verbose_name": "Demo verbose name", + "plugin_variables": [ + { + "form": None, + "form_definition": None, + "name": "Now", + "key": "now", + "source": "", + "service_fetch_configuration": None, + "prefill_plugin": "", + "prefill_attribute": "", + "prefill_identifier_role": IdentifierRoles.main, + "data_type": FormVariableDataTypes.string, + "data_format": "", + "is_sensitive_data": False, + "initial_value": "demo initial value", + } + ], + }, + data[0], + ) diff --git a/src/openforms/variables/urls.py b/src/openforms/variables/urls.py index 45c9c67c84..7161165c82 100644 --- a/src/openforms/variables/urls.py +++ b/src/openforms/variables/urls.py @@ -1,6 +1,6 @@ from django.urls import path -from .api.views import StaticFormVariablesView +from .api.views import RegistrationPluginVariablesView, StaticFormVariablesView app_name = "variables" @@ -9,5 +9,10 @@ "static", StaticFormVariablesView.as_view(), name="static", - ) + ), + path( + "registration", + RegistrationPluginVariablesView.as_view(), + name="registration", + ), ]