Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#5039] Fix email registration errors #5098

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
)
Loading