mirror of
https://github.com/wagtail/wagtail.git
synced 2024-11-24 10:58:52 +01:00
add tests for front-end form submission
This commit is contained in:
parent
738fa90e19
commit
f68f9a0461
@ -80,6 +80,7 @@ if not settings.configured:
|
||||
'wagtail.wagtailembeds',
|
||||
'wagtail.wagtailsearch',
|
||||
'wagtail.wagtailredirects',
|
||||
'wagtail.wagtailforms',
|
||||
'wagtail.tests',
|
||||
],
|
||||
PASSWORD_HASHERS=(
|
||||
|
53
wagtail/tests/fixtures/test.json
vendored
53
wagtail/tests/fixtures/test.json
vendored
@ -23,7 +23,7 @@
|
||||
"model": "wagtailcore.page",
|
||||
"fields": {
|
||||
"title": "Welcome to the Wagtail test site!",
|
||||
"numchild": 1,
|
||||
"numchild": 2,
|
||||
"show_in_menus": false,
|
||||
"live": true,
|
||||
"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,
|
||||
"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.wagtailimages.edit_handlers import ImageChooserPanel
|
||||
from wagtail.wagtaildocs.edit_handlers import DocumentChooserPanel
|
||||
from wagtail.wagtailforms.models import AbstractEmailForm, AbstractFormField
|
||||
|
||||
|
||||
EVENT_AUDIENCE_CHOICES = (
|
||||
@ -196,3 +197,20 @@ EventIndex.content_panels = [
|
||||
FieldPanel('title', classname="full title"),
|
||||
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")
|
||||
]
|
||||
|
15
wagtail/tests/templates/tests/form_page.html
Normal file
15
wagtail/tests/templates/tests/form_page.html
Normal file
@ -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>
|
11
wagtail/tests/templates/tests/form_page_landing.html
Normal file
11
wagtail/tests/templates/tests/form_page_landing.html
Normal file
@ -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 wagtail.wagtailcore.models import Page
|
||||
from wagtail.wagtailforms.models import FormSubmission
|
||||
|
||||
@unittest.skip("Need real tests")
|
||||
class SimpleTest(TestCase):
|
||||
def test_basic_addition(self):
|
||||
"""
|
||||
Tests that 1 + 1 always equals 2.
|
||||
"""
|
||||
self.assertEqual(1 + 1, 2)
|
||||
class TestFormSubmission(TestCase):
|
||||
fixtures = ['test.json']
|
||||
|
||||
def test_get_form(self):
|
||||
response = self.client.get('/contact-us/')
|
||||
self.assertContains(response, "Your email")
|
||||
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())
|
||||
|
Loading…
Reference in New Issue
Block a user