Skip to content

Commit

Permalink
[#3607] Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Dec 20, 2023
1 parent 70e2b6a commit 940910d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/openforms/validations/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ def _flat(it):

def filter_empty_values(value: Any) -> Any:
if not isinstance(value, dict):
# Let the serializer raise a the validation error
# Let the serializer raise the validation error
return value

return {k: v for k, v in value.items() if v}


Expand Down Expand Up @@ -141,7 +142,6 @@ def validate(
SerializerClass = validator.callable.value_serializer
serializer = SerializerClass(data={"value": filter_empty_values(value)})
serializer.is_valid(raise_exception=True)

try:
validator(serializer.data["value"], submission)
except (DJ_ValidationError, DRF_ValidationError) as e:
Expand Down
51 changes: 51 additions & 0 deletions src/openforms/validations/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from openforms.submissions.tests.factories import SubmissionFactory
from openforms.validations.registry import Registry, StringValueSerializer
from openforms.validations.tests.test_registry import (
DictValidator,
DjangoValidator,
DRFValidator,
function_validator,
Expand Down Expand Up @@ -38,6 +39,12 @@ def setUp(self):
"demo", verbose_name="Demo function", for_components=(), is_demo_plugin=True
)(function_validator)

register(
"dict_validator",
verbose_name="Dict validator",
for_components=("addressNL",),
)(DictValidator)

patcher = patch("openforms.validations.api.views.register", new=register)
patcher.start()
self.addCleanup(patcher.stop)
Expand Down Expand Up @@ -76,6 +83,11 @@ def test_validations_list(self):
"label": "Django function Validator",
"for_components": [],
},
{
"id": "dict_validator",
"label": "Dict validator",
"for_components": ["addressNL"],
},
],
)

Expand Down Expand Up @@ -137,6 +149,11 @@ def test_validations_list(self):
"label": "Django function Validator",
"for_components": [],
},
{
"id": "dict_validator",
"label": "Dict validator",
"for_components": ["addressNL"],
},
],
)

Expand All @@ -154,6 +171,16 @@ def test_default_string_serializer(self):
self.assertFalse(StringValueSerializer(data={"value": None}).is_valid())
self.assertFalse(StringValueSerializer(data={"bazz": "buzz"}).is_valid())

def test_validation_forbidden(self):
submission = SubmissionFactory.create()
submission_uuid = str(submission.uuid)
url = reverse("api:validate-value", kwargs={"validator": "django"})

response = self.client.post(
url, {"value": "VALID", "submission_uuid": submission_uuid}, format="json"
)
self.assertEqual(response.status_code, 403)

def test_validation(self):
submission = SubmissionFactory.create()
submission_uuid = str(submission.uuid)
Expand Down Expand Up @@ -183,3 +210,27 @@ def test_validation(self):
}
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, expected)

def test_validation_dict(self):
# This test exists to add coverage to the `filter_empty_values` function
submission = SubmissionFactory.create()
submission_uuid = str(submission.uuid)
url = reverse("api:validate-value", kwargs={"validator": "dict_validator"})
session = self.client.session
session[SUBMISSIONS_SESSION_KEY] = [submission_uuid]
session.save()

response = self.client.post(
url,
{
"value": {"field": "dummy", "extra": ""},
"submission_uuid": submission_uuid,
},
format="json",
)
expected = {
"is_valid": True,
"messages": [],
}
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, expected)
17 changes: 17 additions & 0 deletions src/openforms/validations/tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@
from django.test import TestCase
from django.utils.translation import gettext as _

from rest_framework import serializers
from rest_framework.exceptions import ValidationError as DRFValidationError

from openforms.validations.registry import Registry, StringValueSerializer


class DummyDataSerializer(serializers.Serializer):
field = serializers.CharField()


class DictValueSerializer(serializers.Serializer):
value = DummyDataSerializer()


class DjangoValidator:
is_enabled = True
components = ("textfield",)
Expand Down Expand Up @@ -37,6 +46,14 @@ def __call__(self, value, submission):
raise DRFValidationError("not VALID value")


class DictValidator:
is_enabled = True
value_serializer = DictValueSerializer

def __call__(self, value, submission):
return True


def function_validator(value, submission):
if value != "VALID":
raise DjangoValidationError("not VALID value")
Expand Down

0 comments on commit 940910d

Please sign in to comment.