Skip to content

Commit d818504

Browse files
committed
fix: use reverse() for YAML export URL, add permission check, fix unauthenticated test
- Replace hardcoded YAML_URL with reverse() in setUpTestData - Add view_interfacenamerule permission check to get() and post() - Add self.client.logout() to test_yaml_export_unauthenticated_responds so it actually exercises unauthenticated access
1 parent cc68f42 commit d818504

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

netbox_interface_name_rules/tests/test_views.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -723,15 +723,20 @@ def test_csv_round_trip_creates_rule(self):
723723
class YAMLExportViewTest(ViewTestBase):
724724
"""Tests for the YAML export view at GET/POST /rules/export/yaml/."""
725725

726-
YAML_URL = "/plugins/interface-name-rules/rules/export/yaml/"
726+
@classmethod
727+
def setUpTestData(cls):
728+
super().setUpTestData()
729+
cls.YAML_URL = reverse("plugins:netbox_interface_name_rules:interfacenamerule_export_yaml")
727730

728731
def test_yaml_export_unauthenticated_responds(self):
729-
"""Unauthenticated GET must not return a server error (4xx or 2xx is acceptable).
732+
"""Unauthenticated GET must not return a server error.
730733
731-
ViewTestBase does not log in by default — this exercises anonymous access.
732-
ConditionalLoginRequiredMixin enforces auth only when LOGIN_REQUIRED=True;
733-
in the test environment that setting may be False, so 200 is also valid.
734+
setUp() logs in the superuser, so we explicitly logout first to exercise
735+
anonymous access. ConditionalLoginRequiredMixin only enforces auth when
736+
LOGIN_REQUIRED=True; in the test environment that setting may be False,
737+
so 200 is also valid alongside a redirect.
734738
"""
739+
self.client.logout()
735740
response = self.client.get(self.YAML_URL)
736741
self.assertIn(response.status_code, [200, 301, 302])
737742

netbox_interface_name_rules/views.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ def _build_response(self, queryset):
138138

139139
def get(self, request):
140140
"""Export all rules as YAML."""
141+
if not request.user.has_perm("netbox_interface_name_rules.view_interfacenamerule"):
142+
raise PermissionDenied
141143
return self._build_response(InterfaceNameRule.objects.all())
142144

143145
def post(self, request):
@@ -147,6 +149,8 @@ def post(self, request):
147149
post their PKs as repeated ``pk`` inputs. Falls back to all rules when
148150
nothing is selected.
149151
"""
152+
if not request.user.has_perm("netbox_interface_name_rules.view_interfacenamerule"):
153+
raise PermissionDenied
150154
pk_list = [int(v) for v in request.POST.getlist("pk") if v.isdigit()]
151155
if pk_list:
152156
queryset = InterfaceNameRule.objects.filter(pk__in=pk_list)

0 commit comments

Comments
 (0)