2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2012-10-18 23:58:52 +00:00
|
|
|
|
2013-09-16 16:52:05 +00:00
|
|
|
from django.contrib.admin.utils import quote
|
2011-09-20 18:30:06 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.template.response import TemplateResponse
|
2013-12-23 15:01:13 +00:00
|
|
|
from django.test import TestCase, override_settings
|
2011-09-20 18:30:06 +00:00
|
|
|
|
2012-12-24 21:43:28 +00:00
|
|
|
from .models import Action, Person, Car
|
2011-09-20 18:30:06 +00:00
|
|
|
|
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
|
|
|
|
ROOT_URLCONF='admin_custom_urls.urls',)
|
2011-09-20 18:30:06 +00:00
|
|
|
class AdminCustomUrlsTest(TestCase):
|
2012-08-26 20:54:49 +00:00
|
|
|
"""
|
|
|
|
Remember that:
|
|
|
|
* The Action model has a CharField PK.
|
|
|
|
* The ModelAdmin for Action customizes the add_view URL, it's
|
|
|
|
'<app name>/<model name>/!add/'
|
|
|
|
"""
|
2011-09-20 18:30:06 +00:00
|
|
|
fixtures = ['users.json', 'actions.json']
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client.login(username='super', password='secret')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
def testBasicAddGet(self):
|
|
|
|
"""
|
2012-08-26 20:54:49 +00:00
|
|
|
Ensure GET on the add_view works.
|
2011-09-20 18:30:06 +00:00
|
|
|
"""
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/admin/admin_custom_urls/action/!add/')
|
2011-09-20 18:30:06 +00:00
|
|
|
self.assertIsInstance(response, TemplateResponse)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def testAddWithGETArgs(self):
|
2012-08-26 20:54:49 +00:00
|
|
|
"""
|
|
|
|
Ensure GET on the add_view plus specifying a field value in the query
|
|
|
|
string works.
|
|
|
|
"""
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/admin/admin_custom_urls/action/!add/', {'name': 'My Action'})
|
2011-09-20 18:30:06 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2012-08-14 12:38:23 +00:00
|
|
|
self.assertContains(response, 'value="My Action"')
|
2011-09-20 18:30:06 +00:00
|
|
|
|
|
|
|
def testBasicAddPost(self):
|
|
|
|
"""
|
2012-08-26 20:54:49 +00:00
|
|
|
Ensure POST on add_view works.
|
2011-09-20 18:30:06 +00:00
|
|
|
"""
|
|
|
|
post_data = {
|
2012-06-07 16:08:47 +00:00
|
|
|
'_popup': '1',
|
|
|
|
"name": 'Action added through a popup',
|
|
|
|
"description": "Description of added action",
|
2011-09-20 18:30:06 +00:00
|
|
|
}
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.post('/admin/admin_custom_urls/action/!add/', post_data)
|
2011-09-20 18:30:06 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertContains(response, 'dismissAddAnotherPopup')
|
|
|
|
self.assertContains(response, 'Action added through a popup')
|
|
|
|
|
|
|
|
def testAdminUrlsNoClash(self):
|
|
|
|
"""
|
2012-08-26 20:54:49 +00:00
|
|
|
Test that some admin URLs work correctly.
|
2011-09-20 18:30:06 +00:00
|
|
|
"""
|
|
|
|
# Should get the change_view for model instance with PK 'add', not show
|
|
|
|
# the add_view
|
2014-01-14 15:43:27 +00:00
|
|
|
response = self.client.get('/admin/admin_custom_urls/action/add/')
|
2011-09-20 18:30:06 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertContains(response, 'Change action')
|
|
|
|
|
|
|
|
# Ditto, but use reverse() to build the URL
|
2012-08-26 20:54:49 +00:00
|
|
|
url = reverse('admin:%s_action_change' % Action._meta.app_label,
|
2012-09-25 01:02:59 +00:00
|
|
|
args=(quote('add'),))
|
2012-08-26 20:54:49 +00:00
|
|
|
response = self.client.get(url)
|
2011-09-20 18:30:06 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertContains(response, 'Change action')
|
|
|
|
|
|
|
|
# Should correctly get the change_view for the model instance with the
|
2012-08-26 20:54:49 +00:00
|
|
|
# funny-looking PK (the one wth a 'path/to/html/document.html' value)
|
|
|
|
url = reverse('admin:%s_action_change' % Action._meta.app_label,
|
2012-09-25 01:02:59 +00:00
|
|
|
args=(quote("path/to/html/document.html"),))
|
2012-08-26 20:54:49 +00:00
|
|
|
response = self.client.get(url)
|
2011-09-20 18:30:06 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertContains(response, 'Change action')
|
|
|
|
self.assertContains(response, 'value="path/to/html/document.html"')
|
2012-10-18 23:58:52 +00:00
|
|
|
|
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
|
|
|
|
ROOT_URLCONF='admin_custom_urls.urls',)
|
2012-12-22 19:00:08 +00:00
|
|
|
class CustomRedirects(TestCase):
|
|
|
|
fixtures = ['users.json', 'actions.json']
|
2012-10-18 23:58:52 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client.login(username='super', password='secret')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
|
|
|
|
2012-12-31 05:33:21 +00:00
|
|
|
def test_post_save_add_redirect(self):
|
2012-12-22 19:00:08 +00:00
|
|
|
"""
|
2012-12-31 05:33:21 +00:00
|
|
|
Ensures that ModelAdmin.response_post_save_add() controls the
|
|
|
|
redirection after the 'Save' button has been pressed when adding a
|
|
|
|
new object.
|
2012-12-22 19:00:08 +00:00
|
|
|
Refs 8001, 18310, 19505.
|
|
|
|
"""
|
2013-10-14 19:13:14 +00:00
|
|
|
post_data = {'name': 'John Doe'}
|
2012-12-22 19:00:08 +00:00
|
|
|
self.assertEqual(Person.objects.count(), 0)
|
|
|
|
response = self.client.post(
|
|
|
|
reverse('admin:admin_custom_urls_person_add'), post_data)
|
|
|
|
persons = Person.objects.all()
|
|
|
|
self.assertEqual(len(persons), 1)
|
|
|
|
self.assertRedirects(
|
|
|
|
response, reverse('admin:admin_custom_urls_person_history', args=[persons[0].pk]))
|
|
|
|
|
2012-12-31 05:33:21 +00:00
|
|
|
def test_post_save_change_redirect(self):
|
|
|
|
"""
|
|
|
|
Ensures that ModelAdmin.response_post_save_change() controls the
|
|
|
|
redirection after the 'Save' button has been pressed when editing an
|
|
|
|
existing object.
|
|
|
|
Refs 8001, 18310, 19505.
|
|
|
|
"""
|
|
|
|
Person.objects.create(name='John Doe')
|
|
|
|
self.assertEqual(Person.objects.count(), 1)
|
|
|
|
person = Person.objects.all()[0]
|
2013-10-14 19:13:14 +00:00
|
|
|
post_data = {'name': 'Jack Doe'}
|
2012-12-31 05:33:21 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse('admin:admin_custom_urls_person_change', args=[person.pk]), post_data)
|
|
|
|
self.assertRedirects(
|
|
|
|
response, reverse('admin:admin_custom_urls_person_delete', args=[person.pk]))
|
|
|
|
|
2012-12-22 19:00:08 +00:00
|
|
|
def test_post_url_continue(self):
|
|
|
|
"""
|
|
|
|
Ensures that the ModelAdmin.response_add()'s parameter `post_url_continue`
|
|
|
|
controls the redirection after an object has been created.
|
|
|
|
"""
|
2013-10-14 19:13:14 +00:00
|
|
|
post_data = {'name': 'SuperFast', '_continue': '1'}
|
2012-12-22 19:00:08 +00:00
|
|
|
self.assertEqual(Car.objects.count(), 0)
|
|
|
|
response = self.client.post(
|
|
|
|
reverse('admin:admin_custom_urls_car_add'), post_data)
|
|
|
|
cars = Car.objects.all()
|
|
|
|
self.assertEqual(len(cars), 1)
|
|
|
|
self.assertRedirects(
|
|
|
|
response, reverse('admin:admin_custom_urls_car_history', args=[cars[0].pk]))
|