Skip to content

Commit 9cbf862

Browse files
xoaryaastktyagi
authored andcommitted
Fix admin URL reversing in malformed admin URL tests
1 parent 0348df0 commit 9cbf862

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

openwisp_controller/config/tests/test_admin.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import django
88
from django.contrib.admin.models import LogEntry
9+
from django.contrib.admin.templatetags.admin_urls import admin_urlname
910
from django.contrib.auth import get_user_model
1011
from django.core.exceptions import ValidationError
1112
from django.core.files.base import ContentFile
@@ -16,7 +17,7 @@
1617
from django.urls import reverse
1718
from reversion.models import Version
1819
from swapper import load_model
19-
20+
from openwisp_controller.config.models import Device, Template, Vpn
2021
from openwisp_utils.tests import (
2122
AdminActionPermTestMixin,
2223
capture_any_output,
@@ -1409,39 +1410,46 @@ def test_malformed_admin_urls_return_404(self):
14091410
device = self._create_device()
14101411
template = self._create_template()
14111412
vpn = self._create_vpn()
1413+
1414+
model_map = {"device": Device, "template": Template, "vpn": Vpn}
14121415
test_cases = [
14131416
("device", device.pk, "config_device"),
14141417
("template", template.pk, "config_template"),
14151418
("vpn", vpn.pk, "config_vpn"),
14161419
]
1420+
14171421
junk_path = "some/junk/path/here/"
14181422
original_bug_junk = "history/1564/undefinedadmin/img/icon-deletelink.svg"
14191423

14201424
for model_name, valid_pk, model_admin_name in test_cases:
14211425
with self.subTest(model=model_name):
1422-
change_url_name = f"admin:{model_admin_name}_change"
1426+
# CHANGE URL (robust across admin namespaces)
1427+
change_url_name = admin_urlname(model_map[model_name]._meta, "change")
14231428
valid_change_url = reverse(change_url_name, args=[valid_pk])
14241429
malformed_change_url = f"{valid_change_url}{junk_path}"
1425-
response_change = self.client.get(malformed_change_url, follow=False)
14261430

1431+
response_change = self.client.get(malformed_change_url, follow=False)
14271432
self.assertEqual(
14281433
response_change.status_code,
14291434
404,
1430-
(f'Malformed "change" URL for {model_name} did not return 404. '),
1435+
f'Malformed "change" URL for {model_name} did not return 404.',
14311436
)
1437+
1438+
# HISTORY URL (kept as existing explicit admin name)
14321439
history_url_name = f"admin:{model_admin_name}_history"
14331440
valid_history_url = reverse(history_url_name, args=[valid_pk])
14341441
malformed_history_url = f"{valid_history_url}{junk_path}"
1435-
response_history = self.client.get(malformed_history_url, follow=False)
14361442

1443+
response_history = self.client.get(malformed_history_url, follow=False)
14371444
self.assertEqual(
14381445
response_history.status_code,
14391446
404,
1440-
(f'Malformed "history" URL for {model_name} did not return 404. '),
1447+
f'Malformed "history" URL for {model_name} did not return 404.',
14411448
)
1449+
1450+
# ORIGINAL BUG URL regression check (#682)
14421451
original_bug_url = f"{valid_history_url}{original_bug_junk}"
14431452
response_original_bug = self.client.get(original_bug_url, follow=False)
1444-
14451453
self.assertEqual(
14461454
response_original_bug.status_code,
14471455
404,

0 commit comments

Comments
 (0)