Validate to and from addresses on email form

This will result in a migration, but the migration from a `CharField` to an `EmailField` won't hit the database, as `EmailField` is just a `CharField` with extra default validation.
pull/8155/head
Jake Howard 2022-03-28 10:01:17 +01:00 zatwierdzone przez LB (Ben Johnston)
rodzic 44e41a72d3
commit 5522769a9c
5 zmienionych plików z 144 dodań i 1 usunięć

Wyświetl plik

@ -40,6 +40,7 @@ Changelog
* Add `trimmed` attribute to all blocktrans tags, so spacing is more reliable in translated strings (Harris Lapiroff)
* Add documentation that describes how to use `ModelAdmin` to manage `Tag`s (Abdulmajeed Isa)
* Rename the setting `BASE_URL` (undocumented) to `WAGTAILADMIN_BASE_URL` and add to documentation, `BASE_URL` will be removed in a future release (Sandil Ranasinghe)
* Validate to and from email addresses within form builder pages when using `AbstractEmailForm` (Jake Howard)
* Fix: When using `simple_translations` ensure that the user is redirected to the page edit view when submitting for a single locale (Mitchel Cabuloy)
* Fix: When previewing unsaved changes to `Form` pages, ensure that all added fields are correctly shown in the preview (Joshua Munn)
* Fix: When Documents (e.g. PDFs) have been configured to be served inline via `WAGTAILDOCS_CONTENT_TYPES` & `WAGTAILDOCS_INLINE_CONTENT_TYPES` ensure that the filename is correctly set in the `Content-Disposition` header so that saving the files will use the correct filename (John-Scott Atlakson)

Wyświetl plik

@ -70,6 +70,7 @@ class LandingPage(Page):
* Add `trimmed` attribute to all blocktrans tags, so spacing is more reliable in translated strings (Harris Lapiroff)
* Add documentation that describes how to use `ModelAdmin` to manage `Tag`s (Abdulmajeed Isa)
* Rename the setting `BASE_URL` (undocumented) to [`WAGTAILADMIN_BASE_URL`](wagtailadmin_base_url) and add to documentation, `BASE_URL` will be removed in a future release (Sandil Ranasinghe)
* Validate to and from email addresses within form builder pages when using `AbstractEmailForm` (Jake Howard)
### Bug fixes

Wyświetl plik

@ -4,6 +4,7 @@ import os
from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder
from django.core.validators import validate_email
from django.db import models
from django.template.response import TemplateResponse
from django.utils.formats import date_format
@ -288,6 +289,11 @@ class AbstractForm(Page):
return super().serve_preview(request, mode_name)
def validate_to_address(value):
for address in value.split(","):
validate_email(address.strip())
class AbstractEmailForm(AbstractForm):
"""
A Form Page that sends email. Pages implementing a form to be send to an email should inherit from it
@ -300,8 +306,9 @@ class AbstractEmailForm(AbstractForm):
help_text=_(
"Optional - form submissions will be emailed to these addresses. Separate multiple addresses by comma."
),
validators=[validate_to_address],
)
from_address = models.CharField(
from_address = models.EmailField(
verbose_name=_("from address"), max_length=255, blank=True
)
subject = models.CharField(verbose_name=_("subject"), max_length=255, blank=True)

Wyświetl plik

@ -4,6 +4,7 @@ import unittest
from django import VERSION as DJANGO_VERSION
from django.core import mail
from django.core.exceptions import ValidationError
from django.test import TestCase, override_settings
from wagtail.contrib.forms.models import FormSubmission
@ -181,6 +182,10 @@ class TestFormSubmission(TestCase):
self.assertEqual(len(mail.outbox), 1)
self.assertIn("Your choices: ", mail.outbox[0].body)
def test_invalid_from_address(self):
with self.assertRaises(ValidationError):
make_form_page(from_address="not an email")
class TestFormWithCustomSubmission(TestCase, WagtailTestUtils):
def setUp(self):
@ -422,6 +427,13 @@ class TestFormSubmissionWithMultipleRecipients(TestCase):
# Create a form page
self.form_page = make_form_page(to_address="to@email.com, another@email.com")
def test_invalid_to_address(self):
with self.assertRaises(ValidationError):
make_form_page(to_address="not an email")
with self.assertRaises(ValidationError):
make_form_page(to_address="to@email.com, not an email")
def test_post_valid_form(self):
response = self.client.post(
"/contact-us/",

Wyświetl plik

@ -0,0 +1,122 @@
# Generated by Django 4.0.3 on 2022-03-28 09:18
from django.db import migrations, models
import wagtail.contrib.forms.models
class Migration(migrations.Migration):
dependencies = [
("tests", "0063_jsonblockcountsstreammodel_and_more"),
]
operations = [
migrations.AlterField(
model_name="formpage",
name="from_address",
field=models.EmailField(
blank=True, max_length=255, verbose_name="from address"
),
),
migrations.AlterField(
model_name="formpage",
name="to_address",
field=models.CharField(
blank=True,
help_text="Optional - form submissions will be emailed to these addresses. Separate multiple addresses by comma.",
max_length=255,
validators=[wagtail.contrib.forms.models.validate_to_address],
verbose_name="to address",
),
),
migrations.AlterField(
model_name="formpagewithcustomformbuilder",
name="from_address",
field=models.EmailField(
blank=True, max_length=255, verbose_name="from address"
),
),
migrations.AlterField(
model_name="formpagewithcustomformbuilder",
name="to_address",
field=models.CharField(
blank=True,
help_text="Optional - form submissions will be emailed to these addresses. Separate multiple addresses by comma.",
max_length=255,
validators=[wagtail.contrib.forms.models.validate_to_address],
verbose_name="to address",
),
),
migrations.AlterField(
model_name="formpagewithcustomsubmission",
name="from_address",
field=models.EmailField(
blank=True, max_length=255, verbose_name="from address"
),
),
migrations.AlterField(
model_name="formpagewithcustomsubmission",
name="to_address",
field=models.CharField(
blank=True,
help_text="Optional - form submissions will be emailed to these addresses. Separate multiple addresses by comma.",
max_length=255,
validators=[wagtail.contrib.forms.models.validate_to_address],
verbose_name="to address",
),
),
migrations.AlterField(
model_name="formpagewithcustomsubmissionlistview",
name="from_address",
field=models.EmailField(
blank=True, max_length=255, verbose_name="from address"
),
),
migrations.AlterField(
model_name="formpagewithcustomsubmissionlistview",
name="to_address",
field=models.CharField(
blank=True,
help_text="Optional - form submissions will be emailed to these addresses. Separate multiple addresses by comma.",
max_length=255,
validators=[wagtail.contrib.forms.models.validate_to_address],
verbose_name="to address",
),
),
migrations.AlterField(
model_name="formpagewithredirect",
name="from_address",
field=models.EmailField(
blank=True, max_length=255, verbose_name="from address"
),
),
migrations.AlterField(
model_name="formpagewithredirect",
name="to_address",
field=models.CharField(
blank=True,
help_text="Optional - form submissions will be emailed to these addresses. Separate multiple addresses by comma.",
max_length=255,
validators=[wagtail.contrib.forms.models.validate_to_address],
verbose_name="to address",
),
),
migrations.AlterField(
model_name="jadeformpage",
name="from_address",
field=models.EmailField(
blank=True, max_length=255, verbose_name="from address"
),
),
migrations.AlterField(
model_name="jadeformpage",
name="to_address",
field=models.CharField(
blank=True,
help_text="Optional - form submissions will be emailed to these addresses. Separate multiple addresses by comma.",
max_length=255,
validators=[wagtail.contrib.forms.models.validate_to_address],
verbose_name="to address",
),
),
]