Skip to content

Commit 9a3dcfa

Browse files
committed
chore(tests): Enhance AccessList API view tests
Refactors and extends AccessList API view test cases. Adds support for testing virtual machines and improves test data setup by using `assigned_object` directly instead of separate fields for object type and ID.
1 parent 539c73a commit 9a3dcfa

File tree

1 file changed

+51
-19
lines changed

1 file changed

+51
-19
lines changed

netbox_acls/tests/api/test_access_lists.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,70 @@
11
from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site
22
from django.contrib.contenttypes.models import ContentType
33
from utilities.testing import APIViewTestCases
4+
from virtualization.models import Cluster, ClusterType, VirtualMachine
45

56
from netbox_acls.choices import *
67
from netbox_acls.models import *
78

89

9-
class ACLTestCase(
10-
APIViewTestCases.APIViewTestCase,
11-
):
12-
"""Test the AccessList Test"""
10+
class AccessListAPIViewTestCase(APIViewTestCases.APIViewTestCase):
11+
"""
12+
API view test case for AccessList.
13+
"""
1314

1415
model = AccessList
1516
view_namespace = "plugins-api:netbox_acls"
1617
brief_fields = ["display", "id", "name", "url"]
18+
user_permissions = (
19+
"dcim.view_site",
20+
"dcim.view_devicetype",
21+
"dcim.view_device",
22+
"virtualization.view_cluster",
23+
"virtualization.view_clustergroup",
24+
"virtualization.view_clustertype",
25+
"virtualization.view_virtualmachine",
26+
)
1727

1828
@classmethod
1929
def setUpTestData(cls):
20-
site = Site.objects.create(name="Site 1", slug="site-1")
30+
"""Set up Access List for API view testing."""
31+
site = Site.objects.create(
32+
name="Site 1",
33+
slug="site-1",
34+
)
35+
36+
# Device
2137
manufacturer = Manufacturer.objects.create(
2238
name="Manufacturer 1",
2339
slug="manufacturer-1",
2440
)
25-
devicetype = DeviceType.objects.create(
41+
device_type = DeviceType.objects.create(
2642
manufacturer=manufacturer,
2743
model="Device Type 1",
2844
)
29-
devicerole = DeviceRole.objects.create(
45+
device_role = DeviceRole.objects.create(
3046
name="Device Role 1",
3147
slug="device-role-1",
3248
)
3349
device = Device.objects.create(
3450
name="Device 1",
3551
site=site,
36-
device_type=devicetype,
37-
role=devicerole,
52+
device_type=device_type,
53+
role=device_role,
54+
)
55+
56+
# Virtual Machine
57+
cluster_type = ClusterType.objects.create(
58+
name="Cluster Type 1",
59+
slug="cluster-type-1",
60+
)
61+
cluster = Cluster.objects.create(
62+
name="Cluster 1",
63+
type=cluster_type,
64+
)
65+
virtual_machine = VirtualMachine.objects.create(
66+
name="VM 1",
67+
cluster=cluster,
3868
)
3969

4070
access_lists = (
@@ -47,16 +77,15 @@ def setUpTestData(cls):
4777
),
4878
AccessList(
4979
name="testacl2",
50-
assigned_object_type=ContentType.objects.get_for_model(Device),
51-
assigned_object_id=device.id,
52-
type=ACLTypeChoices.TYPE_STANDARD,
53-
default_action=ACLActionChoices.ACTION_DENY,
80+
assigned_object=device,
81+
type=ACLTypeChoices.TYPE_EXTENDED,
82+
default_action=ACLActionChoices.ACTION_PERMIT,
5483
),
5584
AccessList(
5685
name="testacl3",
57-
assigned_object_type=ContentType.objects.get_for_model(Device),
58-
assigned_object_id=device.id,
59-
type=ACLTypeChoices.TYPE_STANDARD,
86+
assigned_object_type=ContentType.objects.get_for_model(VirtualMachine),
87+
assigned_object_id=virtual_machine.id,
88+
type=ACLTypeChoices.TYPE_EXTENDED,
6089
default_action=ACLActionChoices.ACTION_DENY,
6190
),
6291
)
@@ -79,9 +108,12 @@ def setUpTestData(cls):
79108
},
80109
{
81110
"name": "testacl6",
82-
"assigned_object_type": "dcim.device",
83-
"assigned_object_id": device.id,
111+
"assigned_object_type": "virtualization.virtualmachine",
112+
"assigned_object_id": virtual_machine.id,
84113
"type": ACLTypeChoices.TYPE_STANDARD,
85-
"default_action": ACLActionChoices.ACTION_DENY,
114+
"default_action": ACLActionChoices.ACTION_PERMIT,
86115
},
87116
]
117+
cls.bulk_update_data = {
118+
"default_action": ACLActionChoices.ACTION_PERMIT,
119+
}

0 commit comments

Comments
 (0)