From 95f2355dcb34d120371784c67aa1b315e15afa6e Mon Sep 17 00:00:00 2001 From: vasileios Date: Tue, 21 May 2024 14:05:28 +0200 Subject: [PATCH 1/2] [#1906] Added regression tests --- .../forms/tests/test_import_export.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/openforms/forms/tests/test_import_export.py b/src/openforms/forms/tests/test_import_export.py index 127219c723..2a0820e0d5 100644 --- a/src/openforms/forms/tests/test_import_export.py +++ b/src/openforms/forms/tests/test_import_export.py @@ -122,6 +122,43 @@ def test_export(self): FormVariableSources.user_defined, form_variables[0]["source"] ) + @tag("gh-1906") + def test_export_reusable_form_definition_uuid_already_exists(self): + form = FormFactory.create() + form_definition = FormDefinitionFactory.create( + configuration={"components": [{"key": "test-key", "type": "file"}]}, + is_reusable=True, + ) + FormStepFactory.create(form=form, form_definition=form_definition) + + call_command("export", form.pk, self.filepath) + + with zipfile.ZipFile(self.filepath, "r") as f: + self.assertEqual( + f.namelist(), + [ + "forms.json", + "formSteps.json", + "formDefinitions.json", + "formLogic.json", + "formVariables.json", + f"{EXPORT_META_KEY}.json", + ], + ) + + form_definitions = json.loads(f.read("formDefinitions.json")) + self.assertEqual(len(form_definitions), 1) + self.assertEqual(form_definitions[0]["uuid"], str(form_definition.uuid)) + self.assertEqual(form_definitions[0]["name"], form_definition.name) + self.assertEqual( + form_definitions[0]["internal_name"], form_definition.internal_name + ) + self.assertEqual(form_definitions[0]["slug"], form_definition.slug) + self.assertEqual( + form_definitions[0]["configuration"], + form_definition.configuration, + ) + def test_import(self): product = ProductFactory.create() merchant = OgoneMerchantFactory.create() @@ -438,6 +475,30 @@ def test_import_form_definition_uuid_already_exists_configuration_different(self self.assertNotEqual(form_logic_2.pk, form_logic_pk) self.assertEqual(imported_form.pk, form_logic_2.form.pk) + @tag("gh-1906") + def test_import_reusable_form_definition_uuid_already_exists(self): + form = FormFactory.create() + form_definition = FormDefinitionFactory.create( + configuration={"components": [{"key": "test-key", "type": "file"}]}, + is_reusable=True, + ) + FormStepFactory.create(form=form, form_definition=form_definition) + + call_command("export", form.pk, self.filepath) + + call_command("import", import_file=self.filepath) + + form_definitions = FormDefinition.objects.all() + fd2 = form_definitions.last() + self.assertEqual(form_definitions.count(), 1) + self.assertEqual(fd2.pk, form_definition.pk) + self.assertEqual(fd2.uuid, form_definition.uuid) + self.assertEqual(fd2.configuration, form_definition.configuration) + self.assertEqual(fd2.login_required, form_definition.login_required) + self.assertEqual(fd2.name, form_definition.name) + self.assertEqual(fd2.internal_name, form_definition.internal_name) + self.assertEqual(fd2.slug, form_definition.slug) + def test_import_form_with_category(self): """ Assert that the category reference is ignored during import. From 04b195750866ee06fbb61a0edf0667b0d3d77852 Mon Sep 17 00:00:00 2001 From: vasileios Date: Tue, 21 May 2024 14:05:44 +0200 Subject: [PATCH 2/2] [#1906] Do not use the dynamic rewrite_formio_components in the export flow When we tried to import a form with a reusable form definition, sometimes, for example in the file component, because of the rewrite_for_request method we had a modified configuration and therefore a different hash. This was creating a new form definition (duplicate). --- src/openforms/conf/locale/nl/LC_MESSAGES/django.po | 10 +++++----- .../forms/api/serializers/form_definition.py | 12 ++++++++---- src/openforms/forms/utils.py | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/openforms/conf/locale/nl/LC_MESSAGES/django.po b/src/openforms/conf/locale/nl/LC_MESSAGES/django.po index ccb4adb04a..eab77946b5 100644 --- a/src/openforms/conf/locale/nl/LC_MESSAGES/django.po +++ b/src/openforms/conf/locale/nl/LC_MESSAGES/django.po @@ -4,7 +4,7 @@ msgid "" msgstr "" "Project-Id-Version: Open Forms\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-05-21 10:26+0200\n" +"POT-Creation-Date: 2024-05-21 13:43+0200\n" "PO-Revision-Date: 2024-05-17 15:22+0200\n" "Last-Translator: Sergei Maertens \n" "Language-Team: Dutch \n" @@ -4874,11 +4874,11 @@ msgstr "Form.io configuratie" msgid "The form definition as Form.io JSON schema" msgstr "De formulier definitie als Form.io JSON schema" -#: openforms/forms/api/serializers/form_definition.py:168 +#: openforms/forms/api/serializers/form_definition.py:172 msgid "Used in forms" msgstr "Gebruikt in formulieren" -#: openforms/forms/api/serializers/form_definition.py:170 +#: openforms/forms/api/serializers/form_definition.py:174 msgid "" "The collection of forms making use of this definition. This includes both " "active and inactive forms." @@ -8257,8 +8257,8 @@ msgid "" "The co-sign component requires the '{field_label}' ({config_verbose_name}) " "to be configured." msgstr "" -"Het mede-ondertekencomponent vereist de configuratie van '{field_label}' " -"({config_verbose_name})." +"Het mede-ondertekencomponent vereist de configuratie van " +"'{field_label}' ({config_verbose_name})." #: openforms/products/api/viewsets.py:15 msgid "Retrieve details of a single product" diff --git a/src/openforms/forms/api/serializers/form_definition.py b/src/openforms/forms/api/serializers/form_definition.py index 586ddf5013..967c154483 100644 --- a/src/openforms/forms/api/serializers/form_definition.py +++ b/src/openforms/forms/api/serializers/form_definition.py @@ -130,10 +130,14 @@ def to_representation(self, instance): # for the dynamic formio configuration in the context of a submission. # 2. The serializers/API endpoints of :module:`openforms.forms.api` for # 'standalone' use/introspection. - rewrite_formio_components_for_request( - instance.configuration_wrapper, - request=self.context["request"], - ) + is_export = self.context.get("is_export", False) + + if not is_export: + rewrite_formio_components_for_request( + instance.configuration_wrapper, + request=self.context["request"], + ) + representation["configuration"] = instance.configuration_wrapper.configuration return representation diff --git a/src/openforms/forms/utils.py b/src/openforms/forms/utils.py index e7b04fc4ef..95d09efcb8 100644 --- a/src/openforms/forms/utils.py +++ b/src/openforms/forms/utils.py @@ -96,7 +96,7 @@ def form_to_json(form_id: int) -> dict: form_definitions = FormDefinitionSerializer( instance=form_definitions, many=True, - context={"request": request}, + context={"request": request, "is_export": True}, ).data form_steps = FormStepSerializer( instance=form_steps, many=True, context={"request": request}