Skip to content

Commit

Permalink
[#4396] Moved public functions to prefill.service and added prefill p…
Browse files Browse the repository at this point in the history
…lugin for ObjectsApi
  • Loading branch information
vaszig committed Oct 18, 2024
1 parent 9cfbc4d commit 96023e0
Show file tree
Hide file tree
Showing 31 changed files with 1,433 additions and 433 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from openforms.authentication.service import AuthAttribute
from openforms.authentication.utils import store_auth_details, store_registrator_details
from openforms.config.models import GlobalConfiguration
from openforms.prefill import prefill_variables
from openforms.prefill.contrib.haalcentraal_brp.plugin import PLUGIN_IDENTIFIER
from openforms.prefill.service import prefill_variables
from openforms.submissions.tests.factories import SubmissionFactory
from openforms.typing import JSONValue
from openforms.utils.tests.vcr import OFVCRMixin
Expand Down
4 changes: 3 additions & 1 deletion src/openforms/formio/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import elasticapm
from rest_framework.request import Request

from openforms.prefill import inject_prefill
from openforms.submissions.models import Submission
from openforms.typing import DataMapping

Expand Down Expand Up @@ -73,6 +72,9 @@ def get_dynamic_configuration(
The configuration is modified in the context of the provided ``submission``
parameter.
"""
# Avoid circular imports
from openforms.prefill.service import inject_prefill

rewrite_formio_components(config_wrapper, submission=submission, data=data)

# Add to each component the custom errors in the current locale
Expand Down
5 changes: 4 additions & 1 deletion src/openforms/formio/tests/test_component_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ def disable_prefill_injection():
"""
Disable prefill to prevent prefill-related queries.
"""
return patch("openforms.formio.service.inject_prefill", new=MagicMock)
return patch(
"openforms.prefill.service.inject_prefill",
new=MagicMock,
)


TEST_CONFIGURATION = {
Expand Down
142 changes: 113 additions & 29 deletions src/openforms/forms/tests/variables/test_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from django.conf import settings
from django.test import override_settings
from django.utils.translation import gettext_lazy as _

from factory.django import FileField
from rest_framework import status
Expand Down Expand Up @@ -901,38 +902,121 @@ def test_validators_accepts_only_numeric_keys(self):
# The variable is considered valid
self.assertEqual(status.HTTP_200_OK, response.status_code)

def test_validate_prefill_consistency(self):
user = SuperUserFactory.create()
def test_bulk_create_and_update_with_prefill_constraints(self):
user = StaffUserFactory.create(user_permissions=["change_form"])
self.client.force_authenticate(user)

form = FormFactory.create()
form_step = FormStepFactory.create(form=form)
form_definition = form_step.form_definition
form_path = reverse("api:form-detail", kwargs={"uuid_or_slug": form.uuid})
form_url = f"http://testserver.com{form_path}"
data = [
{
"form": form_url,
"form_definition": "",
"name": "Variable 1",
"key": "variable1",
"source": FormVariableSources.user_defined,
"dataType": FormVariableDataTypes.string,
"prefillPlugin": "demo",
"prefillAttribute": "",
}
]

response = self.client.put(
reverse(
"api:form-variables",
kwargs={"uuid_or_slug": form.uuid},
),
data=data,
form_definition_path = reverse(
"api:formdefinition-detail", kwargs={"uuid": form_definition.uuid}
)
form_definition_url = f"http://testserver.com{form_definition_path}"

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
error = response.json()
self.assertEqual(error["code"], "invalid")
self.assertEqual(len(error["invalidParams"]), 1)
self.assertEqual(
error["invalidParams"][0]["name"],
"0.prefillAttribute",
)
with self.subTest("component source with prefill options"):
data = [
{
"form": form_url,
"form_definition": form_definition_url,
"key": form_definition.configuration["components"][0]["key"],
"name": "Test",
"service_fetch_configuration": None,
"data_type": FormVariableDataTypes.string,
"source": FormVariableSources.component,
"prefill_options": {
"variables_mapping": [
{"variable_key": "data", "target_path": ["test"]}
]
},
}
]

response = self.client.put(
reverse(
"api:form-variables",
kwargs={"uuid_or_slug": form.uuid},
),
data=data,
)

self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
self.assertEqual(response.json()["invalidParams"][0]["code"], "invalid")
self.assertEqual(
response.json()["invalidParams"][0]["reason"],
_("Prefill options should not be specified for component variables."),
)

with self.subTest(
"component source with prefill attribute and not prefill plugin"
):
data = [
{
"form": form_url,
"form_definition": form_definition_url,
"key": form_definition.configuration["components"][0]["key"],
"name": "Test",
"service_fetch_configuration": None,
"data_type": FormVariableDataTypes.string,
"source": FormVariableSources.component,
"prefill_attribute": "test",
}
]

response = self.client.put(
reverse(
"api:form-variables",
kwargs={"uuid_or_slug": form.uuid},
),
data=data,
)

self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
self.assertEqual(response.json()["invalidParams"][0]["code"], "invalid")
self.assertEqual(
response.json()["invalidParams"][0]["reason"],
_(
"Prefill attribute cannot be specified without prefill plugin for component variables."
),
)

with self.subTest(
"both sources with prefill plugin, prefill attribute and prefill options"
):
data = [
{
"form": form_url,
"form_definition": form_definition_url,
"key": form_definition.configuration["components"][0]["key"],
"name": "Test",
"service_fetch_configuration": None,
"data_type": FormVariableDataTypes.string,
"source": FormVariableSources.user_defined,
"prefill_plugin": "demo",
"prefill_attribute": "test",
"prefill_options": {
"variables_mapping": [
{"variable_key": "data", "target_path": ["test"]}
]
},
}
]

response = self.client.put(
reverse(
"api:form-variables",
kwargs={"uuid_or_slug": form.uuid},
),
data=data,
)

self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
self.assertEqual(response.json()["invalidParams"][0]["code"], "invalid")
self.assertEqual(
response.json()["invalidParams"][0]["reason"],
_(
"Prefill plugin, attribute and options can not be specified at the same time."
),
)
Loading

0 comments on commit 96023e0

Please sign in to comment.