|
| 1 | +from itertools import cycle |
| 2 | + |
1 | 3 | from dcim.models import (
|
2 | 4 | Device,
|
3 | 5 | DeviceRole,
|
|
14 | 16 | from netaddr import IPNetwork
|
15 | 17 | from virtualization.models import Cluster, ClusterType, VirtualMachine, VMInterface
|
16 | 18 |
|
17 |
| -from netbox_acls.choices import * |
18 | 19 | from netbox_acls.models import *
|
19 | 20 |
|
20 | 21 |
|
@@ -81,8 +82,8 @@ class TestAccessList(BaseTestCase):
|
81 | 82 |
|
82 | 83 | common_acl_params = {
|
83 | 84 | "assigned_object_id": 1,
|
84 |
| - "type": ACLTypeChoices.TYPE_EXTENDED, |
85 |
| - "default_action": ACLActionChoices.ACTION_PERMIT, |
| 85 | + "type": "extended", |
| 86 | + "default_action": "permit", |
86 | 87 | }
|
87 | 88 |
|
88 | 89 | def test_wrong_assigned_object_type_fail(self):
|
@@ -114,11 +115,6 @@ def test_duplicate_name_success(self):
|
114 | 115 | Test that AccessList names can be non-unique if associated to different devices.
|
115 | 116 | """
|
116 | 117 |
|
117 |
| - params = { |
118 |
| - "name": "GOOD-DUPLICATE-ACL", |
119 |
| - "type": ACLTypeChoices.TYPE_STANDARD, |
120 |
| - "default_action": ACLActionChoices.ACTION_PERMIT, |
121 |
| - } |
122 | 118 | AccessList.objects.create(
|
123 | 119 | name="GOOD-DUPLICATE-ACL",
|
124 | 120 | assigned_object_type=ContentType.objects.get_for_model(Device),
|
@@ -171,7 +167,65 @@ def test_duplicate_name_fail(self):
|
171 | 167 | acl_2.full_clean()
|
172 | 168 | # TODO: test_duplicate_name_fail - VirtualChassis & Cluster
|
173 | 169 |
|
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() |
175 | 229 |
|
176 | 230 |
|
177 | 231 | class TestACLInterfaceAssignment(BaseTestCase):
|
|
0 commit comments