From efa879169dad6df58726846dfab03d406bd4c268 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Fri, 6 Dec 2024 15:49:09 +0100 Subject: [PATCH] :white_check_mark: [#4320] Add unit tests for the cosign state --- src/openforms/submissions/cosigning.py | 14 +- .../submissions/tests/test_cosign_state.py | 150 ++++++++++++++++++ 2 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 src/openforms/submissions/tests/test_cosign_state.py diff --git a/src/openforms/submissions/cosigning.py b/src/openforms/submissions/cosigning.py index 2ffbeed42b..c3bae8bd1a 100644 --- a/src/openforms/submissions/cosigning.py +++ b/src/openforms/submissions/cosigning.py @@ -18,17 +18,21 @@ from __future__ import annotations +import logging from functools import cached_property from typing import TYPE_CHECKING, NotRequired, TypedDict from openforms.authentication.constants import AuthAttribute from openforms.authentication.typing import FormAuth from openforms.formio.typing import Component +from openforms.submissions.models import submission from openforms.typing import JSONObject if TYPE_CHECKING: from .models import Submission +logger = logging.getLogger(__name__) + type CosignData = CosignV1Data | CosignV2Data @@ -155,7 +159,15 @@ def email(self) -> str: variables_state = self.submission.load_submission_value_variables_state() values = variables_state.get_data(as_formio_data=True) - cosigner_email = values[cosign_component["key"]] + if (key := cosign_component["key"]) not in values: + logger.info( + "Inconsistent state - there is a cosign component, but no value is" + "associated with it (submission %s).", + self.submission.uuid, + extra={"submission": submission.uuid}, + ) + return "" + cosigner_email = values[key] assert isinstance(cosigner_email, str) return cosigner_email diff --git a/src/openforms/submissions/tests/test_cosign_state.py b/src/openforms/submissions/tests/test_cosign_state.py new file mode 100644 index 0000000000..a0913b92fc --- /dev/null +++ b/src/openforms/submissions/tests/test_cosign_state.py @@ -0,0 +1,150 @@ +from django.test import TestCase + +from ..cosigning import CosignState +from .factories import SubmissionFactory + + +class CosignStateTests(TestCase): + + def test_string_representation(self): + submission = SubmissionFactory.build(public_registration_reference="OF-123") + cosign = CosignState(submission=submission) + + str_repr = str(cosign) + + self.assertEqual( + str_repr, ">" + ) + + def test_cosign_required_state(self): + submissions = ( + ( + SubmissionFactory.create(cosigned=False), + False, + ), + ( + SubmissionFactory.from_components( + [ + { + "type": "cosign", + "key": "cosign", + "hidden": False, + "validate": {"required": False}, + } + ], + cosigned=False, + ), + False, + ), + ( + SubmissionFactory.from_components( + [ + { + "type": "cosign", + "key": "cosign", + "hidden": False, + "validate": {"required": True}, + } + ], + cosigned=True, + ), + True, + ), + ( + SubmissionFactory.from_components( + [ + { + "type": "cosign", + "key": "cosign", + "hidden": False, + "validate": {"required": False}, + } + ], + submitted_data={"cosign": "cosigner@example.com"}, + cosigned=False, + ), + True, + ), + ( + SubmissionFactory.from_components( + [ + { + "type": "cosign", + "key": "cosign", + "hidden": False, + "validate": {"required": False}, + } + ], + submitted_data={"cosign": "cosigner@example.com"}, + cosigned=True, + ), + True, + ), + ) + + for index, (submission, expected) in enumerate(submissions): + with self.subTest(f"submission at index {index}"): + cosign = CosignState(submission=submission) + + self.assertEqual(cosign.is_required, expected) + + def test_is_waiting(self): + submissions = ( + # without cosign + ( + SubmissionFactory.create(cosigned=False), + False, + ), + # not signed yet + ( + SubmissionFactory.from_components( + [ + { + "type": "cosign", + "key": "cosign", + "hidden": False, + "validate": {"required": True}, + } + ], + cosigned=False, + ), + True, + ), + ( + SubmissionFactory.from_components( + [ + { + "type": "cosign", + "key": "cosign", + "hidden": False, + "validate": {"required": False}, + } + ], + submitted_data={"cosign": "cosigner@example.com"}, + cosigned=False, + ), + True, + ), + # signed + ( + SubmissionFactory.from_components( + [ + { + "type": "cosign", + "key": "cosign", + "hidden": False, + "validate": {"required": False}, + } + ], + submitted_data={"cosign": "cosigner@example.com"}, + cosigned=True, + ), + False, + ), + ) + + for index, (submission, expected) in enumerate(submissions): + with self.subTest(f"submission at index {index}"): + cosign = CosignState(submission=submission) + + self.assertEqual(cosign.is_waiting, expected)