Skip to content

Commit 3f4c9d7

Browse files
committed
update tests
1 parent d094bf6 commit 3f4c9d7

File tree

2 files changed

+217
-12
lines changed

2 files changed

+217
-12
lines changed

netbox_acls/tests/test_api.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ def test_root(self):
1818
Test the API root
1919
"""
2020
url = reverse("plugins-api:netbox_acls-api:api-root")
21-
response = self.client.get(f"{url}?format=api", **self.header)
21+
response = self.client.get("{}?format=api".format(url), **self.header)
2222

2323
self.assertEqual(response.status_code, status.HTTP_200_OK)
2424

2525

2626
class ACLTestCase(
27+
APIViewTestCases.CreateObjectViewTestCase,
28+
APIViewTestCases.DeleteObjectViewTestCase,
2729
APIViewTestCases.GetObjectViewTestCase,
2830
APIViewTestCases.ListObjectsViewTestCase,
29-
APIViewTestCases.CreateObjectViewTestCase,
3031
APIViewTestCases.UpdateObjectViewTestCase,
31-
APIViewTestCases.DeleteObjectViewTestCase,
3232
):
3333
"""
3434
Test the AccessList Test
@@ -65,49 +65,67 @@ def setUpTestData(cls):
6565

6666
access_lists = (
6767
AccessList(
68-
name="testacl1",
68+
name="testacl-standard-deny-1",
6969
assigned_object_type=ContentType.objects.get_for_model(Device),
7070
assigned_object_id=device.id,
7171
type=ACLTypeChoices.TYPE_STANDARD,
7272
default_action=ACLActionChoices.ACTION_DENY,
73+
comments="Comment Test 1",
7374
),
7475
AccessList(
75-
name="testacl2",
76+
name="testacl-standard-permit-1",
7677
assigned_object_type=ContentType.objects.get_for_model(Device),
7778
assigned_object_id=device.id,
7879
type=ACLTypeChoices.TYPE_STANDARD,
79-
default_action=ACLActionChoices.ACTION_DENY,
80+
default_action=ACLActionChoices.ACTION_PERMIT,
81+
comments="Comment Test 2",
8082
),
8183
AccessList(
82-
name="testacl3",
84+
name="testacl-extended-deny-1",
8385
assigned_object_type=ContentType.objects.get_for_model(Device),
8486
assigned_object_id=device.id,
85-
type=ACLTypeChoices.TYPE_STANDARD,
87+
type=ACLTypeChoices.TYPE_EXTENDED,
8688
default_action=ACLActionChoices.ACTION_DENY,
8789
),
90+
AccessList(
91+
name="testacl-extended-permit-1",
92+
assigned_object_type=ContentType.objects.get_for_model(Device),
93+
assigned_object_id=device.id,
94+
type=ACLTypeChoices.TYPE_EXTENDED,
95+
default_action=ACLActionChoices.ACTION_PERMIT,
96+
),
8897
)
8998
AccessList.objects.bulk_create(access_lists)
9099

91100
cls.create_data = [
92101
{
93-
"name": "testacl4",
102+
"name": "testacl-standard-deny-2",
94103
"assigned_object_type": "dcim.device",
95104
"assigned_object_id": device.id,
96105
"type": ACLTypeChoices.TYPE_STANDARD,
97106
"default_action": ACLActionChoices.ACTION_DENY,
107+
"comments": "Comment Test 3",
98108
},
99109
{
100-
"name": "testacl5",
110+
"name": "testacl-standard-permit-2",
111+
"assigned_object_type": "dcim.device",
112+
"assigned_object_id": device.id,
113+
"type": ACLTypeChoices.TYPE_STANDARD,
114+
"default_action": ACLActionChoices.ACTION_DENY,
115+
"comments": "Comment Test 4",
116+
},
117+
{
118+
"name": "testacl-extended-deny-2",
101119
"assigned_object_type": "dcim.device",
102120
"assigned_object_id": device.id,
103121
"type": ACLTypeChoices.TYPE_EXTENDED,
104122
"default_action": ACLActionChoices.ACTION_DENY,
105123
},
106124
{
107-
"name": "testacl6",
125+
"name": "testacl-extended-permit-2",
108126
"assigned_object_type": "dcim.device",
109127
"assigned_object_id": device.id,
110-
"type": ACLTypeChoices.TYPE_STANDARD,
128+
"type": ACLTypeChoices.TYPE_EXTENDED,
111129
"default_action": ACLActionChoices.ACTION_DENY,
112130
},
113131
]

netbox_acls/tests/test_models.py

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
from dcim.models import Device, DeviceRole, DeviceType, Interface, Manufacturer, Site
2+
from django.contrib.contenttypes.models import ContentType
3+
from django.core.exceptions import ValidationError
4+
from django.test import TestCase
5+
from ipam.models import Prefix
6+
from netaddr import IPNetwork
7+
from virtualization.models import Cluster, ClusterType, VirtualMachine, VMInterface
8+
9+
from netbox_acls.choices import *
10+
from netbox_acls.models import *
11+
12+
13+
class BaseTestCase(TestCase):
14+
"""
15+
Base test case for netbox_acls models.
16+
"""
17+
18+
@classmethod
19+
def setUpTestData(cls):
20+
"""
21+
Create base data to test using including:
22+
- 1 of each of the following: test site, manufacturer, device type, device role, cluster type, cluster, & virtual machine
23+
- 2 devices, prefixes, 2 interfaces, and 2 vminterfaces
24+
"""
25+
26+
site = Site.objects.create(name="Site 1", slug="site-1")
27+
manufacturer = Manufacturer.objects.create(
28+
name="Manufacturer 1",
29+
slug="manufacturer-1",
30+
)
31+
devicetype = DeviceType.objects.create(
32+
manufacturer=manufacturer,
33+
model="Device Type 1",
34+
)
35+
devicerole = DeviceRole.objects.create(
36+
name="Device Role 1",
37+
slug="device-role-1",
38+
)
39+
device = Device.objects.create(
40+
name="Device 1",
41+
site=site,
42+
device_type=devicetype,
43+
device_role=devicerole,
44+
)
45+
cluster_member = Device.objects.create(
46+
name="Cluster Device",
47+
site=site,
48+
device_type=devicetype,
49+
device_role=devicerole,
50+
)
51+
clustertype = ClusterType.objects.create(name="Cluster Type 1")
52+
cluster = Cluster.objects.create(
53+
name="Cluster 1",
54+
type=clustertype,
55+
)
56+
virtual_machine = VirtualMachine.objects.create(name="VirtualMachine 1")
57+
58+
interfaces = Interface.objects.bulk_create(
59+
(
60+
Interface(name="Interface 1", device=device, type="1000baset"),
61+
Interface(name="Interface 2", device=device, type="1000baset"),
62+
)
63+
)
64+
vminterfaces = VMInterface.objects.bulk_create(
65+
(
66+
VMInterface(name="Interface 1", virtual_machine=virtual_machine),
67+
VMInterface(name="Interface 2", virtual_machine=virtual_machine),
68+
)
69+
)
70+
prefixes = Prefix.objects.bulk_create(
71+
(
72+
Prefix(prefix=IPNetwork("10.0.0.0/24")),
73+
Prefix(prefix=IPNetwork("192.168.1.0/24")),
74+
)
75+
)
76+
77+
78+
class TestAccessList(BaseTestCase):
79+
"""
80+
Test AccessList model.
81+
"""
82+
83+
def test_alphanumeric_plus_success(self):
84+
"""
85+
Test that AccessList names with alphanumeric characters, '_', or '-' pass validation.
86+
"""
87+
acl_good_name = AccessList(
88+
name="Testacl-good_name-1",
89+
assigned_object_type=ContentType.objects.get_for_model(Device),
90+
assigned_object_id=1,
91+
type=ACLTypeChoices.TYPE_EXTENDED,
92+
default_action=ACLActionChoices.ACTION_PERMIT,
93+
)
94+
self.assertRaises(ValidationError, acl_good_name.clean)
95+
96+
def test_duplicate_name_success(self):
97+
"""
98+
Test that AccessList names can be non-unique if associated to different devices.
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+
new_acl = AccessList(
111+
**params,
112+
assigned_object_type=ContentType.objects.get_for_model(VirtualMachine),
113+
assigned_object_id=1,
114+
)
115+
self.assertIsNone(new_acl.clean())
116+
# TODO: test_duplicate_name_fail - VM & Cluster
117+
118+
def test_alphanumeric_plus_fail(self):
119+
"""
120+
Test that AccessList names with non-alphanumeric (exluding '_' and '-') characters fail validation.
121+
"""
122+
non_alphanumeric_plus_chars = " !@#$%^&*()[]{};:,./<>?\|~=+"
123+
124+
for i, char in enumerate(non_alphanumeric_plus_chars, start=1):
125+
bade_acl_name = AccessList(
126+
name=f"Testacl-bad_name_{i}_{char}",
127+
assigned_object_type=ContentType.objects.get_for_model(Device),
128+
assigned_object_id=1,
129+
type=ACLTypeChoices.TYPE_EXTENDED,
130+
default_action=ACLActionChoices.ACTION_PERMIT,
131+
comments=f'ACL with "{char}" in name',
132+
)
133+
self.assertIsNone(bade_acl_name.clean)
134+
135+
def test_duplicate_name_fail(self):
136+
"""
137+
Test that AccessList names must be unique per device.
138+
"""
139+
params = {
140+
"name": "FAIL-DUPLICATE-ACL",
141+
"assigned_object_type": "dcim.device",
142+
"assigned_object_id": 1,
143+
"type": ACLTypeChoices.TYPE_STANDARD,
144+
"default_action": ACLActionChoices.ACTION_PERMIT,
145+
}
146+
AccessList.objects.create(**params)
147+
duplicate_acl = AccessList(**params)
148+
self.assertRaises(ValidationError, duplicate_acl.clean())
149+
# TODO: test_duplicate_name_fail - VM & Cluster
150+
151+
# TODO: Test choices for AccessList Model
152+
153+
154+
class TestACLInterfaceAssignment(BaseTestCase):
155+
"""
156+
Test ACLInterfaceAssignment model.
157+
"""
158+
159+
def test_acl_assignment_success(self):
160+
"""
161+
Test that ACLInterfaceAssignment passes validation if the ACL is assigned to the host and not already assigned to the interface and direction.
162+
"""
163+
pass
164+
# TODO: test_acl_assignment_success - VM & Device
165+
166+
def test_acl_assignment_fail(self):
167+
"""
168+
Test that ACLInterfaceAssignment fails validation if the ACL is not assigned to the parent host.
169+
"""
170+
pass
171+
# TODO: test_acl_assignment_fail - VM & Device
172+
173+
def test_duplicate_assignment_fail(self):
174+
"""
175+
Test that ACLInterfaceAssignment fails validation if the ACL already is assigned to the same interface and direction.
176+
"""
177+
pass
178+
# TODO: test_duplicate_assignment_fail - VM & Device
179+
180+
def test_acl_already_assinged_fail(self):
181+
"""
182+
Test that ACLInterfaceAssignment fails validation if the interface already has an ACL assigned in the same direction.
183+
"""
184+
pass
185+
## TODO: test_acl_already_assinged_fail - VM & Device
186+
187+
# TODO: Test choices for ACLInterfaceAssignment Model

0 commit comments

Comments
 (0)