kopia lustrzana https://github.com/wagtail/wagtail
add tests for front-end form submission
rodzic
738fa90e19
commit
f68f9a0461
|
@ -80,6 +80,7 @@ if not settings.configured:
|
||||||
'wagtail.wagtailembeds',
|
'wagtail.wagtailembeds',
|
||||||
'wagtail.wagtailsearch',
|
'wagtail.wagtailsearch',
|
||||||
'wagtail.wagtailredirects',
|
'wagtail.wagtailredirects',
|
||||||
|
'wagtail.wagtailforms',
|
||||||
'wagtail.tests',
|
'wagtail.tests',
|
||||||
],
|
],
|
||||||
PASSWORD_HASHERS=(
|
PASSWORD_HASHERS=(
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
"model": "wagtailcore.page",
|
"model": "wagtailcore.page",
|
||||||
"fields": {
|
"fields": {
|
||||||
"title": "Welcome to the Wagtail test site!",
|
"title": "Welcome to the Wagtail test site!",
|
||||||
"numchild": 1,
|
"numchild": 2,
|
||||||
"show_in_menus": false,
|
"show_in_menus": false,
|
||||||
"live": true,
|
"live": true,
|
||||||
"depth": 2,
|
"depth": 2,
|
||||||
|
@ -141,6 +141,57 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
"pk": 7,
|
||||||
|
"model": "wagtailcore.page",
|
||||||
|
"fields": {
|
||||||
|
"title": "Contact us",
|
||||||
|
"numchild": 0,
|
||||||
|
"show_in_menus": true,
|
||||||
|
"live": true,
|
||||||
|
"depth": 3,
|
||||||
|
"content_type": ["tests", "formpage"],
|
||||||
|
"path": "000100010002",
|
||||||
|
"url_path": "/home/contact-us/",
|
||||||
|
"slug": "contact-us"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pk": 7,
|
||||||
|
"model": "tests.formpage",
|
||||||
|
"fields": {
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
"pk": 1,
|
||||||
|
"model": "tests.formfield",
|
||||||
|
"fields": {
|
||||||
|
"sort_order": 1,
|
||||||
|
"label": "Your email",
|
||||||
|
"field_type": "email",
|
||||||
|
"required": true,
|
||||||
|
"choices": "",
|
||||||
|
"default_value": "",
|
||||||
|
"help_text": "",
|
||||||
|
"page": 7
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pk": 2,
|
||||||
|
"model": "tests.formfield",
|
||||||
|
"fields": {
|
||||||
|
"sort_order": 2,
|
||||||
|
"label": "Your message",
|
||||||
|
"field_type": "multiline",
|
||||||
|
"required": true,
|
||||||
|
"choices": "",
|
||||||
|
"default_value": "",
|
||||||
|
"help_text": "",
|
||||||
|
"page": 7
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"pk": 1,
|
"pk": 1,
|
||||||
"model": "wagtailcore.site",
|
"model": "wagtailcore.site",
|
||||||
|
|
|
@ -5,6 +5,7 @@ from wagtail.wagtailcore.fields import RichTextField
|
||||||
from wagtail.wagtailadmin.edit_handlers import FieldPanel, MultiFieldPanel, InlinePanel, PageChooserPanel
|
from wagtail.wagtailadmin.edit_handlers import FieldPanel, MultiFieldPanel, InlinePanel, PageChooserPanel
|
||||||
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
|
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
|
||||||
from wagtail.wagtaildocs.edit_handlers import DocumentChooserPanel
|
from wagtail.wagtaildocs.edit_handlers import DocumentChooserPanel
|
||||||
|
from wagtail.wagtailforms.models import AbstractEmailForm, AbstractFormField
|
||||||
|
|
||||||
|
|
||||||
EVENT_AUDIENCE_CHOICES = (
|
EVENT_AUDIENCE_CHOICES = (
|
||||||
|
@ -196,3 +197,20 @@ EventIndex.content_panels = [
|
||||||
FieldPanel('title', classname="full title"),
|
FieldPanel('title', classname="full title"),
|
||||||
FieldPanel('intro', classname="full"),
|
FieldPanel('intro', classname="full"),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class FormField(AbstractFormField):
|
||||||
|
page = ParentalKey('FormPage', related_name='form_fields')
|
||||||
|
|
||||||
|
class FormPage(AbstractEmailForm):
|
||||||
|
pass
|
||||||
|
|
||||||
|
FormPage.content_panels = [
|
||||||
|
FieldPanel('title', classname="full title"),
|
||||||
|
InlinePanel(FormPage, 'form_fields', label="Form fields"),
|
||||||
|
MultiFieldPanel([
|
||||||
|
FieldPanel('to_address', classname="full"),
|
||||||
|
FieldPanel('from_address', classname="full"),
|
||||||
|
FieldPanel('subject', classname="full"),
|
||||||
|
], "Email")
|
||||||
|
]
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
{% load pageurl %}
|
||||||
|
<!DOCTYPE HTML>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>{{ self.title }}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>{{ self.title }}</h1>
|
||||||
|
<form action="{% pageurl self %}" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{ form.as_p }}
|
||||||
|
<input type="submit">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1,11 @@
|
||||||
|
{% load pageurl %}
|
||||||
|
<!DOCTYPE HTML>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>{{ self.title }}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>{{ self.title }}</h1>
|
||||||
|
<p>Thank you for your feedback.</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -1,19 +1,30 @@
|
||||||
"""
|
|
||||||
This file demonstrates writing tests using the unittest module. These will pass
|
|
||||||
when you run "manage.py test".
|
|
||||||
|
|
||||||
Replace this with more appropriate tests for your application.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from wagtail.wagtailcore.models import Page
|
||||||
|
from wagtail.wagtailforms.models import FormSubmission
|
||||||
|
|
||||||
@unittest.skip("Need real tests")
|
class TestFormSubmission(TestCase):
|
||||||
class SimpleTest(TestCase):
|
fixtures = ['test.json']
|
||||||
def test_basic_addition(self):
|
|
||||||
"""
|
def test_get_form(self):
|
||||||
Tests that 1 + 1 always equals 2.
|
response = self.client.get('/contact-us/')
|
||||||
"""
|
self.assertContains(response, "Your email")
|
||||||
self.assertEqual(1 + 1, 2)
|
self.assertNotContains(response, "Thank you for your feedback")
|
||||||
|
|
||||||
|
def test_post_invalid_form(self):
|
||||||
|
response = self.client.post('/contact-us/', {
|
||||||
|
'your-email': 'bob', 'your-message': 'hello world'
|
||||||
|
})
|
||||||
|
self.assertNotContains(response, "Thank you for your feedback")
|
||||||
|
self.assertContains(response, "Enter a valid email address.")
|
||||||
|
|
||||||
|
def test_post_valid_form(self):
|
||||||
|
response = self.client.post('/contact-us/', {
|
||||||
|
'your-email': 'bob@example.com', 'your-message': 'hello world'
|
||||||
|
})
|
||||||
|
self.assertNotContains(response, "Your email")
|
||||||
|
self.assertContains(response, "Thank you for your feedback")
|
||||||
|
|
||||||
|
form_page = Page.objects.get(url_path='/home/contact-us/')
|
||||||
|
|
||||||
|
self.assertTrue(FormSubmission.objects.filter(page=form_page, form_data__contains='hello world').exists())
|
||||||
|
|
Ładowanie…
Reference in New Issue