From cd260d03bd7c43b78ad3a394e1b62f526f40f4ce Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 6 Feb 2015 23:25:15 +0100 Subject: [PATCH] [1.8.x] Replaced hardcoded URLs in admin_* tests Refs #15779. This will allow easier admin URL changes, when needed. Thanks Simon Charette for the review. Backport of 32e6a7d3a57b2287d55e8b8efa4e8cb7643b1720 from master --- django/contrib/auth/admin.py | 2 +- tests/admin_changelist/tests.py | 4 +- tests/admin_custom_urls/tests.py | 8 +- tests/admin_inlines/tests.py | 115 +-- tests/admin_views/customadmin.py | 2 +- tests/admin_views/tests.py | 1195 +++++++++++++++--------------- tests/admin_views/urls.py | 4 +- tests/admin_widgets/tests.py | 35 +- 8 files changed, 707 insertions(+), 658 deletions(-) diff --git a/django/contrib/auth/admin.py b/django/contrib/auth/admin.py index 90394c8f093..2df33c3c86d 100644 --- a/django/contrib/auth/admin.py +++ b/django/contrib/auth/admin.py @@ -79,7 +79,7 @@ class UserAdmin(admin.ModelAdmin): def get_urls(self): return [ - url(r'^(\d+)/password/$', self.admin_site.admin_view(self.user_change_password)), + url(r'^(\d+)/password/$', self.admin_site.admin_view(self.user_change_password), name='auth_user_password_change'), ] + super(UserAdmin, self).get_urls() def lookup_allowed(self, lookup, value): diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py index 5ee5a512dba..7b35c84b45c 100644 --- a/tests/admin_changelist/tests.py +++ b/tests/admin_changelist/tests.py @@ -365,7 +365,7 @@ class ChangeListTests(TestCase): username='super', email='super@localhost', password='secret') self.client.login(username='super', password='secret') event = Event.objects.create(date=datetime.date.today()) - response = self.client.get('/admin/admin_changelist/event/') + response = self.client.get(reverse('admin:admin_changelist_event_changelist')) self.assertContains(response, formats.localize(event.date)) self.assertNotContains(response, six.text_type(event.date)) @@ -678,7 +678,7 @@ class SeleniumFirefoxTests(AdminSeleniumWebDriverTestCase): """ self.admin_login(username='super', password='secret') self.selenium.get('%s%s' % (self.live_server_url, - '/admin/auth/user/')) + reverse('admin:auth_user_changelist'))) form_id = '#changelist-form' diff --git a/tests/admin_custom_urls/tests.py b/tests/admin_custom_urls/tests.py index ea3c0704b3c..63cebed7084 100644 --- a/tests/admin_custom_urls/tests.py +++ b/tests/admin_custom_urls/tests.py @@ -26,7 +26,9 @@ class AdminCustomUrlsTest(TestCase): """ Ensure GET on the add_view works. """ - response = self.client.get('/admin/admin_custom_urls/action/!add/') + add_url = reverse('admin:admin_custom_urls_action_add') + self.assertTrue(add_url.endswith('/!add/')) + response = self.client.get(add_url) self.assertIsInstance(response, TemplateResponse) self.assertEqual(response.status_code, 200) @@ -35,7 +37,7 @@ class AdminCustomUrlsTest(TestCase): Ensure GET on the add_view plus specifying a field value in the query string works. """ - response = self.client.get('/admin/admin_custom_urls/action/!add/', {'name': 'My Action'}) + response = self.client.get(reverse('admin:admin_custom_urls_action_add'), {'name': 'My Action'}) self.assertEqual(response.status_code, 200) self.assertContains(response, 'value="My Action"') @@ -48,7 +50,7 @@ class AdminCustomUrlsTest(TestCase): "name": 'Action added through a popup', "description": "Description of added action", } - response = self.client.post('/admin/admin_custom_urls/action/!add/', post_data) + response = self.client.post(reverse('admin:admin_custom_urls_action_add'), post_data) self.assertEqual(response.status_code, 200) self.assertContains(response, 'dismissAddRelatedObjectPopup') self.assertContains(response, 'Action added through a popup') diff --git a/tests/admin_inlines/tests.py b/tests/admin_inlines/tests.py index ed931a0cc13..d5c1728bbb2 100644 --- a/tests/admin_inlines/tests.py +++ b/tests/admin_inlines/tests.py @@ -2,21 +2,25 @@ from __future__ import unicode_literals import warnings -from django.contrib.admin import TabularInline, ModelAdmin -from django.contrib.admin.tests import AdminSeleniumWebDriverTestCase +from django.contrib.admin import ModelAdmin, TabularInline from django.contrib.admin.helpers import InlineAdminForm -from django.contrib.auth.models import User, Permission +from django.contrib.admin.tests import AdminSeleniumWebDriverTestCase +from django.contrib.auth.models import Permission, User from django.contrib.contenttypes.models import ContentType -from django.test import TestCase, override_settings, RequestFactory +from django.core.urlresolvers import reverse +from django.test import RequestFactory, TestCase, override_settings from django.utils.encoding import force_text -# local test models -from .admin import InnerInline, site as admin_site -from .models import (Holder, Inner, Holder2, Inner2, Holder3, Inner3, Person, - OutfitItem, Fashionista, Teacher, Parent, Child, Author, Book, Profile, - ProfileCollection, ParentModelWithCustomPk, ChildModel1, ChildModel2, - Sighting, Novel, Chapter, FootNote, BinaryTree, SomeParentModel, - SomeChildModel, Poll, Question, Inner4Stacked, Inner4Tabular, Holder4) +from .admin import site as admin_site +from .admin import InnerInline +from .models import ( + Author, BinaryTree, Book, Chapter, Child, ChildModel1, ChildModel2, + Fashionista, FootNote, Holder, Holder2, Holder3, Holder4, Inner, Inner2, + Inner3, Inner4Stacked, Inner4Tabular, Novel, OutfitItem, Parent, + ParentModelWithCustomPk, Person, Poll, Profile, + ProfileCollection, Question, Sighting, SomeChildModel, + SomeParentModel, Teacher, +) INLINE_CHANGELINK_HTML = 'class="inlinechangelink">Change' @@ -30,7 +34,6 @@ class TestInline(TestCase): holder = Holder(dummy=13) holder.save() Inner(dummy=42, holder=holder).save() - self.change_url = '/admin/admin_inlines/holder/%i/' % holder.id result = self.client.login(username='super', password='secret') self.assertEqual(result, True) @@ -40,7 +43,10 @@ class TestInline(TestCase): """ can_delete should be passed to inlineformset factory. """ - response = self.client.get(self.change_url) + holder = Holder.objects.get(dummy=13) + response = self.client.get( + reverse('admin:admin_inlines_holder_change', args=(holder.id,)) + ) inner_formset = response.context['inline_admin_formsets'][0].formset expected = InnerInline.can_delete actual = inner_formset.can_delete @@ -50,13 +56,14 @@ class TestInline(TestCase): """Bug #13174.""" holder = Holder.objects.create(dummy=42) Inner.objects.create(holder=holder, dummy=42, readonly='') - response = self.client.get('/admin/admin_inlines/holder/%i/' - % holder.id) + response = self.client.get( + reverse('admin:admin_inlines_holder_change', args=(holder.id,)) + ) self.assertContains(response, '') def test_many_to_many_inlines(self): "Autogenerated many-to-many inlines are displayed correctly (#13407)" - response = self.client.get('/admin/admin_inlines/author/add/') + response = self.client.get(reverse('admin:admin_inlines_author_add')) # The heading for the m2m inline block uses the right text self.assertContains(response, '

Author-book relationships

') # The "add another" label is correct @@ -77,7 +84,7 @@ class TestInline(TestCase): 'max_weight': 0, 'shoppingweakness_set-0-item': item.id, } - response = self.client.post('/admin/admin_inlines/fashionista/add/', data) + response = self.client.post(reverse('admin:admin_inlines_fashionista_add'), data) self.assertEqual(response.status_code, 302) self.assertEqual(len(Fashionista.objects.filter(person__firstname='Imelda')), 1) @@ -94,7 +101,7 @@ class TestInline(TestCase): 'title_set-0-title1': 'a title', 'title_set-0-title2': 'a different title', } - response = self.client.post('/admin/admin_inlines/titlecollection/add/', data) + response = self.client.post(reverse('admin:admin_inlines_titlecollection_add'), data) # Here colspan is "4": two fields (title1 and title2), one hidden field and the delete checkbox. self.assertContains(response, '') @@ -102,14 +109,14 @@ class TestInline(TestCase): """Admin inline `readonly_field` shouldn't invoke parent ModelAdmin callable""" # Identically named callable isn't present in the parent ModelAdmin, # rendering of the add view shouldn't explode - response = self.client.get('/admin/admin_inlines/novel/add/') + response = self.client.get(reverse('admin:admin_inlines_novel_add')) self.assertEqual(response.status_code, 200) # View should have the child inlines section self.assertContains(response, '
') def test_callable_lookup(self): """Admin inline should invoke local callable when its name is listed in readonly_fields""" - response = self.client.get('/admin/admin_inlines/poll/add/') + response = self.client.get(reverse('admin:admin_inlines_poll_add')) self.assertEqual(response.status_code, 200) # Add parent object view should have the child inlines section self.assertContains(response, '
') @@ -123,11 +130,11 @@ class TestInline(TestCase): using both the stacked and tabular layouts. Ref #8190. """ - response = self.client.get('/admin/admin_inlines/holder4/add/') + response = self.client.get(reverse('admin:admin_inlines_holder4_add')) self.assertContains(response, '

Awesome stacked help text is awesome.

', 4) self.assertContains(response, '(Awesome tabular help text is awesome.)', 1) # ReadOnly fields - response = self.client.get('/admin/admin_inlines/capofamiglia/add/') + response = self.client.get(reverse('admin:admin_inlines_capofamiglia_add')) self.assertContains(response, '(Help text for ReadOnlyInline)', 1) def test_inline_hidden_field_no_column(self): @@ -135,7 +142,7 @@ class TestInline(TestCase): parent = SomeParentModel.objects.create(name='a') SomeChildModel.objects.create(name='b', position='0', parent=parent) SomeChildModel.objects.create(name='c', position='1', parent=parent) - response = self.client.get('/admin/admin_inlines/someparentmodel/%s/' % parent.pk) + response = self.client.get(reverse('admin:admin_inlines_someparentmodel_change', args=(parent.pk,))) self.assertNotContains(response, '') self.assertContains(response, ( '', html=True) @@ -172,7 +179,7 @@ class TestInline(TestCase): """ holder = Holder.objects.create(pk=123456789, dummy=42) inner = Inner.objects.create(pk=987654321, holder=holder, dummy=42, readonly='') - response = self.client.get('/admin/admin_inlines/holder/%i/' % holder.id) + response = self.client.get(reverse('admin:admin_inlines_holder_change', args=(holder.id,))) inner_shortcut = 'r/%s/%s/' % (ContentType.objects.get_for_model(inner).pk, inner.pk) self.assertContains(response, inner_shortcut) @@ -184,7 +191,7 @@ class TestInline(TestCase): parent = ParentModelWithCustomPk.objects.create(my_own_pk="foo", name="Foo") child1 = ChildModel1.objects.create(my_own_pk="bar", name="Bar", parent=parent) child2 = ChildModel2.objects.create(my_own_pk="baz", name="Baz", parent=parent) - response = self.client.get('/admin/admin_inlines/parentmodelwithcustompk/foo/') + response = self.client.get(reverse('admin:admin_inlines_parentmodelwithcustompk_change', args=('foo',))) child1_shortcut = 'r/%s/%s/' % (ContentType.objects.get_for_model(child1).pk, child1.pk) child2_shortcut = 'r/%s/%s/' % (ContentType.objects.get_for_model(child2).pk, child2.pk) self.assertContains(response, child1_shortcut) @@ -203,7 +210,7 @@ class TestInline(TestCase): 'sighting_set-0-place': 'Zone 51', '_save': 'Save', } - response = self.client.post('/admin/admin_inlines/extraterrestrial/add/', data) + response = self.client.post(reverse('admin:admin_inlines_extraterrestrial_add'), data) self.assertEqual(response.status_code, 302) self.assertEqual(Sighting.objects.filter(et__name='Martian').count(), 1) @@ -217,11 +224,11 @@ class TestInline(TestCase): # The total number of forms will remain the same in either case total_forms_hidden = '' - response = self.client.get('/admin/admin_inlines/binarytree/add/') + response = self.client.get(reverse('admin:admin_inlines_binarytree_add')) self.assertContains(response, max_forms_input % 3) self.assertContains(response, total_forms_hidden) - response = self.client.get("/admin/admin_inlines/binarytree/%d/" % bt_head.id) + response = self.client.get(reverse('admin:admin_inlines_binarytree_change', args=(bt_head.id,))) self.assertContains(response, max_forms_input % 2) self.assertContains(response, total_forms_hidden) @@ -240,7 +247,7 @@ class TestInline(TestCase): min_forms = '' total_forms = '' - request = self.factory.get('/admin/admin_inlines/binarytree/add/') + request = self.factory.get(reverse('admin:admin_inlines_binarytree_add')) request.user = User(username='super', is_superuser=True) response = modeladmin.changeform_view(request) self.assertContains(response, min_forms) @@ -268,20 +275,20 @@ class TestInline(TestCase): min_forms = '' total_forms = '' - request = self.factory.get('/admin/admin_inlines/binarytree/add/') + request = self.factory.get(reverse('admin:admin_inlines_binarytree_add')) request.user = User(username='super', is_superuser=True) response = modeladmin.changeform_view(request) self.assertContains(response, min_forms % 2) self.assertContains(response, total_forms % 5) - request = self.factory.get("/admin/admin_inlines/binarytree/%d/" % bt_head.id) + request = self.factory.get(reverse('admin:admin_inlines_binarytree_change', args=(bt_head.id,))) request.user = User(username='super', is_superuser=True) response = modeladmin.changeform_view(request, object_id=str(bt_head.id)) self.assertContains(response, min_forms % 5) self.assertContains(response, total_forms % 8) def test_inline_nonauto_noneditable_pk(self): - response = self.client.get('/admin/admin_inlines/author/add/') + response = self.client.get(reverse('admin:admin_inlines_author_add')) self.assertContains(response, '', html=True) @@ -290,7 +297,7 @@ class TestInline(TestCase): html=True) def test_inline_editable_pk(self): - response = self.client.get('/admin/admin_inlines/author/add/') + response = self.client.get(reverse('admin:admin_inlines_author_add')) self.assertContains(response, '', html=True, count=1) @@ -301,7 +308,7 @@ class TestInline(TestCase): def test_stacked_inline_edit_form_contains_has_original_class(self): holder = Holder.objects.create(dummy=1) holder.inner_set.create(dummy=1) - response = self.client.get('/admin/admin_inlines/holder/%s/' % holder.pk) + response = self.client.get(reverse('admin:admin_inlines_holder_change', args=(holder.pk,))) self.assertContains( response, '