2019-08-12 16:00:07 +00:00
|
|
|
from datetime import datetime
|
2023-03-10 17:38:31 +00:00
|
|
|
from unittest import mock
|
2024-07-03 14:21:33 +00:00
|
|
|
|
2013-02-22 13:58:41 +00:00
|
|
|
from django.contrib import admin
|
2024-07-03 14:21:33 +00:00
|
|
|
from django.contrib.auth.models import Permission
|
|
|
|
|
from django.contrib.auth.models import User
|
2013-03-02 14:43:27 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2019-04-06 14:13:56 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2018-03-31 16:32:46 +00:00
|
|
|
from django.template.defaultfilters import linebreaksbr
|
2024-07-03 14:21:33 +00:00
|
|
|
from django.test import RequestFactory
|
|
|
|
|
from django.test import TestCase
|
2022-10-13 00:07:49 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2013-02-22 13:58:41 +00:00
|
|
|
|
2017-06-02 08:51:33 +00:00
|
|
|
from constance import settings
|
2023-04-07 13:16:39 +00:00
|
|
|
from constance.admin import Config
|
|
|
|
|
from constance.forms import ConstanceForm
|
2024-07-03 14:21:33 +00:00
|
|
|
from constance.utils import get_values
|
2013-02-22 13:58:41 +00:00
|
|
|
|
2023-04-07 13:16:39 +00:00
|
|
|
|
2013-02-22 13:58:41 +00:00
|
|
|
class TestAdmin(TestCase):
|
|
|
|
|
model = Config
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
2019-12-23 21:20:41 +00:00
|
|
|
super().setUp()
|
2013-02-22 13:58:41 +00:00
|
|
|
self.rf = RequestFactory()
|
2025-10-07 09:25:07 +00:00
|
|
|
self.superuser = User.objects.create_superuser("admin", "nimda", "a@a.cz")
|
|
|
|
|
self.normaluser = User.objects.create_user("normal", "nimda", "b@b.cz")
|
2013-03-02 14:43:27 +00:00
|
|
|
self.normaluser.is_staff = True
|
|
|
|
|
self.normaluser.save()
|
2013-02-22 13:58:41 +00:00
|
|
|
self.options = admin.site._registry[self.model]
|
|
|
|
|
|
|
|
|
|
def test_changelist(self):
|
2025-10-07 09:25:07 +00:00
|
|
|
self.client.login(username="admin", password="nimda")
|
|
|
|
|
request = self.rf.get("/admin/constance/config/")
|
2013-03-02 14:43:27 +00:00
|
|
|
request.user = self.superuser
|
|
|
|
|
response = self.options.changelist_view(request, {})
|
2013-04-12 15:34:48 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2013-03-02 14:43:27 +00:00
|
|
|
|
|
|
|
|
def test_custom_auth(self):
|
|
|
|
|
settings.SUPERUSER_ONLY = False
|
2025-10-07 09:25:07 +00:00
|
|
|
self.client.login(username="normal", password="nimda")
|
|
|
|
|
request = self.rf.get("/admin/constance/config/")
|
2013-03-02 14:43:27 +00:00
|
|
|
request.user = self.normaluser
|
2024-07-03 14:21:33 +00:00
|
|
|
self.assertRaises(PermissionDenied, self.options.changelist_view, request, {})
|
2025-10-07 09:25:07 +00:00
|
|
|
self.assertFalse(request.user.has_perm("constance.change_config"))
|
2013-03-02 14:43:27 +00:00
|
|
|
|
|
|
|
|
# reload user to reset permission cache
|
2025-10-07 09:25:07 +00:00
|
|
|
request = self.rf.get("/admin/constance/config/")
|
2013-03-02 14:43:27 +00:00
|
|
|
request.user = User.objects.get(pk=self.normaluser.pk)
|
|
|
|
|
|
2025-10-07 09:25:07 +00:00
|
|
|
request.user.user_permissions.add(Permission.objects.get(codename="change_config"))
|
|
|
|
|
self.assertTrue(request.user.has_perm("constance.change_config"))
|
2013-03-02 14:43:27 +00:00
|
|
|
|
2013-02-22 13:58:41 +00:00
|
|
|
response = self.options.changelist_view(request, {})
|
2013-04-12 15:34:48 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2015-08-25 11:54:11 +00:00
|
|
|
|
2015-09-24 10:21:29 +00:00
|
|
|
def test_linebreaks(self):
|
2025-10-07 09:25:07 +00:00
|
|
|
self.client.login(username="admin", password="nimda")
|
|
|
|
|
request = self.rf.get("/admin/constance/config/")
|
2015-09-24 10:21:29 +00:00
|
|
|
request.user = self.superuser
|
|
|
|
|
response = self.options.changelist_view(request, {})
|
2025-10-07 09:25:07 +00:00
|
|
|
self.assertContains(response, "LINEBREAK_VALUE")
|
|
|
|
|
self.assertContains(response, linebreaksbr("eggs\neggs"))
|
2017-06-02 08:52:16 +00:00
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
@mock.patch(
|
2025-10-07 09:25:07 +00:00
|
|
|
"constance.settings.CONFIG_FIELDSETS",
|
2024-07-03 14:21:33 +00:00
|
|
|
{
|
2025-10-07 09:25:07 +00:00
|
|
|
"Numbers": ("INT_VALUE",),
|
|
|
|
|
"Text": ("STRING_VALUE",),
|
2024-07-03 14:21:33 +00:00
|
|
|
},
|
|
|
|
|
)
|
2017-06-02 08:52:16 +00:00
|
|
|
def test_fieldset_headers(self):
|
2025-10-07 09:25:07 +00:00
|
|
|
self.client.login(username="admin", password="nimda")
|
|
|
|
|
request = self.rf.get("/admin/constance/config/")
|
2017-06-02 08:52:16 +00:00
|
|
|
request.user = self.superuser
|
|
|
|
|
response = self.options.changelist_view(request, {})
|
2025-11-04 16:30:02 +00:00
|
|
|
self.assertContains(response, "Numbers</h2>")
|
|
|
|
|
self.assertContains(response, "Text</h2>")
|
2017-06-02 08:52:16 +00:00
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
@mock.patch(
|
2025-10-07 09:25:07 +00:00
|
|
|
"constance.settings.CONFIG_FIELDSETS",
|
2024-07-03 14:21:33 +00:00
|
|
|
(
|
2025-10-07 09:25:07 +00:00
|
|
|
("Numbers", ("INT_VALUE",)),
|
|
|
|
|
("Text", ("STRING_VALUE",)),
|
2024-07-03 14:21:33 +00:00
|
|
|
),
|
|
|
|
|
)
|
2022-07-13 15:01:25 +00:00
|
|
|
def test_fieldset_tuple(self):
|
2025-10-07 09:25:07 +00:00
|
|
|
self.client.login(username="admin", password="nimda")
|
|
|
|
|
request = self.rf.get("/admin/constance/config/")
|
2022-07-13 15:01:25 +00:00
|
|
|
request.user = self.superuser
|
|
|
|
|
response = self.options.changelist_view(request, {})
|
2025-11-04 16:30:02 +00:00
|
|
|
self.assertContains(response, "Numbers</h2>")
|
|
|
|
|
self.assertContains(response, "Text</h2>")
|
2022-07-13 15:01:25 +00:00
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
@mock.patch(
|
2025-10-07 09:25:07 +00:00
|
|
|
"constance.settings.CONFIG_FIELDSETS",
|
2024-07-03 14:21:33 +00:00
|
|
|
{
|
2025-10-07 09:25:07 +00:00
|
|
|
"Numbers": {
|
|
|
|
|
"fields": (
|
|
|
|
|
"INT_VALUE",
|
|
|
|
|
"DECIMAL_VALUE",
|
2024-07-03 14:21:33 +00:00
|
|
|
),
|
2025-10-07 09:25:07 +00:00
|
|
|
"collapse": True,
|
2024-07-03 14:21:33 +00:00
|
|
|
},
|
2025-10-07 09:25:07 +00:00
|
|
|
"Text": {
|
|
|
|
|
"fields": (
|
|
|
|
|
"STRING_VALUE",
|
|
|
|
|
"LINEBREAK_VALUE",
|
2024-07-03 14:21:33 +00:00
|
|
|
),
|
2025-10-07 09:25:07 +00:00
|
|
|
"collapse": True,
|
2024-07-03 14:21:33 +00:00
|
|
|
},
|
2020-03-16 16:33:19 +00:00
|
|
|
},
|
2024-07-03 14:21:33 +00:00
|
|
|
)
|
2020-03-16 16:33:19 +00:00
|
|
|
def test_collapsed_fieldsets(self):
|
2025-10-07 09:25:07 +00:00
|
|
|
self.client.login(username="admin", password="nimda")
|
|
|
|
|
request = self.rf.get("/admin/constance/config/")
|
2020-03-16 16:33:19 +00:00
|
|
|
request.user = self.superuser
|
|
|
|
|
response = self.options.changelist_view(request, {})
|
2025-10-07 09:25:07 +00:00
|
|
|
self.assertContains(response, "module collapse")
|
2020-03-16 16:33:19 +00:00
|
|
|
|
2025-10-07 09:25:07 +00:00
|
|
|
@mock.patch("constance.settings.CONFIG_FIELDSETS", {"FieldSetOne": ("INT_VALUE",)})
|
2024-07-03 14:21:33 +00:00
|
|
|
@mock.patch(
|
2025-10-07 09:25:07 +00:00
|
|
|
"constance.settings.CONFIG",
|
2024-07-03 14:21:33 +00:00
|
|
|
{
|
2025-10-07 09:25:07 +00:00
|
|
|
"INT_VALUE": (1, "some int"),
|
2024-07-03 14:21:33 +00:00
|
|
|
},
|
|
|
|
|
)
|
2025-10-07 09:25:07 +00:00
|
|
|
@mock.patch("constance.settings.IGNORE_ADMIN_VERSION_CHECK", True)
|
|
|
|
|
@mock.patch("constance.forms.ConstanceForm.save", lambda _: None)
|
|
|
|
|
@mock.patch("constance.forms.ConstanceForm.is_valid", lambda _: True)
|
2019-04-06 14:13:56 +00:00
|
|
|
def test_submit(self):
|
|
|
|
|
"""
|
|
|
|
|
Test that submitting the admin page results in an http redirect when
|
|
|
|
|
everything is in order.
|
|
|
|
|
"""
|
2025-10-07 09:25:07 +00:00
|
|
|
initial_value = {"INT_VALUE": settings.CONFIG["INT_VALUE"][0]}
|
2024-07-03 14:21:33 +00:00
|
|
|
|
2025-10-07 09:25:07 +00:00
|
|
|
self.client.login(username="admin", password="nimda")
|
2024-07-03 14:21:33 +00:00
|
|
|
|
|
|
|
|
request = self.rf.post(
|
2025-10-07 09:25:07 +00:00
|
|
|
"/admin/constance/config/",
|
2024-07-03 14:21:33 +00:00
|
|
|
data={
|
|
|
|
|
**initial_value,
|
2025-10-07 09:25:07 +00:00
|
|
|
"version": "123",
|
2024-07-03 14:21:33 +00:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
2019-04-06 14:13:56 +00:00
|
|
|
request.user = self.superuser
|
|
|
|
|
request._dont_enforce_csrf_checks = True
|
2022-10-13 00:07:49 +00:00
|
|
|
|
2025-10-07 09:25:07 +00:00
|
|
|
with mock.patch("django.contrib.messages.add_message") as mock_message, mock.patch.object(
|
|
|
|
|
ConstanceForm, "__init__", **initial_value, return_value=None
|
2024-07-05 14:38:26 +00:00
|
|
|
) as mock_form:
|
|
|
|
|
response = self.options.changelist_view(request, {})
|
|
|
|
|
mock_form.assert_called_with(data=request.POST, files=request.FILES, initial=initial_value, request=request)
|
2025-10-07 09:25:07 +00:00
|
|
|
mock_message.assert_called_with(request, 25, _("Live settings updated successfully."))
|
2022-10-13 00:07:49 +00:00
|
|
|
|
2019-04-06 14:13:56 +00:00
|
|
|
self.assertIsInstance(response, HttpResponseRedirect)
|
|
|
|
|
|
2025-10-07 09:25:07 +00:00
|
|
|
@mock.patch("constance.settings.CONFIG_FIELDSETS", {"FieldSetOne": ("MULTILINE",)})
|
2024-07-03 14:21:33 +00:00
|
|
|
@mock.patch(
|
2025-10-07 09:25:07 +00:00
|
|
|
"constance.settings.CONFIG",
|
2024-07-03 14:21:33 +00:00
|
|
|
{
|
2025-10-07 09:25:07 +00:00
|
|
|
"MULTILINE": ("Hello\nWorld", "multiline value"),
|
2024-07-03 14:21:33 +00:00
|
|
|
},
|
|
|
|
|
)
|
2025-10-07 09:25:07 +00:00
|
|
|
@mock.patch("constance.settings.IGNORE_ADMIN_VERSION_CHECK", True)
|
2020-05-26 12:08:17 +00:00
|
|
|
def test_newlines_normalization(self):
|
2025-10-07 09:25:07 +00:00
|
|
|
self.client.login(username="admin", password="nimda")
|
2024-07-03 14:21:33 +00:00
|
|
|
request = self.rf.post(
|
2025-10-07 09:25:07 +00:00
|
|
|
"/admin/constance/config/",
|
2024-07-03 14:21:33 +00:00
|
|
|
data={
|
2025-10-07 09:25:07 +00:00
|
|
|
"MULTILINE": "Hello\r\nWorld",
|
|
|
|
|
"version": "123",
|
2024-07-03 14:21:33 +00:00
|
|
|
},
|
|
|
|
|
)
|
2020-05-26 12:08:17 +00:00
|
|
|
request.user = self.superuser
|
|
|
|
|
request._dont_enforce_csrf_checks = True
|
2025-10-07 09:25:07 +00:00
|
|
|
with mock.patch("django.contrib.messages.add_message"):
|
2020-05-26 12:08:17 +00:00
|
|
|
response = self.options.changelist_view(request, {})
|
|
|
|
|
self.assertIsInstance(response, HttpResponseRedirect)
|
2025-10-07 09:25:07 +00:00
|
|
|
self.assertEqual(get_values()["MULTILINE"], "Hello\nWorld")
|
2020-05-26 12:08:17 +00:00
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
@mock.patch(
|
2025-10-07 09:25:07 +00:00
|
|
|
"constance.settings.CONFIG",
|
2024-07-03 14:21:33 +00:00
|
|
|
{
|
2025-10-07 09:25:07 +00:00
|
|
|
"DATETIME_VALUE": (datetime(2019, 8, 7, 18, 40, 0), "some naive datetime"),
|
2024-07-03 14:21:33 +00:00
|
|
|
},
|
|
|
|
|
)
|
2025-10-07 09:25:07 +00:00
|
|
|
@mock.patch("constance.settings.IGNORE_ADMIN_VERSION_CHECK", True)
|
|
|
|
|
@mock.patch("tests.redis_mockup.Connection.set", mock.MagicMock())
|
2019-08-12 16:00:07 +00:00
|
|
|
def test_submit_aware_datetime(self):
|
|
|
|
|
"""
|
|
|
|
|
Test that submitting the admin page results in an http redirect when
|
|
|
|
|
everything is in order.
|
|
|
|
|
"""
|
2024-07-03 14:21:33 +00:00
|
|
|
request = self.rf.post(
|
2025-10-07 09:25:07 +00:00
|
|
|
"/admin/constance/config/",
|
2024-07-03 14:21:33 +00:00
|
|
|
data={
|
2025-10-07 09:25:07 +00:00
|
|
|
"DATETIME_VALUE_0": "2019-08-07",
|
|
|
|
|
"DATETIME_VALUE_1": "19:17:01",
|
|
|
|
|
"version": "123",
|
2024-07-03 14:21:33 +00:00
|
|
|
},
|
|
|
|
|
)
|
2019-08-12 16:00:07 +00:00
|
|
|
request.user = self.superuser
|
|
|
|
|
request._dont_enforce_csrf_checks = True
|
2025-10-07 09:25:07 +00:00
|
|
|
with mock.patch("django.contrib.messages.add_message"):
|
2019-08-12 16:00:07 +00:00
|
|
|
response = self.options.changelist_view(request, {})
|
|
|
|
|
self.assertIsInstance(response, HttpResponseRedirect)
|
|
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
@mock.patch(
|
2025-10-07 09:25:07 +00:00
|
|
|
"constance.settings.CONFIG_FIELDSETS",
|
2024-07-03 14:21:33 +00:00
|
|
|
{
|
2025-10-07 09:25:07 +00:00
|
|
|
"Numbers": ("INT_VALUE",),
|
|
|
|
|
"Text": ("STRING_VALUE",),
|
2024-07-03 14:21:33 +00:00
|
|
|
},
|
|
|
|
|
)
|
2019-04-06 14:13:56 +00:00
|
|
|
def test_inconsistent_fieldset_submit(self):
|
|
|
|
|
"""
|
|
|
|
|
Test that the admin page warns users if the CONFIG_FIELDSETS setting
|
|
|
|
|
doesn't account for every field in CONFIG.
|
|
|
|
|
"""
|
2025-10-07 09:25:07 +00:00
|
|
|
self.client.login(username="admin", password="nimda")
|
|
|
|
|
request = self.rf.post("/admin/constance/config/", data=None)
|
2019-04-06 14:13:56 +00:00
|
|
|
request.user = self.superuser
|
|
|
|
|
request._dont_enforce_csrf_checks = True
|
2025-10-07 09:25:07 +00:00
|
|
|
with mock.patch("django.contrib.messages.add_message"):
|
2022-10-13 17:16:31 +00:00
|
|
|
response = self.options.changelist_view(request, {})
|
2025-10-07 09:25:07 +00:00
|
|
|
self.assertContains(response, "is missing field(s)")
|
2019-04-06 14:13:56 +00:00
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
@mock.patch(
|
2025-10-07 09:25:07 +00:00
|
|
|
"constance.settings.CONFIG_FIELDSETS",
|
2024-07-03 14:21:33 +00:00
|
|
|
{
|
2025-10-07 09:25:07 +00:00
|
|
|
"Fieldsets": (
|
|
|
|
|
"STRING_VALUE",
|
|
|
|
|
"INT_VALUE",
|
2024-07-03 14:21:33 +00:00
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
)
|
2017-06-02 08:52:16 +00:00
|
|
|
def test_fieldset_ordering_1(self):
|
2024-07-05 14:38:26 +00:00
|
|
|
"""Ordering of inner list should be preserved."""
|
2025-10-07 09:25:07 +00:00
|
|
|
self.client.login(username="admin", password="nimda")
|
|
|
|
|
request = self.rf.get("/admin/constance/config/")
|
2017-06-02 08:52:16 +00:00
|
|
|
request.user = self.superuser
|
|
|
|
|
response = self.options.changelist_view(request, {})
|
|
|
|
|
response.render()
|
2019-12-23 21:20:41 +00:00
|
|
|
content_str = response.content.decode()
|
2025-10-07 09:25:07 +00:00
|
|
|
self.assertGreater(content_str.find("INT_VALUE"), content_str.find("STRING_VALUE"))
|
2017-06-02 08:52:16 +00:00
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
@mock.patch(
|
2025-10-07 09:25:07 +00:00
|
|
|
"constance.settings.CONFIG_FIELDSETS",
|
2024-07-03 14:21:33 +00:00
|
|
|
{
|
2025-10-07 09:25:07 +00:00
|
|
|
"Fieldsets": (
|
|
|
|
|
"INT_VALUE",
|
|
|
|
|
"STRING_VALUE",
|
2024-07-03 14:21:33 +00:00
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
)
|
2017-06-02 08:52:16 +00:00
|
|
|
def test_fieldset_ordering_2(self):
|
2024-07-05 14:38:26 +00:00
|
|
|
"""Ordering of inner list should be preserved."""
|
2025-10-07 09:25:07 +00:00
|
|
|
self.client.login(username="admin", password="nimda")
|
|
|
|
|
request = self.rf.get("/admin/constance/config/")
|
2017-06-02 08:52:16 +00:00
|
|
|
request.user = self.superuser
|
|
|
|
|
response = self.options.changelist_view(request, {})
|
|
|
|
|
response.render()
|
2019-12-23 21:20:41 +00:00
|
|
|
content_str = response.content.decode()
|
2025-10-07 09:25:07 +00:00
|
|
|
self.assertGreater(content_str.find("STRING_VALUE"), content_str.find("INT_VALUE"))
|
2018-03-06 10:16:44 +00:00
|
|
|
|
|
|
|
|
def test_labels(self):
|
|
|
|
|
self.assertEqual(type(self.model._meta.label), str)
|
|
|
|
|
self.assertEqual(type(self.model._meta.label_lower), str)
|