Skip to content

Commit

Permalink
[#5039] Changed email registration nonField errors to specific fields…
Browse files Browse the repository at this point in the history
… errors
  • Loading branch information
vaszig committed Feb 11, 2025
1 parent 47c74d6 commit 3ebd8a0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
8 changes: 1 addition & 7 deletions src/openforms/forms/tests/test_api_form_plugin_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,4 @@ def test_cannot_overwrite_only_registration_email_html_template(self):
data = response.json()
error = data["invalidParams"][0]["reason"]

self.assertEqual(
error,
_(
"The fields {fields} must all have a non-empty value as soon as one of them "
"does."
).format(fields="email_content_template_html, email_content_template_text"),
)
self.assertEqual(error, _("You must also specify text along with the html."))
35 changes: 27 additions & 8 deletions src/openforms/registrations/contrib/email/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from rest_framework import serializers
from typing_extensions import NotRequired

from openforms.api.validators import AllOrNoneTruthyFieldsValidator
from openforms.emails.validators import URLSanitationValidator
from openforms.formio.api.fields import FormioVariableKeyField
from openforms.template.validators import DjangoTemplateValidator
Expand Down Expand Up @@ -123,13 +122,33 @@ class EmailOptionsSerializer(JsonSchemaSerializerMixin, serializers.Serializer):
],
)

class Meta:
validators = [
AllOrNoneTruthyFieldsValidator(
"email_content_template_html",
"email_content_template_text",
),
]
def validate(self, attrs: Options) -> Options:
html = attrs.get("email_content_template_html")
text = attrs.get("email_content_template_text")

if not html and not text:
return attrs

if html and not text:
raise serializers.ValidationError(
{
"email_content_template_text": _(
"You must also specify text along with the html."
),
},
code="required",
)
if text and not html:
raise serializers.ValidationError(
{
"email_content_template_html": _(
"You must also specify html along with the text."
),
},
code="required",
)

return attrs


# sanity check for development - keep serializer and type definitions in sync
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from django.test import TestCase

from ..config import EmailOptionsSerializer


class EmailOptionsSerializerTest(TestCase):
def test_email_and_text_content_fields(self):
with self.subTest("both fields missing"):
serializer = EmailOptionsSerializer(
data={
"to_emails": ["[email protected]"],
"email_content_template_html": "",
"email_content_template_text": "",
},
context={"request": None},
)

self.assertTrue(serializer.is_valid())

with self.subTest("text is missing"):
serializer = EmailOptionsSerializer(
data={
"to_emails": ["[email protected]"],
"email_content_template_html": "<p>some text</p>",
"email_content_template_text": "",
},
context={"request": None},
)

self.assertFalse(serializer.is_valid())
self.assertEqual(
serializer.errors["email_content_template_text"][0].code, "required"
)

with self.subTest("html is missing"):
serializer = EmailOptionsSerializer(
data={
"to_emails": ["[email protected]"],
"email_content_template_html": "",
"email_content_template_text": "some text",
},
context={"request": None},
)

self.assertFalse(serializer.is_valid())
self.assertEqual(
serializer.errors["email_content_template_html"][0].code, "required"
)

0 comments on commit 3ebd8a0

Please sign in to comment.