|
6 | 6 |
|
7 | 7 | import django |
8 | 8 | from django.contrib.admin.models import LogEntry |
| 9 | +from django.contrib.admin.templatetags.admin_urls import admin_urlname |
9 | 10 | from django.contrib.auth import get_user_model |
10 | 11 | from django.core.exceptions import ValidationError |
11 | 12 | from django.core.files.base import ContentFile |
|
16 | 17 | from django.urls import reverse |
17 | 18 | from reversion.models import Version |
18 | 19 | from swapper import load_model |
19 | | - |
| 20 | +from openwisp_controller.config.models import Device, Template, Vpn |
20 | 21 | from openwisp_utils.tests import ( |
21 | 22 | AdminActionPermTestMixin, |
22 | 23 | capture_any_output, |
@@ -1409,39 +1410,46 @@ def test_malformed_admin_urls_return_404(self): |
1409 | 1410 | device = self._create_device() |
1410 | 1411 | template = self._create_template() |
1411 | 1412 | vpn = self._create_vpn() |
| 1413 | + |
| 1414 | + model_map = {"device": Device, "template": Template, "vpn": Vpn} |
1412 | 1415 | test_cases = [ |
1413 | 1416 | ("device", device.pk, "config_device"), |
1414 | 1417 | ("template", template.pk, "config_template"), |
1415 | 1418 | ("vpn", vpn.pk, "config_vpn"), |
1416 | 1419 | ] |
| 1420 | + |
1417 | 1421 | junk_path = "some/junk/path/here/" |
1418 | 1422 | original_bug_junk = "history/1564/undefinedadmin/img/icon-deletelink.svg" |
1419 | 1423 |
|
1420 | 1424 | for model_name, valid_pk, model_admin_name in test_cases: |
1421 | 1425 | 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") |
1423 | 1428 | valid_change_url = reverse(change_url_name, args=[valid_pk]) |
1424 | 1429 | malformed_change_url = f"{valid_change_url}{junk_path}" |
1425 | | - response_change = self.client.get(malformed_change_url, follow=False) |
1426 | 1430 |
|
| 1431 | + response_change = self.client.get(malformed_change_url, follow=False) |
1427 | 1432 | self.assertEqual( |
1428 | 1433 | response_change.status_code, |
1429 | 1434 | 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.', |
1431 | 1436 | ) |
| 1437 | + |
| 1438 | + # HISTORY URL (kept as existing explicit admin name) |
1432 | 1439 | history_url_name = f"admin:{model_admin_name}_history" |
1433 | 1440 | valid_history_url = reverse(history_url_name, args=[valid_pk]) |
1434 | 1441 | malformed_history_url = f"{valid_history_url}{junk_path}" |
1435 | | - response_history = self.client.get(malformed_history_url, follow=False) |
1436 | 1442 |
|
| 1443 | + response_history = self.client.get(malformed_history_url, follow=False) |
1437 | 1444 | self.assertEqual( |
1438 | 1445 | response_history.status_code, |
1439 | 1446 | 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.', |
1441 | 1448 | ) |
| 1449 | + |
| 1450 | + # ORIGINAL BUG URL regression check (#682) |
1442 | 1451 | original_bug_url = f"{valid_history_url}{original_bug_junk}" |
1443 | 1452 | response_original_bug = self.client.get(original_bug_url, follow=False) |
1444 | | - |
1445 | 1453 | self.assertEqual( |
1446 | 1454 | response_original_bug.status_code, |
1447 | 1455 | 404, |
|
0 commit comments