Skip to content

Commit

Permalink
[#3688] Add test for API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Mar 13, 2024
1 parent 01af7c6 commit 8db0efa
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
97 changes: 95 additions & 2 deletions src/openforms/variables/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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],
)
9 changes: 7 additions & 2 deletions src/openforms/variables/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import path

from .api.views import StaticFormVariablesView
from .api.views import RegistrationPluginVariablesView, StaticFormVariablesView

app_name = "variables"

Expand All @@ -9,5 +9,10 @@
"static",
StaticFormVariablesView.as_view(),
name="static",
)
),
path(
"registration",
RegistrationPluginVariablesView.as_view(),
name="registration",
),
]

0 comments on commit 8db0efa

Please sign in to comment.