From f68f9a04613e8a4e878b83e2cbfa380ede71b6c6 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Wed, 7 May 2014 16:39:56 +0100 Subject: [PATCH] add tests for front-end form submission --- runtests.py | 1 + wagtail/tests/fixtures/test.json | 53 ++++++++++++++++++- wagtail/tests/models.py | 18 +++++++ wagtail/tests/templates/tests/form_page.html | 15 ++++++ .../templates/tests/form_page_landing.html | 11 ++++ wagtail/wagtailforms/tests.py | 43 +++++++++------ 6 files changed, 124 insertions(+), 17 deletions(-) create mode 100644 wagtail/tests/templates/tests/form_page.html create mode 100644 wagtail/tests/templates/tests/form_page_landing.html diff --git a/runtests.py b/runtests.py index 9e3e1718b1..16176978f2 100755 --- a/runtests.py +++ b/runtests.py @@ -80,6 +80,7 @@ if not settings.configured: 'wagtail.wagtailembeds', 'wagtail.wagtailsearch', 'wagtail.wagtailredirects', + 'wagtail.wagtailforms', 'wagtail.tests', ], PASSWORD_HASHERS=( diff --git a/wagtail/tests/fixtures/test.json b/wagtail/tests/fixtures/test.json index 571a9df3f9..9e1163b541 100644 --- a/wagtail/tests/fixtures/test.json +++ b/wagtail/tests/fixtures/test.json @@ -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", diff --git a/wagtail/tests/models.py b/wagtail/tests/models.py index 9124cb253d..4ef327ff57 100644 --- a/wagtail/tests/models.py +++ b/wagtail/tests/models.py @@ -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") +] diff --git a/wagtail/tests/templates/tests/form_page.html b/wagtail/tests/templates/tests/form_page.html new file mode 100644 index 0000000000..5fbd2ae9f2 --- /dev/null +++ b/wagtail/tests/templates/tests/form_page.html @@ -0,0 +1,15 @@ +{% load pageurl %} + + + + {{ self.title }} + + +

{{ self.title }}

+
+ {% csrf_token %} + {{ form.as_p }} + +
+ + diff --git a/wagtail/tests/templates/tests/form_page_landing.html b/wagtail/tests/templates/tests/form_page_landing.html new file mode 100644 index 0000000000..e29a6a942f --- /dev/null +++ b/wagtail/tests/templates/tests/form_page_landing.html @@ -0,0 +1,11 @@ +{% load pageurl %} + + + + {{ self.title }} + + +

{{ self.title }}

+

Thank you for your feedback.

+ + diff --git a/wagtail/wagtailforms/tests.py b/wagtail/wagtailforms/tests.py index 95cbacbf0c..9456c7a675 100644 --- a/wagtail/wagtailforms/tests.py +++ b/wagtail/wagtailforms/tests.py @@ -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())