Skip to content

Commit 33865aa

Browse files
committed
chore(tests): Add ACLInterfaceAssignment API view tests
Introduces tests for ACLInterfaceAssignment API views. Covers device and virtual machine interfaces with various ACL directions and assignments, ensuring comprehensive test coverage for API behavior.
1 parent 9a3dcfa commit 33865aa

File tree

1 file changed

+150
-2
lines changed

1 file changed

+150
-2
lines changed

netbox_acls/tests/api/test_access_lists.py

Lines changed: 150 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site
1+
from dcim.choices import InterfaceTypeChoices
2+
from dcim.models import Device, DeviceRole, DeviceType, Interface, Manufacturer, Site
23
from django.contrib.contenttypes.models import ContentType
34
from utilities.testing import APIViewTestCases
4-
from virtualization.models import Cluster, ClusterType, VirtualMachine
5+
from virtualization.models import Cluster, ClusterType, VirtualMachine, VMInterface
56

67
from netbox_acls.choices import *
78
from netbox_acls.models import *
@@ -117,3 +118,150 @@ def setUpTestData(cls):
117118
cls.bulk_update_data = {
118119
"default_action": ACLActionChoices.ACTION_PERMIT,
119120
}
121+
122+
123+
class ACLInterfaceAssignmentAPIViewTestCase(APIViewTestCases.APIViewTestCase):
124+
"""
125+
API view test case for ACLInterfaceAssignment.
126+
"""
127+
128+
model = ACLInterfaceAssignment
129+
view_namespace = "plugins-api:netbox_acls"
130+
brief_fields = ["access_list", "display", "id", "url"]
131+
user_permissions = (
132+
"dcim.view_site",
133+
"dcim.view_devicetype",
134+
"dcim.view_device",
135+
"dcim.view_interface",
136+
"virtualization.view_cluster",
137+
"virtualization.view_clustergroup",
138+
"virtualization.view_clustertype",
139+
"virtualization.view_virtualmachine",
140+
"virtualization.view_vminterface",
141+
"netbox_acls.view_accesslist",
142+
)
143+
144+
@classmethod
145+
def setUpTestData(cls):
146+
"""Set up ACL Interface Assignment for API view testing."""
147+
site = Site.objects.create(
148+
name="Site 1",
149+
slug="site-1",
150+
)
151+
152+
# Device
153+
manufacturer = Manufacturer.objects.create(
154+
name="Manufacturer 1",
155+
slug="manufacturer-1",
156+
)
157+
device_type = DeviceType.objects.create(
158+
manufacturer=manufacturer,
159+
model="Device Type 1",
160+
)
161+
device_role = DeviceRole.objects.create(
162+
name="Device Role 1",
163+
slug="device-role-1",
164+
)
165+
device = Device.objects.create(
166+
name="Device 1",
167+
site=site,
168+
device_type=device_type,
169+
role=device_role,
170+
)
171+
device_interface1 = device.interfaces.create(
172+
name="DeviceInterface1",
173+
device=device,
174+
type=InterfaceTypeChoices.TYPE_1GE_FIXED,
175+
)
176+
device_interface2 = device.interfaces.create(
177+
name="DeviceInterface2",
178+
device=device,
179+
type=InterfaceTypeChoices.TYPE_1GE_FIXED,
180+
)
181+
device_interface3 = device.interfaces.create(
182+
name="DeviceInterface3",
183+
device=device,
184+
type=InterfaceTypeChoices.TYPE_1GE_FIXED,
185+
)
186+
187+
# Virtual Machine
188+
cluster_type = ClusterType.objects.create(
189+
name="Cluster Type 1",
190+
slug="cluster-type-1",
191+
)
192+
cluster = Cluster.objects.create(
193+
name="Cluster 1",
194+
type=cluster_type,
195+
)
196+
virtual_machine = VirtualMachine.objects.create(
197+
name="VM 1",
198+
cluster=cluster,
199+
)
200+
virtual_machine_interface1 = virtual_machine.interfaces.create(
201+
name="eth0",
202+
virtual_machine=virtual_machine,
203+
)
204+
virtual_machine_interface2 = virtual_machine.interfaces.create(
205+
name="eth1",
206+
virtual_machine=virtual_machine,
207+
)
208+
virtual_machine_interface3 = virtual_machine.interfaces.create(
209+
name="eth2",
210+
virtual_machine=virtual_machine,
211+
)
212+
213+
# AccessList
214+
access_list_device = AccessList.objects.create(
215+
name="testacl1",
216+
assigned_object=device,
217+
type=ACLTypeChoices.TYPE_STANDARD,
218+
default_action=ACLActionChoices.ACTION_DENY,
219+
)
220+
access_list_vm = AccessList.objects.create(
221+
name="testacl2",
222+
assigned_object=virtual_machine,
223+
type=ACLTypeChoices.TYPE_EXTENDED,
224+
default_action=ACLActionChoices.ACTION_PERMIT,
225+
)
226+
227+
acl_interface_assignments = (
228+
ACLInterfaceAssignment(
229+
access_list=access_list_device,
230+
direction=ACLAssignmentDirectionChoices.DIRECTION_INGRESS,
231+
assigned_object_type=ContentType.objects.get_for_model(Interface),
232+
assigned_object_id=device_interface1.id,
233+
),
234+
ACLInterfaceAssignment(
235+
access_list=access_list_device,
236+
direction=ACLAssignmentDirectionChoices.DIRECTION_EGRESS,
237+
assigned_object=device_interface2,
238+
),
239+
ACLInterfaceAssignment(
240+
access_list=access_list_vm,
241+
direction=ACLAssignmentDirectionChoices.DIRECTION_EGRESS,
242+
assigned_object_type=ContentType.objects.get_for_model(VMInterface),
243+
assigned_object_id=virtual_machine_interface1.id,
244+
),
245+
)
246+
ACLInterfaceAssignment.objects.bulk_create(acl_interface_assignments)
247+
248+
cls.create_data = [
249+
{
250+
"access_list": access_list_device.id,
251+
"assigned_object_type": "dcim.interface",
252+
"assigned_object_id": device_interface3.id,
253+
"direction": ACLAssignmentDirectionChoices.DIRECTION_EGRESS,
254+
},
255+
{
256+
"access_list": access_list_vm.id,
257+
"assigned_object_type": "virtualization.vminterface",
258+
"assigned_object_id": virtual_machine_interface2.id,
259+
"direction": ACLAssignmentDirectionChoices.DIRECTION_INGRESS,
260+
},
261+
{
262+
"access_list": access_list_vm.id,
263+
"assigned_object_type": "virtualization.vminterface",
264+
"assigned_object_id": virtual_machine_interface3.id,
265+
"direction": ACLAssignmentDirectionChoices.DIRECTION_EGRESS,
266+
},
267+
]

0 commit comments

Comments
 (0)