Skip to content

Commit ddaae26

Browse files
committed
chore(tests): Enhance test fixtures
Refactor test setup to improve data creation and reusability. Add detailed fixtures for devices, virtual chassis, clusters, virtual machines, and prefixes. Update all assignments to reference the new fixtures for better consistency.
1 parent 2702dfa commit ddaae26

File tree

3 files changed

+134
-96
lines changed

3 files changed

+134
-96
lines changed

netbox_acls/tests/models/base.py

Lines changed: 80 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
DeviceType,
55
Manufacturer,
66
Site,
7+
VirtualChassis,
78
)
89
from django.test import TestCase
910
from ipam.models import Prefix
10-
from virtualization.models import VirtualMachine
11+
from virtualization.models import Cluster, ClusterType, VirtualMachine
1112

1213

1314
class BaseTestCase(TestCase):
@@ -20,50 +21,97 @@ def setUpTestData(cls):
2021
"""
2122
Create base data to test using including
2223
- 1 of each of the following: test site, manufacturer, device type
23-
device role, cluster type, cluster, virtual_chassis, and
24+
device role, cluster type, cluster, virtual chassis, and
2425
virtual machine
25-
- 2 devices, prefixes, 2 interfaces, and 2 vminterfaces
26+
- 2 of each Device, prefix
2627
"""
2728

28-
site = Site.objects.create(name="Site 1", slug="site-1")
29+
# Sites
30+
site = Site.objects.create(
31+
name="Site 1",
32+
slug="site-1",
33+
)
34+
35+
# Device Types
2936
manufacturer = Manufacturer.objects.create(
3037
name="Manufacturer 1",
3138
slug="manufacturer-1",
3239
)
33-
devicetype = DeviceType.objects.create(
40+
device_type = DeviceType.objects.create(
3441
manufacturer=manufacturer,
3542
model="Device Type 1",
3643
)
37-
devicerole = DeviceRole.objects.create(
44+
45+
# Device Roles
46+
device_role = DeviceRole.objects.create(
3847
name="Device Role 1",
3948
slug="device-role-1",
4049
)
41-
device = Device.objects.create(
50+
51+
# Devices
52+
cls.device1 = Device.objects.create(
4253
name="Device 1",
4354
site=site,
44-
device_type=devicetype,
45-
device_role=devicerole,
55+
device_type=device_type,
56+
role=device_role,
57+
)
58+
cls.device2 = Device.objects.create(
59+
name="Device 2",
60+
site=site,
61+
device_type=device_type,
62+
role=device_role,
63+
)
64+
65+
# Virtual Chassis
66+
cls.virtual_chassis1 = VirtualChassis.objects.create(
67+
name="Virtual Chassis 1",
68+
)
69+
70+
# Virtual Chassis Members
71+
cls.virtual_chassis_member1 = Device.objects.create(
72+
name="VC Device",
73+
site=site,
74+
device_type=device_type,
75+
role=device_role,
76+
virtual_chassis=cls.virtual_chassis1,
77+
vc_position=1,
78+
)
79+
80+
# Virtualization Cluster Type
81+
cluster_type = ClusterType.objects.create(
82+
name="Cluster Type 1",
83+
)
84+
85+
# Virtualization Cluster
86+
cluster = Cluster.objects.create(
87+
name="Cluster 1",
88+
type=cluster_type,
89+
)
90+
91+
# Virtualization Cluster Member
92+
cls.cluster_member1 = Device.objects.create(
93+
name="Cluster Device",
94+
site=site,
95+
device_type=device_type,
96+
role=device_role,
97+
)
98+
99+
# Virtual Machine
100+
cls.virtual_machine1 = VirtualMachine.objects.create(
101+
name="VirtualMachine 1",
102+
status="active",
103+
cluster=cluster,
104+
)
105+
cls.virtual_machine2 = VirtualMachine.objects.create(
106+
name="VirtualMachine 2",
107+
status="active",
108+
cluster=cluster,
109+
)
110+
111+
# Prefix
112+
cls.prefix1 = Prefix.objects.create(
113+
prefix="10.1.0.0/16",
114+
)
115+
cls.prefix2 = Prefix.objects.create(
116+
prefix="10.2.0.0/16",
46117
)
47-
# virtual_chassis = VirtualChassis.objects.create(name="Virtual Chassis 1")
48-
# virtual_chassis_member = Device.objects.create(
49-
# name="VC Device",
50-
# site=site,
51-
# device_type=devicetype,
52-
# device_role=devicerole,
53-
# virtual_chassis=virtual_chassis,
54-
# vc_position=1,
55-
# )
56-
# cluster_member = Device.objects.create(
57-
# name="Cluster Device",
58-
# site=site,
59-
# device_type=devicetype,
60-
# device_role=devicerole,
61-
# )
62-
# clustertype = ClusterType.objects.create(name="Cluster Type 1")
63-
# cluster = Cluster.objects.create(
64-
# name="Cluster 1",
65-
# type=clustertype,
66-
# )
67-
virtual_machine = VirtualMachine.objects.create(name="VirtualMachine 1")
68-
virtual_machine.save()
69-
prefix = Prefix.objects.create(prefix="10.0.0.0/8")

netbox_acls/tests/models/test_accesslists.py

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from django.contrib.contenttypes.models import ContentType
55
from django.core.exceptions import ValidationError
66
from ipam.models import Prefix
7-
from virtualization.models import VirtualMachine
87

98
from netbox_acls.models import AccessList
109

@@ -20,16 +19,16 @@ class TestAccessList(BaseTestCase):
2019
"type": "extended",
2120
"default_action": "permit",
2221
}
23-
# device = Device.objects.first()
2422

2523
def test_wrong_assigned_object_type_fail(self):
2624
"""
27-
Test that AccessList cannot be assigned to an object type other than Device, VirtualChassis, VirtualMachine, or Cluster.
25+
Test that AccessList cannot be assigned to an object type other than Device, VirtualChassis, VirtualMachine,
26+
or Cluster.
2827
"""
2928
acl_bad_gfk = AccessList(
3029
name="TestACL_Wrong_GFK",
3130
assigned_object_type=ContentType.objects.get_for_model(Prefix),
32-
assigned_object_id=Prefix.objects.first(),
31+
assigned_object_id=self.prefix1.id,
3332
**self.common_acl_params,
3433
)
3534
with self.assertRaises(ValidationError):
@@ -40,31 +39,24 @@ def test_alphanumeric_plus_success(self):
4039
Test that AccessList names with alphanumeric characters, '_', or '-' pass validation.
4140
"""
4241
acl_good_name = AccessList(
43-
name="Testacl-Good_Name-1",
42+
name="Test-ACL-Good_Name-1",
4443
assigned_object_type=ContentType.objects.get_for_model(Device),
45-
assigned_object_id=1, # TODO - replace with Device.objects.first()
44+
assigned_object_id=self.device1.id,
4645
**self.common_acl_params,
4746
)
4847
acl_good_name.full_clean()
49-
# TODO: test_alphanumeric_plus_success - VirtualChassis, VirtualMachine & Cluster
5048

5149
def test_duplicate_name_success(self):
5250
"""
5351
Test that AccessList names can be non-unique if associated with different devices.
5452
"""
55-
AccessList.objects.create(
53+
# Device
54+
device_acl = AccessList(
5655
name="GOOD-DUPLICATE-ACL",
57-
assigned_object_type=ContentType.objects.get_for_model(Device),
58-
assigned_object_id=1, # TODO - replace with Device.objects.first()
59-
**self.common_acl_params,
60-
)
61-
vm_acl = AccessList(
62-
name="GOOD-DUPLICATE-ACL",
63-
assigned_object_type=ContentType.objects.get_for_model(VirtualMachine),
64-
assigned_object_id=1, # TODO - replace with VirtualMachine.objects.first().id,
56+
assigned_object=self.device1,
6557
**self.common_acl_params,
6658
)
67-
vm_acl.full_clean()
59+
device_acl.full_clean()
6860
# TODO: test_duplicate_name_success - VirtualChassis, VirtualMachine & Cluster
6961
# vc_acl = AccessList(
7062
# "name": "GOOD-DUPLICATE-ACL",
@@ -81,23 +73,23 @@ def test_alphanumeric_plus_fail(self):
8173

8274
for i, char in enumerate(non_alphanumeric_plus_chars, start=1):
8375
bad_acl_name = AccessList(
84-
name=f"Testacl-bad_name_{i}_{char}",
85-
assigned_object_type=ContentType.objects.get_for_model(Device),
76+
name=f"Test-ACL-bad_name_{i}_{char}",
77+
assigned_object=self.device1,
8678
comments=f'ACL with "{char}" in name',
8779
**self.common_acl_params,
8880
)
8981
with self.assertRaises(ValidationError):
9082
bad_acl_name.full_clean()
9183

92-
def test_duplicate_name_fail(self):
84+
def test_duplicate_name_per_device_fail(self):
9385
"""
9486
Test that AccessList names must be unique per device.
9587
"""
9688
params = {
9789
"name": "FAIL-DUPLICATE-ACL",
9890
"assigned_object_type": ContentType.objects.get_for_model(Device),
91+
"assigned_object_id": self.device1.id,
9992
**self.common_acl_params,
100-
"assigned_object_id": 1, # TODO - replace with Device.objects.first()
10193
}
10294
acl_1 = AccessList.objects.create(**params)
10395
acl_1.save()
@@ -122,11 +114,10 @@ def test_valid_acl_choices(self):
122114
for default_action, acl_type in valid_acl_choices:
123115
valid_acl_choice = AccessList(
124116
name=f"TestACL_Valid_Choice_{default_action}_{acl_type}",
125-
comments=f"VALID ACL CHOICES USED: {default_action=} {acl_type=}",
117+
assigned_object=self.device1,
126118
type=acl_type,
127119
default_action=default_action,
128-
assigned_object_type=ContentType.objects.get_for_model(Device),
129-
assigned_object_id=1, # TODO - replace with Device.objects.first()
120+
comments=f"VALID ACL CHOICES USED: {default_action=} {acl_type=}",
130121
)
131122
valid_acl_choice.full_clean()
132123

@@ -138,11 +129,10 @@ def test_invalid_acl_choices(self):
138129
invalid_acl_default_action_choice = "log"
139130
invalid_acl_default_action = AccessList(
140131
name=f"TestACL_Valid_Choice_{invalid_acl_default_action_choice}_{valid_acl_types[0]}",
141-
comments=f"INVALID ACL DEFAULT CHOICE USED: default_action='{invalid_acl_default_action_choice}'",
132+
assigned_object=self.device1,
142133
type=valid_acl_types[0],
143134
default_action=invalid_acl_default_action_choice,
144-
assigned_object_type=ContentType.objects.get_for_model(Device),
145-
assigned_object_id=1, # TODO - replace with Device.objects.first()
135+
comments=f"INVALID ACL DEFAULT CHOICE USED: default_action='{invalid_acl_default_action_choice}'",
146136
)
147137
with self.assertRaises(ValidationError):
148138
invalid_acl_default_action.full_clean()
@@ -151,11 +141,10 @@ def test_invalid_acl_choices(self):
151141
invalid_acl_type = "super-dupper-extended"
152142
invalid_acl_type = AccessList(
153143
name=f"TestACL_Valid_Choice_{valid_acl_default_action_choices[0]}_{invalid_acl_type}",
154-
comments=f"INVALID ACL DEFAULT CHOICE USED: type='{invalid_acl_type}'",
144+
assigned_object=self.device1,
155145
type=invalid_acl_type,
156146
default_action=valid_acl_default_action_choices[0],
157-
assigned_object_type=ContentType.objects.get_for_model(Device),
158-
assigned_object_id=1, # TODO - replace with Device.objects.first()
147+
comments=f"INVALID ACL DEFAULT CHOICE USED: type='{invalid_acl_type}'",
159148
)
160149
with self.assertRaises(ValidationError):
161150
invalid_acl_type.full_clean()

0 commit comments

Comments
 (0)