Skip to content

Commit 568feb0

Browse files
committed
build acl models tests
1 parent c062b63 commit 568feb0

File tree

1 file changed

+63
-9
lines changed

1 file changed

+63
-9
lines changed

netbox_acls/tests/test_models.py

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from itertools import cycle
2+
13
from dcim.models import (
24
Device,
35
DeviceRole,
@@ -14,7 +16,6 @@
1416
from netaddr import IPNetwork
1517
from virtualization.models import Cluster, ClusterType, VirtualMachine, VMInterface
1618

17-
from netbox_acls.choices import *
1819
from netbox_acls.models import *
1920

2021

@@ -81,8 +82,8 @@ class TestAccessList(BaseTestCase):
8182

8283
common_acl_params = {
8384
"assigned_object_id": 1,
84-
"type": ACLTypeChoices.TYPE_EXTENDED,
85-
"default_action": ACLActionChoices.ACTION_PERMIT,
85+
"type": "extended",
86+
"default_action": "permit",
8687
}
8788

8889
def test_wrong_assigned_object_type_fail(self):
@@ -114,11 +115,6 @@ def test_duplicate_name_success(self):
114115
Test that AccessList names can be non-unique if associated to different devices.
115116
"""
116117

117-
params = {
118-
"name": "GOOD-DUPLICATE-ACL",
119-
"type": ACLTypeChoices.TYPE_STANDARD,
120-
"default_action": ACLActionChoices.ACTION_PERMIT,
121-
}
122118
AccessList.objects.create(
123119
name="GOOD-DUPLICATE-ACL",
124120
assigned_object_type=ContentType.objects.get_for_model(Device),
@@ -171,7 +167,65 @@ def test_duplicate_name_fail(self):
171167
acl_2.full_clean()
172168
# TODO: test_duplicate_name_fail - VirtualChassis & Cluster
173169

174-
# TODO: Test choices for AccessList Model
170+
def test_valid_acl_choices(self):
171+
"""
172+
Test that AccessList action choices using VALID choices.
173+
"""
174+
valid_acl_default_action_choices = ["permit", "deny"]
175+
valid_acl_types = ["standard", "extended"]
176+
if len(valid_acl_default_action_choices) > len(valid_acl_types):
177+
valid_acl_choices = list(
178+
zip(valid_acl_default_action_choices, cycle(valid_acl_types))
179+
)
180+
elif len(valid_acl_default_action_choices) < len(valid_acl_types):
181+
valid_acl_choices = list(
182+
zip(cycle(valid_acl_default_action_choices), valid_acl_types)
183+
)
184+
else:
185+
valid_acl_choices = list(
186+
zip(valid_acl_default_action_choices, valid_acl_types)
187+
)
188+
189+
for default_action, acl_type in valid_acl_choices:
190+
valid_acl_choice = AccessList(
191+
name=f"TestACL_Valid_Choice_{default_action}_{acl_type}",
192+
comments=f"VALID ACL CHOICES USED: {default_action=} {acl_type=}",
193+
type=acl_type,
194+
default_action=default_action,
195+
assigned_object_type=ContentType.objects.get_for_model(Device),
196+
assigned_object_id=1,
197+
)
198+
valid_acl_choice.full_clean()
199+
200+
def test_invalid_acl_choices(self):
201+
"""
202+
Test that AccessList action choices using INVALID choices.
203+
"""
204+
valid_acl_types = ["standard", "extended"]
205+
invalid_acl_default_action_choice = "log"
206+
invalid_acl_default_action = AccessList(
207+
name=f"TestACL_Valid_Choice_{invalid_acl_default_action_choice}_{valid_acl_types[0]}",
208+
comments=f"INVALID ACL DEFAULT CHOICE USED: default_action='{invalid_acl_default_action_choice}'",
209+
type=valid_acl_types[0],
210+
default_action=invalid_acl_default_action_choice,
211+
assigned_object_type=ContentType.objects.get_for_model(Device),
212+
assigned_object_id=1,
213+
)
214+
with self.assertRaises(ValidationError):
215+
invalid_acl_default_action.full_clean()
216+
217+
valid_acl_default_action_choices = ["permit", "deny"]
218+
invalid_acl_type = "super-dupper-extended"
219+
invalid_acl_type = AccessList(
220+
name=f"TestACL_Valid_Choice_{valid_acl_default_action_choices[0]}_{invalid_acl_type}",
221+
comments=f"INVALID ACL DEFAULT CHOICE USED: type='{invalid_acl_type}'",
222+
type=invalid_acl_type,
223+
default_action=valid_acl_default_action_choices[0],
224+
assigned_object_type=ContentType.objects.get_for_model(Device),
225+
assigned_object_id=1,
226+
)
227+
with self.assertRaises(ValidationError):
228+
invalid_acl_type.full_clean()
175229

176230

177231
class TestACLInterfaceAssignment(BaseTestCase):

0 commit comments

Comments
 (0)