|
| 1 | +from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site |
| 2 | +from ipam.models import Prefix |
| 3 | +from utilities.testing import APIViewTestCases |
| 4 | +from virtualization.models import Cluster, ClusterType, VirtualMachine |
| 5 | + |
| 6 | +from netbox_acls.choices import * |
| 7 | +from netbox_acls.models import * |
| 8 | + |
| 9 | + |
| 10 | +class ACLStandardRuleAPIViewTestCase(APIViewTestCases.APIViewTestCase): |
| 11 | + """ |
| 12 | + API view test case for ACLStandardRule. |
| 13 | + """ |
| 14 | + |
| 15 | + model = ACLStandardRule |
| 16 | + view_namespace = "plugins-api:netbox_acls" |
| 17 | + brief_fields = ["access_list", "display", "id", "index", "url"] |
| 18 | + user_permissions = ( |
| 19 | + "dcim.view_site", |
| 20 | + "dcim.view_manufacturer", |
| 21 | + "dcim.view_devicetype", |
| 22 | + "dcim.view_device", |
| 23 | + "ipam.view_prefix", |
| 24 | + "virtualization.view_cluster", |
| 25 | + "virtualization.view_clustergroup", |
| 26 | + "virtualization.view_clustertype", |
| 27 | + "virtualization.view_virtualmachine", |
| 28 | + "netbox_acls.view_accesslist", |
| 29 | + ) |
| 30 | + |
| 31 | + @classmethod |
| 32 | + def setUpTestData(cls): |
| 33 | + """Set up ACL Standard Rule for API view testing.""" |
| 34 | + site = Site.objects.create( |
| 35 | + name="Site 1", |
| 36 | + slug="site-1", |
| 37 | + ) |
| 38 | + |
| 39 | + # Device |
| 40 | + manufacturer = Manufacturer.objects.create( |
| 41 | + name="Manufacturer 1", |
| 42 | + slug="manufacturer-1", |
| 43 | + ) |
| 44 | + device_type = DeviceType.objects.create( |
| 45 | + manufacturer=manufacturer, |
| 46 | + model="Device Type 1", |
| 47 | + ) |
| 48 | + device_role = DeviceRole.objects.create( |
| 49 | + name="Device Role 1", |
| 50 | + slug="device-role-1", |
| 51 | + ) |
| 52 | + device = Device.objects.create( |
| 53 | + name="Device 1", |
| 54 | + site=site, |
| 55 | + device_type=device_type, |
| 56 | + role=device_role, |
| 57 | + ) |
| 58 | + |
| 59 | + # Virtual Machine |
| 60 | + cluster_type = ClusterType.objects.create( |
| 61 | + name="Cluster Type 1", |
| 62 | + slug="cluster-type-1", |
| 63 | + ) |
| 64 | + cluster = Cluster.objects.create( |
| 65 | + name="Cluster 1", |
| 66 | + type=cluster_type, |
| 67 | + ) |
| 68 | + virtual_machine = VirtualMachine.objects.create( |
| 69 | + name="VM 1", |
| 70 | + cluster=cluster, |
| 71 | + ) |
| 72 | + |
| 73 | + # AccessList |
| 74 | + access_list_device = AccessList.objects.create( |
| 75 | + name="testacl1", |
| 76 | + assigned_object=device, |
| 77 | + type=ACLTypeChoices.TYPE_STANDARD, |
| 78 | + default_action=ACLActionChoices.ACTION_DENY, |
| 79 | + ) |
| 80 | + access_list_vm = AccessList.objects.create( |
| 81 | + name="testacl2", |
| 82 | + assigned_object=virtual_machine, |
| 83 | + type=ACLTypeChoices.TYPE_STANDARD, |
| 84 | + default_action=ACLActionChoices.ACTION_PERMIT, |
| 85 | + ) |
| 86 | + |
| 87 | + # Prefix |
| 88 | + prefix1 = Prefix.objects.create( |
| 89 | + prefix="10.0.0.0/24", |
| 90 | + ) |
| 91 | + prefix2 = Prefix.objects.create( |
| 92 | + prefix="10.0.1.0/24", |
| 93 | + ) |
| 94 | + |
| 95 | + acl_standard_rules = ( |
| 96 | + ACLStandardRule( |
| 97 | + access_list=access_list_device, |
| 98 | + index=10, |
| 99 | + description="Rule 10", |
| 100 | + action=ACLRuleActionChoices.ACTION_PERMIT, |
| 101 | + source_prefix=prefix1, |
| 102 | + ), |
| 103 | + ACLStandardRule( |
| 104 | + access_list=access_list_device, |
| 105 | + index=20, |
| 106 | + description="Rule 20", |
| 107 | + action=ACLRuleActionChoices.ACTION_REMARK, |
| 108 | + remark="Remark 1", |
| 109 | + ), |
| 110 | + ACLStandardRule( |
| 111 | + access_list=access_list_vm, |
| 112 | + index=10, |
| 113 | + description="Rule 10", |
| 114 | + action=ACLRuleActionChoices.ACTION_DENY, |
| 115 | + source_prefix=prefix2, |
| 116 | + ), |
| 117 | + ) |
| 118 | + ACLStandardRule.objects.bulk_create(acl_standard_rules) |
| 119 | + |
| 120 | + cls.create_data = [ |
| 121 | + { |
| 122 | + "access_list": access_list_device.id, |
| 123 | + "index": 30, |
| 124 | + "description": "Rule 30", |
| 125 | + "action": ACLRuleActionChoices.ACTION_DENY, |
| 126 | + "source_prefix": prefix2.id, |
| 127 | + }, |
| 128 | + { |
| 129 | + "access_list": access_list_vm.id, |
| 130 | + "index": 20, |
| 131 | + "description": "Rule 30", |
| 132 | + "action": ACLRuleActionChoices.ACTION_PERMIT, |
| 133 | + "source_prefix": prefix1.id, |
| 134 | + }, |
| 135 | + { |
| 136 | + "access_list": access_list_vm.id, |
| 137 | + "index": 30, |
| 138 | + "description": "Rule 30", |
| 139 | + "action": ACLRuleActionChoices.ACTION_REMARK, |
| 140 | + "remark": "Remark 2", |
| 141 | + }, |
| 142 | + ] |
| 143 | + cls.bulk_update_data = { |
| 144 | + "description": "Rule bulk update", |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | +class ACLExtendedRuleAPIViewTestCase(APIViewTestCases.APIViewTestCase): |
| 149 | + """ |
| 150 | + API view test case for ACLExtendedRule. |
| 151 | + """ |
| 152 | + |
| 153 | + model = ACLExtendedRule |
| 154 | + view_namespace = "plugins-api:netbox_acls" |
| 155 | + brief_fields = ["access_list", "display", "id", "index", "url"] |
| 156 | + user_permissions = ( |
| 157 | + "dcim.view_site", |
| 158 | + "dcim.view_manufacturer", |
| 159 | + "dcim.view_devicetype", |
| 160 | + "dcim.view_device", |
| 161 | + "ipam.view_prefix", |
| 162 | + "virtualization.view_cluster", |
| 163 | + "virtualization.view_clustergroup", |
| 164 | + "virtualization.view_clustertype", |
| 165 | + "virtualization.view_virtualmachine", |
| 166 | + "netbox_acls.view_accesslist", |
| 167 | + ) |
| 168 | + |
| 169 | + @classmethod |
| 170 | + def setUpTestData(cls): |
| 171 | + """Set up ACL Extended Rule for API view testing.""" |
| 172 | + site = Site.objects.create( |
| 173 | + name="Site 1", |
| 174 | + slug="site-1", |
| 175 | + ) |
| 176 | + |
| 177 | + # Device |
| 178 | + manufacturer = Manufacturer.objects.create( |
| 179 | + name="Manufacturer 1", |
| 180 | + slug="manufacturer-1", |
| 181 | + ) |
| 182 | + device_type = DeviceType.objects.create( |
| 183 | + manufacturer=manufacturer, |
| 184 | + model="Device Type 1", |
| 185 | + ) |
| 186 | + device_role = DeviceRole.objects.create( |
| 187 | + name="Device Role 1", |
| 188 | + slug="device-role-1", |
| 189 | + ) |
| 190 | + device = Device.objects.create( |
| 191 | + name="Device 1", |
| 192 | + site=site, |
| 193 | + device_type=device_type, |
| 194 | + role=device_role, |
| 195 | + ) |
| 196 | + |
| 197 | + # Virtual Machine |
| 198 | + cluster_type = ClusterType.objects.create( |
| 199 | + name="Cluster Type 1", |
| 200 | + slug="cluster-type-1", |
| 201 | + ) |
| 202 | + cluster = Cluster.objects.create( |
| 203 | + name="Cluster 1", |
| 204 | + type=cluster_type, |
| 205 | + ) |
| 206 | + virtual_machine = VirtualMachine.objects.create( |
| 207 | + name="VM 1", |
| 208 | + cluster=cluster, |
| 209 | + ) |
| 210 | + |
| 211 | + # AccessList |
| 212 | + access_list_device = AccessList.objects.create( |
| 213 | + name="testacl1", |
| 214 | + assigned_object=device, |
| 215 | + type=ACLTypeChoices.TYPE_EXTENDED, |
| 216 | + default_action=ACLActionChoices.ACTION_DENY, |
| 217 | + ) |
| 218 | + access_list_vm = AccessList.objects.create( |
| 219 | + name="testacl2", |
| 220 | + assigned_object=virtual_machine, |
| 221 | + type=ACLTypeChoices.TYPE_EXTENDED, |
| 222 | + default_action=ACLActionChoices.ACTION_PERMIT, |
| 223 | + ) |
| 224 | + |
| 225 | + # Prefix |
| 226 | + prefix1 = Prefix.objects.create( |
| 227 | + prefix="10.0.0.0/24", |
| 228 | + ) |
| 229 | + prefix2 = Prefix.objects.create( |
| 230 | + prefix="10.0.1.0/24", |
| 231 | + ) |
| 232 | + |
| 233 | + acl_extended_rules = ( |
| 234 | + ACLExtendedRule( |
| 235 | + access_list=access_list_device, |
| 236 | + index=10, |
| 237 | + description="Rule 10", |
| 238 | + action=ACLRuleActionChoices.ACTION_PERMIT, |
| 239 | + protocol=ACLProtocolChoices.PROTOCOL_TCP, |
| 240 | + source_prefix=prefix1, |
| 241 | + source_ports=[22, 443], |
| 242 | + destination_prefix=prefix1, |
| 243 | + destination_ports=[22, 443], |
| 244 | + ), |
| 245 | + ACLExtendedRule( |
| 246 | + access_list=access_list_device, |
| 247 | + index=20, |
| 248 | + description="Rule 20", |
| 249 | + action=ACLRuleActionChoices.ACTION_REMARK, |
| 250 | + remark="Remark 1", |
| 251 | + ), |
| 252 | + ACLExtendedRule( |
| 253 | + access_list=access_list_vm, |
| 254 | + index=10, |
| 255 | + description="Rule 10", |
| 256 | + action=ACLRuleActionChoices.ACTION_DENY, |
| 257 | + source_prefix=prefix2, |
| 258 | + destination_prefix=prefix1, |
| 259 | + ), |
| 260 | + ) |
| 261 | + ACLExtendedRule.objects.bulk_create(acl_extended_rules) |
| 262 | + |
| 263 | + cls.create_data = [ |
| 264 | + { |
| 265 | + "access_list": access_list_device.id, |
| 266 | + "index": 30, |
| 267 | + "description": "Rule 30", |
| 268 | + "action": ACLRuleActionChoices.ACTION_DENY, |
| 269 | + "protocol": ACLProtocolChoices.PROTOCOL_UDP, |
| 270 | + "source_prefix": prefix2.id, |
| 271 | + "source_ports": [53], |
| 272 | + "destination_prefix": prefix2.id, |
| 273 | + "destination_ports": [53], |
| 274 | + }, |
| 275 | + { |
| 276 | + "access_list": access_list_vm.id, |
| 277 | + "index": 20, |
| 278 | + "description": "Rule 30", |
| 279 | + "action": ACLRuleActionChoices.ACTION_PERMIT, |
| 280 | + "protocol": ACLProtocolChoices.PROTOCOL_ICMP, |
| 281 | + "source_prefix": prefix1.id, |
| 282 | + "destination_prefix": prefix2.id, |
| 283 | + }, |
| 284 | + { |
| 285 | + "access_list": access_list_vm.id, |
| 286 | + "index": 30, |
| 287 | + "description": "Rule 30", |
| 288 | + "action": ACLRuleActionChoices.ACTION_REMARK, |
| 289 | + "remark": "Remark 2", |
| 290 | + }, |
| 291 | + ] |
| 292 | + cls.bulk_update_data = { |
| 293 | + "description": "Rule bulk update", |
| 294 | + } |
0 commit comments