Skip to content

Commit 4cd6fe9

Browse files
committed
add draft test for plugin models
1 parent b1999e7 commit 4cd6fe9

File tree

1 file changed

+238
-0
lines changed

1 file changed

+238
-0
lines changed

netbox_acls/tests/test_models.py

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
from dcim.models import (
2+
Device,
3+
DeviceRole,
4+
DeviceType,
5+
Interface,
6+
Manufacturer,
7+
Site,
8+
VirtualChassis,
9+
)
10+
from django.contrib.contenttypes.models import ContentType
11+
from django.core.exceptions import ValidationError
12+
from django.test import TestCase
13+
from ipam.models import Prefix
14+
from netaddr import IPNetwork
15+
from virtualization.models import Cluster, ClusterType, VirtualMachine, VMInterface
16+
17+
from netbox_acls.choices import *
18+
from netbox_acls.models import *
19+
20+
21+
class BaseTestCase(TestCase):
22+
"""
23+
Base test case for netbox_acls models.
24+
"""
25+
26+
@classmethod
27+
def setUpTestData(cls):
28+
"""
29+
Create base data to test using including:
30+
- 1 of each of the following: test site, manufacturer, device type, device role, cluster type, cluster, virtual_chassis, & virtual machine
31+
- 2 devices, prefixes, 2 interfaces, and 2 vminterfaces
32+
"""
33+
34+
site = Site.objects.create(name="Site 1", slug="site-1")
35+
manufacturer = Manufacturer.objects.create(
36+
name="Manufacturer 1",
37+
slug="manufacturer-1",
38+
)
39+
devicetype = DeviceType.objects.create(
40+
manufacturer=manufacturer,
41+
model="Device Type 1",
42+
)
43+
devicerole = DeviceRole.objects.create(
44+
name="Device Role 1",
45+
slug="device-role-1",
46+
)
47+
device = Device.objects.create(
48+
name="Device 1",
49+
site=site,
50+
device_type=devicetype,
51+
device_role=devicerole,
52+
)
53+
virtual_chassis = VirtualChassis.objects.create(name="Virtual Chassis 1")
54+
virtual_chassis_member = Device.objects.create(
55+
name="VC Device",
56+
site=site,
57+
device_type=devicetype,
58+
device_role=devicerole,
59+
virtual_chassis=virtual_chassis,
60+
vc_position=1,
61+
)
62+
cluster_member = Device.objects.create(
63+
name="Cluster Device",
64+
site=site,
65+
device_type=devicetype,
66+
device_role=devicerole,
67+
)
68+
clustertype = ClusterType.objects.create(name="Cluster Type 1")
69+
cluster = Cluster.objects.create(
70+
name="Cluster 1",
71+
type=clustertype,
72+
)
73+
virtual_machine = VirtualMachine.objects.create(name="VirtualMachine 1")
74+
75+
76+
class TestAccessList(BaseTestCase):
77+
"""
78+
Test AccessList model.
79+
"""
80+
81+
def test_alphanumeric_plus_success(self):
82+
"""
83+
Test that AccessList names with alphanumeric characters, '_', or '-' pass validation.
84+
"""
85+
acl_good_name = AccessList(
86+
name="Testacl-good_name-1",
87+
assigned_object_type=ContentType.objects.get_for_model(Device),
88+
assigned_object_id=1,
89+
type=ACLTypeChoices.TYPE_EXTENDED,
90+
default_action=ACLActionChoices.ACTION_PERMIT,
91+
)
92+
acl_good_name.full_clean()
93+
# TODO: test_alphanumeric_plus_success - VirtualChassis & Cluster
94+
95+
def test_duplicate_name_success(self):
96+
"""
97+
Test that AccessList names can be non-unique if associated to different devices.
98+
"""
99+
100+
params = {
101+
"name": "GOOD-DUPLICATE-ACL",
102+
"type": ACLTypeChoices.TYPE_STANDARD,
103+
"default_action": ACLActionChoices.ACTION_PERMIT,
104+
}
105+
AccessList.objects.create(
106+
**params,
107+
assigned_object_type=ContentType.objects.get_for_model(Device),
108+
assigned_object_id=1,
109+
)
110+
vm_acl = AccessList(
111+
**params,
112+
assigned_object_type=ContentType.objects.get_for_model(VirtualMachine),
113+
assigned_object_id=1,
114+
)
115+
vm_acl.full_clean()
116+
# TODO: test_duplicate_name_success - VirtualChassis & Cluster
117+
#vc_acl = AccessList(
118+
# **params,
119+
# assigned_object_type=ContentType.objects.get_for_model(VirtualChassis),
120+
# assigned_object_id=1,
121+
#)
122+
#vc_acl.full_clean()
123+
124+
def test_alphanumeric_plus_fail(self):
125+
"""
126+
Test that AccessList names with non-alphanumeric (exluding '_' and '-') characters fail validation.
127+
"""
128+
non_alphanumeric_plus_chars = " !@#$%^&*()[]{};:,./<>?\|~=+"
129+
130+
for i, char in enumerate(non_alphanumeric_plus_chars, start=1):
131+
bad_acl_name = AccessList(
132+
name=f"Testacl-bad_name_{i}_{char}",
133+
assigned_object_type=ContentType.objects.get_for_model(Device),
134+
assigned_object_id=1,
135+
type=ACLTypeChoices.TYPE_EXTENDED,
136+
default_action=ACLActionChoices.ACTION_PERMIT,
137+
comments=f'ACL with "{char}" in name',
138+
)
139+
with self.assertRaises(ValidationError):
140+
bad_acl_name.full_clean()
141+
142+
def test_duplicate_name_fail(self):
143+
"""
144+
Test that AccessList names must be unique per device.
145+
"""
146+
params = {
147+
"name": "FAIL-DUPLICATE-ACL",
148+
"assigned_object_type": ContentType.objects.get_for_model(Device),
149+
"assigned_object_id": 1,
150+
"type": ACLTypeChoices.TYPE_STANDARD,
151+
"default_action": ACLActionChoices.ACTION_PERMIT,
152+
}
153+
acl_1 = AccessList.objects.create(**params)
154+
acl_1.save()
155+
acl_2 = AccessList(**params)
156+
with self.assertRaises(ValidationError):
157+
acl_2.full_clean()
158+
# TODO: test_duplicate_name_fail - VirtualChassis & Cluster
159+
160+
# TODO: Test choices for AccessList Model
161+
162+
163+
class TestACLInterfaceAssignment(BaseTestCase):
164+
"""
165+
Test ACLInterfaceAssignment model.
166+
"""
167+
168+
@classmethod
169+
def setUpTestData(cls):
170+
"""
171+
Extend BaseTestCase's setUpTestData() to create additional data for testing.
172+
"""
173+
super().setUpTestData()
174+
175+
#interfaces = Interface.objects.bulk_create(
176+
# (
177+
# Interface(name="Interface 1", device=device, type="1000baset"),
178+
# Interface(name="Interface 2", device=device, type="1000baset"),
179+
# )
180+
#)
181+
#vminterfaces = VMInterface.objects.bulk_create(
182+
# (
183+
# VMInterface(name="Interface 1", virtual_machine=virtual_machine),
184+
# VMInterface(name="Interface 2", virtual_machine=virtual_machine),
185+
# )
186+
#)
187+
# prefixes = Prefix.objects.bulk_create(
188+
# (
189+
# Prefix(prefix=IPNetwork("10.0.0.0/24")),
190+
# Prefix(prefix=IPNetwork("192.168.1.0/24")),
191+
# )
192+
# )
193+
194+
def test_acl_interface_assignment_success(self):
195+
"""
196+
Test that ACLInterfaceAssignment passes validation if the ACL is assigned to the host and not already assigned to the interface and direction.
197+
"""
198+
pass
199+
# TODO: test_acl_interface_assignment_success - VM & Device
200+
201+
def test_acl_interface_assignment_fail(self):
202+
"""
203+
Test that ACLInterfaceAssignment fails validation if the ACL is not assigned to the parent host.
204+
"""
205+
pass
206+
# TODO: test_acl_interface_assignment_fail - VM & Device
207+
208+
def test_duplicate_assignment_fail(self):
209+
"""
210+
Test that ACLInterfaceAssignment fails validation if the ACL already is assigned to the same interface and direction.
211+
"""
212+
pass
213+
# TODO: test_duplicate_assignment_fail - VM & Device
214+
215+
def test_acl_already_assinged_fail(self):
216+
"""
217+
Test that ACLInterfaceAssignment fails validation if the interface already has an ACL assigned in the same direction.
218+
"""
219+
pass
220+
## TODO: test_acl_already_assinged_fail - VM & Device
221+
222+
# TODO: Test choices for ACLInterfaceAssignment Model
223+
224+
225+
# TODO: Investigate a Base model for ACLStandardRule and ACLExtendedRule
226+
227+
class TestACLStandardRule(BaseTestCase):
228+
"""
229+
Test ACLStandardRule model.
230+
"""
231+
# TODO: Develop tests for ACLStandardRule model
232+
233+
234+
class ACLExtendedRule(BaseTestCase):
235+
"""
236+
Test ACLExtendedRule model.
237+
"""
238+
# TODO: Develop tests for ACLExtendedRule model

0 commit comments

Comments
 (0)