Skip to content

Commit

Permalink
✅ [#4320] Add unit tests for the cosign state
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-maertens committed Dec 6, 2024
1 parent 6b62c56 commit efa8791
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/openforms/submissions/cosigning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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

Expand Down
150 changes: 150 additions & 0 deletions src/openforms/submissions/tests/test_cosign_state.py
Original file line number Diff line number Diff line change
@@ -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, "<CosignState submission=<Submission reference=OF-123>>"
)

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": "[email protected]"},
cosigned=False,
),
True,
),
(
SubmissionFactory.from_components(
[
{
"type": "cosign",
"key": "cosign",
"hidden": False,
"validate": {"required": False},
}
],
submitted_data={"cosign": "[email protected]"},
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": "[email protected]"},
cosigned=False,
),
True,
),
# signed
(
SubmissionFactory.from_components(
[
{
"type": "cosign",
"key": "cosign",
"hidden": False,
"validate": {"required": False},
}
],
submitted_data={"cosign": "[email protected]"},
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)

0 comments on commit efa8791

Please sign in to comment.