test_edit.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. from __future__ import absolute_import
  2. import warnings
  3. from django.core.exceptions import ImproperlyConfigured
  4. from django.core.urlresolvers import reverse
  5. from django import forms
  6. from django.test import TestCase
  7. from django.utils.unittest import expectedFailure
  8. from django.views.generic.base import View
  9. from django.views.generic.edit import FormMixin, CreateView, UpdateView
  10. from . import views
  11. from .models import Artist, Author
  12. class FormMixinTests(TestCase):
  13. def test_initial_data(self):
  14. """ Test instance independence of initial data dict (see #16138) """
  15. initial_1 = FormMixin().get_initial()
  16. initial_1['foo'] = 'bar'
  17. initial_2 = FormMixin().get_initial()
  18. self.assertNotEqual(initial_1, initial_2)
  19. class BasicFormTests(TestCase):
  20. urls = 'generic_views.urls'
  21. def test_post_data(self):
  22. res = self.client.post('/contact/', {'name': "Me", 'message': "Hello"})
  23. self.assertRedirects(res, 'http://testserver/list/authors/')
  24. class ModelFormMixinTests(TestCase):
  25. def test_get_form(self):
  26. form_class = views.AuthorGetQuerySetFormView().get_form_class()
  27. self.assertEqual(form_class._meta.model, Author)
  28. class CreateViewTests(TestCase):
  29. urls = 'generic_views.urls'
  30. def test_create(self):
  31. res = self.client.get('/edit/authors/create/')
  32. self.assertEqual(res.status_code, 200)
  33. self.assertTrue(isinstance(res.context['form'], forms.ModelForm))
  34. self.assertTrue(isinstance(res.context['view'], View))
  35. self.assertFalse('object' in res.context)
  36. self.assertFalse('author' in res.context)
  37. self.assertTemplateUsed(res, 'generic_views/author_form.html')
  38. res = self.client.post('/edit/authors/create/',
  39. {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  40. self.assertEqual(res.status_code, 302)
  41. self.assertRedirects(res, 'http://testserver/list/authors/')
  42. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
  43. def test_create_invalid(self):
  44. res = self.client.post('/edit/authors/create/',
  45. {'name': 'A' * 101, 'slug': 'randall-munroe'})
  46. self.assertEqual(res.status_code, 200)
  47. self.assertTemplateUsed(res, 'generic_views/author_form.html')
  48. self.assertEqual(len(res.context['form'].errors), 1)
  49. self.assertEqual(Author.objects.count(), 0)
  50. def test_create_with_object_url(self):
  51. res = self.client.post('/edit/artists/create/',
  52. {'name': 'Rene Magritte'})
  53. self.assertEqual(res.status_code, 302)
  54. artist = Artist.objects.get(name='Rene Magritte')
  55. self.assertRedirects(res, 'http://testserver/detail/artist/%d/' % artist.pk)
  56. self.assertQuerysetEqual(Artist.objects.all(), ['<Artist: Rene Magritte>'])
  57. def test_create_with_redirect(self):
  58. res = self.client.post('/edit/authors/create/redirect/',
  59. {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  60. self.assertEqual(res.status_code, 302)
  61. self.assertRedirects(res, 'http://testserver/edit/authors/create/')
  62. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
  63. def test_create_with_interpolated_redirect(self):
  64. res = self.client.post('/edit/authors/create/interpolate_redirect/',
  65. {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  66. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
  67. self.assertEqual(res.status_code, 302)
  68. pk = Author.objects.all()[0].pk
  69. self.assertRedirects(res, 'http://testserver/edit/author/%d/update/' % pk)
  70. def test_create_with_special_properties(self):
  71. res = self.client.get('/edit/authors/create/special/')
  72. self.assertEqual(res.status_code, 200)
  73. self.assertTrue(isinstance(res.context['form'], views.AuthorForm))
  74. self.assertFalse('object' in res.context)
  75. self.assertFalse('author' in res.context)
  76. self.assertTemplateUsed(res, 'generic_views/form.html')
  77. res = self.client.post('/edit/authors/create/special/',
  78. {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  79. self.assertEqual(res.status_code, 302)
  80. obj = Author.objects.get(slug='randall-munroe')
  81. self.assertRedirects(res, reverse('author_detail', kwargs={'pk': obj.pk}))
  82. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
  83. def test_create_without_redirect(self):
  84. try:
  85. res = self.client.post('/edit/authors/create/naive/',
  86. {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  87. self.fail('Should raise exception -- No redirect URL provided, and no get_absolute_url provided')
  88. except ImproperlyConfigured:
  89. pass
  90. def test_create_restricted(self):
  91. res = self.client.post('/edit/authors/create/restricted/',
  92. {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  93. self.assertEqual(res.status_code, 302)
  94. self.assertRedirects(res, 'http://testserver/accounts/login/?next=/edit/authors/create/restricted/')
  95. def test_create_view_with_restricted_fields(self):
  96. class MyCreateView(CreateView):
  97. model = Author
  98. fields = ['name']
  99. self.assertEqual(list(MyCreateView().get_form_class().base_fields),
  100. ['name'])
  101. def test_create_view_all_fields(self):
  102. with warnings.catch_warnings(record=True) as w:
  103. warnings.simplefilter("always", PendingDeprecationWarning)
  104. class MyCreateView(CreateView):
  105. model = Author
  106. fields = '__all__'
  107. self.assertEqual(list(MyCreateView().get_form_class().base_fields),
  108. ['name', 'slug'])
  109. self.assertEqual(len(w), 0)
  110. def test_create_view_without_explicit_fields(self):
  111. with warnings.catch_warnings(record=True) as w:
  112. warnings.simplefilter("always", PendingDeprecationWarning)
  113. class MyCreateView(CreateView):
  114. model = Author
  115. # Until end of the deprecation cycle, should still create the form
  116. # as before:
  117. self.assertEqual(list(MyCreateView().get_form_class().base_fields),
  118. ['name', 'slug'])
  119. # but with a warning:
  120. self.assertEqual(w[0].category, PendingDeprecationWarning)
  121. class UpdateViewTests(TestCase):
  122. urls = 'generic_views.urls'
  123. def test_update_post(self):
  124. a = Author.objects.create(
  125. name='Randall Munroe',
  126. slug='randall-munroe',
  127. )
  128. res = self.client.get('/edit/author/%d/update/' % a.pk)
  129. self.assertEqual(res.status_code, 200)
  130. self.assertTrue(isinstance(res.context['form'], forms.ModelForm))
  131. self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
  132. self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk))
  133. self.assertTemplateUsed(res, 'generic_views/author_form.html')
  134. # Modification with both POST and PUT (browser compatible)
  135. res = self.client.post('/edit/author/%d/update/' % a.pk,
  136. {'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe'})
  137. self.assertEqual(res.status_code, 302)
  138. self.assertRedirects(res, 'http://testserver/list/authors/')
  139. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
  140. @expectedFailure
  141. def test_update_put(self):
  142. a = Author.objects.create(
  143. name='Randall Munroe',
  144. slug='randall-munroe',
  145. )
  146. res = self.client.get('/edit/author/%d/update/' % a.pk)
  147. self.assertEqual(res.status_code, 200)
  148. self.assertTemplateUsed(res, 'generic_views/author_form.html')
  149. res = self.client.put('/edit/author/%d/update/' % a.pk,
  150. {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
  151. # Here is the expected failure. PUT data are not processed in any special
  152. # way by django. So the request will equal to a POST without data, hence
  153. # the form will be invalid and redisplayed with errors (status code 200).
  154. # See also #12635
  155. self.assertEqual(res.status_code, 302)
  156. self.assertRedirects(res, 'http://testserver/list/authors/')
  157. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
  158. def test_update_invalid(self):
  159. a = Author.objects.create(
  160. name='Randall Munroe',
  161. slug='randall-munroe',
  162. )
  163. res = self.client.post('/edit/author/%d/update/' % a.pk,
  164. {'name': 'A' * 101, 'slug': 'randall-munroe'})
  165. self.assertEqual(res.status_code, 200)
  166. self.assertTemplateUsed(res, 'generic_views/author_form.html')
  167. self.assertEqual(len(res.context['form'].errors), 1)
  168. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
  169. def test_update_with_object_url(self):
  170. a = Artist.objects.create(name='Rene Magritte')
  171. res = self.client.post('/edit/artists/%d/update/' % a.pk,
  172. {'name': 'Rene Magritte'})
  173. self.assertEqual(res.status_code, 302)
  174. self.assertRedirects(res, 'http://testserver/detail/artist/%d/' % a.pk)
  175. self.assertQuerysetEqual(Artist.objects.all(), ['<Artist: Rene Magritte>'])
  176. def test_update_with_redirect(self):
  177. a = Author.objects.create(
  178. name='Randall Munroe',
  179. slug='randall-munroe',
  180. )
  181. res = self.client.post('/edit/author/%d/update/redirect/' % a.pk,
  182. {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
  183. self.assertEqual(res.status_code, 302)
  184. self.assertRedirects(res, 'http://testserver/edit/authors/create/')
  185. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
  186. def test_update_with_interpolated_redirect(self):
  187. a = Author.objects.create(
  188. name='Randall Munroe',
  189. slug='randall-munroe',
  190. )
  191. res = self.client.post('/edit/author/%d/update/interpolate_redirect/' % a.pk,
  192. {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
  193. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
  194. self.assertEqual(res.status_code, 302)
  195. pk = Author.objects.all()[0].pk
  196. self.assertRedirects(res, 'http://testserver/edit/author/%d/update/' % pk)
  197. def test_update_with_special_properties(self):
  198. a = Author.objects.create(
  199. name='Randall Munroe',
  200. slug='randall-munroe',
  201. )
  202. res = self.client.get('/edit/author/%d/update/special/' % a.pk)
  203. self.assertEqual(res.status_code, 200)
  204. self.assertTrue(isinstance(res.context['form'], views.AuthorForm))
  205. self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
  206. self.assertEqual(res.context['thingy'], Author.objects.get(pk=a.pk))
  207. self.assertFalse('author' in res.context)
  208. self.assertTemplateUsed(res, 'generic_views/form.html')
  209. res = self.client.post('/edit/author/%d/update/special/' % a.pk,
  210. {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
  211. self.assertEqual(res.status_code, 302)
  212. self.assertRedirects(res, 'http://testserver/detail/author/%d/' % a.pk)
  213. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
  214. def test_update_without_redirect(self):
  215. try:
  216. a = Author.objects.create(
  217. name='Randall Munroe',
  218. slug='randall-munroe',
  219. )
  220. res = self.client.post('/edit/author/%d/update/naive/' % a.pk,
  221. {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
  222. self.fail('Should raise exception -- No redirect URL provided, and no get_absolute_url provided')
  223. except ImproperlyConfigured:
  224. pass
  225. def test_update_get_object(self):
  226. a = Author.objects.create(
  227. pk=1,
  228. name='Randall Munroe',
  229. slug='randall-munroe',
  230. )
  231. res = self.client.get('/edit/author/update/')
  232. self.assertEqual(res.status_code, 200)
  233. self.assertTrue(isinstance(res.context['form'], forms.ModelForm))
  234. self.assertTrue(isinstance(res.context['view'], View))
  235. self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
  236. self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk))
  237. self.assertTemplateUsed(res, 'generic_views/author_form.html')
  238. # Modification with both POST and PUT (browser compatible)
  239. res = self.client.post('/edit/author/update/',
  240. {'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe'})
  241. self.assertEqual(res.status_code, 302)
  242. self.assertRedirects(res, 'http://testserver/list/authors/')
  243. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
  244. class DeleteViewTests(TestCase):
  245. urls = 'generic_views.urls'
  246. def test_delete_by_post(self):
  247. a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  248. res = self.client.get('/edit/author/%d/delete/' % a.pk)
  249. self.assertEqual(res.status_code, 200)
  250. self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
  251. self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk))
  252. self.assertTemplateUsed(res, 'generic_views/author_confirm_delete.html')
  253. # Deletion with POST
  254. res = self.client.post('/edit/author/%d/delete/' % a.pk)
  255. self.assertEqual(res.status_code, 302)
  256. self.assertRedirects(res, 'http://testserver/list/authors/')
  257. self.assertQuerysetEqual(Author.objects.all(), [])
  258. def test_delete_by_delete(self):
  259. # Deletion with browser compatible DELETE method
  260. a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  261. res = self.client.delete('/edit/author/%d/delete/' % a.pk)
  262. self.assertEqual(res.status_code, 302)
  263. self.assertRedirects(res, 'http://testserver/list/authors/')
  264. self.assertQuerysetEqual(Author.objects.all(), [])
  265. def test_delete_with_redirect(self):
  266. a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  267. res = self.client.post('/edit/author/%d/delete/redirect/' % a.pk)
  268. self.assertEqual(res.status_code, 302)
  269. self.assertRedirects(res, 'http://testserver/edit/authors/create/')
  270. self.assertQuerysetEqual(Author.objects.all(), [])
  271. def test_delete_with_interpolated_redirect(self):
  272. a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  273. res = self.client.post('/edit/author/%d/delete/interpolate_redirect/' % a.pk)
  274. self.assertEqual(res.status_code, 302)
  275. self.assertRedirects(res, 'http://testserver/edit/authors/create/?deleted=%d' % a.pk)
  276. self.assertQuerysetEqual(Author.objects.all(), [])
  277. def test_delete_with_special_properties(self):
  278. a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  279. res = self.client.get('/edit/author/%d/delete/special/' % a.pk)
  280. self.assertEqual(res.status_code, 200)
  281. self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
  282. self.assertEqual(res.context['thingy'], Author.objects.get(pk=a.pk))
  283. self.assertFalse('author' in res.context)
  284. self.assertTemplateUsed(res, 'generic_views/confirm_delete.html')
  285. res = self.client.post('/edit/author/%d/delete/special/' % a.pk)
  286. self.assertEqual(res.status_code, 302)
  287. self.assertRedirects(res, 'http://testserver/list/authors/')
  288. self.assertQuerysetEqual(Author.objects.all(), [])
  289. def test_delete_without_redirect(self):
  290. try:
  291. a = Author.objects.create(
  292. name='Randall Munroe',
  293. slug='randall-munroe',
  294. )
  295. res = self.client.post('/edit/author/%d/delete/naive/' % a.pk)
  296. self.fail('Should raise exception -- No redirect URL provided, and no get_absolute_url provided')
  297. except ImproperlyConfigured:
  298. pass