44from django .contrib .contenttypes .models import ContentType
55from django .core .exceptions import ValidationError
66from ipam .models import Prefix
7- from virtualization .models import VirtualMachine
87
98from netbox_acls .models import AccessList
109
@@ -20,16 +19,16 @@ class TestAccessList(BaseTestCase):
2019 "type" : "extended" ,
2120 "default_action" : "permit" ,
2221 }
23- # device = Device.objects.first()
2422
2523 def test_wrong_assigned_object_type_fail (self ):
2624 """
27- Test that AccessList cannot be assigned to an object type other than Device, VirtualChassis, VirtualMachine, or Cluster.
25+ Test that AccessList cannot be assigned to an object type other than Device, VirtualChassis, VirtualMachine,
26+ or Cluster.
2827 """
2928 acl_bad_gfk = AccessList (
3029 name = "TestACL_Wrong_GFK" ,
3130 assigned_object_type = ContentType .objects .get_for_model (Prefix ),
32- assigned_object_id = Prefix . objects . first () ,
31+ assigned_object_id = self . prefix1 . id ,
3332 ** self .common_acl_params ,
3433 )
3534 with self .assertRaises (ValidationError ):
@@ -40,31 +39,24 @@ def test_alphanumeric_plus_success(self):
4039 Test that AccessList names with alphanumeric characters, '_', or '-' pass validation.
4140 """
4241 acl_good_name = AccessList (
43- name = "Testacl -Good_Name-1" ,
42+ name = "Test-ACL -Good_Name-1" ,
4443 assigned_object_type = ContentType .objects .get_for_model (Device ),
45- assigned_object_id = 1 , # TODO - replace with Device.objects.first()
44+ assigned_object_id = self . device1 . id ,
4645 ** self .common_acl_params ,
4746 )
4847 acl_good_name .full_clean ()
49- # TODO: test_alphanumeric_plus_success - VirtualChassis, VirtualMachine & Cluster
5048
5149 def test_duplicate_name_success (self ):
5250 """
5351 Test that AccessList names can be non-unique if associated with different devices.
5452 """
55- AccessList .objects .create (
53+ # Device
54+ device_acl = AccessList (
5655 name = "GOOD-DUPLICATE-ACL" ,
57- assigned_object_type = ContentType .objects .get_for_model (Device ),
58- assigned_object_id = 1 , # TODO - replace with Device.objects.first()
59- ** self .common_acl_params ,
60- )
61- vm_acl = AccessList (
62- name = "GOOD-DUPLICATE-ACL" ,
63- assigned_object_type = ContentType .objects .get_for_model (VirtualMachine ),
64- assigned_object_id = 1 , # TODO - replace with VirtualMachine.objects.first().id,
56+ assigned_object = self .device1 ,
6557 ** self .common_acl_params ,
6658 )
67- vm_acl .full_clean ()
59+ device_acl .full_clean ()
6860 # TODO: test_duplicate_name_success - VirtualChassis, VirtualMachine & Cluster
6961 # vc_acl = AccessList(
7062 # "name": "GOOD-DUPLICATE-ACL",
@@ -81,23 +73,23 @@ def test_alphanumeric_plus_fail(self):
8173
8274 for i , char in enumerate (non_alphanumeric_plus_chars , start = 1 ):
8375 bad_acl_name = AccessList (
84- name = f"Testacl -bad_name_{ i } _{ char } " ,
85- assigned_object_type = ContentType . objects . get_for_model ( Device ) ,
76+ name = f"Test-ACL -bad_name_{ i } _{ char } " ,
77+ assigned_object = self . device1 ,
8678 comments = f'ACL with "{ char } " in name' ,
8779 ** self .common_acl_params ,
8880 )
8981 with self .assertRaises (ValidationError ):
9082 bad_acl_name .full_clean ()
9183
92- def test_duplicate_name_fail (self ):
84+ def test_duplicate_name_per_device_fail (self ):
9385 """
9486 Test that AccessList names must be unique per device.
9587 """
9688 params = {
9789 "name" : "FAIL-DUPLICATE-ACL" ,
9890 "assigned_object_type" : ContentType .objects .get_for_model (Device ),
91+ "assigned_object_id" : self .device1 .id ,
9992 ** self .common_acl_params ,
100- "assigned_object_id" : 1 , # TODO - replace with Device.objects.first()
10193 }
10294 acl_1 = AccessList .objects .create (** params )
10395 acl_1 .save ()
@@ -122,11 +114,10 @@ def test_valid_acl_choices(self):
122114 for default_action , acl_type in valid_acl_choices :
123115 valid_acl_choice = AccessList (
124116 name = f"TestACL_Valid_Choice_{ default_action } _{ acl_type } " ,
125- comments = f"VALID ACL CHOICES USED: { default_action = } { acl_type = } " ,
117+ assigned_object = self . device1 ,
126118 type = acl_type ,
127119 default_action = default_action ,
128- assigned_object_type = ContentType .objects .get_for_model (Device ),
129- assigned_object_id = 1 , # TODO - replace with Device.objects.first()
120+ comments = f"VALID ACL CHOICES USED: { default_action = } { acl_type = } " ,
130121 )
131122 valid_acl_choice .full_clean ()
132123
@@ -138,11 +129,10 @@ def test_invalid_acl_choices(self):
138129 invalid_acl_default_action_choice = "log"
139130 invalid_acl_default_action = AccessList (
140131 name = f"TestACL_Valid_Choice_{ invalid_acl_default_action_choice } _{ valid_acl_types [0 ]} " ,
141- comments = f"INVALID ACL DEFAULT CHOICE USED: default_action=' { invalid_acl_default_action_choice } '" ,
132+ assigned_object = self . device1 ,
142133 type = valid_acl_types [0 ],
143134 default_action = invalid_acl_default_action_choice ,
144- assigned_object_type = ContentType .objects .get_for_model (Device ),
145- assigned_object_id = 1 , # TODO - replace with Device.objects.first()
135+ comments = f"INVALID ACL DEFAULT CHOICE USED: default_action='{ invalid_acl_default_action_choice } '" ,
146136 )
147137 with self .assertRaises (ValidationError ):
148138 invalid_acl_default_action .full_clean ()
@@ -151,11 +141,10 @@ def test_invalid_acl_choices(self):
151141 invalid_acl_type = "super-dupper-extended"
152142 invalid_acl_type = AccessList (
153143 name = f"TestACL_Valid_Choice_{ valid_acl_default_action_choices [0 ]} _{ invalid_acl_type } " ,
154- comments = f"INVALID ACL DEFAULT CHOICE USED: type=' { invalid_acl_type } '" ,
144+ assigned_object = self . device1 ,
155145 type = invalid_acl_type ,
156146 default_action = valid_acl_default_action_choices [0 ],
157- assigned_object_type = ContentType .objects .get_for_model (Device ),
158- assigned_object_id = 1 , # TODO - replace with Device.objects.first()
147+ comments = f"INVALID ACL DEFAULT CHOICE USED: type='{ invalid_acl_type } '" ,
159148 )
160149 with self .assertRaises (ValidationError ):
161150 invalid_acl_type .full_clean ()
0 commit comments