diff options
author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
tree | f0506b668a013d0063e5fba3dbf4863b466713ba /tests/admin_custom_urls | |
parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
download | django-9c19aff7c7561e3a82978a272ecdaad40dda5c00.tar.gz |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'tests/admin_custom_urls')
-rw-r--r-- | tests/admin_custom_urls/models.py | 24 | ||||
-rw-r--r-- | tests/admin_custom_urls/tests.py | 96 | ||||
-rw-r--r-- | tests/admin_custom_urls/urls.py | 2 |
3 files changed, 79 insertions, 43 deletions
diff --git a/tests/admin_custom_urls/models.py b/tests/admin_custom_urls/models.py index 8b91383b0f..ea3ea61f8b 100644 --- a/tests/admin_custom_urls/models.py +++ b/tests/admin_custom_urls/models.py @@ -21,7 +21,7 @@ class ActionAdmin(admin.ModelAdmin): The Action model has a CharField PK. """ - list_display = ('name', 'description') + list_display = ("name", "description") def remove_url(self, name): """ @@ -38,14 +38,15 @@ class ActionAdmin(admin.ModelAdmin): def wrap(view): def wrapper(*args, **kwargs): return self.admin_site.admin_view(view)(*args, **kwargs) + return update_wrapper(wrapper, view) info = self.model._meta.app_label, self.model._meta.model_name - view_name = '%s_%s_add' % info + view_name = "%s_%s_add" % info return [ - re_path('^!add/$', wrap(self.add_view), name=view_name), + re_path("^!add/$", wrap(self.add_view), name=view_name), ] + self.remove_url(view_name) @@ -54,14 +55,15 @@ class Person(models.Model): class PersonAdmin(admin.ModelAdmin): - def response_post_save_add(self, request, obj): return HttpResponseRedirect( - reverse('admin:admin_custom_urls_person_history', args=[obj.pk])) + reverse("admin:admin_custom_urls_person_history", args=[obj.pk]) + ) def response_post_save_change(self, request, obj): return HttpResponseRedirect( - reverse('admin:admin_custom_urls_person_delete', args=[obj.pk])) + reverse("admin:admin_custom_urls_person_delete", args=[obj.pk]) + ) class Car(models.Model): @@ -69,15 +71,17 @@ class Car(models.Model): class CarAdmin(admin.ModelAdmin): - def response_add(self, request, obj, post_url_continue=None): return super().response_add( - request, obj, - post_url_continue=reverse('admin:admin_custom_urls_car_history', args=[obj.pk]), + request, + obj, + post_url_continue=reverse( + "admin:admin_custom_urls_car_history", args=[obj.pk] + ), ) -site = admin.AdminSite(name='admin_custom_urls') +site = admin.AdminSite(name="admin_custom_urls") site.register(Action, ActionAdmin) site.register(Person, PersonAdmin) site.register(Car, CarAdmin) diff --git a/tests/admin_custom_urls/tests.py b/tests/admin_custom_urls/tests.py index ebd3291f2c..d401976ebb 100644 --- a/tests/admin_custom_urls/tests.py +++ b/tests/admin_custom_urls/tests.py @@ -8,7 +8,9 @@ from django.urls import reverse from .models import Action, Car, Person -@override_settings(ROOT_URLCONF='admin_custom_urls.urls',) +@override_settings( + ROOT_URLCONF="admin_custom_urls.urls", +) class AdminCustomUrlsTest(TestCase): """ Remember that: @@ -19,18 +21,22 @@ class AdminCustomUrlsTest(TestCase): @classmethod def setUpTestData(cls): - cls.superuser = User.objects.create_superuser(username='super', password='secret', email='super@example.com') - 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.") + cls.superuser = User.objects.create_superuser( + username="super", password="secret", email="super@example.com" + ) + 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.' + 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' + name="javascript:alert('Hello world');\">Click here</a>", + description="An action with a name suspected of being a XSS attempt", ) def setUp(self): @@ -40,8 +46,8 @@ class AdminCustomUrlsTest(TestCase): """ Ensure GET on the add_view works. """ - add_url = reverse('admin_custom_urls:admin_custom_urls_action_add') - self.assertTrue(add_url.endswith('/!add/')) + add_url = reverse("admin_custom_urls: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) @@ -51,7 +57,10 @@ class AdminCustomUrlsTest(TestCase): Ensure GET on the add_view plus specifying a field value in the query string works. """ - response = self.client.get(reverse('admin_custom_urls:admin_custom_urls_action_add'), {'name': 'My Action'}) + response = self.client.get( + reverse("admin_custom_urls:admin_custom_urls_action_add"), + {"name": "My Action"}, + ) self.assertContains(response, 'value="My Action"') def test_basic_add_POST(self): @@ -59,28 +68,33 @@ class AdminCustomUrlsTest(TestCase): Ensure POST on add_view works. """ post_data = { - IS_POPUP_VAR: '1', - "name": 'Action added through a popup', + IS_POPUP_VAR: "1", + "name": "Action added through a popup", "description": "Description of added action", } - response = self.client.post(reverse('admin_custom_urls:admin_custom_urls_action_add'), post_data) - self.assertContains(response, 'Action added through a popup') + response = self.client.post( + reverse("admin_custom_urls:admin_custom_urls_action_add"), post_data + ) + self.assertContains(response, "Action added through a popup") def test_admin_URLs_no_clash(self): # Should get the change_view for model instance with PK 'add', not show # the add_view - url = reverse('admin_custom_urls:%s_action_change' % Action._meta.app_label, args=(quote('add'),)) + url = reverse( + "admin_custom_urls:%s_action_change" % Action._meta.app_label, + args=(quote("add"),), + ) response = self.client.get(url) - self.assertContains(response, 'Change action') + self.assertContains(response, "Change action") # Should correctly get the change_view for the model instance with the # funny-looking PK (the one with a 'path/to/html/document.html' value) url = reverse( - 'admin_custom_urls:%s_action_change' % Action._meta.app_label, - args=(quote("path/to/html/document.html"),) + "admin_custom_urls:%s_action_change" % Action._meta.app_label, + args=(quote("path/to/html/document.html"),), ) response = self.client.get(url) - self.assertContains(response, 'Change action') + self.assertContains(response, "Change action") self.assertContains(response, 'value="path/to/html/document.html"') def test_post_save_add_redirect(self): @@ -88,12 +102,16 @@ class AdminCustomUrlsTest(TestCase): ModelAdmin.response_post_save_add() controls the redirection after the 'Save' button has been pressed when adding a new object. """ - post_data = {'name': 'John Doe'} + post_data = {"name": "John Doe"} self.assertEqual(Person.objects.count(), 0) - response = self.client.post(reverse('admin_custom_urls:admin_custom_urls_person_add'), post_data) + response = self.client.post( + reverse("admin_custom_urls:admin_custom_urls_person_add"), post_data + ) persons = Person.objects.all() self.assertEqual(len(persons), 1) - redirect_url = reverse('admin_custom_urls:admin_custom_urls_person_history', args=[persons[0].pk]) + redirect_url = reverse( + "admin_custom_urls:admin_custom_urls_person_history", args=[persons[0].pk] + ) self.assertRedirects(response, redirect_url) def test_post_save_change_redirect(self): @@ -101,21 +119,35 @@ class AdminCustomUrlsTest(TestCase): ModelAdmin.response_post_save_change() controls the redirection after the 'Save' button has been pressed when editing an existing object. """ - Person.objects.create(name='John Doe') + Person.objects.create(name="John Doe") self.assertEqual(Person.objects.count(), 1) person = Person.objects.all()[0] - 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])) + 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] + ), + ) def test_post_url_continue(self): """ The ModelAdmin.response_add()'s parameter `post_url_continue` controls the redirection after an object has been created. """ - post_data = {'name': 'SuperFast', '_continue': '1'} + post_data = {"name": "SuperFast", "_continue": "1"} self.assertEqual(Car.objects.count(), 0) - response = self.client.post(reverse('admin_custom_urls:admin_custom_urls_car_add'), post_data) + response = self.client.post( + reverse("admin_custom_urls:admin_custom_urls_car_add"), post_data + ) cars = Car.objects.all() self.assertEqual(len(cars), 1) - self.assertRedirects(response, reverse('admin_custom_urls:admin_custom_urls_car_history', args=[cars[0].pk])) + self.assertRedirects( + response, + reverse( + "admin_custom_urls:admin_custom_urls_car_history", args=[cars[0].pk] + ), + ) diff --git a/tests/admin_custom_urls/urls.py b/tests/admin_custom_urls/urls.py index ade49b3957..9dd7574792 100644 --- a/tests/admin_custom_urls/urls.py +++ b/tests/admin_custom_urls/urls.py @@ -3,5 +3,5 @@ from django.urls import path from .models import site urlpatterns = [ - path('admin/', site.urls), + path("admin/", site.urls), ] |