tests.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from __future__ import unicode_literals
  2. from django.contrib.admin.utils import quote
  3. from django.core.urlresolvers import reverse
  4. from django.template.response import TemplateResponse
  5. from django.test import TestCase, override_settings
  6. from .models import Action, Person, Car
  7. @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
  8. class AdminCustomUrlsTest(TestCase):
  9. """
  10. Remember that:
  11. * The Action model has a CharField PK.
  12. * The ModelAdmin for Action customizes the add_view URL, it's
  13. '<app name>/<model name>/!add/'
  14. """
  15. urls = 'admin_custom_urls.urls'
  16. fixtures = ['users.json', 'actions.json']
  17. def setUp(self):
  18. self.client.login(username='super', password='secret')
  19. def tearDown(self):
  20. self.client.logout()
  21. def testBasicAddGet(self):
  22. """
  23. Ensure GET on the add_view works.
  24. """
  25. response = self.client.get('/admin/admin_custom_urls/action/!add/')
  26. self.assertIsInstance(response, TemplateResponse)
  27. self.assertEqual(response.status_code, 200)
  28. def testAddWithGETArgs(self):
  29. """
  30. Ensure GET on the add_view plus specifying a field value in the query
  31. string works.
  32. """
  33. response = self.client.get('/admin/admin_custom_urls/action/!add/', {'name': 'My Action'})
  34. self.assertEqual(response.status_code, 200)
  35. self.assertContains(response, 'value="My Action"')
  36. def testBasicAddPost(self):
  37. """
  38. Ensure POST on add_view works.
  39. """
  40. post_data = {
  41. '_popup': '1',
  42. "name": 'Action added through a popup',
  43. "description": "Description of added action",
  44. }
  45. response = self.client.post('/admin/admin_custom_urls/action/!add/', post_data)
  46. self.assertEqual(response.status_code, 200)
  47. self.assertContains(response, 'dismissAddAnotherPopup')
  48. self.assertContains(response, 'Action added through a popup')
  49. def testAdminUrlsNoClash(self):
  50. """
  51. Test that some admin URLs work correctly.
  52. """
  53. # Should get the change_view for model instance with PK 'add', not show
  54. # the add_view
  55. response = self.client.get('/admin/admin_custom_urls/action/add/')
  56. self.assertEqual(response.status_code, 200)
  57. self.assertContains(response, 'Change action')
  58. # Ditto, but use reverse() to build the URL
  59. url = reverse('admin:%s_action_change' % Action._meta.app_label,
  60. args=(quote('add'),))
  61. response = self.client.get(url)
  62. self.assertEqual(response.status_code, 200)
  63. self.assertContains(response, 'Change action')
  64. # Should correctly get the change_view for the model instance with the
  65. # funny-looking PK (the one wth a 'path/to/html/document.html' value)
  66. url = reverse('admin:%s_action_change' % Action._meta.app_label,
  67. args=(quote("path/to/html/document.html"),))
  68. response = self.client.get(url)
  69. self.assertEqual(response.status_code, 200)
  70. self.assertContains(response, 'Change action')
  71. self.assertContains(response, 'value="path/to/html/document.html"')
  72. @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
  73. class CustomRedirects(TestCase):
  74. urls = 'admin_custom_urls.urls'
  75. fixtures = ['users.json', 'actions.json']
  76. def setUp(self):
  77. self.client.login(username='super', password='secret')
  78. def tearDown(self):
  79. self.client.logout()
  80. def test_post_save_add_redirect(self):
  81. """
  82. Ensures that ModelAdmin.response_post_save_add() controls the
  83. redirection after the 'Save' button has been pressed when adding a
  84. new object.
  85. Refs 8001, 18310, 19505.
  86. """
  87. post_data = {'name': 'John Doe'}
  88. self.assertEqual(Person.objects.count(), 0)
  89. response = self.client.post(
  90. reverse('admin:admin_custom_urls_person_add'), post_data)
  91. persons = Person.objects.all()
  92. self.assertEqual(len(persons), 1)
  93. self.assertRedirects(
  94. response, reverse('admin:admin_custom_urls_person_history', args=[persons[0].pk]))
  95. def test_post_save_change_redirect(self):
  96. """
  97. Ensures that ModelAdmin.response_post_save_change() controls the
  98. redirection after the 'Save' button has been pressed when editing an
  99. existing object.
  100. Refs 8001, 18310, 19505.
  101. """
  102. Person.objects.create(name='John Doe')
  103. self.assertEqual(Person.objects.count(), 1)
  104. person = Person.objects.all()[0]
  105. post_data = {'name': 'Jack Doe'}
  106. response = self.client.post(
  107. reverse('admin:admin_custom_urls_person_change', args=[person.pk]), post_data)
  108. self.assertRedirects(
  109. response, reverse('admin:admin_custom_urls_person_delete', args=[person.pk]))
  110. def test_post_url_continue(self):
  111. """
  112. Ensures that the ModelAdmin.response_add()'s parameter `post_url_continue`
  113. controls the redirection after an object has been created.
  114. """
  115. post_data = {'name': 'SuperFast', '_continue': '1'}
  116. self.assertEqual(Car.objects.count(), 0)
  117. response = self.client.post(
  118. reverse('admin:admin_custom_urls_car_add'), post_data)
  119. cars = Car.objects.all()
  120. self.assertEqual(len(cars), 1)
  121. self.assertRedirects(
  122. response, reverse('admin:admin_custom_urls_car_history', args=[cars[0].pk]))