/django/contrib/formtools/wizard/tests/wizardtests/tests.py

https://github.com/rskumar/django · Python · 283 lines · 241 code · 42 blank · 0 comment · 1 complexity · 1d698019f75fb64672bc22989aa4c9ac MD5 · raw file

  1. from __future__ import with_statement
  2. import os
  3. from django.test import TestCase
  4. from django.conf import settings
  5. from django.contrib.auth.models import User
  6. from django.contrib.formtools import wizard
  7. class WizardTests(object):
  8. urls = 'django.contrib.formtools.wizard.tests.wizardtests.urls'
  9. def setUp(self):
  10. self.testuser, created = User.objects.get_or_create(username='testuser1')
  11. self.wizard_step_data[0]['form1-user'] = self.testuser.pk
  12. def test_initial_call(self):
  13. response = self.client.get(self.wizard_url)
  14. wizard = response.context['wizard']
  15. self.assertEqual(response.status_code, 200)
  16. self.assertEqual(wizard['steps'].current, 'form1')
  17. self.assertEqual(wizard['steps'].step0, 0)
  18. self.assertEqual(wizard['steps'].step1, 1)
  19. self.assertEqual(wizard['steps'].last, 'form4')
  20. self.assertEqual(wizard['steps'].prev, None)
  21. self.assertEqual(wizard['steps'].next, 'form2')
  22. self.assertEqual(wizard['steps'].count, 4)
  23. def test_form_post_error(self):
  24. response = self.client.post(self.wizard_url, self.wizard_step_1_data)
  25. self.assertEqual(response.status_code, 200)
  26. self.assertEqual(response.context['wizard']['steps'].current, 'form1')
  27. self.assertEqual(response.context['wizard']['form'].errors,
  28. {'name': [u'This field is required.'],
  29. 'user': [u'This field is required.']})
  30. def test_form_post_success(self):
  31. response = self.client.post(self.wizard_url, self.wizard_step_data[0])
  32. wizard = response.context['wizard']
  33. self.assertEqual(response.status_code, 200)
  34. self.assertEqual(wizard['steps'].current, 'form2')
  35. self.assertEqual(wizard['steps'].step0, 1)
  36. self.assertEqual(wizard['steps'].prev, 'form1')
  37. self.assertEqual(wizard['steps'].next, 'form3')
  38. def test_form_stepback(self):
  39. response = self.client.get(self.wizard_url)
  40. self.assertEqual(response.status_code, 200)
  41. self.assertEqual(response.context['wizard']['steps'].current, 'form1')
  42. response = self.client.post(self.wizard_url, self.wizard_step_data[0])
  43. self.assertEqual(response.status_code, 200)
  44. self.assertEqual(response.context['wizard']['steps'].current, 'form2')
  45. response = self.client.post(self.wizard_url, {
  46. 'wizard_prev_step': response.context['wizard']['steps'].prev})
  47. self.assertEqual(response.status_code, 200)
  48. self.assertEqual(response.context['wizard']['steps'].current, 'form1')
  49. def test_template_context(self):
  50. response = self.client.get(self.wizard_url)
  51. self.assertEqual(response.status_code, 200)
  52. self.assertEqual(response.context['wizard']['steps'].current, 'form1')
  53. self.assertEqual(response.context.get('another_var', None), None)
  54. response = self.client.post(self.wizard_url, self.wizard_step_data[0])
  55. self.assertEqual(response.status_code, 200)
  56. self.assertEqual(response.context['wizard']['steps'].current, 'form2')
  57. self.assertEqual(response.context.get('another_var', None), True)
  58. def test_form_finish(self):
  59. response = self.client.get(self.wizard_url)
  60. self.assertEqual(response.status_code, 200)
  61. self.assertEqual(response.context['wizard']['steps'].current, 'form1')
  62. response = self.client.post(self.wizard_url, self.wizard_step_data[0])
  63. self.assertEqual(response.status_code, 200)
  64. self.assertEqual(response.context['wizard']['steps'].current, 'form2')
  65. post_data = self.wizard_step_data[1]
  66. post_data['form2-file1'] = open(__file__)
  67. response = self.client.post(self.wizard_url, post_data)
  68. self.assertEqual(response.status_code, 200)
  69. self.assertEqual(response.context['wizard']['steps'].current, 'form3')
  70. response = self.client.post(self.wizard_url, self.wizard_step_data[2])
  71. self.assertEqual(response.status_code, 200)
  72. self.assertEqual(response.context['wizard']['steps'].current, 'form4')
  73. response = self.client.post(self.wizard_url, self.wizard_step_data[3])
  74. self.assertEqual(response.status_code, 200)
  75. all_data = response.context['form_list']
  76. self.assertEqual(all_data[1]['file1'].read(), open(__file__).read())
  77. del all_data[1]['file1']
  78. self.assertEqual(all_data, [
  79. {'name': u'Pony', 'thirsty': True, 'user': self.testuser},
  80. {'address1': u'123 Main St', 'address2': u'Djangoland'},
  81. {'random_crap': u'blah blah'},
  82. [{'random_crap': u'blah blah'},
  83. {'random_crap': u'blah blah'}]])
  84. def test_cleaned_data(self):
  85. response = self.client.get(self.wizard_url)
  86. self.assertEqual(response.status_code, 200)
  87. response = self.client.post(self.wizard_url, self.wizard_step_data[0])
  88. self.assertEqual(response.status_code, 200)
  89. post_data = self.wizard_step_data[1]
  90. post_data['form2-file1'] = open(__file__)
  91. response = self.client.post(self.wizard_url, post_data)
  92. self.assertEqual(response.status_code, 200)
  93. response = self.client.post(self.wizard_url, self.wizard_step_data[2])
  94. self.assertEqual(response.status_code, 200)
  95. response = self.client.post(self.wizard_url, self.wizard_step_data[3])
  96. self.assertEqual(response.status_code, 200)
  97. all_data = response.context['all_cleaned_data']
  98. self.assertEqual(all_data['file1'].read(), open(__file__).read())
  99. del all_data['file1']
  100. self.assertEqual(all_data, {
  101. 'name': u'Pony', 'thirsty': True, 'user': self.testuser,
  102. 'address1': u'123 Main St', 'address2': u'Djangoland',
  103. 'random_crap': u'blah blah', 'formset-form4': [
  104. {'random_crap': u'blah blah'},
  105. {'random_crap': u'blah blah'}]})
  106. def test_manipulated_data(self):
  107. response = self.client.get(self.wizard_url)
  108. self.assertEqual(response.status_code, 200)
  109. response = self.client.post(self.wizard_url, self.wizard_step_data[0])
  110. self.assertEqual(response.status_code, 200)
  111. post_data = self.wizard_step_data[1]
  112. post_data['form2-file1'] = open(__file__)
  113. response = self.client.post(self.wizard_url, post_data)
  114. self.assertEqual(response.status_code, 200)
  115. response = self.client.post(self.wizard_url, self.wizard_step_data[2])
  116. self.assertEqual(response.status_code, 200)
  117. self.client.cookies.pop('sessionid', None)
  118. self.client.cookies.pop('wizard_cookie_contact_wizard', None)
  119. response = self.client.post(self.wizard_url, self.wizard_step_data[3])
  120. self.assertEqual(response.status_code, 200)
  121. self.assertEqual(response.context['wizard']['steps'].current, 'form1')
  122. def test_form_refresh(self):
  123. response = self.client.get(self.wizard_url)
  124. self.assertEqual(response.status_code, 200)
  125. self.assertEqual(response.context['wizard']['steps'].current, 'form1')
  126. response = self.client.post(self.wizard_url, self.wizard_step_data[0])
  127. self.assertEqual(response.status_code, 200)
  128. self.assertEqual(response.context['wizard']['steps'].current, 'form2')
  129. response = self.client.post(self.wizard_url, self.wizard_step_data[0])
  130. self.assertEqual(response.status_code, 200)
  131. self.assertEqual(response.context['wizard']['steps'].current, 'form2')
  132. post_data = self.wizard_step_data[1]
  133. post_data['form2-file1'] = open(__file__)
  134. response = self.client.post(self.wizard_url, post_data)
  135. self.assertEqual(response.status_code, 200)
  136. self.assertEqual(response.context['wizard']['steps'].current, 'form3')
  137. response = self.client.post(self.wizard_url, self.wizard_step_data[2])
  138. self.assertEqual(response.status_code, 200)
  139. self.assertEqual(response.context['wizard']['steps'].current, 'form4')
  140. response = self.client.post(self.wizard_url, self.wizard_step_data[0])
  141. self.assertEqual(response.status_code, 200)
  142. self.assertEqual(response.context['wizard']['steps'].current, 'form2')
  143. response = self.client.post(self.wizard_url, self.wizard_step_data[3])
  144. self.assertEqual(response.status_code, 200)
  145. class SessionWizardTests(WizardTests, TestCase):
  146. wizard_url = '/wiz_session/'
  147. wizard_step_1_data = {
  148. 'session_contact_wizard-current_step': 'form1',
  149. }
  150. wizard_step_data = (
  151. {
  152. 'form1-name': 'Pony',
  153. 'form1-thirsty': '2',
  154. 'session_contact_wizard-current_step': 'form1',
  155. },
  156. {
  157. 'form2-address1': '123 Main St',
  158. 'form2-address2': 'Djangoland',
  159. 'session_contact_wizard-current_step': 'form2',
  160. },
  161. {
  162. 'form3-random_crap': 'blah blah',
  163. 'session_contact_wizard-current_step': 'form3',
  164. },
  165. {
  166. 'form4-INITIAL_FORMS': '0',
  167. 'form4-TOTAL_FORMS': '2',
  168. 'form4-MAX_NUM_FORMS': '0',
  169. 'form4-0-random_crap': 'blah blah',
  170. 'form4-1-random_crap': 'blah blah',
  171. 'session_contact_wizard-current_step': 'form4',
  172. }
  173. )
  174. class CookieWizardTests(WizardTests, TestCase):
  175. wizard_url = '/wiz_cookie/'
  176. wizard_step_1_data = {
  177. 'cookie_contact_wizard-current_step': 'form1',
  178. }
  179. wizard_step_data = (
  180. {
  181. 'form1-name': 'Pony',
  182. 'form1-thirsty': '2',
  183. 'cookie_contact_wizard-current_step': 'form1',
  184. },
  185. {
  186. 'form2-address1': '123 Main St',
  187. 'form2-address2': 'Djangoland',
  188. 'cookie_contact_wizard-current_step': 'form2',
  189. },
  190. {
  191. 'form3-random_crap': 'blah blah',
  192. 'cookie_contact_wizard-current_step': 'form3',
  193. },
  194. {
  195. 'form4-INITIAL_FORMS': '0',
  196. 'form4-TOTAL_FORMS': '2',
  197. 'form4-MAX_NUM_FORMS': '0',
  198. 'form4-0-random_crap': 'blah blah',
  199. 'form4-1-random_crap': 'blah blah',
  200. 'cookie_contact_wizard-current_step': 'form4',
  201. }
  202. )
  203. class WizardTestKwargs(TestCase):
  204. wizard_url = '/wiz_other_template/'
  205. wizard_step_1_data = {
  206. 'cookie_contact_wizard-current_step': 'form1',
  207. }
  208. wizard_step_data = (
  209. {
  210. 'form1-name': 'Pony',
  211. 'form1-thirsty': '2',
  212. 'cookie_contact_wizard-current_step': 'form1',
  213. },
  214. {
  215. 'form2-address1': '123 Main St',
  216. 'form2-address2': 'Djangoland',
  217. 'cookie_contact_wizard-current_step': 'form2',
  218. },
  219. {
  220. 'form3-random_crap': 'blah blah',
  221. 'cookie_contact_wizard-current_step': 'form3',
  222. },
  223. {
  224. 'form4-INITIAL_FORMS': '0',
  225. 'form4-TOTAL_FORMS': '2',
  226. 'form4-MAX_NUM_FORMS': '0',
  227. 'form4-0-random_crap': 'blah blah',
  228. 'form4-1-random_crap': 'blah blah',
  229. 'cookie_contact_wizard-current_step': 'form4',
  230. }
  231. )
  232. urls = 'django.contrib.formtools.wizard.tests.wizardtests.urls'
  233. def setUp(self):
  234. self.testuser, created = User.objects.get_or_create(username='testuser1')
  235. self.wizard_step_data[0]['form1-user'] = self.testuser.pk
  236. def test_template(self):
  237. templates = os.path.join(os.path.dirname(__file__), 'templates')
  238. with self.settings(
  239. TEMPLATE_DIRS=list(settings.TEMPLATE_DIRS) + [templates]):
  240. response = self.client.get(self.wizard_url)
  241. self.assertTemplateUsed(response, 'other_wizard_form.html')