tests.py 5.4 KB

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