2013-09-17 00:52:05 +08:00
|
|
|
from django.contrib.admin.utils import quote
|
2021-05-20 02:10:58 +08:00
|
|
|
from django.contrib.admin.views.main import IS_POPUP_VAR
|
2015-02-23 08:53:57 +08:00
|
|
|
from django.contrib.auth.models import User
|
2011-09-21 02:30:06 +08:00
|
|
|
from django.template.response import TemplateResponse
|
2013-12-23 23:01:13 +08:00
|
|
|
from django.test import TestCase, override_settings
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import reverse
|
2011-09-21 02:30:06 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import Action, Car, Person
|
2011-09-21 02:30:06 +08:00
|
|
|
|
|
|
|
|
2016-02-06 04:56:52 +08:00
|
|
|
@override_settings(ROOT_URLCONF='admin_custom_urls.urls',)
|
2011-09-21 02:30:06 +08:00
|
|
|
class AdminCustomUrlsTest(TestCase):
|
2012-08-27 04:54:49 +08: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/'
|
|
|
|
"""
|
2015-02-23 08:53:57 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2016-02-06 04:56:52 +08:00
|
|
|
cls.superuser = User.objects.create_superuser(username='super', password='secret', email='super@example.com')
|
2015-02-23 08:53:57 +08:00
|
|
|
Action.objects.create(name='delete', description='Remove things.')
|
|
|
|
Action.objects.create(name='rename', description='Gives things other names.')
|
|
|
|
Action.objects.create(name='add', description='Add things.')
|
|
|
|
Action.objects.create(name='path/to/file/', description="An action with '/' in its name.")
|
|
|
|
Action.objects.create(
|
|
|
|
name='path/to/html/document.html',
|
|
|
|
description='An action with a name similar to a HTML doc path.'
|
|
|
|
)
|
|
|
|
Action.objects.create(
|
|
|
|
name='javascript:alert(\'Hello world\');">Click here</a>',
|
|
|
|
description='An action with a name suspected of being a XSS attempt'
|
|
|
|
)
|
2011-09-21 02:30:06 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
2016-02-06 04:56:52 +08:00
|
|
|
self.client.force_login(self.superuser)
|
2011-09-21 02:30:06 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_basic_add_GET(self):
|
2011-09-21 02:30:06 +08:00
|
|
|
"""
|
2012-08-27 04:54:49 +08:00
|
|
|
Ensure GET on the add_view works.
|
2011-09-21 02:30:06 +08:00
|
|
|
"""
|
2015-03-31 00:33:26 +08:00
|
|
|
add_url = reverse('admin_custom_urls:admin_custom_urls_action_add')
|
2015-02-07 06:25:15 +08:00
|
|
|
self.assertTrue(add_url.endswith('/!add/'))
|
|
|
|
response = self.client.get(add_url)
|
2011-09-21 02:30:06 +08:00
|
|
|
self.assertIsInstance(response, TemplateResponse)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_add_with_GET_args(self):
|
2012-08-27 04:54:49 +08:00
|
|
|
"""
|
|
|
|
Ensure GET on the add_view plus specifying a field value in the query
|
|
|
|
string works.
|
|
|
|
"""
|
2015-03-31 00:33:26 +08:00
|
|
|
response = self.client.get(reverse('admin_custom_urls:admin_custom_urls_action_add'), {'name': 'My Action'})
|
2012-08-14 20:38:23 +08:00
|
|
|
self.assertContains(response, 'value="My Action"')
|
2011-09-21 02:30:06 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_basic_add_POST(self):
|
2011-09-21 02:30:06 +08:00
|
|
|
"""
|
2012-08-27 04:54:49 +08:00
|
|
|
Ensure POST on add_view works.
|
2011-09-21 02:30:06 +08:00
|
|
|
"""
|
|
|
|
post_data = {
|
2021-05-20 02:10:58 +08:00
|
|
|
IS_POPUP_VAR: '1',
|
2012-06-08 00:08:47 +08:00
|
|
|
"name": 'Action added through a popup',
|
|
|
|
"description": "Description of added action",
|
2011-09-21 02:30:06 +08:00
|
|
|
}
|
2015-03-31 00:33:26 +08:00
|
|
|
response = self.client.post(reverse('admin_custom_urls:admin_custom_urls_action_add'), post_data)
|
2011-09-21 02:30:06 +08:00
|
|
|
self.assertContains(response, 'Action added through a popup')
|
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_admin_URLs_no_clash(self):
|
2011-09-21 02:30:06 +08:00
|
|
|
# Should get the change_view for model instance with PK 'add', not show
|
|
|
|
# the add_view
|
2016-04-08 10:04:45 +08:00
|
|
|
url = reverse('admin_custom_urls:%s_action_change' % Action._meta.app_label, args=(quote('add'),))
|
2012-08-27 04:54:49 +08:00
|
|
|
response = self.client.get(url)
|
2011-09-21 02:30:06 +08:00
|
|
|
self.assertContains(response, 'Change action')
|
|
|
|
|
|
|
|
# Should correctly get the change_view for the model instance with the
|
2015-01-20 22:54:12 +08:00
|
|
|
# funny-looking PK (the one with a 'path/to/html/document.html' value)
|
2016-04-08 10:04:45 +08:00
|
|
|
url = reverse(
|
|
|
|
'admin_custom_urls:%s_action_change' % Action._meta.app_label,
|
|
|
|
args=(quote("path/to/html/document.html"),)
|
|
|
|
)
|
2012-08-27 04:54:49 +08:00
|
|
|
response = self.client.get(url)
|
2011-09-21 02:30:06 +08:00
|
|
|
self.assertContains(response, 'Change action')
|
|
|
|
self.assertContains(response, 'value="path/to/html/document.html"')
|
2012-10-19 07:58:52 +08:00
|
|
|
|
2012-12-31 13:33:21 +08:00
|
|
|
def test_post_save_add_redirect(self):
|
2012-12-23 03:00:08 +08:00
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
ModelAdmin.response_post_save_add() controls the redirection after
|
|
|
|
the 'Save' button has been pressed when adding a new object.
|
2012-12-23 03:00:08 +08:00
|
|
|
"""
|
2013-10-15 03:13:14 +08:00
|
|
|
post_data = {'name': 'John Doe'}
|
2012-12-23 03:00:08 +08:00
|
|
|
self.assertEqual(Person.objects.count(), 0)
|
2016-04-08 10:04:45 +08:00
|
|
|
response = self.client.post(reverse('admin_custom_urls:admin_custom_urls_person_add'), post_data)
|
2012-12-23 03:00:08 +08:00
|
|
|
persons = Person.objects.all()
|
|
|
|
self.assertEqual(len(persons), 1)
|
2016-04-08 10:04:45 +08:00
|
|
|
redirect_url = reverse('admin_custom_urls:admin_custom_urls_person_history', args=[persons[0].pk])
|
|
|
|
self.assertRedirects(response, redirect_url)
|
2012-12-23 03:00:08 +08:00
|
|
|
|
2012-12-31 13:33:21 +08:00
|
|
|
def test_post_save_change_redirect(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
ModelAdmin.response_post_save_change() controls the redirection after
|
|
|
|
the 'Save' button has been pressed when editing an existing object.
|
2012-12-31 13:33:21 +08:00
|
|
|
"""
|
|
|
|
Person.objects.create(name='John Doe')
|
|
|
|
self.assertEqual(Person.objects.count(), 1)
|
|
|
|
person = Person.objects.all()[0]
|
2016-04-08 10:04:45 +08:00
|
|
|
post_url = reverse('admin_custom_urls:admin_custom_urls_person_change', args=[person.pk])
|
|
|
|
response = self.client.post(post_url, {'name': 'Jack Doe'})
|
|
|
|
self.assertRedirects(response, reverse('admin_custom_urls:admin_custom_urls_person_delete', args=[person.pk]))
|
2012-12-31 13:33:21 +08:00
|
|
|
|
2012-12-23 03:00:08 +08:00
|
|
|
def test_post_url_continue(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The ModelAdmin.response_add()'s parameter `post_url_continue` controls
|
|
|
|
the redirection after an object has been created.
|
2012-12-23 03:00:08 +08:00
|
|
|
"""
|
2013-10-15 03:13:14 +08:00
|
|
|
post_data = {'name': 'SuperFast', '_continue': '1'}
|
2012-12-23 03:00:08 +08:00
|
|
|
self.assertEqual(Car.objects.count(), 0)
|
2016-04-08 10:04:45 +08:00
|
|
|
response = self.client.post(reverse('admin_custom_urls:admin_custom_urls_car_add'), post_data)
|
2012-12-23 03:00:08 +08:00
|
|
|
cars = Car.objects.all()
|
|
|
|
self.assertEqual(len(cars), 1)
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertRedirects(response, reverse('admin_custom_urls:admin_custom_urls_car_history', args=[cars[0].pk]))
|