|
| 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