2017-06-19 19:10:26 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
|
|
|
|
from django.contrib.admin.views.main import IS_POPUP_VAR
|
2017-06-19 19:28:28 +00:00
|
|
|
from django.contrib.auth.models import Permission, User
|
2017-06-19 19:10:26 +00:00
|
|
|
from django.core import mail
|
2023-06-12 03:20:55 +00:00
|
|
|
from django.db import connection
|
2017-06-19 19:10:26 +00:00
|
|
|
from django.template.loader import render_to_string
|
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.test import TestCase, override_settings
|
2023-06-12 03:20:55 +00:00
|
|
|
from django.test.utils import CaptureQueriesContext
|
2017-06-19 19:10:26 +00:00
|
|
|
from django.urls import reverse
|
|
|
|
|
2017-09-04 09:24:34 +00:00
|
|
|
from .admin import SubscriberAdmin
|
2017-06-19 19:10:26 +00:00
|
|
|
from .forms import MediaActionForm
|
|
|
|
from .models import (
|
2022-02-03 19:24:19 +00:00
|
|
|
Actor,
|
|
|
|
Answer,
|
|
|
|
Book,
|
|
|
|
ExternalSubscriber,
|
|
|
|
Question,
|
|
|
|
Subscriber,
|
2018-02-02 19:44:07 +00:00
|
|
|
UnchangeableObject,
|
2017-06-19 19:10:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="admin_views.urls")
|
2017-06-19 19:10:26 +00:00
|
|
|
class AdminActionsTest(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2022-02-03 19:24:19 +00:00
|
|
|
cls.superuser = User.objects.create_superuser(
|
|
|
|
username="super", password="secret", email="super@example.com"
|
|
|
|
)
|
|
|
|
cls.s1 = ExternalSubscriber.objects.create(
|
|
|
|
name="John Doe", email="john@example.org"
|
|
|
|
)
|
|
|
|
cls.s2 = Subscriber.objects.create(
|
|
|
|
name="Max Mustermann", email="max@example.org"
|
|
|
|
)
|
2017-06-19 19:10:26 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client.force_login(self.superuser)
|
|
|
|
|
|
|
|
def test_model_admin_custom_action(self):
|
|
|
|
"""A custom action defined in a ModelAdmin method."""
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [self.s1.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "mail_admin",
|
|
|
|
"index": 0,
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
self.client.post(
|
|
|
|
reverse("admin:admin_views_subscriber_changelist"), action_data
|
|
|
|
)
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(mail.outbox[0].subject, "Greetings from a ModelAdmin action")
|
2017-06-19 19:10:26 +00:00
|
|
|
|
|
|
|
def test_model_admin_default_delete_action(self):
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [self.s1.pk, self.s2.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "delete_selected",
|
|
|
|
"index": 0,
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
|
|
|
delete_confirmation_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [self.s1.pk, self.s2.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "delete_selected",
|
|
|
|
"post": "yes",
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
confirmation = self.client.post(
|
|
|
|
reverse("admin:admin_views_subscriber_changelist"), action_data
|
|
|
|
)
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertIsInstance(confirmation, TemplateResponse)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertContains(
|
|
|
|
confirmation, "Are you sure you want to delete the selected subscribers?"
|
|
|
|
)
|
|
|
|
self.assertContains(confirmation, "<h2>Summary</h2>")
|
|
|
|
self.assertContains(confirmation, "<li>Subscribers: 2</li>")
|
|
|
|
self.assertContains(confirmation, "<li>External subscribers: 1</li>")
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertContains(confirmation, ACTION_CHECKBOX_NAME, count=2)
|
2023-06-12 03:20:55 +00:00
|
|
|
with CaptureQueriesContext(connection) as ctx:
|
|
|
|
self.client.post(
|
|
|
|
reverse("admin:admin_views_subscriber_changelist"),
|
|
|
|
delete_confirmation_data,
|
|
|
|
)
|
|
|
|
# Log entries are inserted in bulk.
|
|
|
|
self.assertEqual(
|
|
|
|
len(
|
|
|
|
[
|
|
|
|
q["sql"]
|
|
|
|
for q in ctx.captured_queries
|
|
|
|
if q["sql"].startswith("INSERT")
|
|
|
|
]
|
|
|
|
),
|
|
|
|
1,
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertEqual(Subscriber.objects.count(), 0)
|
|
|
|
|
2018-02-23 18:12:09 +00:00
|
|
|
def test_default_delete_action_nonexistent_pk(self):
|
|
|
|
self.assertFalse(Subscriber.objects.filter(id=9998).exists())
|
|
|
|
action_data = {
|
2022-02-03 19:24:19 +00:00
|
|
|
ACTION_CHECKBOX_NAME: ["9998"],
|
|
|
|
"action": "delete_selected",
|
|
|
|
"index": 0,
|
2018-02-23 18:12:09 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("admin:admin_views_subscriber_changelist"), action_data
|
|
|
|
)
|
|
|
|
self.assertContains(
|
|
|
|
response, "Are you sure you want to delete the selected subscribers?"
|
|
|
|
)
|
|
|
|
self.assertContains(response, "<ul></ul>", html=True)
|
2018-02-23 18:12:09 +00:00
|
|
|
|
2021-09-09 05:42:05 +00:00
|
|
|
@override_settings(USE_THOUSAND_SEPARATOR=True, NUMBER_GROUPING=3)
|
2017-06-19 19:10:26 +00:00
|
|
|
def test_non_localized_pk(self):
|
|
|
|
"""
|
|
|
|
If USE_THOUSAND_SEPARATOR is set, the ids for the objects selected for
|
|
|
|
deletion are rendered without separators.
|
|
|
|
"""
|
|
|
|
s = ExternalSubscriber.objects.create(id=9999)
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [s.pk, self.s2.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "delete_selected",
|
|
|
|
"index": 0,
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("admin:admin_views_subscriber_changelist"), action_data
|
|
|
|
)
|
|
|
|
self.assertTemplateUsed(response, "admin/delete_selected_confirmation.html")
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertContains(response, 'value="9999"') # Instead of 9,999
|
|
|
|
self.assertContains(response, 'value="%s"' % self.s2.pk)
|
|
|
|
|
|
|
|
def test_model_admin_default_delete_action_protected(self):
|
|
|
|
"""
|
|
|
|
The default delete action where some related objects are protected
|
|
|
|
from deletion.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
q1 = Question.objects.create(question="Why?")
|
|
|
|
a1 = Answer.objects.create(question=q1, answer="Because.")
|
|
|
|
a2 = Answer.objects.create(question=q1, answer="Yes.")
|
|
|
|
q2 = Question.objects.create(question="Wherefore?")
|
2017-06-19 19:10:26 +00:00
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [q1.pk, q2.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "delete_selected",
|
|
|
|
"index": 0,
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
|
|
|
delete_confirmation_data = action_data.copy()
|
2022-02-03 19:24:19 +00:00
|
|
|
delete_confirmation_data["post"] = "yes"
|
|
|
|
response = self.client.post(
|
|
|
|
reverse("admin:admin_views_question_changelist"), action_data
|
|
|
|
)
|
|
|
|
self.assertContains(
|
|
|
|
response, "would require deleting the following protected related objects"
|
|
|
|
)
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertContains(
|
|
|
|
response,
|
2022-02-03 19:24:19 +00:00
|
|
|
'<li>Answer: <a href="%s">Because.</a></li>'
|
|
|
|
% reverse("admin:admin_views_answer_change", args=(a1.pk,)),
|
|
|
|
html=True,
|
2017-06-19 19:10:26 +00:00
|
|
|
)
|
|
|
|
self.assertContains(
|
|
|
|
response,
|
2022-02-03 19:24:19 +00:00
|
|
|
'<li>Answer: <a href="%s">Yes.</a></li>'
|
|
|
|
% reverse("admin:admin_views_answer_change", args=(a2.pk,)),
|
|
|
|
html=True,
|
2017-06-19 19:10:26 +00:00
|
|
|
)
|
|
|
|
# A POST request to delete protected objects displays the page which
|
|
|
|
# says the deletion is prohibited.
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("admin:admin_views_question_changelist"), delete_confirmation_data
|
|
|
|
)
|
|
|
|
self.assertContains(
|
|
|
|
response, "would require deleting the following protected related objects"
|
|
|
|
)
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertEqual(Question.objects.count(), 2)
|
|
|
|
|
|
|
|
def test_model_admin_default_delete_action_no_change_url(self):
|
|
|
|
"""
|
|
|
|
The default delete action doesn't break if a ModelAdmin removes the
|
|
|
|
change_view URL (#20640).
|
|
|
|
"""
|
|
|
|
obj = UnchangeableObject.objects.create()
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: obj.pk,
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "delete_selected",
|
|
|
|
"index": "0",
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("admin:admin_views_unchangeableobject_changelist"), action_data
|
|
|
|
)
|
2020-09-06 15:44:34 +00:00
|
|
|
# No 500 caused by NoReverseMatch. The page doesn't display a link to
|
|
|
|
# the nonexistent change page.
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertContains(
|
|
|
|
response, "<li>Unchangeable object: %s</li>" % obj, 1, html=True
|
|
|
|
)
|
2017-06-19 19:10:26 +00:00
|
|
|
|
2017-09-04 09:24:34 +00:00
|
|
|
def test_delete_queryset_hook(self):
|
|
|
|
delete_confirmation_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [self.s1.pk, self.s2.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "delete_selected",
|
|
|
|
"post": "yes",
|
|
|
|
"index": 0,
|
2017-09-04 09:24:34 +00:00
|
|
|
}
|
|
|
|
SubscriberAdmin.overridden = False
|
2022-02-03 19:24:19 +00:00
|
|
|
self.client.post(
|
|
|
|
reverse("admin:admin_views_subscriber_changelist"), delete_confirmation_data
|
|
|
|
)
|
2017-09-04 09:24:34 +00:00
|
|
|
# SubscriberAdmin.delete_queryset() sets overridden to True.
|
|
|
|
self.assertIs(SubscriberAdmin.overridden, True)
|
2022-02-22 09:29:38 +00:00
|
|
|
self.assertEqual(Subscriber.objects.count(), 0)
|
2017-09-04 09:24:34 +00:00
|
|
|
|
2017-03-30 09:13:15 +00:00
|
|
|
def test_delete_selected_uses_get_deleted_objects(self):
|
|
|
|
"""The delete_selected action uses ModelAdmin.get_deleted_objects()."""
|
2022-02-03 19:24:19 +00:00
|
|
|
book = Book.objects.create(name="Test Book")
|
2017-03-30 09:13:15 +00:00
|
|
|
data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [book.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "delete_selected",
|
|
|
|
"index": 0,
|
2017-03-30 09:13:15 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.post(reverse("admin2:admin_views_book_changelist"), data)
|
2017-03-30 09:13:15 +00:00
|
|
|
# BookAdmin.get_deleted_objects() returns custom text.
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertContains(response, "a deletable object")
|
2017-03-30 09:13:15 +00:00
|
|
|
|
2017-06-19 19:10:26 +00:00
|
|
|
def test_custom_function_mail_action(self):
|
|
|
|
"""A custom action may be defined in a function."""
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [self.s1.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "external_mail",
|
|
|
|
"index": 0,
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
self.client.post(
|
|
|
|
reverse("admin:admin_views_externalsubscriber_changelist"), action_data
|
|
|
|
)
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(mail.outbox[0].subject, "Greetings from a function action")
|
2017-06-19 19:10:26 +00:00
|
|
|
|
|
|
|
def test_custom_function_action_with_redirect(self):
|
|
|
|
"""Another custom action defined in a function."""
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [self.s1.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "redirect_to",
|
|
|
|
"index": 0,
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("admin:admin_views_externalsubscriber_changelist"), action_data
|
|
|
|
)
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
|
|
|
|
def test_default_redirect(self):
|
|
|
|
"""
|
|
|
|
Actions which don't return an HttpResponse are redirected to the same
|
|
|
|
page, retaining the querystring (which may contain changelist info).
|
|
|
|
"""
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [self.s1.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "external_mail",
|
|
|
|
"index": 0,
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
url = reverse("admin:admin_views_externalsubscriber_changelist") + "?o=1"
|
2017-06-19 19:10:26 +00:00
|
|
|
response = self.client.post(url, action_data)
|
|
|
|
self.assertRedirects(response, url)
|
|
|
|
|
|
|
|
def test_custom_function_action_streaming_response(self):
|
|
|
|
"""A custom action may return a StreamingHttpResponse."""
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [self.s1.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "download",
|
|
|
|
"index": 0,
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("admin:admin_views_externalsubscriber_changelist"), action_data
|
|
|
|
)
|
2023-02-17 09:35:19 +00:00
|
|
|
content = b"".join(list(response))
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(content, b"This is the content of the file")
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_custom_function_action_no_perm_response(self):
|
|
|
|
"""A custom action may returns an HttpResponse with a 403 code."""
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [self.s1.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "no_perm",
|
|
|
|
"index": 0,
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("admin:admin_views_externalsubscriber_changelist"), action_data
|
|
|
|
)
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertEqual(response.status_code, 403)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response.content, b"No permission to perform this action")
|
2017-06-19 19:10:26 +00:00
|
|
|
|
|
|
|
def test_actions_ordering(self):
|
|
|
|
"""Actions are ordered as expected."""
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.get(
|
|
|
|
reverse("admin:admin_views_externalsubscriber_changelist")
|
|
|
|
)
|
|
|
|
self.assertContains(
|
|
|
|
response,
|
|
|
|
"""<label>Action: <select name="action" required>
|
2017-06-19 19:10:26 +00:00
|
|
|
<option value="" selected>---------</option>
|
|
|
|
<option value="delete_selected">Delete selected external
|
|
|
|
subscribers</option>
|
|
|
|
<option value="redirect_to">Redirect to (Awesome action)</option>
|
|
|
|
<option value="external_mail">External mail (Another awesome
|
|
|
|
action)</option>
|
|
|
|
<option value="download">Download subscription</option>
|
|
|
|
<option value="no_perm">No permission to run</option>
|
2022-02-03 19:24:19 +00:00
|
|
|
</select>""",
|
|
|
|
html=True,
|
|
|
|
)
|
2017-06-19 19:10:26 +00:00
|
|
|
|
|
|
|
def test_model_without_action(self):
|
|
|
|
"""A ModelAdmin might not have any actions."""
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.get(
|
|
|
|
reverse("admin:admin_views_oldsubscriber_changelist")
|
|
|
|
)
|
|
|
|
self.assertIsNone(response.context["action_form"])
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertNotContains(
|
2022-02-03 19:24:19 +00:00
|
|
|
response,
|
|
|
|
'<input type="checkbox" class="action-select"',
|
|
|
|
msg_prefix="Found an unexpected action toggle checkboxbox in response",
|
2017-06-19 19:10:26 +00:00
|
|
|
)
|
|
|
|
self.assertNotContains(response, '<input type="checkbox" class="action-select"')
|
|
|
|
|
|
|
|
def test_model_without_action_still_has_jquery(self):
|
|
|
|
"""
|
|
|
|
A ModelAdmin without any actions still has jQuery included on the page.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.get(
|
|
|
|
reverse("admin:admin_views_oldsubscriber_changelist")
|
|
|
|
)
|
|
|
|
self.assertIsNone(response.context["action_form"])
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertContains(
|
2022-02-03 19:24:19 +00:00
|
|
|
response,
|
|
|
|
"jquery.min.js",
|
2022-02-04 07:08:27 +00:00
|
|
|
msg_prefix=(
|
|
|
|
"jQuery missing from admin pages for model with no admin actions"
|
|
|
|
),
|
2017-06-19 19:10:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_action_column_class(self):
|
|
|
|
"""The checkbox column class is present in the response."""
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.get(reverse("admin:admin_views_subscriber_changelist"))
|
|
|
|
self.assertIsNotNone(response.context["action_form"])
|
|
|
|
self.assertContains(response, "action-checkbox-column")
|
2017-06-19 19:10:26 +00:00
|
|
|
|
|
|
|
def test_multiple_actions_form(self):
|
|
|
|
"""
|
|
|
|
Actions come from the form whose submit button was pressed (#10618).
|
|
|
|
"""
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [self.s1.pk],
|
|
|
|
# Two different actions selected on the two forms...
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": ["external_mail", "delete_selected"],
|
2017-06-19 19:10:26 +00:00
|
|
|
# ...but "go" was clicked on the top form.
|
2022-02-03 19:24:19 +00:00
|
|
|
"index": 0,
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
self.client.post(
|
|
|
|
reverse("admin:admin_views_externalsubscriber_changelist"), action_data
|
|
|
|
)
|
2017-06-19 19:10:26 +00:00
|
|
|
# The action sends mail rather than deletes.
|
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(mail.outbox[0].subject, "Greetings from a function action")
|
2017-06-19 19:10:26 +00:00
|
|
|
|
|
|
|
def test_media_from_actions_form(self):
|
|
|
|
"""
|
|
|
|
The action form's media is included in the changelist view's media.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.get(reverse("admin:admin_views_subscriber_changelist"))
|
2017-06-19 19:10:26 +00:00
|
|
|
media_path = MediaActionForm.Media.js[0]
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertIsInstance(response.context["action_form"], MediaActionForm)
|
|
|
|
self.assertIn("media", response.context)
|
|
|
|
self.assertIn(media_path, response.context["media"]._js)
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertContains(response, media_path)
|
|
|
|
|
|
|
|
def test_user_message_on_none_selected(self):
|
|
|
|
"""
|
|
|
|
User sees a warning when 'Go' is pressed and no items are selected.
|
|
|
|
"""
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "delete_selected",
|
|
|
|
"index": 0,
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
url = reverse("admin:admin_views_subscriber_changelist")
|
2017-06-19 19:10:26 +00:00
|
|
|
response = self.client.post(url, action_data)
|
|
|
|
self.assertRedirects(response, url, fetch_redirect_response=False)
|
|
|
|
response = self.client.get(response.url)
|
2022-02-04 07:08:27 +00:00
|
|
|
msg = (
|
|
|
|
"Items must be selected in order to perform actions on them. No items have "
|
|
|
|
"been changed."
|
|
|
|
)
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertContains(response, msg)
|
|
|
|
self.assertEqual(Subscriber.objects.count(), 2)
|
|
|
|
|
|
|
|
def test_user_message_on_no_action(self):
|
|
|
|
"""
|
|
|
|
User sees a warning when 'Go' is pressed and no action is selected.
|
|
|
|
"""
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [self.s1.pk, self.s2.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "",
|
|
|
|
"index": 0,
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
url = reverse("admin:admin_views_subscriber_changelist")
|
2017-06-19 19:10:26 +00:00
|
|
|
response = self.client.post(url, action_data)
|
|
|
|
self.assertRedirects(response, url, fetch_redirect_response=False)
|
|
|
|
response = self.client.get(response.url)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertContains(response, "No action selected.")
|
2017-06-19 19:10:26 +00:00
|
|
|
self.assertEqual(Subscriber.objects.count(), 2)
|
|
|
|
|
|
|
|
def test_selection_counter(self):
|
|
|
|
"""The selection counter is there."""
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.get(reverse("admin:admin_views_subscriber_changelist"))
|
|
|
|
self.assertContains(response, "0 of 2 selected")
|
2017-06-19 19:10:26 +00:00
|
|
|
|
|
|
|
def test_popup_actions(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
"""Actions aren't shown in popups."""
|
|
|
|
changelist_url = reverse("admin:admin_views_subscriber_changelist")
|
2017-06-19 19:10:26 +00:00
|
|
|
response = self.client.get(changelist_url)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertIsNotNone(response.context["action_form"])
|
|
|
|
response = self.client.get(changelist_url + "?%s" % IS_POPUP_VAR)
|
|
|
|
self.assertIsNone(response.context["action_form"])
|
2017-06-19 19:10:26 +00:00
|
|
|
|
|
|
|
def test_popup_template_response_on_add(self):
|
|
|
|
"""
|
|
|
|
Success on popups shall be rendered from template in order to allow
|
|
|
|
easy customization.
|
|
|
|
"""
|
|
|
|
response = self.client.post(
|
2022-02-03 19:24:19 +00:00
|
|
|
reverse("admin:admin_views_actor_add") + "?%s=1" % IS_POPUP_VAR,
|
|
|
|
{"name": "Troy McClure", "age": "55", IS_POPUP_VAR: "1"},
|
2017-06-19 19:10:26 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
response.template_name,
|
|
|
|
[
|
|
|
|
"admin/admin_views/actor/popup_response.html",
|
|
|
|
"admin/admin_views/popup_response.html",
|
|
|
|
"admin/popup_response.html",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
self.assertTemplateUsed(response, "admin/popup_response.html")
|
2017-06-19 19:10:26 +00:00
|
|
|
|
|
|
|
def test_popup_template_response_on_change(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
instance = Actor.objects.create(name="David Tennant", age=45)
|
2017-06-19 19:10:26 +00:00
|
|
|
response = self.client.post(
|
2022-02-03 19:24:19 +00:00
|
|
|
reverse("admin:admin_views_actor_change", args=(instance.pk,))
|
|
|
|
+ "?%s=1" % IS_POPUP_VAR,
|
|
|
|
{"name": "David Tennant", "age": "46", IS_POPUP_VAR: "1"},
|
2017-06-19 19:10:26 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
response.template_name,
|
|
|
|
[
|
|
|
|
"admin/admin_views/actor/popup_response.html",
|
|
|
|
"admin/admin_views/popup_response.html",
|
|
|
|
"admin/popup_response.html",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
self.assertTemplateUsed(response, "admin/popup_response.html")
|
2017-06-19 19:10:26 +00:00
|
|
|
|
|
|
|
def test_popup_template_response_on_delete(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
instance = Actor.objects.create(name="David Tennant", age=45)
|
2017-06-19 19:10:26 +00:00
|
|
|
response = self.client.post(
|
2022-02-03 19:24:19 +00:00
|
|
|
reverse("admin:admin_views_actor_delete", args=(instance.pk,))
|
|
|
|
+ "?%s=1" % IS_POPUP_VAR,
|
|
|
|
{IS_POPUP_VAR: "1"},
|
2017-06-19 19:10:26 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
response.template_name,
|
|
|
|
[
|
|
|
|
"admin/admin_views/actor/popup_response.html",
|
|
|
|
"admin/admin_views/popup_response.html",
|
|
|
|
"admin/popup_response.html",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
self.assertTemplateUsed(response, "admin/popup_response.html")
|
2017-06-19 19:10:26 +00:00
|
|
|
|
|
|
|
def test_popup_template_escaping(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
popup_response_data = json.dumps(
|
|
|
|
{
|
|
|
|
"new_value": "new_value\\",
|
|
|
|
"obj": "obj\\",
|
|
|
|
"value": "value\\",
|
|
|
|
}
|
|
|
|
)
|
2017-06-19 19:10:26 +00:00
|
|
|
context = {
|
2022-02-03 19:24:19 +00:00
|
|
|
"popup_response_data": popup_response_data,
|
2017-06-19 19:10:26 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
output = render_to_string("admin/popup_response.html", context)
|
|
|
|
self.assertIn(r""value\\"", output)
|
|
|
|
self.assertIn(r""new_value\\"", output)
|
|
|
|
self.assertIn(r""obj\\"", output)
|
2017-06-19 19:28:28 +00:00
|
|
|
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="admin_views.urls")
|
2017-06-19 19:28:28 +00:00
|
|
|
class AdminActionsPermissionTests(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2022-02-03 19:24:19 +00:00
|
|
|
cls.s1 = ExternalSubscriber.objects.create(
|
|
|
|
name="John Doe", email="john@example.org"
|
|
|
|
)
|
|
|
|
cls.s2 = Subscriber.objects.create(
|
|
|
|
name="Max Mustermann", email="max@example.org"
|
|
|
|
)
|
2018-11-24 01:59:38 +00:00
|
|
|
cls.user = User.objects.create_user(
|
2022-02-03 19:24:19 +00:00
|
|
|
username="user",
|
|
|
|
password="secret",
|
|
|
|
email="user@example.com",
|
2017-06-19 19:28:28 +00:00
|
|
|
is_staff=True,
|
|
|
|
)
|
2022-02-03 19:24:19 +00:00
|
|
|
permission = Permission.objects.get(codename="change_subscriber")
|
2018-11-24 01:59:38 +00:00
|
|
|
cls.user.user_permissions.add(permission)
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client.force_login(self.user)
|
2017-06-19 19:28:28 +00:00
|
|
|
|
|
|
|
def test_model_admin_no_delete_permission(self):
|
|
|
|
"""
|
|
|
|
Permission is denied if the user doesn't have delete permission for the
|
|
|
|
model (Subscriber).
|
|
|
|
"""
|
|
|
|
action_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [self.s1.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "delete_selected",
|
2017-06-19 19:28:28 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
url = reverse("admin:admin_views_subscriber_changelist")
|
2018-06-18 19:07:29 +00:00
|
|
|
response = self.client.post(url, action_data)
|
|
|
|
self.assertRedirects(response, url, fetch_redirect_response=False)
|
|
|
|
response = self.client.get(response.url)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertContains(response, "No action selected.")
|
2017-06-19 19:28:28 +00:00
|
|
|
|
|
|
|
def test_model_admin_no_delete_permission_externalsubscriber(self):
|
|
|
|
"""
|
|
|
|
Permission is denied if the user doesn't have delete permission for a
|
|
|
|
related model (ExternalSubscriber).
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
permission = Permission.objects.get(codename="delete_subscriber")
|
2017-06-19 19:28:28 +00:00
|
|
|
self.user.user_permissions.add(permission)
|
|
|
|
delete_confirmation_data = {
|
|
|
|
ACTION_CHECKBOX_NAME: [self.s1.pk, self.s2.pk],
|
2022-02-03 19:24:19 +00:00
|
|
|
"action": "delete_selected",
|
|
|
|
"post": "yes",
|
2017-06-19 19:28:28 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("admin:admin_views_subscriber_changelist"), delete_confirmation_data
|
|
|
|
)
|
2017-06-19 19:28:28 +00:00
|
|
|
self.assertEqual(response.status_code, 403)
|