mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-11 08:43:10 +00:00
A few improvements to the wagtailforms submission tests
This commit is contained in:
parent
a104c5eb16
commit
2a84d7ef1d
2 changed files with 26 additions and 5 deletions
3
wagtail/tests/fixtures/test.json
vendored
3
wagtail/tests/fixtures/test.json
vendored
|
|
@ -183,6 +183,9 @@
|
|||
"pk": 8,
|
||||
"model": "tests.formpage",
|
||||
"fields": {
|
||||
"to_address": "to@email.com",
|
||||
"from_address": "from@email.com",
|
||||
"subject": "The subject"
|
||||
}
|
||||
},
|
||||
|
||||
|
|
|
|||
|
|
@ -1,32 +1,50 @@
|
|||
from django.test import TestCase
|
||||
from django.core import mail
|
||||
|
||||
from wagtail.wagtailcore.models import Page
|
||||
from wagtail.wagtailforms.models import FormSubmission
|
||||
|
||||
|
||||
class TestFormSubmission(TestCase):
|
||||
fixtures = ['test.json']
|
||||
|
||||
def test_get_form(self):
|
||||
response = self.client.get('/contact-us/')
|
||||
|
||||
# Check response
|
||||
self.assertContains(response, """<label for="id_your-email">Your email</label>""")
|
||||
self.assertNotContains(response, "Thank you for your feedback")
|
||||
self.assertTemplateUsed(response, 'tests/form_page.html')
|
||||
self.assertTemplateNotUsed(response, 'tests/form_page_landing.html')
|
||||
|
||||
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")
|
||||
|
||||
# Check response
|
||||
self.assertContains(response, "Enter a valid email address.")
|
||||
self.assertTemplateUsed(response, 'tests/form_page.html')
|
||||
self.assertTemplateNotUsed(response, 'tests/form_page_landing.html')
|
||||
|
||||
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")
|
||||
|
||||
# Check response
|
||||
self.assertContains(response, "Thank you for your feedback.")
|
||||
self.assertTemplateNotUsed(response, 'tests/form_page.html')
|
||||
self.assertTemplateUsed(response, 'tests/form_page_landing.html')
|
||||
|
||||
# Check that an email was sent
|
||||
self.assertEqual(len(mail.outbox), 1)
|
||||
self.assertEqual(mail.outbox[0].subject, "The subject")
|
||||
self.assertTrue("Your message: hello world" in mail.outbox[0].body)
|
||||
self.assertEqual(mail.outbox[0].to, ['to@email.com'])
|
||||
self.assertEqual(mail.outbox[0].from_email, 'from@email.com')
|
||||
|
||||
# Check that form submission was saved correctly
|
||||
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 a new issue