From af94e5c7ef029113869883c8a1f3b4e2955a27e3 Mon Sep 17 00:00:00 2001 From: robinvandermolen Date: Thu, 5 Dec 2024 14:58:55 +0100 Subject: [PATCH] :bug: [#3283] Display the submission registration_backend in the admin The submission admin display used to show the first registration_backend of the form. Due to form logic, the registration_backend on the submission could be different. The admin now shows the exact registration_backend that was used by the submission --- src/openforms/submissions/admin.py | 2 +- src/openforms/submissions/tests/test_admin.py | 122 +++++++++++++++++- 2 files changed, 121 insertions(+), 3 deletions(-) diff --git a/src/openforms/submissions/admin.py b/src/openforms/submissions/admin.py index 6160856886..9a41a71a02 100644 --- a/src/openforms/submissions/admin.py +++ b/src/openforms/submissions/admin.py @@ -361,7 +361,7 @@ def successfully_processed(self, obj) -> bool | None: @admin.display(description=_("Registration backend")) def get_registration_backend(self, obj): - return backend if (backend := obj.form.registration_backends.first()) else "-" + return obj.registration_backend or "-" @admin.display(description=_("Appointment status")) def get_appointment_status(self, obj): diff --git a/src/openforms/submissions/tests/test_admin.py b/src/openforms/submissions/tests/test_admin.py index 8d8bf98f53..4b51ce1bc7 100644 --- a/src/openforms/submissions/tests/test_admin.py +++ b/src/openforms/submissions/tests/test_admin.py @@ -12,16 +12,24 @@ from maykin_2fa.test import disable_admin_mfa from openforms.accounts.tests.factories import UserFactory -from openforms.forms.tests.factories import FormVariableFactory +from openforms.forms.tests.factories import ( + FormFactory, + FormLogicFactory, + FormRegistrationBackendFactory, + FormStepFactory, + FormVariableFactory, +) from openforms.logging.logevent import submission_start from openforms.logging.models import TimelineLogProxy from openforms.submissions.models.submission import Submission from openforms.variables.constants import FormVariableDataTypes from ...config.models import GlobalConfiguration +from ...forms.constants import LogicActionTypes from ..admin import SubmissionAdmin, SubmissionTimeListFilter from ..constants import PostSubmissionEvents, RegistrationStatuses -from .factories import SubmissionFactory +from ..form_logic import evaluate_form_logic +from .factories import SubmissionFactory, SubmissionStepFactory @disable_admin_mfa() @@ -248,6 +256,116 @@ def test_change_view_with_broken_price_variable_config(self): self.assertEqual(change_page.status_code, 200) + def test_changing_registration_backend_with_form_logic_is_correctly_displayed_in_admin( + self, + ): + form = FormFactory.create() + email = FormRegistrationBackendFactory.create( + form=form, + backend="email", + key="email", + name="Email", + ) + objects_api = FormRegistrationBackendFactory.create( + form=form, + backend="objects_api", + key="objects_api", + name="Objects api", + ) + form_step = FormStepFactory.create( + form=form, + form_definition__configuration={ + "components": [ + { + "type": "textfield", + "key": "text1", + } + ] + }, + ) + FormLogicFactory.create( + form=form, + json_logic_trigger={"==": [{"var": "text1"}, "trigger-rule"]}, + actions=[ + { + "action": { + "type": LogicActionTypes.set_registration_backend, + "value": f"{objects_api.key}", + }, + }, + ], + ) + + with self.subTest( + "Submission doesn't trigger logic and uses the default registration backend" + ): + # Without triggering the form logic, the submission should use + # the first backend registration of the form + submission = SubmissionFactory.create(form=form, completed=True) + submission_step = SubmissionStepFactory.create( + submission=submission, form_step=form_step, data={"text1": "test"} + ) + + # Evaluate the logic, and save the changes + evaluate_form_logic(submission, submission_step, submission.data) + submission.save() + + change_url = reverse( + "admin:submissions_submission_change", + kwargs={"object_id": submission.pk}, + ) + + change_page = self.client.get(change_url) + self.assertEqual(change_page.status_code, 200) + + # The email registration should be used + self.assertEqual(submission.registration_backend, email) + + # The admin page should show the email registration as the one being used + registration_backend_field = change_page.pyquery.find( + ".form-row.field-get_registration_backend > div > div" + ) + self.assertEqual( + registration_backend_field.text(), + f"Registratie backend:\nEmail van {form.name}", + ) + + with self.subTest( + "Submission that does trigger the logic and gets the logic defined backend registration" + ): + # When triggering the form logic, the submission should use + # the objects_api backend registration of the form + submission = SubmissionFactory.create(form=form, completed=True) + submission_step = SubmissionStepFactory.create( + submission=submission, + form_step=form_step, + data={"text1": "trigger-rule"}, + ) + + # Evaluate the logic, and save the changes + evaluate_form_logic(submission, submission_step, submission.data) + submission.save() + + change_url = reverse( + "admin:submissions_submission_change", + kwargs={"object_id": submission.pk}, + ) + + change_page = self.client.get(change_url) + self.assertEqual(change_page.status_code, 200) + + # The objects api registration should be used + self.assertEqual(submission.registration_backend, objects_api) + + # The admin page should show the objects api registration as the one being used + registration_backend_field = change_page.pyquery.find( + ".form-row.field-get_registration_backend > div > div" + ) + self.assertEqual( + registration_backend_field.text(), + f"Registratie backend:\nObjects api van {form.name}", + ) + class TestSubmissionTimeListFilterAdmin(TestCase): def test_time_filtering(self):