test_models.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. # -*- coding: utf-8 -*-
  2. import json
  3. from django.core import mail
  4. from django.test import TestCase
  5. from wagtail.tests.testapp.models import CustomFormPageSubmission, FormField, JadeFormPage
  6. from wagtail.tests.utils import WagtailTestUtils
  7. from wagtail.core.models import Page
  8. from wagtail.contrib.forms.models import FormSubmission
  9. from wagtail.contrib.forms.tests.utils import make_form_page, make_form_page_with_custom_submission
  10. class TestFormSubmission(TestCase):
  11. def setUp(self):
  12. # Create a form page
  13. self.form_page = make_form_page()
  14. def test_get_form(self):
  15. response = self.client.get('/contact-us/')
  16. # Check response
  17. self.assertContains(response, """<label for="id_your-email">Your email</label>""")
  18. self.assertTemplateUsed(response, 'tests/form_page.html')
  19. self.assertTemplateNotUsed(response, 'tests/form_page_landing.html')
  20. # check that variables defined in get_context are passed through to the template (#1429)
  21. self.assertContains(response, "<p>hello world</p>")
  22. def test_post_invalid_form(self):
  23. response = self.client.post('/contact-us/', {
  24. 'your-email': 'bob',
  25. 'your-message': 'hello world',
  26. 'your-choices': ''
  27. })
  28. # Check response
  29. self.assertContains(response, "Enter a valid email address.")
  30. self.assertTemplateUsed(response, 'tests/form_page.html')
  31. self.assertTemplateNotUsed(response, 'tests/form_page_landing.html')
  32. def test_post_valid_form(self):
  33. response = self.client.post('/contact-us/', {
  34. 'your-email': 'bob@example.com',
  35. 'your-message': 'hello world',
  36. 'your-choices': {'foo': '', 'bar': '', 'baz': ''}
  37. })
  38. # Check response
  39. self.assertContains(response, "Thank you for your feedback.")
  40. self.assertTemplateNotUsed(response, 'tests/form_page.html')
  41. self.assertTemplateUsed(response, 'tests/form_page_landing.html')
  42. # check that variables defined in get_context are passed through to the template (#1429)
  43. self.assertContains(response, "<p>hello world</p>")
  44. # Check that an email was sent
  45. self.assertEqual(len(mail.outbox), 1)
  46. self.assertEqual(mail.outbox[0].subject, "The subject")
  47. self.assertIn("Your message: hello world", mail.outbox[0].body)
  48. self.assertEqual(mail.outbox[0].to, ['to@email.com'])
  49. self.assertEqual(mail.outbox[0].from_email, 'from@email.com')
  50. # Check that form submission was saved correctly
  51. form_page = Page.objects.get(url_path='/home/contact-us/')
  52. self.assertTrue(FormSubmission.objects.filter(page=form_page, form_data__contains='hello world').exists())
  53. def test_post_unicode_characters(self):
  54. self.client.post('/contact-us/', {
  55. 'your-email': 'bob@example.com',
  56. 'your-message': 'こんにちは、世界',
  57. 'your-choices': {'foo': '', 'bar': '', 'baz': ''}
  58. })
  59. # Check the email
  60. self.assertEqual(len(mail.outbox), 1)
  61. self.assertIn("Your message: こんにちは、世界", mail.outbox[0].body)
  62. # Check the form submission
  63. submission = FormSubmission.objects.get()
  64. submission_data = json.loads(submission.form_data)
  65. self.assertEqual(submission_data['your-message'], 'こんにちは、世界')
  66. def test_post_multiple_values(self):
  67. response = self.client.post('/contact-us/', {
  68. 'your-email': 'bob@example.com',
  69. 'your-message': 'hello world',
  70. 'your-choices': {'foo': 'on', 'bar': 'on', 'baz': 'on'}
  71. })
  72. # Check response
  73. self.assertContains(response, "Thank you for your feedback.")
  74. self.assertTemplateNotUsed(response, 'tests/form_page.html')
  75. self.assertTemplateUsed(response, 'tests/form_page_landing.html')
  76. # Check that the three checkbox values were saved correctly
  77. form_page = Page.objects.get(url_path='/home/contact-us/')
  78. submission = FormSubmission.objects.filter(
  79. page=form_page, form_data__contains='hello world'
  80. )
  81. self.assertIn("foo", submission[0].form_data)
  82. self.assertIn("bar", submission[0].form_data)
  83. self.assertIn("baz", submission[0].form_data)
  84. # Check that the all the multiple checkbox values are serialised in the
  85. # email correctly
  86. self.assertEqual(len(mail.outbox), 1)
  87. self.assertIn("bar", mail.outbox[0].body)
  88. self.assertIn("foo", mail.outbox[0].body)
  89. self.assertIn("baz", mail.outbox[0].body)
  90. def test_post_blank_checkbox(self):
  91. response = self.client.post('/contact-us/', {
  92. 'your-email': 'bob@example.com',
  93. 'your-message': 'hello world',
  94. 'your-choices': {},
  95. })
  96. # Check response
  97. self.assertContains(response, "Thank you for your feedback.")
  98. self.assertTemplateNotUsed(response, 'tests/form_page.html')
  99. self.assertTemplateUsed(response, 'tests/form_page_landing.html')
  100. # Check that the checkbox was serialised in the email correctly
  101. self.assertEqual(len(mail.outbox), 1)
  102. self.assertIn("Your choices: ", mail.outbox[0].body)
  103. class TestFormWithCustomSubmission(TestCase, WagtailTestUtils):
  104. def setUp(self):
  105. # Create a form page
  106. self.form_page = make_form_page_with_custom_submission()
  107. self.user = self.login()
  108. def test_get_form(self):
  109. response = self.client.get('/contact-us/')
  110. # Check response
  111. self.assertContains(response, """<label for="id_your-email">Your email</label>""")
  112. self.assertTemplateUsed(response, 'tests/form_page_with_custom_submission.html')
  113. self.assertTemplateNotUsed(response, 'tests/form_page_with_custom_submission_landing.html')
  114. self.assertNotContains(response, '<div>You must log in first.</div>', html=True)
  115. self.assertContains(response, '<p>Boring intro text</p>', html=True)
  116. # check that variables defined in get_context are passed through to the template (#1429)
  117. self.assertContains(response, "<p>hello world</p>")
  118. def test_get_form_with_anonymous_user(self):
  119. self.client.logout()
  120. response = self.client.get('/contact-us/')
  121. # Check response
  122. self.assertNotContains(response, """<label for="id_your-email">Your email</label>""")
  123. self.assertTemplateUsed(response, 'tests/form_page_with_custom_submission.html')
  124. self.assertTemplateNotUsed(response, 'tests/form_page_with_custom_submission_landing.html')
  125. self.assertContains(response, '<div>You must log in first.</div>', html=True)
  126. self.assertNotContains(response, '<p>Boring intro text</p>', html=True)
  127. # check that variables defined in get_context are passed through to the template (#1429)
  128. self.assertContains(response, "<p>hello world</p>")
  129. def test_post_invalid_form(self):
  130. response = self.client.post('/contact-us/', {
  131. 'your-email': 'bob',
  132. 'your-message': 'hello world',
  133. 'your-choices': ''
  134. })
  135. # Check response
  136. self.assertContains(response, "Enter a valid email address.")
  137. self.assertTemplateUsed(response, 'tests/form_page_with_custom_submission.html')
  138. self.assertTemplateNotUsed(response, 'tests/form_page_with_custom_submission_landing.html')
  139. def test_post_valid_form(self):
  140. response = self.client.post('/contact-us/', {
  141. 'your-email': 'bob@example.com',
  142. 'your-message': 'hello world',
  143. 'your-choices': {'foo': '', 'bar': '', 'baz': ''}
  144. })
  145. # Check response
  146. self.assertContains(response, "Thank you for your patience!")
  147. self.assertTemplateNotUsed(response, 'tests/form_page_with_custom_submission.html')
  148. self.assertTemplateUsed(response, 'tests/form_page_with_custom_submission_landing.html')
  149. # check that variables defined in get_context are passed through to the template (#1429)
  150. self.assertContains(response, "<p>hello world</p>")
  151. # Check that an email was sent
  152. self.assertEqual(len(mail.outbox), 1)
  153. self.assertEqual(mail.outbox[0].subject, "The subject")
  154. self.assertIn("Your message: hello world", mail.outbox[0].body)
  155. self.assertEqual(mail.outbox[0].to, ['to@email.com'])
  156. self.assertEqual(mail.outbox[0].from_email, 'from@email.com')
  157. # Check that form submission was saved correctly
  158. form_page = Page.objects.get(url_path='/home/contact-us/')
  159. self.assertTrue(CustomFormPageSubmission.objects.filter(page=form_page, form_data__contains='hello world').exists())
  160. def test_post_form_twice(self):
  161. # First submission
  162. response = self.client.post('/contact-us/', {
  163. 'your-email': 'bob@example.com',
  164. 'your-message': 'hello world',
  165. 'your-choices': {'foo': '', 'bar': '', 'baz': ''}
  166. })
  167. # Check response
  168. self.assertTemplateNotUsed(response, 'tests/form_page_with_custom_submission.html')
  169. self.assertTemplateUsed(response, 'tests/form_page_with_custom_submission_landing.html')
  170. self.assertContains(response, '<p>Thank you for your patience!</p>', html=True)
  171. self.assertNotContains(response, '<div>The form is already filled.</div>', html=True)
  172. # Check that first form submission was saved correctly
  173. submissions_qs = CustomFormPageSubmission.objects.filter(user=self.user, page=self.form_page)
  174. self.assertEqual(submissions_qs.count(), 1)
  175. self.assertTrue(submissions_qs.filter(form_data__contains='hello world').exists())
  176. # Second submission
  177. response = self.client.post('/contact-us/', {
  178. 'your-email': 'bob@example.com',
  179. 'your-message': 'hello world',
  180. 'your-choices': {'foo': '', 'bar': '', 'baz': ''}
  181. })
  182. # Check response
  183. self.assertTemplateUsed(response, 'tests/form_page_with_custom_submission.html')
  184. self.assertTemplateNotUsed(response, 'tests/form_page_with_custom_submission_landing.html')
  185. self.assertNotContains(response, '<p>Thank you for your patience!</p>', html=True)
  186. self.assertContains(response, '<div>The form is already filled.</div>', html=True)
  187. self.assertNotContains(response, '<div>You must log in first.</div>', html=True)
  188. self.assertNotContains(response, '<p>Boring intro text</p>', html=True)
  189. # Check that first submission exists and second submission wasn't saved
  190. submissions_qs = CustomFormPageSubmission.objects.filter(user=self.user, page=self.form_page)
  191. self.assertEqual(submissions_qs.count(), 1)
  192. self.assertTrue(submissions_qs.filter(form_data__contains='hello world').exists())
  193. self.assertFalse(submissions_qs.filter(form_data__contains='hello cruel world').exists())
  194. def test_post_unicode_characters(self):
  195. self.client.post('/contact-us/', {
  196. 'your-email': 'bob@example.com',
  197. 'your-message': 'こんにちは、世界',
  198. 'your-choices': {'foo': '', 'bar': '', 'baz': ''}
  199. })
  200. # Check the email
  201. self.assertEqual(len(mail.outbox), 1)
  202. self.assertIn("Your message: こんにちは、世界", mail.outbox[0].body)
  203. # Check the form submission
  204. submission = CustomFormPageSubmission.objects.get()
  205. submission_data = json.loads(submission.form_data)
  206. self.assertEqual(submission_data['your-message'], 'こんにちは、世界')
  207. def test_post_multiple_values(self):
  208. response = self.client.post('/contact-us/', {
  209. 'your-email': 'bob@example.com',
  210. 'your-message': 'hello world',
  211. 'your-choices': {'foo': 'on', 'bar': 'on', 'baz': 'on'}
  212. })
  213. # Check response
  214. self.assertContains(response, "Thank you for your patience!")
  215. self.assertTemplateNotUsed(response, 'tests/form_page_with_custom_submission.html')
  216. self.assertTemplateUsed(response, 'tests/form_page_with_custom_submission_landing.html')
  217. # Check that the three checkbox values were saved correctly
  218. form_page = Page.objects.get(url_path='/home/contact-us/')
  219. submission = CustomFormPageSubmission.objects.filter(
  220. page=form_page, form_data__contains='hello world'
  221. )
  222. self.assertIn("foo", submission[0].form_data)
  223. self.assertIn("bar", submission[0].form_data)
  224. self.assertIn("baz", submission[0].form_data)
  225. def test_post_blank_checkbox(self):
  226. response = self.client.post('/contact-us/', {
  227. 'your-email': 'bob@example.com',
  228. 'your-message': 'hello world',
  229. 'your-choices': {},
  230. })
  231. # Check response
  232. self.assertContains(response, "Thank you for your patience!")
  233. self.assertTemplateNotUsed(response, 'tests/form_page_with_custom_submission.html')
  234. self.assertTemplateUsed(response, 'tests/form_page_with_custom_submission_landing.html')
  235. # Check that the checkbox was serialised in the email correctly
  236. self.assertEqual(len(mail.outbox), 1)
  237. self.assertIn("Your choices: None", mail.outbox[0].body)
  238. class TestFormSubmissionWithMultipleRecipients(TestCase):
  239. def setUp(self):
  240. # Create a form page
  241. self.form_page = make_form_page(to_address='to@email.com, another@email.com')
  242. def test_post_valid_form(self):
  243. response = self.client.post('/contact-us/', {
  244. 'your-email': 'bob@example.com',
  245. 'your-message': 'hello world',
  246. 'your-choices': {'foo': '', 'bar': '', 'baz': ''}
  247. })
  248. # Check response
  249. self.assertContains(response, "Thank you for your feedback.")
  250. self.assertTemplateNotUsed(response, 'tests/form_page.html')
  251. self.assertTemplateUsed(response, 'tests/form_page_landing.html')
  252. # check that variables defined in get_context are passed through to the template (#1429)
  253. self.assertContains(response, "<p>hello world</p>")
  254. # Check that one email was sent, but to two recipients
  255. self.assertEqual(len(mail.outbox), 1)
  256. self.assertEqual(mail.outbox[0].subject, "The subject")
  257. self.assertIn("Your message: hello world", mail.outbox[0].body)
  258. self.assertEqual(mail.outbox[0].from_email, 'from@email.com')
  259. self.assertEqual(set(mail.outbox[0].to), {'to@email.com', 'another@email.com'})
  260. # Check that form submission was saved correctly
  261. form_page = Page.objects.get(url_path='/home/contact-us/')
  262. self.assertTrue(FormSubmission.objects.filter(page=form_page, form_data__contains='hello world').exists())
  263. class TestFormSubmissionWithMultipleRecipientsAndWithCustomSubmission(TestCase, WagtailTestUtils):
  264. def setUp(self):
  265. # Create a form page
  266. self.form_page = make_form_page_with_custom_submission(
  267. to_address='to@email.com, another@email.com'
  268. )
  269. self.user = self.login()
  270. def test_post_valid_form(self):
  271. response = self.client.post('/contact-us/', {
  272. 'your-email': 'bob@example.com',
  273. 'your-message': 'hello world',
  274. 'your-choices': {'foo': '', 'bar': '', 'baz': ''}
  275. })
  276. # Check response
  277. self.assertContains(response, "Thank you for your patience!")
  278. self.assertTemplateNotUsed(response, 'tests/form_page_with_custom_submission.html')
  279. self.assertTemplateUsed(response, 'tests/form_page_with_custom_submission_landing.html')
  280. # check that variables defined in get_context are passed through to the template (#1429)
  281. self.assertContains(response, "<p>hello world</p>")
  282. # Check that one email was sent, but to two recipients
  283. self.assertEqual(len(mail.outbox), 1)
  284. self.assertEqual(mail.outbox[0].subject, "The subject")
  285. self.assertIn("Your message: hello world", mail.outbox[0].body)
  286. self.assertEqual(mail.outbox[0].from_email, 'from@email.com')
  287. self.assertEqual(set(mail.outbox[0].to), {'to@email.com', 'another@email.com'})
  288. # Check that form submission was saved correctly
  289. form_page = Page.objects.get(url_path='/home/contact-us/')
  290. self.assertTrue(
  291. CustomFormPageSubmission.objects.filter(page=form_page, form_data__contains='hello world').exists()
  292. )
  293. class TestIssue798(TestCase):
  294. fixtures = ['test.json']
  295. def setUp(self):
  296. self.assertTrue(self.client.login(username='siteeditor', password='password'))
  297. self.form_page = Page.objects.get(url_path='/home/contact-us/').specific
  298. # Add a number field to the page
  299. FormField.objects.create(
  300. page=self.form_page,
  301. label="Your favourite number",
  302. field_type='number',
  303. )
  304. def test_post(self):
  305. response = self.client.post('/contact-us/', {
  306. 'your-email': 'bob@example.com',
  307. 'your-message': 'hello world',
  308. 'your-choices': {'foo': '', 'bar': '', 'baz': ''},
  309. 'your-favourite-number': '7.3',
  310. })
  311. # Check response
  312. self.assertTemplateUsed(response, 'tests/form_page_landing.html')
  313. # Check that form submission was saved correctly
  314. self.assertTrue(FormSubmission.objects.filter(page=self.form_page, form_data__contains='7.3').exists())
  315. class TestNonHtmlExtension(TestCase):
  316. fixtures = ['test.json']
  317. def test_non_html_extension(self):
  318. form_page = JadeFormPage(title="test")
  319. self.assertEqual(form_page.landing_page_template, "tests/form_page_landing.jade")