2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import absolute_import, unicode_literals
|
2011-10-14 02:51:33 +08:00
|
|
|
|
2011-09-21 02:30:06 +08:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.test import TestCase
|
2012-05-01 02:38:44 +08:00
|
|
|
from django.test.utils import override_settings
|
2011-09-21 02:30:06 +08:00
|
|
|
|
2011-10-14 02:51:33 +08:00
|
|
|
from .models import Action
|
2011-09-21 02:30:06 +08:00
|
|
|
|
|
|
|
|
2012-05-01 02:38:44 +08:00
|
|
|
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
|
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/'
|
|
|
|
"""
|
2011-09-21 02:30:06 +08: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-27 04:54:49 +08:00
|
|
|
Ensure GET on the add_view works.
|
2011-09-21 02:30:06 +08:00
|
|
|
"""
|
|
|
|
response = self.client.get('/custom_urls/admin/admin_custom_urls/action/!add/')
|
|
|
|
self.assertIsInstance(response, TemplateResponse)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def testAddWithGETArgs(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.
|
|
|
|
"""
|
2011-09-21 02:30:06 +08:00
|
|
|
response = self.client.get('/custom_urls/admin/admin_custom_urls/action/!add/', {'name': 'My Action'})
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2012-08-14 20:38:23 +08:00
|
|
|
self.assertContains(response, 'value="My Action"')
|
2011-09-21 02:30:06 +08:00
|
|
|
|
|
|
|
def testBasicAddPost(self):
|
|
|
|
"""
|
2012-08-27 04:54:49 +08:00
|
|
|
Ensure POST on add_view works.
|
2011-09-21 02:30:06 +08:00
|
|
|
"""
|
|
|
|
post_data = {
|
2012-06-08 00:08:47 +08:00
|
|
|
'_popup': '1',
|
|
|
|
"name": 'Action added through a popup',
|
|
|
|
"description": "Description of added action",
|
2011-09-21 02:30:06 +08:00
|
|
|
}
|
|
|
|
response = self.client.post('/custom_urls/admin/admin_custom_urls/action/!add/', post_data)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertContains(response, 'dismissAddAnotherPopup')
|
|
|
|
self.assertContains(response, 'Action added through a popup')
|
|
|
|
|
|
|
|
def testAdminUrlsNoClash(self):
|
|
|
|
"""
|
2012-08-27 04:54:49 +08:00
|
|
|
Test that some admin URLs work correctly.
|
2011-09-21 02:30:06 +08:00
|
|
|
"""
|
|
|
|
# Should get the change_view for model instance with PK 'add', not show
|
|
|
|
# the add_view
|
|
|
|
response = self.client.get('/custom_urls/admin/admin_custom_urls/action/add/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertContains(response, 'Change action')
|
|
|
|
|
|
|
|
# Ditto, but use reverse() to build the URL
|
2012-08-27 04:54:49 +08:00
|
|
|
url = reverse('admin:%s_action_change' % Action._meta.app_label,
|
2011-09-21 02:30:06 +08:00
|
|
|
args=('add',))
|
2012-08-27 04:54:49 +08:00
|
|
|
response = self.client.get(url)
|
2011-09-21 02:30:06 +08: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-27 04:54:49 +08:00
|
|
|
# funny-looking PK (the one wth a 'path/to/html/document.html' value)
|
|
|
|
url = reverse('admin:%s_action_change' % Action._meta.app_label,
|
2011-09-21 02:30:06 +08:00
|
|
|
args=("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.assertEqual(response.status_code, 200)
|
|
|
|
self.assertContains(response, 'Change action')
|
|
|
|
self.assertContains(response, 'value="path/to/html/document.html"')
|
2012-08-27 04:54:49 +08:00
|
|
|
|
|
|
|
def testChangeViewHistoryButton(self):
|
|
|
|
url = reverse('admin:%s_action_change' % Action._meta.app_label,
|
|
|
|
args=('The name of an action',))
|
|
|
|
response = self.client.get(url)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
expected_link = reverse('admin:%s_action_history' %
|
|
|
|
Action._meta.app_label,
|
|
|
|
args=('The name of an action',))
|
|
|
|
self.assertContains(response, '<a href="%s" class="historylink"' %
|
|
|
|
expected_link)
|